Challenge - 5 Problems
Histogram Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Largest Rectangle Calculation
What is the output of the following C++ code that calculates the largest rectangle area in a histogram using a stack?
DSA C++
int largestRectangleArea(vector<int>& heights) { stack<int> s; int max_area = 0; int i = 0; while (i < heights.size()) { if (s.empty() || heights[s.top()] <= heights[i]) { s.push(i++); } else { int top = s.top(); s.pop(); int area = heights[top] * (s.empty() ? i : i - s.top() - 1); max_area = max(max_area, area); } } while (!s.empty()) { int top = s.top(); s.pop(); int area = heights[top] * (s.empty() ? i : i - s.top() - 1); max_area = max(max_area, area); } return max_area; } int main() { vector<int> hist = {2, 1, 5, 6, 2, 3}; cout << largestRectangleArea(hist) << endl; return 0; }
Attempts:
2 left
💡 Hint
Think about the largest rectangle that can be formed by consecutive bars.
✗ Incorrect
The largest rectangle area in the histogram {2,1,5,6,2,3} is 10, formed by bars with heights 5 and 6 (width 2).
🧠 Conceptual
intermediateUnderstanding Stack Usage in Histogram Problem
Why do we use a stack to solve the Largest Rectangle in Histogram problem efficiently?
Attempts:
2 left
💡 Hint
Think about how the stack helps find the width of rectangles.
✗ Incorrect
The stack stores indices of bars in increasing height order. When a smaller bar is found, it means the rectangle with the height of the popped bar ends, allowing area calculation.
🔧 Debug
advancedIdentify the Error in Stack-Based Histogram Code
What error will occur when running this code snippet for largest rectangle in histogram?
DSA C++
int largestRectangleArea(vector<int>& heights) { stack<int> s; int max_area = 0; int i = 0; while (i <= heights.size()) { if (s.empty() || heights[s.top()] <= heights[i]) { s.push(i++); } else { int top = s.top(); s.pop(); int area = heights[top] * (s.empty() ? i : i - s.top() - 1); max_area = max(max_area, area); } } return max_area; }
Attempts:
2 left
💡 Hint
Check the loop condition and array access carefully.
✗ Incorrect
The loop runs while i <= heights.size(), but heights[i] is accessed without checking if i < heights.size(), causing out-of-range access.
❓ Predict Output
advancedOutput of Largest Rectangle for Uniform Heights
What is the output of the following code for a histogram with uniform heights?
DSA C++
int largestRectangleArea(vector<int>& heights) { stack<int> s; int max_area = 0; int i = 0; while (i < heights.size()) { if (s.empty() || heights[s.top()] <= heights[i]) { s.push(i++); } else { int top = s.top(); s.pop(); int area = heights[top] * (s.empty() ? i : i - s.top() - 1); max_area = max(max_area, area); } } while (!s.empty()) { int top = s.top(); s.pop(); int area = heights[top] * (s.empty() ? i : i - s.top() - 1); max_area = max(max_area, area); } return max_area; } int main() { vector<int> hist = {4, 4, 4, 4}; cout << largestRectangleArea(hist) << endl; return 0; }
Attempts:
2 left
💡 Hint
All bars have the same height, so the largest rectangle covers all bars.
✗ Incorrect
With all bars height 4 and count 4, largest rectangle area is 4 * 4 = 16.
🧠 Conceptual
expertTime Complexity of Stack-Based Largest Rectangle Algorithm
What is the time complexity of the stack-based algorithm to find the largest rectangle in a histogram with n bars?
Attempts:
2 left
💡 Hint
Each bar is pushed and popped at most once.
✗ Incorrect
Each bar is pushed onto the stack once and popped once, so total operations are proportional to n, making time complexity O(n).
