0
0
DSA Pythonprogramming~20 mins

Traversal Forward and Backward in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Traversal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
A"10 -> 20 -> 30 -> null\n10 -> 20 -> 30 -> null"
B"10 -> 20 -> 30 -> null\n30 -> 20 -> 10 -> null"
C"30 -> 20 -> 10 -> null\n10 -> 20 -> 30 -> null"
D"30 -> 20 -> 10 -> null\n30 -> 20 -> 10 -> null"
Attempts:
2 left
💡 Hint
Think about how the doubly linked list stores references to both next and previous nodes.
🧠 Conceptual
intermediate
1:00remaining
Understanding Traversal Direction in Doubly Linked Lists
In a doubly linked list, which pointer is used to move backward during traversal?
AThe 'next' pointer
BThe 'tail' pointer
CThe 'head' pointer
DThe 'prev' pointer
Attempts:
2 left
💡 Hint
Backward means going to the previous node.
🔧 Debug
advanced
2: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
A"Logical error: starts from 'head' instead of 'tail', only visits the head node"
BInfinite loop because current never becomes None
CNo error, returns correct backward traversal
DTypeError because 'prev' is not a valid attribute
Attempts:
2 left
💡 Hint
Think about the starting point for backward traversal.
Predict Output
advanced
2: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())
A"1 -> 2 -> 3 -> null\n1 -> 2 -> 3 -> null\n3 -> 2 -> 1 -> null"
B"3 -> 2 -> 1 -> null\n1 -> 2 -> 3 -> null\n3 -> 2 -> 1 -> null"
C"1 -> 2 -> 3 -> null\n3 -> 2 -> 1 -> null\n1 -> 2 -> 3 -> null"
D"3 -> 2 -> 1 -> null\n3 -> 2 -> 1 -> null\n1 -> 2 -> 3 -> null"
Attempts:
2 left
💡 Hint
The list does not change between traversals.
🚀 Application
expert
1: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?
A3 unique nodes
B4 unique nodes
C5 unique nodes
D2 unique nodes
Attempts:
2 left
💡 Hint
Count nodes visited without repeating.