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 Go code that calculates the largest rectangle area in a histogram using a stack?
DSA Go
package main import "fmt" func largestRectangleArea(heights []int) int { stack := []int{} maxArea := 0 heights = append(heights, 0) // Append zero to flush stack at end for i, h := range heights { for len(stack) > 0 && heights[stack[len(stack)-1]] > h { height := heights[stack[len(stack)-1]] stack = stack[:len(stack)-1] width := i if len(stack) > 0 { width = i - stack[len(stack)-1] - 1 } area := height * width if area > maxArea { maxArea = area } } stack = append(stack, i) } return maxArea } func main() { histogram := []int{2, 1, 5, 6, 2, 3} fmt.Println(largestRectangleArea(histogram)) }
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).
❓ Predict Output
intermediateOutput of Largest Rectangle for Uniform Heights
What is the output of the following Go code for a histogram with uniform heights?
DSA Go
package main import "fmt" func largestRectangleArea(heights []int) int { stack := []int{} maxArea := 0 heights = append(heights, 0) for i, h := range heights { for len(stack) > 0 && heights[stack[len(stack)-1]] > h { height := heights[stack[len(stack)-1]] stack = stack[:len(stack)-1] width := i if len(stack) > 0 { width = i - stack[len(stack)-1] - 1 } area := height * width if area > maxArea { maxArea = area } } stack = append(stack, i) } return maxArea } func main() { histogram := []int{4, 4, 4, 4} fmt.Println(largestRectangleArea(histogram)) }
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, the largest rectangle area is 4 * 4 = 16.
❓ Predict Output
advancedOutput for Decreasing Heights Histogram
What is the output of this Go code for a histogram with strictly decreasing heights?
DSA Go
package main import "fmt" func largestRectangleArea(heights []int) int { stack := []int{} maxArea := 0 heights = append(heights, 0) for i, h := range heights { for len(stack) > 0 && heights[stack[len(stack)-1]] > h { height := heights[stack[len(stack)-1]] stack = stack[:len(stack)-1] width := i if len(stack) > 0 { width = i - stack[len(stack)-1] - 1 } area := height * width if area > maxArea { maxArea = area } } stack = append(stack, i) } return maxArea } func main() { histogram := []int{6, 5, 4, 3, 2, 1} fmt.Println(largestRectangleArea(histogram)) }
Attempts:
2 left
💡 Hint
Consider the largest rectangle formed by the first few bars.
✗ Incorrect
The largest rectangle area is 12, formed by the first three bars with heights 6, 5, and 4 (minimum height 4, width 3).
🧠 Conceptual
advancedStack Usage in Largest Rectangle Algorithm
Why does the largest rectangle in histogram algorithm use a stack to store indices of bars?
Attempts:
2 left
💡 Hint
Think about how the algorithm finds when to calculate areas.
✗ Incorrect
The stack stores indices of bars in increasing height order. When a smaller bar is found, it calculates areas for bars taller than the current one.
❓ Predict Output
expertOutput for Complex Histogram with Multiple Peaks
What is the output of the following Go code for a histogram with multiple peaks and valleys?
DSA Go
package main import "fmt" func largestRectangleArea(heights []int) int { stack := []int{} maxArea := 0 heights = append(heights, 0) for i, h := range heights { for len(stack) > 0 && heights[stack[len(stack)-1]] > h { height := heights[stack[len(stack)-1]] stack = stack[:len(stack)-1] width := i if len(stack) > 0 { width = i - stack[len(stack)-1] - 1 } area := height * width if area > maxArea { maxArea = area } } stack = append(stack, i) } return maxArea } func main() { histogram := []int{2, 4, 2, 1, 10, 6, 10} fmt.Println(largestRectangleArea(histogram)) }
Attempts:
2 left
💡 Hint
Look for the largest rectangle that spans multiple bars including the peak 10.
✗ Incorrect
The largest rectangle area is 18, formed by bars at indices 4, 5, and 6 with heights 10, 6, and 10 (width 3, height 6).
