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 anything to the stack.
✗ Incorrect
We use push() to add the current index i onto the stack.
2fill in blank
mediumComplete the while loop condition to pop indices with stock price less or equal to current price.
DSA Java
while (!stack.isEmpty() && prices[stack.peek()] [1] prices[i]) { stack.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using < misses equal prices.
Using > or >= reverses the logic.
✗ Incorrect
We pop while the price at stack top is less than or equal to current price.
3fill in blank
hardFix the error in calculating span when stack is empty.
DSA Java
span[i] = stack.isEmpty() ? [1] : i - stack.peek();Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 results in zero span incorrectly.
Using i alone misses the +1 needed.
✗ Incorrect
If stack is empty, span is i+1, but since i starts at 0, span is i+1 which equals i+1, so span[i] = i+1. Here, 1 is the base span for first day.
4fill in blank
hardFill both blanks to complete the for loop and initialize the stack.
DSA Java
Stack<Integer> stack = new Stack<>(); for (int [1] = 0; [2] < prices.length; [1]++) { // loop body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in initialization and condition causes errors.
Using j or k is valid but inconsistent here.
✗ Incorrect
The loop variable is commonly named i and used consistently in initialization and condition.
5fill in blank
hardFill all three blanks to complete the span calculation and stack update.
DSA Java
span[i] = stack.isEmpty() ? [1] : i - stack.peek(); stack.[2](i); return [3];
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push removes elements incorrectly.
Returning wrong variable causes errors.
✗ Incorrect
Span is i+1 if stack empty, push current index i, and return the span array.
