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
Assigning this.next instead of this.value
Using null instead of the passed value
✗ Incorrect
The constructor assigns the passed value to this.value to store the node's data.
2fill in blank
mediumComplete the enqueue method to add a new node at the end of the queue.
DSA Javascript
enqueue(value) {
const newNode = new Node(value);
if (this.rear === null) {
this.front = newNode;
this.rear = newNode;
} else {
this.rear.[1] = newNode;
this.rear = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning value instead of next
Using front or prev which are incorrect here
✗ Incorrect
The rear node's next pointer should point to the new node to link it at the end.
3fill in blank
hardFix the error in the dequeue method to correctly remove the front node.
DSA Javascript
dequeue() {
if (this.front === null) {
return null;
}
const removedValue = this.front.value;
this.front = this.front.[1];
if (this.front === null) {
this.rear = null;
}
return removedValue;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prev or value instead of next
Assigning front to front which causes no change
✗ Incorrect
To remove the front node, move the front pointer to front.next.
4fill in blank
hardFill both blanks to check if the queue is empty.
DSA Javascript
isEmpty() {
return this.[1] === [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking rear instead of front
Comparing to undefined instead of null
✗ Incorrect
The queue is empty if front is null, meaning no nodes are present.
5fill in blank
hardFill all three blanks to implement the peek method that returns the front value without removing it.
DSA Javascript
peek() {
if (this.[1] === [2]) {
return null;
}
return this.front.[3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking rear instead of front
Returning front instead of front.value
✗ Incorrect
Peek returns the value of the front node if it exists; otherwise, null.
