Largest Rectangle in Histogram
Reported by candidates from Shipsy's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The classic first-attempt mistake on Largest Rectangle in Histogram is writing the O(n^2) expand-from-every-bar loop and calling it done. Shipsy candidates reported this one in July 2026, and the interviewer didn't stop at a working answer. They wanted the brute force, why it's slow, then the monotonic-stack version with time and space analysis. So you need to explain the reasoning, not just paste code. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the stack solution in real time. Know the idea first, though. It's short.
The problem
Given an array heights of non-negative bar heights, where every bar has width 1, return the area of the largest rectangle that can be formed using one or more consecutive bars. Interview follow-up The interviewer asked for a brute-force approach, why it is inefficient, the optimal monotonic-stack solution, time and space analysis, and a clean implementation. They emphasized understanding the reasoning rather than only reaching the final answer. 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
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a monotonic stack of indices with increasing heights. Walk the array, and when the current bar is shorter than the stack top, pop. Each popped bar is the limiting height of a rectangle. Its right edge is the current index, and its left edge is the new stack top plus one. Width is i - stack[-1] - 1, or i if the stack is empty. Append a sentinel height of 0 at the end so everything flushes. Each index is pushed and popped once, so it's O(n) time and O(n) space. The common pitfall is getting the width wrong after a pop, or forgetting the final flush. Brute force checks every pair or expands from every bar, which is O(n^2). Test on [2,1,5,6,2,3] and expect 10. If the live OA freezes you, StealthCoder is the hedge that hands you the working stack code.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderThis 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 Shipsy's OA.
Shipsy reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Largest Rectangle in Histogram FAQ
What's the trick to Largest Rectangle in Histogram?+
Use a stack of indices with strictly increasing heights. When a shorter bar arrives, pop taller bars and compute the area each one can span. The popped bar is the height, and the new stack top and current index set the width boundaries.
Why is the brute force too slow?+
For every bar you expand left and right until you hit a shorter bar, or check every pair of bounds. That's O(n^2) in the worst case, such as a sorted array. The stack removes the repeated scanning by resolving each bar exactly once.
What are the time and space complexities of the optimal solution?+
Time is O(n) because each index is pushed once and popped once. Space is O(n) for the stack in the worst case, like a strictly increasing histogram. Say the amortized argument out loud, since Shipsy's interviewers reportedly cared about the reasoning.
What edge cases should I test?+
Test a single bar, an all-equal array, strictly increasing, strictly decreasing, and a zero height in the middle. Also check the width formula when the stack is empty after a pop. Example [2,4] should return 4, and [2,1,5,6,2,3] should return 10.
How do I prepare for this in 48 hours?+
Write the brute force once, then the stack version from memory twice. Trace [2,1,5,6,2,3] by hand and track the stack at each step. Practice explaining why popping is safe. That's enough to handle the follow-up questions.