Challenge - 5 Problems
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Circular Linked List Traversal
What is the printed output of the following C++ code that traverses a circular linked list?
DSA C++
struct Node {
int data;
Node* next;
};
#include <iostream>
int main() {
Node* head = new Node{1, nullptr};
Node* second = new Node{2, nullptr};
Node* third = new Node{3, nullptr};
head->next = second;
second->next = third;
third->next = head; // circular link
Node* temp = head;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
std::cout << "null" << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
The loop stops when the traversal returns to the head node.
✗ Incorrect
The do-while loop prints each node's data once and stops when it reaches the head again, preventing infinite loops in circular lists.
🧠 Conceptual
intermediateUnderstanding Circular Linked List Traversal Condition
In a circular linked list traversal, why is the condition usually written as
while (temp != head) instead of while (temp != nullptr)?Attempts:
2 left
💡 Hint
Think about what the last node points to in a circular linked list.
✗ Incorrect
In circular linked lists, the last node points back to the head, so nullptr is never encountered during traversal. The loop stops when temp equals head again.
🔧 Debug
advancedIdentify the Bug in Circular Linked List Traversal
What error will occur when running this traversal code on a circular linked list?
DSA C++
Node* temp = head; while (temp != nullptr) { std::cout << temp->data << " -> "; temp = temp->next; } std::cout << "null" << std::endl;
Attempts:
2 left
💡 Hint
Consider what happens to temp in a circular linked list.
✗ Incorrect
Since the last node points back to head, temp never becomes nullptr, causing an infinite loop.
❓ Predict Output
advancedOutput After Inserting Node in Circular Linked List
Given the circular linked list 1 -> 2 -> 3 -> back to 1, what is the output after inserting a new node with value 4 after node with value 2 and traversing?
DSA C++
struct Node {
int data;
Node* next;
};
#include <iostream>
int main() {
Node* head = new Node{1, nullptr};
Node* second = new Node{2, nullptr};
Node* third = new Node{3, nullptr};
head->next = second;
second->next = third;
third->next = head;
// Insert new node with value 4 after second
Node* newNode = new Node{4, second->next};
second->next = newNode;
Node* temp = head;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
std::cout << "null" << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
The new node is inserted after node 2, so it appears between 2 and 3.
✗ Incorrect
The new node with value 4 is linked after node 2 and before node 3, so traversal prints 1, 2, 4, 3.
🧠 Conceptual
expertWhy Use Circular Linked Lists Over Linear Linked Lists?
Which of the following is the best reason to use a circular linked list instead of a linear linked list?
Attempts:
2 left
💡 Hint
Think about continuous traversal without stopping.
✗ Incorrect
Circular linked lists allow continuous traversal from any node without needing to reset to head, useful in applications like round-robin scheduling.
