Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push the current index onto the stack.
DSA Java
stack.[1](i);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push causes removal instead of addition.
Using peek does not add the element.
Using empty is a check, not a modification.
✗ Incorrect
We use stack.push(i) to add the current index to the stack.
2fill in blank
mediumComplete the code to calculate the width of the rectangle when popping from the stack.
DSA Java
int width = stack.isEmpty() ? i : i - stack.[1]() - 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() removes the element prematurely.
Using push() or empty() are incorrect for accessing the top element.
✗ Incorrect
We use stack.peek() to get the top index without removing it, to calculate width.
3fill in blank
hardFix the error in the condition to pop from the stack when current height is less than stack top height.
DSA Java
while (!stack.isEmpty() && heights[i] < heights[stack.[1]()]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() in the condition removes elements unexpectedly.
Using push() or empty() are invalid here.
✗ Incorrect
We use stack.peek() to check the top index height without removing it.
4fill in blank
hardFill both blanks to calculate the area and update maxArea inside the while loop.
DSA Java
int height = heights[stack.[1]()]; int width = stack.isEmpty() ? i : i - stack.[2]() - 1; maxArea = Math.max(maxArea, height * width);
Attempts:
3 left
💡 Hint
Common Mistakes
Using peek() instead of pop() for height causes incorrect height calculation.
Using pop() for width calculation removes elements prematurely.
✗ Incorrect
We pop the top index to get height, then peek the new top to calculate width.
5fill in blank
hardFill all three blanks to complete the final while loop that clears the stack after processing all bars.
DSA Java
while (!stack.isEmpty()) { int height = heights[stack.[1]()]; stack.[2](); int width = stack.isEmpty() ? n : n - stack.[3]() - 1; maxArea = Math.max(maxArea, height * width); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() before getting height causes errors.
Using push() instead of pop() or peek() is incorrect.
✗ Incorrect
Peek to get height index, pop to remove it, peek again to calculate width.
