Summary Ranges
Asked at Netflix
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.
Asked At
| 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).
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
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 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 is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.
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.