EasyArrayUpdated Sep 2026

228. Summary Ranges

Asked at Netflix

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Array

Companies

1

Problem

You get a sorted array of unique integers called nums. Your job is to group the numbers into the smallest possible list of ranges, then return that list as strings.

A range covers every integer from a start value a to an end value b, both included. The ranges must cover every number in nums exactly once, and they must not cover any number that is not in nums. Because the array is sorted and has no duplicates, a range is simply a run of numbers where each one is exactly 1 bigger than the one before it.

Format each range like this: if the range holds a single number, write just that number, for example "7". If it holds two or more numbers, write "a->b", for example "0->2". Return the list of these strings in the same order as the numbers appear in the array.

Group consecutive numbers into ranges

nums0->24->57012457012345Three groups: the first two have more than one number,the last one is a single value.
Runs of numbers that step up by 1 become one range; a number with a gap on both sides becomes a range of length 1.

Constraints

  • ●0 <= nums.length <= 20
  • ●-2^31 <= nums[i] <= 2^31 - 1
  • ●All values in nums are unique.
  • ●nums is sorted in ascending order.

Examples

Example 1
Inputnums = [0,1,2,4,5,7]
Output["0->2","4->5","7"]
Why0, 1 and 2 are consecutive, so they form one range written as "0->2". Then 4 and 5 are consecutive, giving "4->5". The value 7 stands alone, so it is written as just "7".
Example 2
Inputnums = [0,2,3,4,6,8,9]
Output["0","2->4","6","8->9"]
Why0 has no neighbor at 1, so it is a single-number range. 2, 3 and 4 are consecutive, giving "2->4". 6 is alone because 5 and 7 are missing. Finally 8 and 9 are consecutive, giving "8->9".

Asked At

1 companies
CompanyDifficulty
NetflixEasyView all Netflix questions →

How to Think About It

1

Brute force: scan each element and check if it connects to the next one.

2

Track the start of each range with a variable start.

3

When nums[i+1] != nums[i] + 1, the current range ends at nums[i].

4

Format the range string: if start == end, output str(start); otherwise "start->end".

5

Optimal is O(n) time since you must visit each element once.

Optimal Approach

Iterate through nums. For each element, mark it as the start of a potential range. Continue extending while the next element is exactly +1. When the sequence breaks or ends, format the range and add it to the result. O(n) time, O(1) extra space (excluding output).

Extend a range until the chain breaks

nums012457012345startiStep 1 — start a new range at index 0, value 0.nums012457012345startiStep 2 — 1 follows 0 and 2 follows 1, so i moves toindex 2. The next value 4 is not 3, so the range endshere.nums012457012345startiStep 3 — emit "0->2", then start again at 4. Itextends to 5, giving "4->5". Value 7 is alone, giving"7".start = nums[i]while nums[i+1] == nums[i] + 1: i += 1if start == nums[i]: emit str(start)else: emit str(start) + "->" + str(nums[i])
Start a range at the current index, keep moving forward while the next value is exactly +1, then format and continue.
ComplexityTime O(n)Space O(1)We walk through the array once, and the only extra memory is a few index and boundary variables (the output list itself is not counted).

What Trips People Up in Real Interviews

1

Edge cases: empty array returns [], single element returns ["x"].

2

Be careful with the last range: it must be flushed after the loop ends.

3

Output format is strict: "0->2" not "0-2" or "0 to 2".

4

The input is sorted and unique — no need to sort or deduplicate.

5

Ask if the interviewer wants the result as strings or as a list of pairs.

Solution Code

O(n) time
class Solution:
    def summaryRanges(self, nums: list[int]) -> list[str]:
        result = []
        i = 0
        while i < len(nums):
            start = nums[i]
            while i + 1 < len(nums) and nums[i + 1] == nums[i] + 1:
                i += 1
            end = nums[i]
            if start == end:
                result.append(str(start))
            else:
                result.append(f"{start}->{end}")
            i += 1
        return result

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Summary Ranges problem?

Given a sorted unique integer array nums, return the smallest list of ranges that cover all numbers in the array exactly. Each range is formatted as [a, b] where a and b are consecutive integers, and printed as "a->b" if a != b, or just "a" if a == b.

How do you solve Summary Ranges?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Summary Ranges?

Summary Ranges is asked at Netflix. It is a easy difficulty problem.

What are common mistakes on Summary Ranges?
  • Edge cases: empty array returns [], single element returns ["x"].
  • Be careful with the last range: it must be flushed after the loop ends.
  • Output format is strict: "0->2" not "0-2" or "0 to 2".
  • The input is sorted and unique — no need to sort or deduplicate.
  • Ask if the interviewer wants the result as strings or as a list of pairs.