Challenge - 5 Problems
Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Forward Traversal in Doubly Linked List
What is the printed output of the following C code that traverses a doubly linked list forward?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; void printForward(Node* head) { Node* current = head; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("NULL\n"); } int main() { Node* n1 = malloc(sizeof(Node)); Node* n2 = malloc(sizeof(Node)); Node* n3 = malloc(sizeof(Node)); n1->data = 10; n1->prev = NULL; n1->next = n2; n2->data = 20; n2->prev = n1; n2->next = n3; n3->data = 30; n3->prev = n2; n3->next = NULL; printForward(n1); return 0; }
Attempts:
2 left
💡 Hint
Follow the next pointers starting from the head node.
✗ Incorrect
The function printForward starts at the head and moves through the next pointers, printing each node's data followed by '->'. The list is 10, 20, 30 in order, ending with NULL.
❓ Predict Output
intermediate2:00remaining
Output of Backward Traversal in Doubly Linked List
What is the printed output of the following C code that traverses a doubly linked list backward?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; void printBackward(Node* tail) { Node* current = tail; while (current != NULL) { printf("%d -> ", current->data); current = current->prev; } printf("NULL\n"); } int main() { Node* n1 = malloc(sizeof(Node)); Node* n2 = malloc(sizeof(Node)); Node* n3 = malloc(sizeof(Node)); n1->data = 10; n1->prev = NULL; n1->next = n2; n2->data = 20; n2->prev = n1; n2->next = n3; n3->data = 30; n3->prev = n2; n3->next = NULL; printBackward(n3); return 0; }
Attempts:
2 left
💡 Hint
Follow the prev pointers starting from the tail node.
✗ Incorrect
The function printBackward starts at the tail and moves through the prev pointers, printing each node's data followed by '->'. The list backward is 30, 20, 10 ending with NULL.
🧠 Conceptual
advanced1:00remaining
Understanding Traversal Pointers in Doubly Linked List
In a doubly linked list, which pointer is used to move backward during traversal?
Attempts:
2 left
💡 Hint
Think about which pointer links to the previous node.
✗ Incorrect
The prev pointer in each node points to the previous node, allowing backward traversal.
🔧 Debug
advanced2:00remaining
Identify the Bug in Backward Traversal Code
What error will occur when running this backward traversal code snippet?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; void printBackward(Node* tail) { Node* current = tail; while (current != NULL) { printf("%d -> ", current->next->data); current = current->prev; } printf("NULL\n"); }
Attempts:
2 left
💡 Hint
Check what happens when current->next is NULL.
✗ Incorrect
Accessing current->next->data when current->next is NULL causes a segmentation fault.
🚀 Application
expert3:00remaining
Result of Mixed Forward and Backward Traversal
Given a doubly linked list with nodes 1 <-> 2 <-> 3 <-> 4, what is the output after this code runs?
1. Traverse forward from head and print nodes.
2. Then traverse backward from tail and print nodes.
Assume print format: "data -> " and ends with "NULL".
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; void printForward(Node* head) { Node* current = head; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("NULL\n"); } void printBackward(Node* tail) { Node* current = tail; while (current != NULL) { printf("%d -> ", current->data); current = current->prev; } printf("NULL\n"); } int main() { Node* n1 = malloc(sizeof(Node)); Node* n2 = malloc(sizeof(Node)); Node* n3 = malloc(sizeof(Node)); Node* n4 = malloc(sizeof(Node)); n1->data = 1; n1->prev = NULL; n1->next = n2; n2->data = 2; n2->prev = n1; n2->next = n3; n3->data = 3; n3->prev = n2; n3->next = n4; n4->data = 4; n4->prev = n3; n4->next = NULL; printForward(n1); printBackward(n4); return 0; }
Attempts:
2 left
💡 Hint
First print forward from head, then backward from tail.
✗ Incorrect
The first printForward prints nodes 1 to 4 in order, the second printBackward prints nodes 4 to 1 in reverse order, both ending with NULL.
