Challenge - 5 Problems
Histogram Mastery
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 Java code that calculates the largest rectangle area in a histogram using a stack?
DSA Java
import java.util.Stack; public class Histogram { public static int largestRectangleArea(int[] heights) { Stack<Integer> stack = new Stack<>(); int maxArea = 0; int i = 0; while (i < heights.length) { if (stack.isEmpty() || heights[i] >= heights[stack.peek()]) { stack.push(i++); } else { int top = stack.pop(); int width = stack.isEmpty() ? i : i - stack.peek() - 1; maxArea = Math.max(maxArea, heights[top] * width); } } while (!stack.isEmpty()) { int top = stack.pop(); int width = stack.isEmpty() ? i : i - stack.peek() - 1; maxArea = Math.max(maxArea, heights[top] * width); } return maxArea; } public static void main(String[] args) { int[] histogram = {2, 1, 5, 6, 2, 3}; System.out.println(largestRectangleArea(histogram)); } }
Attempts:
2 left
💡 Hint
Think about the largest rectangle that can be formed by consecutive bars in the histogram.
✗ 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 quickly.
✗ Incorrect
The stack stores indices of bars in increasing height order, allowing quick calculation of rectangle widths when a smaller bar is encountered.
🔧 Debug
advancedIdentify the Error in Stack Implementation
What error will occur if we replace 'stack.peek()' with 'stack.pop()' inside the width calculation in the code below?
while (!stack.isEmpty()) {
int top = stack.pop();
int width = stack.isEmpty() ? i : i - stack.peek() - 1;
maxArea = Math.max(maxArea, heights[top] * width);
}
Attempts:
2 left
💡 Hint
Consider what happens if you remove elements from the stack multiple times unintentionally.
✗ Incorrect
Using pop() instead of peek() removes an extra element from the stack, causing incorrect width calculation.
❓ Predict Output
advancedOutput with All Bars of Equal Height
What is the output of the largest rectangle area for the histogram with all bars of height 4: {4, 4, 4, 4}?
DSA Java
import java.util.Stack; public class HistogramEqual { public static int largestRectangleArea(int[] heights) { Stack<Integer> stack = new Stack<>(); int maxArea = 0; int i = 0; while (i < heights.length) { if (stack.isEmpty() || heights[i] >= heights[stack.peek()]) { stack.push(i++); } else { int top = stack.pop(); int width = stack.isEmpty() ? i : i - stack.peek() - 1; maxArea = Math.max(maxArea, heights[top] * width); } } while (!stack.isEmpty()) { int top = stack.pop(); int width = stack.isEmpty() ? i : i - stack.peek() - 1; maxArea = Math.max(maxArea, heights[top] * width); } return maxArea; } public static void main(String[] args) { int[] histogram = {4, 4, 4, 4}; System.out.println(largestRectangleArea(histogram)); } }
Attempts:
2 left
💡 Hint
All bars have the same height, so the largest rectangle covers all bars.
✗ Incorrect
The largest rectangle covers all 4 bars with height 4, area = 4 * 4 = 16.
🚀 Application
expertMaximum Rectangle Area in a Binary Matrix Using Histogram Logic
Given a binary matrix, we can find the largest rectangle of 1s by treating each row as a histogram and applying the largest rectangle in histogram algorithm. Which step is NOT part of this approach?
Attempts:
2 left
💡 Hint
Think about how histogram heights are updated row by row.
✗ Incorrect
We do NOT reset histogram heights to zero after each row; instead, we accumulate heights of consecutive 1s to build histograms.
