Challenge - 5 Problems
Traversal Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Forward and Backward Traversal in Doubly Linked List
What is the output of the following Java code that traverses a doubly linked list forward and then backward?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } public class DLLTraversal { public static void main(String[] args) { 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; // Forward traversal Node curr = head; while (curr != null) { System.out.print(curr.data + " -> "); curr = curr.next; } System.out.print("null\n"); // Backward traversal curr = third; while (curr != null) { System.out.print(curr.data + " -> "); curr = curr.prev; } System.out.print("null\n"); } }
Attempts:
2 left
💡 Hint
Think about how the 'next' and 'prev' pointers are used in forward and backward traversal.
✗ Incorrect
The forward traversal starts from head and moves using 'next' pointers printing 1 -> 2 -> 3 -> null. The backward traversal starts from the last node (third) and moves using 'prev' pointers printing 3 -> 2 -> 1 -> null.
🧠 Conceptual
intermediateUnderstanding Traversal Direction 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 a doubly linked list points to the previous node, allowing backward traversal.
🔧 Debug
advancedIdentify the Error in Backward Traversal Code
What error will occur when running this backward traversal code snippet on a doubly linked list?
DSA Java
Node curr = head; while (curr != null) { curr = curr.prev; System.out.print(curr.data + " -> "); } System.out.print("null\n");
Attempts:
2 left
💡 Hint
Consider what happens when you try to access prev of the head node.
✗ Incorrect
The head node's prev pointer is null. Accessing curr.prev when curr is head leads to NullPointerException.
❓ Predict Output
advancedOutput After Mixed Forward and Backward Traversal
What is the output of this Java code that traverses forward two nodes and then backward one node in a doubly linked list?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } public class MixedTraversal { public static void main(String[] args) { Node head = new Node(10); Node second = new Node(20); Node third = new Node(30); head.next = second; second.prev = head; second.next = third; third.prev = second; Node curr = head; System.out.print(curr.data + " -> "); curr = curr.next; System.out.print(curr.data + " -> "); curr = curr.next; System.out.print(curr.data + " -> "); curr = curr.prev; System.out.print(curr.data + " -> "); System.out.print("null\n"); } }
Attempts:
2 left
💡 Hint
Trace the pointer moves carefully step by step.
✗ Incorrect
The code prints 10, then moves next to 20, prints 20, moves next to 30, prints 30, then moves prev to 20 and prints 20 again before printing null.
🧠 Conceptual
expertWhy is Backward Traversal Useful in Doubly Linked Lists?
Which of the following is the best reason why backward traversal is useful in doubly linked lists?
Attempts:
2 left
💡 Hint
Think about what backward traversal enables compared to singly linked lists.
✗ Incorrect
Backward traversal allows moving from tail to head efficiently without extra data structures or memory.
