Largest Rectangle in Histogram
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
With up to 100000 bars in the array, the obvious O(n^2) approach of trying every pair of bars will choke, and that's the whole point of the Largest Rectangle in Histogram question Bloomberg candidates reported in April 2020. You need linear time. The pattern is a monotonic stack, and once you see it the code is about fifteen lines. If you've got an OA coming up, learn the trick tonight. StealthCoder sits invisibly on your screen as a backup if your mind goes blank mid-assessment, but the idea below is small enough to memorize.
The problem
Given an integer array heights representing a histogram, where every bar has width 1, return the area of the largest rectangle that can be formed using one or more consecutive bars. Function largestRectangleArea(heights: int[]) → int Examples Example 1 heights = [2,1,5,6,2,3] return = 10 The bars of heights 5 and 6 form a rectangle of height 5 and width 2. Example 2 heights = [2,4] return = 4 The best area is 4, achieved either by the second bar alone or by both bars at height 2. Example 3 heights = [0] return = 0 The only bar has height 0, so no positive-area rectangle exists. Constraints 1 <= heights.length <= 100000. 0 <= heights[i] <= 10000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: for each bar, the best rectangle using that bar as the limiting height extends left and right until it hits a shorter bar. A monotonic stack of increasing heights finds those boundaries in one pass. Push indices. When the current bar is shorter than the stack top, pop it, use its height, and compute width from the new stack top to the current index. Append a sentinel 0 at the end so everything gets flushed. The common pitfall is off-by-one width: if the stack is empty after popping, width is i, otherwise i - stack[-1] - 1. Another is forgetting equal heights, which still works but can be sloppy. Brute force is O(n^2) and fails at 100000 bars. If you freeze during the live OA, StealthCoder can supply the stack solution so you can still ship a clean O(n) answer, but know the pop-and-measure loop cold.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Largest Rectangle in Histogram 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
This OA pattern shows up on LeetCode as largest rectangle in histogram. If you have time before the OA, drill that.
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.
Largest Rectangle in Histogram FAQ
What's the trick for Largest Rectangle in Histogram?+
Use a monotonic increasing stack of indices. When a shorter bar arrives, pop taller bars and compute the area each could span, using the popped height and the width between the new stack top and the current index. One pass, O(n) time and space.
Why does brute force fail here?+
Heights can have 100000 entries. Checking every left and right pair, or expanding from every bar, is O(n^2), around ten billion operations in the worst case. That times out. You need the single-pass stack approach or an equivalent linear method.
How do I get the width right after popping?+
After popping index top, if the stack is empty, the rectangle reaches back to the start, so width is i. Otherwise width is i - stack[-1] - 1. Most wrong answers on this problem come from this one line, so test it on [2,1,5,6,2,3].
Is this problem still asked, and was it a Bloomberg thing?+
It was reported as a Bloomberg OA question in April 2020. It's a classic monotonic stack problem, and that pattern keeps showing up in assessments, so it's worth knowing even if your exact question differs.
How do I prepare in 48 hours?+
Write this solution from scratch twice without looking. Then trace the three examples by hand, including [0] returning 0. Add the sentinel zero trick and edge cases like a single bar and all equal heights. That covers it. You don't need a big problem list.