Recall & Review
beginner
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 with bars popped from the stack as the smallest bar until the stack is in order again.
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 subtracting indices when popping bars from the stack.
Click to reveal answer
intermediate
What happens when we encounter a bar shorter than the bar at the top of the stack?
We pop bars from the stack and calculate the area for each popped bar using its height and the width determined by current index and the new top of the stack.
Click to reveal answer
intermediate
How do we handle remaining bars in the stack after processing all histogram bars?
Pop all remaining bars and calculate area for each using the total histogram length as the right boundary.
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?
✗ Incorrect
A stack is used to keep track of bars in increasing order to calculate areas efficiently.
When do we calculate the area of a rectangle in the stack-based approach?
✗ Incorrect
We calculate area when we find a bar shorter than the top of the stack, popping taller bars.
What does the width of the rectangle depend on when popping a bar from the stack?
✗ Incorrect
Width is calculated as current index minus the index of new top of stack minus one.
What is pushed onto the stack during the algorithm?
✗ Incorrect
Indices of bars are pushed to help calculate widths later.
What is the overall time complexity of the stack-based largest rectangle algorithm?
✗ Incorrect
Each bar is pushed and popped once, so the time complexity is linear 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 areas are calculated.
You got /5 concepts.
Describe how to calculate the width of the rectangle when popping a bar from the stack.
Remember the stack stores indices, not heights.
You got /3 concepts.
