Reported January 2020
Bloombergtwo pointers

Maximal Consecutive Number Ranges

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

The data structure here is just the array itself, plus two index pointers. That's the whole trick behind the Bloomberg OA question "Maximal Consecutive Number Ranges", reported in January 2020. You get a strictly increasing list and need to collapse runs of consecutive values into strings like "-1->2", with singletons printed alone. It's an easy one if you stay calm and a mess if you overthink it. Sliding window is the hinted pattern, though it's really a single scan. If your brain freezes mid-assessment, StealthCoder runs invisibly on your desktop and hands you the solution as a safety net.

The problem

nums is strictly increasing. Return every maximal consecutive range in order. Format a multi-value range as start->end and a singleton as its value.

Function
consecutiveRanges(nums: int[]) → String[]

Examples
Example 1
nums = [-1,0,1,2,5,6,8]
return = ["-1->2","5->6","8"]
There are two multi-value runs and one singleton.

Constraints
0 <= nums.length <= 10^5.
Values are unique and strictly increasing.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Walk the array once with a start index. Move an end pointer forward while the next value equals the current value plus one. When the run breaks, emit one string. If start equals end, output the number alone. Otherwise output start->end. Then jump start to the next index. That's O(n) time and O(1) extra space beyond the output. The common pitfalls are all edge cases. An empty array must return an empty list. The last run has to be flushed after the loop ends, or you need a loop shape that handles it. Negative numbers format fine with string conversion, but don't split on a hyphen. Since values are strictly increasing and unique, you never need to worry about duplicates. Use a plain while loop rather than a for loop with a mutable counter. If you blank on the loop shape during the live OA, StealthCoder is the hedge that gives you a clean version fast.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Maximal Consecutive Number Ranges cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as summary ranges. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximal Consecutive Number Ranges FAQ

How hard is Maximal Consecutive Number Ranges really?+

It's easy. One pass, two pointers, a string format rule. The difficulty is in the edge cases, not the algorithm. Empty input, a single element, and flushing the final run trip people up more than the core logic does.

What's the trick to solving it fast?+

Keep a start index and extend while nums[i+1] equals nums[i]+1. When the run stops, emit start->end if the run has more than one value, otherwise just the number. Then set start to the next index and repeat until the array ends.

Is this a sliding window problem?+

Loosely. The tag hints at it because you expand a window while values stay consecutive, then reset. In practice it's a linear scan with a run start. You don't need a hash map, a set, or any sorting since the input is already strictly increasing.

What edge cases should I test before submitting?+

Test an empty array, a single element, all values consecutive, no values consecutive, and negatives crossing zero like [-1,0,1]. Also check that the last run gets emitted. The example with [-1,0,1,2,5,6,8] covers a multi-run, a pair, and a trailing singleton.

How do I prepare for this in 48 hours?+

Write the scan from memory twice in your strongest language. Then do a few similar array-run problems, like summary ranges or grouping consecutive elements. Bloomberg reported this in January 2020, and the pattern is common, so nail the loop shape and the output formatting.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it