Bird
0
0
DSA Cprogramming~20 mins

Largest Rectangle in Histogram Using Stack in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Histogram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Largest Rectangle Calculation
What is the output of the following code that calculates the largest rectangle area in a histogram using a stack?
DSA C
int largestRectangleArea(int* heights, int heightsSize) {
    int maxArea = 0;
    int stack[heightsSize];
    int top = -1;
    int i = 0;
    while (i < heightsSize) {
        if (top == -1 || heights[i] >= heights[stack[top]]) {
            stack[++top] = i++;
        } else {
            int h = heights[stack[top--]];
            int width = top == -1 ? i : i - stack[top] - 1;
            int area = h * width;
            if (area > maxArea) maxArea = area;
        }
    }
    while (top != -1) {
        int h = heights[stack[top--]];
        int width = top == -1 ? i : i - stack[top] - 1;
        int area = h * width;
        if (area > maxArea) maxArea = area;
    }
    return maxArea;
}

int main() {
    int histogram[] = {2, 1, 5, 6, 2, 3};
    int size = sizeof(histogram) / sizeof(histogram[0]);
    int result = largestRectangleArea(histogram, size);
    printf("%d\n", result);
    return 0;
}
A8
B12
C6
D10
Attempts:
2 left
💡 Hint
Think about the largest rectangle that can be formed by consecutive bars.
🧠 Conceptual
intermediate
1:30remaining
Understanding Stack Usage in Histogram Problem
Why do we use a stack to solve the Largest Rectangle in Histogram problem efficiently?
ATo keep track of indices of bars in increasing height order for quick area calculation
BTo sort the histogram bars by height before calculating areas
CTo store the maximum area found so far
DTo reverse the histogram array for easier processing
Attempts:
2 left
💡 Hint
Think about how the stack helps find the width of rectangles.
🔧 Debug
advanced
2:00remaining
Identify the Error in Stack Implementation
What error will occur if we forget to check 'top == -1' before accessing 'stack[top]' in the largest rectangle code?
DSA C
if (heights[i] >= heights[stack[top]]) {
    stack[++top] = i++;
} else {
    int h = heights[stack[top--]];
    int width = i - stack[top] - 1; // Potential error here
    int area = h * width;
    if (area > maxArea) maxArea = area;
}
AThe width calculation will always be zero
BThe code will run correctly without any error
CAccessing stack[top] when top == -1 causes undefined behavior or crash
DThe stack will overflow
Attempts:
2 left
💡 Hint
What happens if you access an array with a negative index?
Predict Output
advanced
1:30remaining
Output with All Bars of Same Height
What is the output of the largest rectangle area code when all histogram bars have the same height?
DSA C
int histogram[] = {4, 4, 4, 4, 4};
int size = sizeof(histogram) / sizeof(histogram[0]);
int result = largestRectangleArea(histogram, size);
printf("%d\n", result);
A20
B16
C4
D5
Attempts:
2 left
💡 Hint
Largest rectangle covers all bars since they have equal height.
🚀 Application
expert
3:00remaining
Maximum Rectangle Area in a Binary Matrix Using Histogram Logic
Given a binary matrix, we convert each row into a histogram of heights of consecutive 1s. Which approach correctly applies the largest rectangle in histogram logic to find the maximum rectangle of 1s in the matrix?
ASum all 1s in the matrix and multiply by the number of rows
BFor each row, update heights array by adding 1 for 1s and resetting to 0 for 0s, then apply largest rectangle area function
CApply largest rectangle area function only on the first row's histogram
DCount the maximum number of consecutive 1s in any column without using histogram logic
Attempts:
2 left
💡 Hint
Think about how each row builds on the previous row's histogram.