Largest Rectangle in Histogram
Stack
Asked at 12 companies
7 recent
- Amazon
- Microsoft
- tcs
- Bloomberg
- Accenture
- Meta
- thoughtspot
- Coupang
- Myntra
+2 moreShow fewer
- Visa
- BitGo
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 160 pointsSolved (Hard) 0/70Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Undo and the back button: Every action is pushed on a stack; undo pops the latest one. Code editors use a stack the same way to check that brackets match.
About this problem
Last-in, first-out. Perfect when the next item depends on the most recent unfinished one.
Try it in the editor below for 20–30 minutes: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.
Examples
Full statement on LeetCode ↗Example 1
- Input
- heights = [2, 1, 5, 6, 2, 3]
- Output
- 10
Walk through it step by step(14 steps · shows the approach, so try first)
- Keep a of bars with rising heights. When a shorter bar arrives, the taller bars on the stack can't extend further right — pop each and measure the widest rectangle it could make.
- Push bar 0 (height 2) — the stack still rises.
- Bar 1 is shorter, so bar 2 stops here. It spans 1 bar wide: 2 × 1 = 2 — new best!
- Push bar 1 (height 1) — the stack still rises.
- Push bar 2 (height 5) — the stack still rises.
- Push bar 3 (height 6) — the stack still rises.
- Bar 2 is shorter, so bar 6 stops here. It spans 1 bar wide: 6 × 1 = 6 — new best!
- Bar 2 is shorter, so bar 5 stops here. It spans 2 bars wide: 5 × 2 = 10 — new best!
- Push bar 4 (height 2) — the stack still rises.
- Push bar 5 (height 3) — the stack still rises.
- End of bars, so bar 3 stops here. It spans 1 bar wide: 3 × 1 = 3.
- End of bars, so bar 2 stops here. It spans 4 bars wide: 2 × 4 = 8.
- End of bars, so bar 1 stops here. It spans 6 bars wide: 1 × 6 = 6.
- The largest rectangle has area 10.
Example 2
Just one item- Input
- heights = [0]
- Output
- 0
Example 3
All the same- Input
- heights = [1, 1, 1, 1]
- Output
- 4
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionKeep a of bars with increasing heights. When a shorter bar arrives, every taller bar on the stack can't extend further right — pop it and measure its rectangle, whose left is the bar below it on the stack.
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
stack = [] # indices with increasing heights
best = 0
for i, h in enumerate(heights + [0]): # 0 flushes the stack at the end
while stack and heights[stack[-1]] >= h:
height = heights[stack.pop()]
left = stack[-1] + 1 if stack else 0
best = max(best, height * (i - left))
stack.append(i)
return bestReference solution, tested in Python, Java and C++.
Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.