Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = [1]; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 'null' instead of the passed value.
Assigning 'next' property instead of 'value'.
✗ Incorrect
The constructor should assign the passed value to the node's value property.
2fill in blank
mediumComplete the code to check if the queue is empty.
DSA Typescript
class Queue { head: Node | null = null; tail: Node | null = null; isEmpty(): boolean { return this.head [1] null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using !== instead of ===, which reverses the logic.
Using > or < operators which are not suitable for null checks.
✗ Incorrect
The queue is empty if head is exactly null.
3fill in blank
hardFix the error in the enqueue method to correctly add a new node at the tail.
DSA Typescript
enqueue(value: number): void {
const newNode = new Node(value);
if (this.tail) {
this.tail.next = [1];
}
this.tail = newNode;
if (!this.head) {
this.head = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'this.tail.next' to 'this.head' or 'this.tail' causing loops.
Setting 'this.tail.next' to null which breaks the chain.
✗ Incorrect
The current tail's next should point to the new node being added.
4fill in blank
hardFill both blanks to complete the dequeue method that removes the head node and returns its value.
DSA Typescript
dequeue(): number | null {
if (this.head === null) {
return null;
}
const value = this.head.value;
this.head = this.head.[1];
if (this.head === null) {
this.[2] = null;
}
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting head to head.prev which does not exist in singly linked list.
Not updating tail when queue becomes empty.
✗ Incorrect
To remove the head, set head to head.next. If head becomes null, tail must also be null.
5fill in blank
hardFill all three blanks to complete the printQueue method that prints all values in the queue.
DSA Typescript
printQueue(): void {
let current = this.[1];
while (current [2] null) {
console.log(current.[3]);
current = current.next;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from tail instead of head.
Using '==' instead of '!==' in the loop condition.
Printing 'current.next' instead of 'current.value'.
✗ Incorrect
Start from head, loop while current is not null, print current.value.
