Bird
0
0
DSA Cprogramming~10 mins

Largest Rectangle in Histogram Using Stack in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the stack pointer.

DSA C
int stack[1000];
int top = [1];
Drag options to blanks, or click blank then click option'
A-1
B0
C1
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing top to 0 causes off-by-one errors.
Using a large number like 1000 causes invalid stack state.
2fill in blank
medium

Complete the code to push an index onto the stack.

DSA C
stack[++top] = [1];
Drag options to blanks, or click blank then click option'
Aheight[i]
Bi
Ctop
Dstack[top]
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing height[i] instead of index causes wrong calculations.
Pushing top or stack[top] is incorrect.
3fill in blank
hard

Fix the error in the condition to pop from stack when current height is less.

DSA C
while (top != -1 && height[stack[top]] [1] height[i]) {
    // pop and calculate area
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes no popping and wrong area calculation.
Using '==' or '>=' changes logic incorrectly.
4fill in blank
hard

Fill both blanks to calculate width correctly after popping from stack.

DSA C
int width = top == -1 ? [1] : [2] - stack[top] - 1;
Drag options to blanks, or click blank then click option'
Ai
Ctop
Dstack[top]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' or 'stack[top]' instead of 'i' causes wrong width.
Not subtracting 1 leads to off-by-one errors.
5fill in blank
hard

Fill all three blanks to finalize max area calculation inside the loop.

DSA C
int height_popped = height[stack[top--]];
int width = top == -1 ? [1] : [2] - stack[top] - 1;
max_area = max(max_area, height_popped * [3]);
Drag options to blanks, or click blank then click option'
Ai
Cwidth
Dtop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' instead of 'width' in area calculation.
Mixing up variables for width calculation.