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 an Unsorted Array
What is the output of the following C# code that performs a linear search for the value 7 in an unsorted array?
DSA C#
int[] arr = {4, 2, 7, 1, 9}; int target = 7; int index = -1; for (int i = 0; i < arr.Length; i++) { if (arr[i] == target) { index = i; break; } } Console.WriteLine(index);
Attempts:
2 left
💡 Hint
Linear search checks each element until it finds the target or reaches the end.
✗ Incorrect
The linear search scans the array from start to end. The value 7 is at index 2, so the output is 2.
❓ Predict Output
intermediateOutput of Binary Search on a Sorted Array
What is the output of this C# code that performs a binary search for the value 5 in a sorted array?
DSA C#
int[] arr = {1, 3, 5, 7, 9}; int target = 5; int left = 0, right = arr.Length - 1; int index = -1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { index = mid; break; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } Console.WriteLine(index);
Attempts:
2 left
💡 Hint
Binary search divides the array and looks at the middle element.
✗ Incorrect
The target 5 is at index 2 in the sorted array, so the binary search finds it and prints 2.
🧠 Conceptual
advancedCost Difference Between Linear and Binary Search
Which statement best describes the real cost difference between linear search and binary search when searching for an element in a large sorted array?
Attempts:
2 left
💡 Hint
Think about how many elements each search inspects in the worst case.
✗ Incorrect
Binary search reduces the search space by half each step, so it checks far fewer elements than linear search, which checks each element one by one.
❓ Predict Output
advancedOutput of Binary Search on an Unsorted Array
What is the output of this C# code that performs a binary search for the value 4 on an unsorted array?
DSA C#
int[] arr = {5, 3, 8, 4, 2}; int target = 4; int left = 0, right = arr.Length - 1; int index = -1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { index = mid; break; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } Console.WriteLine(index);
Attempts:
2 left
💡 Hint
Binary search requires a sorted array to work correctly.
✗ Incorrect
Because the array is unsorted, binary search does not find the target 4 and returns -1.
🧠 Conceptual
expertWhen Linear Search Can Be More Efficient Than Binary Search
In which scenario can linear search be more efficient than binary search despite the array being sorted?
Attempts:
2 left
💡 Hint
Consider the cost of binary search setup versus finding an element quickly at the start.
✗ Incorrect
If the target is near the start, linear search finds it quickly without the overhead of binary search steps.
