Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push the current index onto the stack.
DSA C#
stack.[1](i);Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop instead of Push.
Using Peek which only reads the top element.
Using Clear which empties the stack.
✗ Incorrect
We use stack.Push(i) to add the current index to the stack.
2fill in blank
mediumComplete the condition to check if the current bar is lower than the bar at the top of the stack.
DSA C#
while (stack.Count > 0 && heights[i] [1] heights[stack.Peek()])
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which is the opposite condition.
Using '==' which only checks equality.
Using '>=' which includes equal heights.
✗ Incorrect
We compare if the current height is less than the height at the top index of the stack.
3fill in blank
hardFix the error in calculating the width when the stack is empty after popping.
DSA C#
int width = stack.Count == 0 ? [1] : i - stack.Peek() - 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 which gives zero width.
Using 1 which is incorrect width.
Using heights[i] which is a height, not width.
✗ Incorrect
If the stack is empty, the width is the current index i because the rectangle extends from 0 to i-1.
4fill in blank
hardFill both blanks to correctly update the max area after popping from the stack.
DSA C#
int height = heights[stack.[1]] ; int width = stack.Count == 0 ? i : i - stack.[2] - 1; maxArea = Math.Max(maxArea, height * width);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Peek instead of Pop to get height.
Using Pop instead of Peek to get width index.
Using Count or Clear which are not correct here.
✗ Incorrect
We pop the top index to get the height, then peek the new top to calculate width.
5fill in blank
hardFill all three blanks to complete the final loop that processes remaining bars in the stack.
DSA C#
while (stack.Count > 0) { int height = heights[stack.[1]]; int width = stack.Count == 0 ? [2] : [3] - stack.Peek() - 1; maxArea = Math.Max(maxArea, height * width); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using stack.Count instead of heights.Length for width.
Using Peek instead of Pop for height.
Using wrong boundary values.
✗ Incorrect
We pop the top index for height, use heights.Length as the right boundary for width calculation.
