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 different variable name that is not defined in the constructor.
Forgetting to assign the value property.
✗ Incorrect
The constructor parameter is named 'value', so we assign 'this.value = value;' to store it in the node.
2fill in blank
mediumComplete the code to initialize the head of the doubly linked list to null.
DSA Typescript
class DoublyLinkedList { head: Node | null; tail: Node | null; constructor() { this.head = [1]; this.tail = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting head to undefined or 0 instead of null.
Assigning head to this, which is incorrect.
✗ Incorrect
The head should start as null to indicate the list is empty.
3fill in blank
hardFix the error in the method that adds a new node at the end of the doubly linked list.
DSA Typescript
addToEnd(value: number): void {
const newNode = new Node(value);
if (this.tail === null) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.prev = [1];
this.tail = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.prev to this.head or newNode itself.
Setting newNode.prev to null which breaks the link.
✗ Incorrect
The new node's prev pointer should point to the current tail before updating tail to newNode.
4fill in blank
hardFill both blanks to correctly remove the head node from the doubly linked list.
DSA Typescript
removeHead(): number | null {
if (this.head === null) return null;
const value = this.head.value;
this.head = this.head.[1];
if (this.head !== null) {
this.head.[2] = null;
} else {
this.tail = null;
}
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names like nextNode or previous.
Not setting the new head's prev to null.
✗ Incorrect
To remove the head, move head to head.next and set new head's prev to null.
5fill in blank
hardFill all three blanks to insert a new node after a given node in the doubly linked list.
DSA Typescript
insertAfter(node: Node, value: number): void {
const newNode = new Node(value);
newNode.prev = node;
newNode.next = node.[1];
if (node.[2] !== null) {
node.[3].prev = newNode;
}
node.next = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prev instead of next for forward links.
Not updating the next node's prev pointer.
✗ Incorrect
The new node's next is node.next; if node.next exists, its prev is updated to newNode; finally, node.next is set to newNode.
