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 JavaScript code that calculates the largest rectangle area in a histogram?
DSA Javascript
function largestRectangleArea(heights) {
const stack = [];
let maxArea = 0;
heights.push(0);
for (let i = 0; i < heights.length; i++) {
while (stack.length && heights[i] < 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]));Attempts:
2 left
💡 Hint
Think about the largest rectangle that can be formed by consecutive bars.
✗ Incorrect
The largest rectangle area is 10, formed by bars with heights 5 and 6 (width 2, height 5).
🧠 Conceptual
intermediateUnderstanding Stack Usage in Histogram Problem
Why do we add a zero height bar at the end of the histogram array in the stack-based largest rectangle algorithm?
Attempts:
2 left
💡 Hint
Think about what happens at the end of the iteration.
✗ Incorrect
Adding a zero height bar forces the algorithm to pop all remaining bars from the stack, ensuring all possible rectangles are considered.
🔧 Debug
advancedIdentify the Error in Stack Implementation
What error will occur when running this code snippet for largest rectangle in histogram?
DSA Javascript
function largestRectangleArea(heights) {
const stack = [];
let maxArea = 0;
for (let i = 0; i <= heights.length; i++) {
while (stack.length && heights[i] < 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]));Attempts:
2 left
💡 Hint
Check the loop boundary and array access.
✗ Incorrect
The loop runs to i <= heights.length but heights[heights.length] is undefined, causing a TypeError when compared.
❓ Predict Output
advancedOutput of Modified Histogram Input
What is the output of this code for the input [6, 2, 5, 4, 5, 1, 6]?
DSA Javascript
function largestRectangleArea(heights) {
const stack = [];
let maxArea = 0;
heights.push(0);
for (let i = 0; i < heights.length; i++) {
while (stack.length && heights[i] < 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([6, 2, 5, 4, 5, 1, 6]));Attempts:
2 left
💡 Hint
Consider the largest rectangle formed by consecutive bars.
✗ Incorrect
The largest rectangle area is 12, formed by bars with heights 5, 4, and 5 (width 3, height 4).
🧠 Conceptual
expertTime Complexity of Stack-Based Histogram Algorithm
What is the time complexity of the stack-based algorithm for finding the largest rectangle in a histogram with n bars?
Attempts:
2 left
💡 Hint
Each bar is pushed and popped at most once.
✗ Incorrect
The algorithm processes each bar once pushing and popping from the stack, resulting in linear time complexity O(n).
