Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the Node class field for storing the next node.
DSA C#
public class Node { public int data; public Node [1]; public Node(int data) { this.data = data; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using unrelated names like 'pointer' or 'link'.
✗ Incorrect
The 'next' field stores the reference to the next node in the linked list queue.
2fill in blank
mediumComplete the code to initialize the front and rear pointers in the Queue constructor.
DSA C#
public class Queue { private Node front, rear; public Queue() { this.front = [1]; this.rear = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing front with a new Node which is incorrect for an empty queue.
Assigning front to rear or vice versa without initialization.
✗ Incorrect
Both front and rear should start as null indicating an empty queue.
3fill in blank
hardFix the error in the Enqueue method to correctly add a new node to the queue.
DSA C#
public void Enqueue(int data) {
Node newNode = new Node(data);
if (rear == null) {
front = rear = newNode;
return;
}
rear.[1] = newNode;
rear = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using incorrect field names like 'link' or 'pointer'.
✗ Incorrect
The 'next' pointer of the current rear node should point to the new node.
4fill in blank
hardFill both blanks to correctly remove the front node in the Dequeue method.
DSA C#
public int Dequeue() {
if (front == null) {
throw new InvalidOperationException("Queue is empty");
}
int value = front.data;
front = front.[1];
if (front == [2]) {
rear = null;
}
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting rear to front instead of null when queue becomes empty.
Using 'prev' instead of 'next' to move front.
✗ Incorrect
We move front to front.next and if front becomes null, rear must also be null.
5fill in blank
hardFill all three blanks to implement the Peek method that returns the front element without removing it.
DSA C#
public int Peek() {
if ([1] == null) {
throw new InvalidOperationException("Queue is empty");
}
return [2].[3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking rear instead of front for emptiness.
Returning rear.data instead of front.data.
Trying to access next instead of data.
✗ Incorrect
Peek checks if front is null, then returns front.data without removing it.
