Challenge - 5 Problems
Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Forward Traversal in Doubly Linked List
What is the output of the following C++ code that traverses a doubly linked list forward?
DSA C++
#include <iostream> using namespace std; struct Node { int data; Node* prev; Node* next; Node(int val) : data(val), prev(nullptr), next(nullptr) {} }; int main() { Node* head = new Node(1); Node* second = new Node(2); Node* third = new Node(3); head->next = second; second->prev = head; second->next = third; third->prev = second; Node* current = head; while (current != nullptr) { cout << current->data << " -> "; current = current->next; } cout << "null" << endl; return 0; }
Attempts:
2 left
💡 Hint
Start from the head and move using the next pointer.
✗ Incorrect
The code starts from the head node and moves forward using the next pointer, printing each node's data. So the output is the sequence 1, 2, 3 followed by null.
❓ Predict Output
intermediateOutput of Backward Traversal in Doubly Linked List
What is the output of the following C++ code that traverses a doubly linked list backward?
DSA C++
#include <iostream> using namespace std; struct Node { int data; Node* prev; Node* next; Node(int val) : data(val), prev(nullptr), next(nullptr) {} }; int main() { Node* head = new Node(1); Node* second = new Node(2); Node* third = new Node(3); head->next = second; second->prev = head; second->next = third; third->prev = second; Node* current = third; while (current != nullptr) { cout << current->data << " -> "; current = current->prev; } cout << "null" << endl; return 0; }
Attempts:
2 left
💡 Hint
Start from the tail and move using the prev pointer.
✗ Incorrect
The code starts from the last node (third) and moves backward using the prev pointer, printing each node's data. So the output is 3, 2, 1 followed by null.
🧠 Conceptual
advancedUnderstanding Traversal Pointers in Doubly Linked List
In a doubly linked list, which pointer is used to move backward during traversal?
Attempts:
2 left
💡 Hint
Backward means going to the previous node.
✗ Incorrect
The prev pointer in each node points to the previous node, allowing backward traversal.
🔧 Debug
advancedIdentify the Bug in Backward Traversal Code
What error will occur when running this code snippet for backward traversal of a doubly linked list?
DSA C++
#include <iostream> using namespace std; struct Node { int data; Node* prev; Node* next; Node(int val) : data(val), prev(nullptr), next(nullptr) {} }; int main() { Node* head = new Node(1); Node* second = new Node(2); Node* third = new Node(3); head->next = second; second->prev = head; second->next = third; third->prev = second; Node* current = head; while (current != nullptr) { current = current->prev; cout << current->data << " -> "; } cout << "null" << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the pointer used for traversal starting from head.
✗ Incorrect
Starting backward traversal from head uses prev pointer which is nullptr, so next iteration tries to access prev of nullptr causing runtime error.
🚀 Application
expertResulting List After Mixed Forward and Backward Traversal
Given a doubly linked list with nodes 10 <-> 20 <-> 30 <-> 40, what is the printed output after this traversal sequence?
1. Traverse forward from head to tail printing nodes.
2. Then traverse backward from tail to head printing nodes.
DSA C++
#include <iostream> using namespace std; struct Node { int data; Node* prev; Node* next; Node(int val) : data(val), prev(nullptr), next(nullptr) {} }; int main() { Node* n1 = new Node(10); Node* n2 = new Node(20); Node* n3 = new Node(30); Node* n4 = new Node(40); n1->next = n2; n2->prev = n1; n2->next = n3; n3->prev = n2; n3->next = n4; n4->prev = n3; Node* current = n1; while (current != nullptr) { cout << current->data << " -> "; current = current->next; } cout << "null" << endl; current = n4; while (current != nullptr) { cout << current->data << " -> "; current = current->prev; } cout << "null" << endl; return 0; }
Attempts:
2 left
💡 Hint
First print forward from head, then backward from tail.
✗ Incorrect
The first loop prints nodes from head to tail: 10, 20, 30, 40. The second loop prints nodes from tail to head: 40, 30, 20, 10.
