0
0
DSA Pythonprogramming~20 mins

Search for a Value in Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of searching for value 3 in this linked list?

Given the linked list: 1 -> 2 -> 3 -> 4 -> null, what will be the output of searching for value 3?

DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def search(head, target):
    current = head
    while current:
        if current.val == target:
            return True
        current = current.next
    return False

# Create linked list
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)

result = search(head, 3)
print(result)
Atrue
BFalse
CNone
DTrue
Attempts:
2 left
💡 Hint

Check if the search function returns a boolean True or the string 'true'.

Predict Output
intermediate
2:00remaining
What is the output when searching for a value not in the list?

Given the linked list: 5 -> 10 -> 15 -> null, what will be the output of searching for value 7?

DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def search(head, target):
    current = head
    while current:
        if current.val == target:
            return True
        current = current.next
    return False

# Create linked list
head = Node(5)
head.next = Node(10)
head.next.next = Node(15)

result = search(head, 7)
print(result)
ANone
BTrue
CFalse
D0
Attempts:
2 left
💡 Hint

What does the function return if the target is not found?

🔧 Debug
advanced
2:00remaining
What error does this code raise when searching for a value?

Consider this code snippet for searching a value in a linked list. What error will it raise when run?

DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def search(head, target):
    current = head
    while current.next:
        if current.val == target:
            return True
        current = current.next
    return False

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)

print(search(head, 3))
ANone
BIndexError
CAttributeError
DInfinite loop
Attempts:
2 left
💡 Hint

Check the loop condition and what happens when current is the last node.

🚀 Application
advanced
2:00remaining
How many nodes are traversed when searching for value 4 in this list?

Given the linked list: 4 -> 7 -> 9 -> 12 -> null, how many nodes does the search function check when searching for value 4?

DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def search(head, target):
    current = head
    count = 0
    while current:
        count += 1
        if current.val == target:
            return count
        current = current.next
    return count

head = Node(4)
head.next = Node(7)
head.next.next = Node(9)
head.next.next.next = Node(12)

result = search(head, 4)
print(result)
A1
B2
C3
D4
Attempts:
2 left
💡 Hint

Think about where the target value is in the list.

🧠 Conceptual
expert
2:00remaining
Which option best describes the time complexity of searching a value in a singly linked list?

What is the time complexity of searching for a value in a singly linked list with n nodes?

AO(1) - Constant time because we can jump to any node directly
BO(n) - Linear time because we may need to check each node
CO(log n) - Logarithmic time due to binary search on the list
DO(n^2) - Quadratic time because of nested loops
Attempts:
2 left
💡 Hint

Think about how nodes are connected and how you access them.