Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given value.
DSA Javascript
class Node { constructor(value) { this.value = [1]; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this.next' instead of 'this.value' for the value property.
Assigning null to value instead of the passed parameter.
✗ Incorrect
The constructor assigns the passed value to the node's value property.
2fill in blank
mediumComplete the push method to add a new node on top of the stack.
DSA Javascript
push(value) {
const newNode = new Node(value);
newNode.next = [1];
this.top = newNode;
this.size++;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to null loses the existing stack.
Assigning newNode.next to newNode causes a cycle.
✗ Incorrect
The new node's next points to the current top node before updating top.
3fill in blank
hardFix the error in the pop method to correctly remove and return the top node's value.
DSA Javascript
pop() {
if (!this.top) return null;
const poppedNode = this.top;
this.top = [1];
this.size--;
return poppedNode.value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting top to null removes the entire stack.
Returning poppedNode instead of poppedNode.value.
✗ Incorrect
After popping, top should point to the next node of the popped node.
4fill in blank
hardFill both blanks to implement the peek method that returns the top value without removing it.
DSA Javascript
peek() {
if ([1]) {
return [2];
}
return null;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking if top is null instead of not null.
Returning the node instead of its value.
✗ Incorrect
Peek returns the value of the top node if it exists.
5fill in blank
hardFill all three blanks to implement the isEmpty method that returns true if stack is empty.
DSA Javascript
isEmpty() {
return this.size [1] 0;
}
// Usage example:
const stack = new Stack();
console.log(stack.isEmpty()); // [2]
stack.push(10);
console.log(stack.isEmpty()); // [3]Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '===' causing wrong boolean.
Swapping true and false in usage examples.
✗ Incorrect
isEmpty returns true if size equals zero, false otherwise.
