Challenge - 5 Problems
Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Linear Search on an Unsorted Array
What is the output of the following C code that performs a linear search for the value 7 in the array?
DSA C
int arr[] = {3, 5, 7, 9, 11}; int n = 5; int target = 7; int i; int found_index = -1; for(i = 0; i < n; i++) { if(arr[i] == target) { found_index = i; break; } } printf("%d\n", found_index);
Attempts:
2 left
💡 Hint
Linear search checks each element until it finds the target or reaches the end.
✗ Incorrect
The linear search finds the target 7 at index 2 in the array and prints 2.
❓ Predict Output
intermediateOutput of Binary Search on a Sorted Array
What is the output of the following C code that performs a binary search for the value 7 in the sorted array?
DSA C
int arr[] = {1, 3, 5, 7, 9, 11}; int n = 6; int target = 7; int left = 0, right = n - 1, mid; int found_index = -1; while(left <= right) { mid = left + (right - left) / 2; if(arr[mid] == target) { found_index = mid; break; } else if(arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } printf("%d\n", found_index);
Attempts:
2 left
💡 Hint
Binary search divides the search space in half each step.
✗ Incorrect
The binary search finds the target 7 at index 3 and prints 3.
🧠 Conceptual
advancedComparing Time Complexity of Linear and Binary Search
Which statement correctly compares the time complexity of linear search and binary search on an array of size n?
Attempts:
2 left
💡 Hint
Think about how many elements each search checks in the worst case.
✗ Incorrect
Linear search checks each element one by one, so O(n). Binary search divides the search space in half each step, so O(log n), but only works on sorted arrays.
❓ Predict Output
advancedOutput of Binary Search on an Unsorted Array
What is the output of the following C code that performs a binary search for the value 7 on an unsorted array?
DSA C
int arr[] = {9, 3, 7, 1, 11}; int n = 5; int target = 7; int left = 0, right = n - 1, mid; int found_index = -1; while(left <= right) { mid = left + (right - left) / 2; if(arr[mid] == target) { found_index = mid; break; } else if(arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } printf("%d\n", found_index);
Attempts:
2 left
💡 Hint
Binary search requires sorted arrays to work correctly.
✗ Incorrect
Even though the array is unsorted, the first mid calculation lands on index 2 where arr[2] == 7, so it finds and prints 2. Binary search is not guaranteed to work on unsorted arrays.
🧠 Conceptual
expertReal Cost Difference Between Linear and Binary Search
Which of the following best describes the real cost difference between linear search and binary search when searching for an element in a large dataset?
Attempts:
2 left
💡 Hint
Consider dataset size and whether the data is sorted.
✗ Incorrect
Binary search requires sorted data and fewer comparisons, making it faster on large sorted datasets. Linear search is simpler and can be faster on small or unsorted data.
