Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the stack array in the MinStack constructor.
DSA Javascript
class MinStack { constructor() { this.stack = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using an object {} instead of an array.
Initializing with null or 0 which are not arrays.
✗ Incorrect
The stack should be initialized as an empty array to hold the stack elements.
2fill in blank
mediumComplete the push method to add a value to the stack array.
DSA Javascript
push(val) {
this.stack.[1](val);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which removes elements.
Using shift or unshift which affect the start of the array.
✗ Incorrect
The push method adds an element to the end of the array, which is how we add to the stack.
3fill in blank
hardFix the error in the getMin method to return the minimum value stored in the minStack array without modifying it.
DSA Javascript
getMin() {
if (this.minStack.length === 0) return null;
return this.minStack[this.minStack.length[1]1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' which accesses beyond the array.
Using other arithmetic operators like '*' or '/'.
✗ Incorrect
Access the last element using `this.minStack.length - 1` to get the current minimum without popping it.
4fill in blank
hardFill both blanks to correctly update minStack when pushing a new value.
DSA Javascript
push(val) {
this.stack.push(val);
if (this.minStack.length === 0 || val [1] this.minStack[this.minStack.length - 1]) {
this.minStack.[2](val);
} else {
this.minStack.push(this.minStack[this.minStack.length - 1]);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' which is the wrong comparison direction.
Using 'pop' which removes instead of adding.
✗ Incorrect
If the new value is less than or equal to the current min (`<=`), push it onto minStack using `push`.
5fill in blank
hardFill all three blanks to correctly pop from both stack and minStack.
DSA Javascript
pop() {
if (this.stack.length === 0) return null;
this.minStack.[1]();
return this.stack.[2]();
}
constructor() {
this.stack = [];
this.minStack = [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using `shift` which removes from the front.
Forgetting to initialize minStack as an empty array.
✗ Incorrect
Pop from both minStack and stack using `pop()`. Initialize minStack as an empty array `[]`.
