Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the stack as an empty array.
DSA Javascript
const stack = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} which creates an object, not an array.
Using null or 0 which are not arrays.
✗ Incorrect
The stack is initialized as an empty array to store indices.
2fill in blank
mediumComplete the code to push the current index i onto the stack.
DSA Javascript
stack.[1](i);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which removes elements instead of adding.
Using shift or unshift which operate on the start of the array.
✗ Incorrect
The push method adds an element to the end of the array (stack).
3fill in blank
hardFix the error in the while loop condition to check if the stack is not empty.
DSA Javascript
while (stack.[1] > 0 && height[i] > height[stack[stack.length - 1]]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() which causes a runtime error.
Using size or count which are not valid for arrays.
✗ Incorrect
Arrays in JavaScript use the length property (without parentheses) to get size.
4fill in blank
hardFill both blanks to calculate the distance and bounded height for trapped water.
DSA Javascript
const distance = i - stack[stack.length - 1] - [1]; const bounded_height = Math.min(height[i], height[stack[stack.length - 1]]) - height[[2]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for distance calculation.
Using stack[stack.length - 1] instead of the popped index for height.
✗ Incorrect
Distance is the gap between current and previous index minus 1. Bounded height uses the popped index from stack.
5fill in blank
hardFill all three blanks to update the total trapped water correctly.
DSA Javascript
water += distance * [1]; stack.[2](i); let top = stack.[3]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using height[i] instead of bounded_height for water calculation.
Using pop instead of push to add index.
Using push instead of pop to remove index.
✗ Incorrect
Water is increased by distance times bounded height. Push adds current index. Pop removes top index.
