Challenge - 5 Problems
Linear Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of linear search index return
What is the output of this Python code that performs a linear search for the value 7 in the list?
DSA Python
def linear_search(arr, target): for i in range(len(arr)): if arr[i] == target: return i return -1 result = linear_search([3, 5, 7, 9, 11], 7) print(result)
Attempts:
2 left
💡 Hint
Remember that list indexes start at 0 and linear search returns the first matching index.
✗ Incorrect
The value 7 is at index 2 in the list. The function returns the index where the target is found.
🧠 Conceptual
intermediateNumber of comparisons in worst case linear search
If a linear search is performed on a list of 10 elements and the target is not present, how many comparisons will be made?
Attempts:
2 left
💡 Hint
In worst case, linear search checks every element once.
✗ Incorrect
If the target is not found, linear search compares with all 10 elements once each.
🔧 Debug
advancedIdentify the error in this linear search code
What error will this code produce when searching for 4 in the list?
DSA Python
def linear_search(arr, target): i = 0 while i < len(arr): if arr[i] == target: return i i += 1 return -1 print(linear_search([1, 2, 3], 4))
Attempts:
2 left
💡 Hint
Check the loop condition and how indexes are accessed.
✗ Incorrect
The loop condition allows i to reach len(arr), which is out of range for indexing.
❓ Predict Output
advancedOutput of linear search with multiple occurrences
What is the output of this code searching for 5 in the list with duplicates?
DSA Python
def linear_search(arr, target): for i in range(len(arr)): if arr[i] == target: return i return -1 print(linear_search([1, 5, 3, 5, 7], 5))
Attempts:
2 left
💡 Hint
Linear search returns the first index where the target is found.
✗ Incorrect
The first 5 is at index 1, so the function returns 1 immediately.
🧠 Conceptual
expertTime complexity of linear search in average case
What is the average time complexity of linear search on a list of n elements?
Attempts:
2 left
💡 Hint
Think about how many elements are checked on average before finding the target.
✗ Incorrect
On average, linear search checks half the elements, which is proportional to n, so O(n).
