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.data = [1]; this.prev = null; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the constructor.
Assigning this.data to null or undefined.
✗ Incorrect
The constructor receives the value as 'value', so we assign it to this.data.
2fill in blank
mediumComplete the code to check if the position to insert is at the head (position 1).
DSA Javascript
if (position === [1]) { newNode.next = this.head; if (this.head !== null) { this.head.prev = newNode; } this.head = newNode; return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for position 0 instead of 1.
Using null or undefined in the comparison.
✗ Incorrect
Position 1 means inserting at the head of the list.
3fill in blank
hardFix the error in the loop condition to find the node before the insertion position.
DSA Javascript
let current = this.head; let count = 1; while (count < [1] - 1 && current !== null) { current = current.next; count++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables like 'pos' or 'index'.
Looping until position instead of position - 1.
✗ Incorrect
The variable 'position' holds the target insertion position.
4fill in blank
hardFill both blanks to correctly insert the new node between nodes.
DSA Javascript
newNode.next = current.[1]; if (current.[2] !== null) { current.[2].prev = newNode; } current.next = newNode; newNode.prev = current;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' for newNode.next.
Not updating the prev pointer of the next node.
✗ Incorrect
We set newNode.next to current.next and update current.next.prev to newNode.
5fill in blank
hardFill the two blanks to complete the insertAtPosition method.
DSA Javascript
insertAtPosition(value, position) {
const newNode = new Node([1]);
if (position === [2]) {
newNode.next = this.head;
if (this.head !== null) {
this.head.prev = newNode;
}
this.head = newNode;
return;
}
let current = this.head;
let count = 1;
while (count < position - 1 && current !== null) {
current = current.next;
count++;
}
if (current === null) {
return;
}
newNode.next = current.next;
if (current.next !== null) {
current.next.prev = newNode;
}
current.next = newNode;
newNode.prev = current;
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing wrong variable to Node constructor.
Checking position against 0 instead of 1.
✗ Incorrect
We pass 'value' to Node, check if position is 1, and use 0 as a distractor.
