By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(Complete Guide for FAANG-Level Interviews)
"This problem separates candidates who ‘know’ algorithms from those who can apply them under pressure. It’s a staple in Google, Meta, and Amazon onsites—master it, and you prove you can optimize brute-force solutions into linear time using stacks, a skill that directly translates to real-world system design."
Before tackling this problem, ensure you’re fluent in: 1. Stacks (LIFO) – How to push/pop and track indices. 2. Monotonic Stacks – Maintaining a stack where elements are in increasing/decreasing order. 3. Time Complexity Analysis – Recognizing O(n²) vs. O(n) trade-offs.
0
width = current_index - stack[-1] - 1
left
right
Mental Checklist (Run This for Every Histogram Problem):
heights
Output: Integer (max area of a rectangle under the histogram).
Brute-Force Intuition
Time: O(n²) → Too slow for large inputs (n > 10⁴).
Optimize with Monotonic Stack
max_area = 0
max_area
Push current bar’s index onto the stack.
Calculate Width Correctly
For a popped bar at index i with height h:
i
h
width = current_index
Edge Cases
All bars same height → return n height.
n height
Time/Space Complexity
Input: heights = [2, 1, 5, 6, 2, 3] Output: 10 (Rectangle formed by bars of height 5 and 6).
heights = [2, 1, 5, 6, 2, 3]
10
5
6
stack = []
i = 0
i=6
i=0
[0]
i=1
heights[1] = 1
heights[0] = 2
height = 2
width = 1 - (-1) - 1 = 1
area = 2 1 = 2
max_area = 2
1
[1]
i=2
2
[1, 2]
i=3
3
[1, 2, 3]
i=4
heights[4] = 2
heights[3] = 6
height = 6
width = 4 - 2 - 1 = 1
area = 6 1 = 6
max_area = 6
heights[2] = 5
height = 5
width = 4 - 1 - 1 = 2
area = 5 2 = 10
max_area = 10
4
[1, 4]
i=5
[1, 4, 5]
area = 3 (6 - 4 - 1) = 3
area = 2 (6 - 1 - 1) = 8
area = 1 6 = 6
def largestRectangleArea(heights): stack = [] max_area = 0 heights.append(0) # Sentinel for i, h in enumerate(heights): while stack and heights[stack[-1]] > h: height = heights[stack.pop()] width = i if not stack else i - stack[-1] - 1 max_area = max(max_area, height width) stack.append(i) return max_area
Input: heights = [3, 3, 3, 3] Output: 12 (Entire histogram is a rectangle).
heights = [3, 3, 3, 3]
12
width = n
def largestRectangleArea(heights): stack = [] max_area = 0 heights.append(0) for i, h in enumerate(heights): while stack and heights[stack[-1]] > h: height = heights[stack.pop()] width = i if not stack else i - stack[-1] - 1 max_area = max(max_area, height width) stack.append(i) return max_area
Output: 12 (4 bars × height 3).
Problem: Given a rows x cols binary matrix, find the largest rectangle containing only 1s. LeetCode: 85 (Follow-up to Histogram problem).
rows x cols
matrix[i][j] == '1'
heights[j] += 1
heights[j] = 0
def maximalRectangle(matrix): if not matrix: return 0 rows, cols = len(matrix), len(matrix[0]) heights = [0] cols max_area = 0 for i in range(rows): for j in range(cols): heights[j] = heights[j] + 1 if matrix[i][j] == '1' else 0 stack = [] heights.append(0) for k, h in enumerate(heights): while stack and heights[stack[-1]] > h: height = heights[stack.pop()] width = k if not stack else k - stack[-1] - 1 max_area = max(max_area, height width) stack.append(k) heights.pop() # Remove sentinel return max_area
i - stack[-1]
i - stack[-1] - 1
heights = []
if not heights: return 0
[0, 0, 0]
"Here’s your 60-second cheat sheet for ‘Largest Rectangle in Histogram’: 1. Brute-force is O(n²)—too slow. You need O(n). 2. Use a monotonic stack to track indices of bars in increasing order. 3. Append a sentinel 0 to force the stack to empty at the end. 4. When popping, calculate area as height × width, where width = current_index - stack[-1] - 1. 5. Edge cases: Empty input, single bar, all bars same height. 6. Follow-up: For 2D matrices, treat each row as a histogram and reuse the stack solution.
height × width
Walk in confident—you’ve got this!
[2, 1, 5, 6, 2, 3]
This framework is directly transferable to other monotonic stack problems (e.g., "Next Greater Element"). Good luck! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.