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 {} which creates an object, not a stack array.
Using null or 0 which are not valid stack initializations.
✗ Incorrect
We use an empty array [] to represent the stack in JavaScript.
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 instead of adds.
Using shift() or unshift() which operate on the start of the array.
✗ Incorrect
To add an element to the top of the stack, we use push().
3fill in blank
hardFix the error in the condition to check if the current height is less than the height at the top of the stack.
DSA Javascript
while (stack.length > 0 && heights[i] [1] heights[stack[stack.length - 1]]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which reverses the logic.
Using '==' which checks equality, not order.
✗ Incorrect
We pop from the stack when the current height is less than the height at the top of the stack.
4fill in blank
hardFill both blanks to calculate the width of the rectangle correctly.
DSA Javascript
const width = stack.length === 0 ? i : i [1] stack[stack.length [2] 1] - 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' which gives wrong width.
Using '0' or no adjustment which miscalculates width.
✗ Incorrect
Width is calculated by subtracting the index before the popped bar from current index minus one.
5fill in blank
hardComplete the code to update maxArea correctly inside the loop.
DSA Javascript
maxArea = Math.[1](maxArea, heights[top] * width);Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' which causes syntax errors.
Using Math.min which finds the smaller value.
✗ Incorrect
We use Math.max to update maxArea with the larger value. No extra operator needed after width.
