Challenge - 5 Problems
Search Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Linear Search on a Sorted List
What is the output of the following Python code that performs a linear search for the number 7 in a sorted list?
DSA Python
def linear_search(arr, target): for i, val in enumerate(arr): if val == target: return i return -1 arr = [1, 3, 5, 7, 9, 11] result = linear_search(arr, 7) print(result)
Attempts:
2 left
💡 Hint
Linear search returns the index of the first matching element.
✗ Incorrect
The number 7 is at index 3 in the list. Linear search checks each element until it finds 7 and returns its index.
❓ Predict Output
intermediateOutput of Binary Search on a Sorted List
What is the output of the following Python code that performs a binary search for the number 7 in a sorted list?
DSA Python
def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 arr = [1, 3, 5, 7, 9, 11] result = binary_search(arr, 7) print(result)
Attempts:
2 left
💡 Hint
Binary search divides the list and finds the target index efficiently.
✗ Incorrect
The number 7 is at index 3. Binary search finds it by checking the middle element and narrowing the search range.
🧠 Conceptual
advancedTime Complexity Comparison
Which statement correctly compares the time complexity of linear search and binary search on a sorted list of size n?
Attempts:
2 left
💡 Hint
Binary search cuts the search space in half each step.
✗ Incorrect
Linear search checks each element one by one, so it takes O(n) time. Binary search divides the list repeatedly, taking O(log n) time.
❓ Predict Output
advancedOutput When Binary Search is Used on an Unsorted List
What is the output of the following code when binary search is used on an unsorted list to find the number 7?
DSA Python
def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 arr = [9, 1, 11, 7, 3, 5] result = binary_search(arr, 7) print(result)
Attempts:
2 left
💡 Hint
Binary search requires a sorted list to work correctly.
✗ Incorrect
The list is unsorted, so binary search cannot reliably find the target. It returns -1 indicating not found.
🧠 Conceptual
expertReal Cost Difference in Practice
Which factor most affects the real-world performance difference between linear search and binary search on large datasets?
Attempts:
2 left
💡 Hint
Binary search only works correctly if data is sorted.
✗ Incorrect
Binary search requires sorted data to work efficiently. If data is unsorted, linear search is the only option, affecting performance.
