0
0
DSA Pythonprogramming~20 mins

Largest Rectangle in Histogram Using Stack in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Histogram Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Largest Rectangle Calculation
What is the output of the following code that calculates the largest rectangle area in a histogram using a stack?
DSA Python
def largest_rectangle_area(heights):
    stack = []
    max_area = 0
    for i, h in enumerate(heights + [0]):
        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

print(largest_rectangle_area([2,1,5,6,2,3]))
A10
B12
C9
D8
Attempts:
2 left
💡 Hint
Think about the largest rectangle that can be formed by combining bars.
🧠 Conceptual
intermediate
1:30remaining
Understanding Stack Usage in Histogram Problem
Why do we add a zero height bar at the end of the histogram array when using a stack to find the largest rectangle?
ATo avoid index errors during iteration
BTo increase the maximum area by adding an extra bar
CTo ensure all bars are popped from the stack by the end of the iteration
DTo mark the start of the histogram
Attempts:
2 left
💡 Hint
Think about what happens to bars still in the stack after the main loop.
🔧 Debug
advanced
2:00remaining
Identify the Error in Stack Implementation
What error will occur when running this code snippet for largest rectangle in histogram?
DSA Python
def largest_rectangle_area(heights):
    stack = []
    max_area = 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

print(largest_rectangle_area([2,1,5,6,2,3]))
ANo error, outputs 10
BIndexError due to empty stack access
CSyntaxError due to missing colon
DIncorrect maximum area calculation
Attempts:
2 left
💡 Hint
Check how width is calculated when stack is not empty.
Predict Output
advanced
1:30remaining
Output of Largest Rectangle for Uniform Heights
What is the output of the largest rectangle area for a histogram with uniform heights?
DSA Python
def largest_rectangle_area(heights):
    stack = []
    max_area = 0
    for i, h in enumerate(heights + [0]):
        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

print(largest_rectangle_area([4,4,4,4,4]))
A20
B16
C4
D0
Attempts:
2 left
💡 Hint
All bars have the same height, so the largest rectangle covers all bars.
🚀 Application
expert
3:00remaining
Applying Largest Rectangle in Histogram to Maximal Rectangle in Binary Matrix
Given a binary matrix, we use the largest rectangle in histogram algorithm on each row's histogram of consecutive ones. What is the maximum rectangle area in the matrix below? Matrix: [[1,0,1,0,0], [1,0,1,1,1], [1,1,1,1,1], [1,0,0,1,0]]
A8
B6
C9
D10
Attempts:
2 left
💡 Hint
Convert each row to a histogram of heights of consecutive ones and apply largest rectangle algorithm.