Maximum Non-Overlapping Longer Intervals
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The piece this Bloomberg OA from December 2020 really tests is how you store accepted intervals so each overlap check is cheap. The task is a greedy simulation. Sort by length descending, then start, then end, and accept anything that doesn't collide with what you've already taken. Intervals are half-open, so [0,10) and [10,12) don't touch. With up to 2000 intervals, the data structure choice decides whether this feels easy or messy. If you freeze mid-assessment, StealthCoder runs invisibly as a safety net, but you can own this one before then.
The problem
Treat intervals as half-open [start,end). Consider them by descending length, breaking ties by ascending start then end. Accept an interval when it does not overlap any already accepted interval. Return the resulting maximal accepted set sorted by start then end. Function prioritizeLongerIntervals(intervals: int[][]) → int[][] Examples Example 1 intervals = [[0,10],[0,4],[4,8],[10,12]] return = [[0,10],[10,12]] The length-ten interval is accepted first and blocks both shorter contained intervals. Constraints 0 <= intervals.length <= 2000. start < end.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that the order is fully specified, so there's nothing to optimize about the greedy itself. Sort with a comparator: length descending, start ascending, end ascending. Then for each interval, test it against accepted ones. With n at 2000, a plain list and an O(n^2) scan passes fine. The cleaner option is a sorted structure keyed by start. Find the neighbor before and the neighbor after, and check just those two. Pitfalls: using closed-interval logic, so [0,10] and [10,12] wrongly collide. The correct overlap test is a.start < b.end and b.start < a.end. Also remember to sort the final output by start then end, since acceptance order is by length. Handle the empty input and return an empty array. If you blank on the neighbor-check idea, StealthCoder is the hedge on the live OA, but the brute-force scan works.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Maximum Non-Overlapping Longer Intervals 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Non-Overlapping Longer Intervals FAQ
What's the trick in this Bloomberg interval problem?+
There's no hidden trick. The rules dictate the order: longest first, ties by start then end. You just simulate it. Accept an interval only if it overlaps nothing already accepted, then sort the result by start and end before returning.
How do I check overlap with half-open intervals?+
Two intervals [a,b) and [c,d) overlap only if a < d and c < b. Touching endpoints like [0,10) and [10,12) don't overlap. Using <= is the most common bug here and it will wrongly reject valid intervals like the [10,12) in the example.
Is an O(n^2) solution fast enough?+
With intervals.length up to 2000, checking each candidate against all accepted intervals is about four million comparisons at worst. That's fine. A sorted structure with neighbor checks gives O(n log n) if you want it cleaner, but brute force is a safe answer.
Why is the output sorted differently from the processing order?+
You process by length descending, so accepted intervals come out in that order. The function must return them sorted by start, then end. Forget the final sort and your output will mismatch on any case with more than one accepted interval.
How do I prepare for this in 48 hours?+
Write the comparator sort and the overlap test from memory. Run the example by hand: [0,10] is accepted first, then [0,4] and [4,8] get blocked, then [10,12] fits. Test the empty array and a case with equal lengths to confirm tie-breaking.