Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a class for a doubly linked list node with a value property.
DSA Typescript
class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = [1]; this.next = null; this.prev = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not declared in the constructor.
Assigning 'this.value' to 'val' or 'data' which are undefined.
✗ Incorrect
The constructor parameter is named 'value', so to assign it to the property, use 'this.value = value;'.
2fill in blank
mediumComplete the code to initialize the head of the doubly linked list with a new node.
DSA Typescript
class DoublyLinkedList { head: Node | null; tail: Node | null; constructor(value: number) { const newNode = new Node(value); this.head = [1]; this.tail = newNode; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to null or to the value instead of the node.
Assigning head to newNode.next which is null.
✗ Incorrect
The head should point to the new node created, so assign 'this.head = newNode;'.
3fill in blank
hardFix the error in the method that adds a new node at the end of the doubly linked list.
DSA Typescript
add(value: number) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = [1];
newNode.prev = this.tail;
this.tail = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning tail.next to value or null instead of the new node.
Assigning tail.next to head which breaks the list.
✗ Incorrect
To link the current tail's next to the new node, assign 'this.tail.next = newNode;'.
4fill in blank
hardFill both blanks to complete the method that removes the last node from the doubly linked list.
DSA Typescript
remove() {
if (!this.tail) return null;
const temp = this.tail;
if (this.head === this.tail) {
this.head = [1];
this.tail = [2];
} else {
this.tail = this.tail.prev;
if (this.tail) this.tail.next = null;
temp.prev = null;
}
return temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting head or tail to the removed node instead of null.
Leaving head or tail pointing to a removed node.
✗ Incorrect
When the list has one node, removing it sets both head and tail to null.
5fill in blank
hardFill all three blanks to complete the method that prints the doubly linked list values from head to tail.
DSA Typescript
printList() {
let current = this.head;
let result = '';
while (current !== [1]) {
result += current.value + ' <-> ';
current = current.[2];
}
result += [3];
console.log(result);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of null as loop end condition.
Using 'prev' instead of 'next' to move forward.
Not adding 'null' at the end of the printed string.
✗ Incorrect
The loop runs until current is null, moving to current.next each time, and ends the string with 'null'.
