Challenge - 5 Problems
Traversal Master
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 printed output after running this Go code that traverses a doubly linked list forward and then backward?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } func main() { head := &Node{data: 1} second := &Node{data: 2} third := &Node{data: 3} head.next = second second.prev = head second.next = third third.prev = second // Forward traversal current := head for current != nil { fmt.Print(current.data, " ") if current.next == nil { break } current = current.next } fmt.Print("| ") // Backward traversal for current != nil { fmt.Print(current.data, " ") current = current.prev } }
Attempts:
2 left
💡 Hint
Think about how the pointers move forward and backward in a doubly linked list.
✗ Incorrect
The forward loop prints nodes from head to tail: 1 2 3. Then the backward loop prints from tail back to head: 3 2 1.
🧠 Conceptual
intermediateUnderstanding Traversal Direction in Doubly Linked Lists
In a doubly linked list, which pointer do you follow to traverse the list backward from the tail node?
Attempts:
2 left
💡 Hint
Backward means going to the previous node.
✗ Incorrect
The prev pointer points to the previous node, so following prev pointers from tail moves backward.
🔧 Debug
advancedIdentify the Bug in Backward Traversal Code
What error will this Go code produce when trying to traverse backward in a doubly linked list?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } func main() { head := &Node{data: 1} second := &Node{data: 2} head.next = second second.prev = head current := head // Attempt backward traversal starting from head for current != nil { fmt.Print(current.data, " ") current = current.prev } }
Attempts:
2 left
💡 Hint
Think about what prev pointer of head points to.
✗ Incorrect
Head's prev pointer is nil, so the loop prints 1 and stops immediately.
❓ Predict Output
advancedOutput of Mixed Forward and Backward Traversal
What is the output of this Go code that traverses forward two nodes and then backward one node in a doubly linked list?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } func main() { n1 := &Node{data: 10} n2 := &Node{data: 20} n3 := &Node{data: 30} n1.next = n2 n2.prev = n1 n2.next = n3 n3.prev = n2 current := n1 // Move forward two nodes for i := 0; i < 2; i++ { current = current.next fmt.Print(current.data, " ") } // Move backward one node current = current.prev fmt.Print("| ", current.data) }
Attempts:
2 left
💡 Hint
Trace the pointer moves carefully step by step.
✗ Incorrect
Forward moves print 20 then 30. Then moving backward one node from 30 goes to 20.
🧠 Conceptual
expertWhy Use Doubly Linked List for Forward and Backward Traversal?
Which is the main advantage of a doubly linked list over a singly linked list when traversing forward and backward?
Attempts:
2 left
💡 Hint
Think about how pointers connect nodes in both types of lists.
✗ Incorrect
Doubly linked lists have prev and next pointers, enabling efficient forward and backward traversal.
