Challenge - 5 Problems
Traversal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Forward and Backward Traversal in Doubly Linked List
What is the printed output after running the following code that traverses a doubly linked list forward and then backward?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None self.prev = None class DoublyLinkedList: def __init__(self): self.head = None self.tail = None def append(self, val): new_node = Node(val) if not self.head: self.head = new_node self.tail = new_node else: self.tail.next = new_node new_node.prev = self.tail self.tail = new_node def traverse_forward(self): current = self.head result = [] while current: result.append(str(current.val)) current = current.next return ' -> '.join(result) + ' -> null' def traverse_backward(self): current = self.tail result = [] while current: result.append(str(current.val)) current = current.prev return ' -> '.join(result) + ' -> null' # Create list and append values dll = DoublyLinkedList() dll.append(10) dll.append(20) dll.append(30) # Print forward and backward traversal print(dll.traverse_forward()) print(dll.traverse_backward())
Attempts:
2 left
💡 Hint
Think about how the doubly linked list stores references to both next and previous nodes.
✗ Incorrect
The forward traversal prints nodes from head to tail: 10 -> 20 -> 30 -> null. The backward traversal prints nodes from tail to head: 30 -> 20 -> 10 -> null.
🧠 Conceptual
intermediate1:00remaining
Understanding Traversal Direction in Doubly Linked Lists
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
advanced2:00remaining
Identify the Error in Backward Traversal Code
What is wrong with this backward traversal code snippet on a doubly linked list?
DSA Python
def traverse_backward(head): current = head result = [] while current: result.append(str(current.val)) current = current.prev return ' -> '.join(result) + ' -> null' # Assume doubly linked list with head node exists
Attempts:
2 left
💡 Hint
Think about the starting point for backward traversal.
✗ Incorrect
Backward traversal must start from the tail node, not the head. Starting at head only visits the head node since head.prev is None immediately after the first iteration. No runtime error occurs.
❓ Predict Output
advanced2:00remaining
Output After Mixed Forward and Backward Traversal
What is the output of the following code that traverses a doubly linked list forward, then backward, then forward again?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None self.prev = None class DoublyLinkedList: def __init__(self): self.head = None self.tail = None def append(self, val): new_node = Node(val) if not self.head: self.head = new_node self.tail = new_node else: self.tail.next = new_node new_node.prev = self.tail self.tail = new_node def traverse_forward(self): current = self.head result = [] while current: result.append(str(current.val)) current = current.next return ' -> '.join(result) + ' -> null' def traverse_backward(self): current = self.tail result = [] while current: result.append(str(current.val)) current = current.prev return ' -> '.join(result) + ' -> null' # Create list and append values dll = DoublyLinkedList() dll.append(1) dll.append(2) dll.append(3) print(dll.traverse_forward()) print(dll.traverse_backward()) print(dll.traverse_forward())
Attempts:
2 left
💡 Hint
The list does not change between traversals.
✗ Incorrect
The list is traversed forward (1->2->3), then backward (3->2->1), then forward again (1->2->3).
🚀 Application
expert1:30remaining
Number of Nodes Visited in Mixed Traversal
Given a doubly linked list with 5 nodes, if you traverse forward 3 nodes starting from head, then backward 2 nodes, how many unique nodes have you visited in total?
Attempts:
2 left
💡 Hint
Count nodes visited without repeating.
✗ Incorrect
Assume nodes labeled 1-2-3-4-5. Forward traversal visits nodes 1, 2, 3 (ending at 3). Backward 2 nodes from there visits 2, then 1. Unique nodes: 1, 2, 3.