Summary Ranges

EasyArrays & Hashingtwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a sorted array nums of **unique** integers. Cover every number in nums with the smallest possible list of ranges of consecutive integers, where each range contains only numbers from nums. Return the ranges in ascending order as strings. A range [a, b] with a != b is written "a->b", and a range with a single number is written "a". An empty input produces an empty list.

Examples

Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
0, 1, 2 are consecutive; so are 4, 5; 7 stands alone.
Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Gaps after 0, 4 and 6 split the numbers into four ranges.

Constraints

  • 0 <= nums.length <= 20
  • -2^31 <= nums[i] <= 2^31 - 1
  • nums is sorted in strictly increasing order

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems