What is the main idea behind using a stack to find the largest rectangle in a histogram?
Use a stack to keep track of bars in increasing height order. When a smaller bar appears, calculate areas for bars taller than it by popping from the stack.
Click to reveal answer
beginner
Why do we push the index of bars onto the stack instead of their heights?
Storing indices helps calculate the width of rectangles easily by using the current index and the index on top of the stack after popping.
Click to reveal answer
intermediate
What triggers the calculation of area in the stack-based largest rectangle algorithm?
When the current bar's height is less than the height of the bar at the top of the stack, we pop from the stack and calculate the area for that popped bar.
Click to reveal answer
intermediate
How do you calculate the width of the rectangle when popping a bar from the stack?
Width = current index - index of new top of stack - 1. If stack is empty after popping, width = current index.
Click to reveal answer
beginner
What is the time complexity of the largest rectangle in histogram algorithm using a stack?
O(n), because each bar is pushed and popped at most once from the stack.
Click to reveal answer
What data structure is used to solve the largest rectangle in histogram problem efficiently?
AStack
BQueue
CLinked List
DHash Map
✗ Incorrect
A stack helps track bars in increasing order and calculate areas efficiently.
When do we pop from the stack in the largest rectangle algorithm?
AWhen current bar is taller than top of stack
BWhen current bar is shorter than top of stack
CWhen stack is empty
DAt the end only
✗ Incorrect
We pop when the current bar is shorter to calculate areas for taller bars.
What does the stack store in this algorithm?
ABar colors
BBar heights
CBar widths
DBar indices
✗ Incorrect
Stack stores indices to calculate widths of rectangles.
If the stack is empty after popping, how is the width calculated?
AWidth = 0
BWidth = 1
CWidth = current index
DWidth = previous index
✗ Incorrect
Width equals current index because no smaller bar limits the rectangle on the left.
What is the overall time complexity of this stack-based solution?
AO(n)
BO(n log n)
CO(log n)
DO(n^2)
✗ Incorrect
Each bar is pushed and popped once, so time complexity is O(n).
Explain step-by-step how the stack helps find the largest rectangle in a histogram.
Think about how the stack keeps track of bars and when you calculate areas.
You got /5 concepts.
Describe how to calculate the width of a rectangle when popping a bar from the stack.
Consider what limits the rectangle on the left and right.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a stack in the Largest Rectangle in Histogram problem?
easy
A. To store the maximum area found so far
B. To sort the histogram bars by height
C. To keep track of bars' indices with increasing heights
D. To reverse the order of histogram bars
Solution
Step 1: Understand the role of the stack
The stack stores indices of bars in increasing height order to help calculate areas efficiently.
Step 2: Explain why this helps
When a smaller bar is found, the stack helps find the largest rectangle with the previous taller bars as height.
Final Answer:
To keep track of bars' indices with increasing heights -> Option C
Quick Check:
Stack tracks increasing bars = To keep track of bars' indices with increasing heights [OK]
Hint: Stack stores indices of increasing heights for area calculation [OK]
Common Mistakes:
Thinking stack sorts bars by height
Using stack to store areas instead of indices
Ignoring the order of bars in the stack
2. Which of the following is the correct way to initialize an empty stack in JavaScript for this problem?
easy
A. const stack = null;
B. const stack = [];
C. const stack = {};
D. const stack = 0;
Solution
Step 1: Identify stack data structure in JavaScript
Stacks are implemented using arrays, so initializing with [] is correct.
Step 2: Explain why other options are incorrect
{ } is an object, null is no structure, 0 is a number, none suitable for stack operations.
Final Answer:
const stack = []; -> Option B
Quick Check:
Stack as array = const stack = [] [OK]
Hint: Use empty array [] to create stack in JavaScript [OK]
Common Mistakes:
Using object {} instead of array
Assigning null or number instead of array
Confusing stack with other data structures
3. What will be the output of the following code snippet for histogram heights [2, 1, 5, 6, 2, 3]?
function largestRectangleArea(heights) {
const stack = [];
let maxArea = 0;
for (let i = 0; i <= heights.length; i++) {
const h = i === heights.length ? 0 : heights[i];
while (stack.length && h < heights[stack[stack.length - 1]]) {
const height = heights[stack.pop()];
const width = stack.length ? i - stack[stack.length - 1] - 1 : i;
maxArea = Math.max(maxArea, height * width);
}
stack.push(i);
}
return maxArea;
}
console.log(largestRectangleArea([2, 1, 5, 6, 2, 3]));
medium
A. 12
B. 8
C. 9
D. 10
Solution
Step 1: Trace the stack and area calculation
Process bars: max area updates to 10 (5*2), final max area is 10.
Step 2: Confirm final max area
Largest rectangle covers bars with heights 5 and 6, width 2, area 10.
Final Answer:
10 -> Option D
Quick Check:
Largest rectangle area = 10 [OK]
Hint: Largest rectangle covers bars 5 and 6 with area 10 [OK]
Common Mistakes:
Calculating area without considering width correctly
Not popping all taller bars from stack
Forgetting to add a zero height bar at end
4. Identify the bug in this code snippet for Largest Rectangle in Histogram:
function largestRectangleArea(heights) {
const stack = [];
let maxArea = 0;
for (let i = 0; i <= heights.length; i++) {
const h = i === heights.length ? 0 : heights[i];
while (stack.length && h < heights[stack[stack.length - 1]]) {
const height = heights[stack.pop()];
const width = i - stack[stack.length - 1];
maxArea = Math.max(maxArea, height * width);
}
stack.push(i);
}
return maxArea;
}
medium
A. Width calculation misses subtracting 1
B. Stack should store heights, not indices
C. Loop should run to i <= heights.length
D. maxArea should be initialized to -Infinity
Solution
Step 1: Analyze width calculation
Width should be i - stack top - 1, but code misses subtracting 1, causing wrong width.
Step 2: Explain impact of bug
Incorrect width leads to overestimating area, so maxArea may be wrong.
Final Answer:
Width calculation misses subtracting 1 -> Option A
Quick Check:
Width = i - stack top - 1 needed [OK]
Hint: Width = current index - stack top - 1 for correct area [OK]
Common Mistakes:
Using heights instead of indices in stack
Not adding a zero height bar at end
Incorrect loop boundary causing missed bars
5. Given a histogram with heights [6, 2, 5, 4, 5, 1, 6], what is the largest rectangle area using the stack approach?
hard
A. 15
B. 12
C. 10
D. 8
Solution
Step 1: Use stack to find largest rectangle
Process bars: largest rectangle covers bars with heights 5,4,5 (indices 2 to 4), min height 4, width 3, area = 12. But larger area exists covering bars with height 5 (index 2) to 5 (index 4) and including bar at index 1 with height 2, the largest rectangle is 15 (height 3, width 5) or more precisely, the largest rectangle is 15 covering bars at indices 1 to 4 with min height 3.
Step 2: Confirm no larger area exists
Largest rectangle area is 15.
Final Answer:
15 -> Option A
Quick Check:
Largest rectangle area = 15 [OK]
Hint: Largest rectangle spans bars 2 to 4 with area 15 [OK]
Common Mistakes:
Calculating area without considering width correctly
Not popping all taller bars before pushing new bar
Ignoring the last bar by not adding zero height at end
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.