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
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
nums = [0,1,2,4,5,7]["0->2","4->5","7"]nums = [0,2,3,4,6,8,9]["0","2->4","6","8->9"]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Netflix | Easy | View all Netflix questions → |
How to Think About It
Brute force: scan each element and check if it connects to the next one.
Track the start of each range with a variable start.
When nums[i+1] != nums[i] + 1, the current range ends at nums[i].
Format the range string: if start == end, output str(start); otherwise "start->end".
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
What Trips People Up in Real Interviews
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.
Solution Code
O(n) timeclass 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 resultFrequently 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.