Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Javascript
class Node { constructor(value) { this.value = [1]; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 'next' instead of 'value'.
Using 'null' instead of the passed value.
✗ Incorrect
The constructor should assign the passed value to this.value to store the node's data.
2fill in blank
mediumComplete the code to check if the queue is empty before enqueueing.
DSA Javascript
enqueue(value) {
const newNode = new Node(value);
if (this.front === [1]) {
this.front = newNode;
this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined instead of null.
Checking for 0 or false which are unrelated.
✗ Incorrect
The queue is empty if this.front is null, so we check for null to set front and rear.
3fill in blank
hardFix the error in updating the rear pointer after enqueueing a new node.
DSA Javascript
enqueue(value) {
const newNode = new Node(value);
if (this.front === null) {
this.front = newNode;
this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = [1];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning rear to front or value instead of newNode.
Using next.next which is undefined.
✗ Incorrect
After linking the new node, rear should point to the newNode to keep track of the last node.
4fill in blank
hardFill both blanks to complete the enqueue method that adds a new node to the queue.
DSA Javascript
enqueue(value) {
const newNode = new Node(value);
if (this.front === [1]) {
this.front = newNode;
this.rear = newNode;
} else {
this.rear.[2] = newNode;
this.rear = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined instead of null for empty check.
Using prev instead of next to link nodes.
✗ Incorrect
Check if front is null to detect empty queue, and link newNode by setting rear.next to newNode.
5fill in blank
hardFill all three blanks to complete the enqueue method with proper node creation and linking.
DSA Javascript
enqueue([1]) { const newNode = new [2]([3]); if (this.front === null) { this.front = newNode; this.rear = newNode; } else { this.rear.next = newNode; this.rear = newNode; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' instead of 'value' inconsistently.
Not matching parameter and constructor argument names.
✗ Incorrect
The method takes 'value', creates a new Node with 'value', and links it properly.
