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#
using System; using System.Collections.Generic; class Program { static int LargestRectangleArea(int[] heights) { Stack<int> stack = new Stack<int>(); int maxArea = 0; int i = 0; while (i < heights.Length) { if (stack.Count == 0 || heights[i] >= heights[stack.Peek()]) { stack.Push(i); i++; } else { int top = stack.Pop(); int width = stack.Count == 0 ? i : i - stack.Peek() - 1; int area = heights[top] * width; if (area > maxArea) maxArea = area; } } while (stack.Count > 0) { int top = stack.Pop(); int width = stack.Count == 0 ? i : i - stack.Peek() - 1; int area = heights[top] * width; if (area > maxArea) maxArea = area; } return maxArea; } static void Main() { int[] histogram = {2, 1, 5, 6, 2, 3}; Console.WriteLine(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 for Uniform Height Histogram
What is the output of the largest rectangle area calculation for a histogram where all bars have the same height?
DSA C#
using System; using System.Collections.Generic; class Program { static int LargestRectangleArea(int[] heights) { Stack<int> stack = new Stack<int>(); int maxArea = 0; int i = 0; while (i < heights.Length) { if (stack.Count == 0 || heights[i] >= heights[stack.Peek()]) { stack.Push(i); i++; } else { int top = stack.Pop(); int width = stack.Count == 0 ? i : i - stack.Peek() - 1; int area = heights[top] * width; if (area > maxArea) maxArea = area; } } while (stack.Count > 0) { int top = stack.Pop(); int width = stack.Count == 0 ? i : i - stack.Peek() - 1; int area = heights[top] * width; if (area > maxArea) maxArea = area; } return maxArea; } static void Main() { int[] histogram = {4, 4, 4, 4}; Console.WriteLine(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.
🔧 Debug
advancedIdentify the Runtime Error in Stack Usage
What runtime error will occur when running this modified largest rectangle code?
DSA C#
using System; using System.Collections.Generic; class Program { static int LargestRectangleArea(int[] heights) { Stack<int> stack = new Stack<int>(); int maxArea = 0; int i = 0; while (i <= heights.Length) { int currentHeight = (i == heights.Length) ? 0 : heights[i]; if (stack.Count == 0 || currentHeight >= heights[stack.Peek()]) { stack.Push(i); i++; } else { int top = stack.Pop(); int width = stack.Count == 0 ? i : i - stack.Peek() - 1; int area = heights[top] * width; if (area > maxArea) maxArea = area; } } return maxArea; } static void Main() { int[] histogram = {2, 4}; Console.WriteLine(LargestRectangleArea(histogram)); } }
Attempts:
2 left
💡 Hint
Consider what happens when popping from an empty stack.
✗ Incorrect
The code tries to pop from the stack without checking if it's empty, causing InvalidOperationException.
🧠 Conceptual
advancedWhy Use a Stack for Largest Rectangle in Histogram?
Why is a stack used in the algorithm to find the largest rectangle in a histogram?
Attempts:
2 left
💡 Hint
Think about how the stack helps find boundaries for rectangles.
✗ Incorrect
The stack keeps indices of bars in increasing height order, allowing quick calculation of rectangle widths when a smaller bar is found.
🚀 Application
expertLargest Rectangle Area for Complex Histogram
Given the histogram 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 formed by bars 5,4,5 or 6,2,5,4.
✗ Incorrect
The largest rectangle area is 12, formed by bars with heights 5,4,5 spanning width 3.
