Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new node at the front of the dequeue.
DSA Javascript
this.front = new Node(data);
this.front.next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to this.front causes a circular reference.
Setting next to this.rear is incorrect because rear is the end of the list.
✗ Incorrect
When adding a new node at the front of an empty dequeue, its next pointer should point to null to start the list.
2fill in blank
mediumComplete the code to remove a node from the rear of the dequeue.
DSA Javascript
if (this.front === this.rear) { this.front = this.rear = null; } else { let current = this.front; while (current.next !== [1]) { current = current.next; } current.next = null; this.rear = current; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using current.next !== this.front will never find the node before rear.
Using current.next !== null will stop too early.
✗ Incorrect
To remove the rear node, we find the node before rear by checking current.next !== this.rear.
3fill in blank
hardFix the error in the code to correctly add a node at the rear of the dequeue.
DSA Javascript
let newNode = new Node(data); if (this.rear === null) { this.front = this.rear = newNode; } else { this.rear.next = [1]; this.rear = newNode; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting rear.next to null disconnects the list.
Setting rear.next to front causes a cycle.
✗ Incorrect
When adding at the rear, the current rear's next should point to the new node.
4fill in blank
hardFill both blanks to check if the dequeue is empty and return the correct boolean.
DSA Javascript
isEmpty() {
return this.front === [1] && this.rear === [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking against undefined is incorrect here.
Checking against 0 or false is not valid for pointers.
✗ Incorrect
The dequeue is empty if both front and rear are null.
5fill in blank
hardFill all three blanks to correctly print all elements from front to rear.
DSA Javascript
print() { let current = this.front; while (current !== [1]) { console.log(current.[2]); current = current.[3]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using front instead of null causes infinite loop.
Printing next instead of data prints wrong value.
✗ Incorrect
We loop until current is null, print current.data, and move to current.next.
