Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element at the front of the dequeue.
DSA Java
public void addFront(int data) {
Node newNode = new Node(data);
if (front == null) {
front = rear = newNode;
} else {
newNode.next = [1];
front = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to rear instead of front
Not updating newNode.next at all
✗ Incorrect
To add at the front, the new node's next should point to the current front node.
2fill in blank
mediumComplete the code to remove an element from the rear of the dequeue.
DSA Java
public int removeRear() {
if (rear == null) {
throw new NoSuchElementException();
}
int value = rear.data;
if (front == rear) {
front = rear = null;
} else {
Node temp = front;
while (temp.next != [1]) {
temp = temp.next;
}
rear = temp;
rear.next = null;
}
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing temp.next to front instead of rear
Using rear.next which is null
✗ Incorrect
To find the node before rear, we check where temp.next equals rear.
3fill in blank
hardFix the error in the code to add an element at the rear of the dequeue.
DSA Java
public void addRear(int data) {
Node newNode = new Node(data);
if (rear == null) {
front = rear = newNode;
} else {
rear.next = [1];
rear = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting rear.next to null which breaks the link
Setting rear.next to front which is incorrect
✗ Incorrect
The current rear's next should point to the new node being added.
4fill in blank
hardFill both blanks to complete the method that checks if the dequeue is empty.
DSA Java
public boolean isEmpty() {
return [1] == [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing rear to newNode which is undefined here
Using front == rear which is not always empty
✗ Incorrect
The dequeue is empty if the front pointer is null.
5fill in blank
hardFill all three blanks to complete the method that prints all elements from front to rear.
DSA Java
public void display() {
Node temp = [1];
while (temp != [2]) {
System.out.print(temp.[3] + " -> ");
temp = temp.next;
}
System.out.println("null");
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from rear instead of front
Checking temp != rear which misses last node
Printing temp.next instead of temp.data
✗ Incorrect
Start from front, loop until temp is null, print temp.data each time.
