Magicsheet logo

Summary Ranges

Easy
72.4%
Updated 6/1/2025

Summary Ranges

What is this problem about?

The Summary Ranges coding problem involves processing a sorted array of unique integers and condensing them into a list of "ranges." A range is defined as a sequence of consecutive integers. For instance, if you have [0, 1, 2, 4, 5, 7], the ranges are "0->2", "4->5", and "7". Your goal is to return a list of strings that represent these condensed intervals in the smallest possible format. This is a practical problem often seen in data compression or UI display logic where you want to simplify a long list of IDs or dates.

Why is this asked in interviews?

This question is a common "Easy" to "Medium" level problem asked by companies like Apple, Uber, and Microsoft. It tests a candidate's ability to handle array iteration and edge cases (like single-element ranges or empty arrays) gracefully. It also evaluates how you manage string formatting and logic involving "start" and "end" pointers. The Summary Ranges interview question is an excellent way for interviewers to see if you can write clean, readable code for a problem that seems simple but requires careful boundary checks.

Algorithmic pattern used

The primary pattern used here is the Two-Pointer or Single-Pass Array Iteration pattern. You iterate through the array once, keeping track of the start of the current consecutive sequence. For each element, you check if the next element is exactly one greater. If it isn't, it means the current range has ended. You then format the range (either as a single number or as "start->end") and move the start pointer to the next element. This linear approach ensures O(n) time complexity.

Example explanation

Consider the input array: [1, 2, 3, 6, 8, 9].

  1. Start at index 0 (value 1). Next is 2 (consecutive), next is 3 (consecutive). Next is 6 (not consecutive).
    • Range 1: "1->3"
  2. Move to index 3 (value 6). Next is 8 (not consecutive).
    • Range 2: "6"
  3. Move to index 4 (value 8). Next is 9 (consecutive). End of array reached.
    • Range 3: "8->9" Final Output: ["1->3", "6", "8->9"].

Common mistakes candidates make

A common error is missing the last range because the loop ends before it is added to the results list. Another mistake is incorrect string formatting, such as adding the -> symbol even when the range consists of only one number. Handling empty inputs or arrays with only one element can also be a stumbling block if not considered early in the implementation.

Interview preparation tip

To prepare for the Summary Ranges interview question, focus on writing "clean" loops. Practice handling the last element of an array inside the loop or immediately after it. Also, get comfortable with your language's string manipulation tools (like StringBuilder in Java or f-strings in Python) to ensure your formatting is both efficient and readable. This problem is a great way to practice the Array interview pattern in a practical context.

Similar Questions