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; this.prev = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the constructor.
Assigning undefined variables.
✗ Incorrect
The constructor parameter is named 'value', so we assign it to this.value.
2fill in blank
mediumComplete the code to check if the list is empty before inserting.
DSA Javascript
insertAtEnd(value) {
const newNode = new Node(value);
if (this.head === [1]) {
this.head = newNode;
this.tail = newNode;
return;
}
// rest of insertion code
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined instead of null.
Using 0 or false which are not pointers.
✗ Incorrect
An empty list has head set to null, so we check for null.
3fill in blank
hardFix the error in linking the new node at the end of the list.
DSA Javascript
insertAtEnd(value) {
const newNode = new Node(value);
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
return;
}
this.tail.next = [1];
newNode.prev = this.tail;
this.tail = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning null or value instead of the new node.
Assigning head instead of new node.
✗ Incorrect
We link the current tail's next pointer to the new node.
4fill in blank
hardFill both blanks to complete the method that inserts at the end and updates pointers.
DSA Javascript
insertAtEnd(value) {
const newNode = new Node(value);
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
return;
}
this.tail.[1] = newNode;
newNode.[2] = this.tail;
this.tail = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping next and prev pointers.
Assigning to head or value properties incorrectly.
✗ Incorrect
The current tail's next points to newNode, and newNode's prev points back to current tail.
5fill in blank
hardFill all three blanks to complete the insertAtEnd method with proper pointer updates and return statement.
DSA Javascript
insertAtEnd(value) {
const newNode = new Node(value);
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
return [1];
}
this.tail.[2] = newNode;
newNode.[3] = this.tail;
this.tail = newNode;
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning undefined or nothing in empty list case.
Mixing up next and prev pointers.
✗ Incorrect
Return the new node when list was empty; link tail.next to newNode and newNode.prev to tail.
