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 TypeScript code that calculates the largest rectangle area in a histogram?
DSA Typescript
function largestRectangleArea(heights: number[]): number {
const stack: number[] = [];
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 === 0 ? i : i - stack[stack.length - 1] - 1;
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 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 does the algorithm use a stack to solve the Largest Rectangle in Histogram problem?
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 calculates areas using bars popped from the stack.
🔧 Debug
advancedIdentify the Error in Stack Implementation
What error will occur when running this modified code snippet for largest rectangle area?
DSA Typescript
function largestRectangleArea(heights: number[]): number {
const stack: number[] = [];
let maxArea = 0;
for (let i = 0; i <= heights.length; i++) {
while (stack.length && (i === heights.length || heights[i] < heights[stack[stack.length - 1]])) {
const height = heights[stack.pop()!];
const width = stack.length === 0 ? i : i - stack[stack.length - 1] - 1;
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 how width is calculated when the stack is not empty.
✗ Incorrect
The width calculation is off by 1, causing an incorrect larger area 15 instead of 10.
📝 Syntax
advancedSyntax Error in Stack-Based Histogram Code
Which option contains a syntax error in the TypeScript code for largest rectangle in histogram?
DSA Typescript
function largestRectangleArea(heights: number[]): number {
const stack: number[] = [];
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 === 0 ? i : i - stack[stack.length - 1] - 1;
maxArea = Math.max(maxArea, height * width);
}
stack.push(i);
}
return maxArea;
}Attempts:
2 left
💡 Hint
Check line endings carefully.
✗ Incorrect
The line 'const stack: number[] = []' is missing a semicolon, causing a syntax error in TypeScript.
🚀 Application
expertMaximum Rectangle Area for Large Histogram
Given a histogram with heights [6, 2, 5, 4, 5, 1, 6], what is the largest rectangle area calculated by the stack-based algorithm?
Attempts:
2 left
💡 Hint
Consider rectangles spanning multiple bars with minimum height.
✗ Incorrect
The largest rectangle area is 12, formed by bars at indices 2,3,4 with heights 5,4,5 (min height 4 * width 3 = 12).
