Hint: Use low + (high - low) / 2 to prevent overflow [OK]
Common Mistakes:
Using (low + high) / 2 causing overflow
Subtracting incorrectly in mid calculation
Multiplying indices instead of adding
3. What will be the output of the following C# code snippet?
int[] arr = {2, 4, 6, 8, 10};
int target = 6;
int low = 0, high = arr.Length - 1;
int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
result = mid;
break;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
Console.WriteLine(result);
medium
A. -1
B. 3
C. 2
D. 0
Solution
Step 1: Trace the binary search steps
Initial low=0, high=4, mid=2 (element 6). arr[2] == target 6, so result=2 and loop breaks.
Step 2: Output the result
Since target found at index 2, Console.WriteLine prints 2.
Final Answer:
2 -> Option C
Quick Check:
Target 6 found at index 2 [OK]
Hint: Check middle element first to find target quickly [OK]
Common Mistakes:
Confusing index with value
Not updating low or high correctly
Assuming target not found returns 0
4. Identify the error in the following iterative binary search code snippet and choose the correct fix:
int BinarySearch(int[] arr, int target) {
int low = 0, high = arr.Length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
medium
A. Change while condition to 'while (low <= high)'
B. Change mid calculation to '(low + high) / 2'
C. Remove 'return -1;' at the end
D. Change 'low = mid + 1;' to 'low = mid - 1;'
Solution
Step 1: Analyze loop condition
The loop uses 'while (low < high)', which misses checking when low equals high, possibly skipping the last element.
Step 2: Correct loop condition
Changing to 'while (low <= high)' ensures all elements are checked including when low equals high.
Final Answer:
Change while condition to 'while (low <= high)' -> Option A
Quick Check:
Loop must include low == high to check all elements [OK]
Hint: Use <= in loop to include last element check [OK]
Common Mistakes:
Using < instead of <= in loop condition
Incorrect mid calculation causing overflow
Wrong update of low or high pointers
5. Given a sorted array with possible duplicate elements, how can you modify the iterative binary search to find the first occurrence of a target value?
hard
A. Return immediately when target is found
B. When target is found, continue searching left half to find earlier occurrence
C. Search only the right half after finding target
D. Use linear search instead of binary search
Solution
Step 1: Understand duplicates in sorted array
Duplicates mean target can appear multiple times; we want the first index where it appears.
Step 2: Modify binary search to find first occurrence
When target found at mid, record mid but continue searching left half (high = mid - 1) to find earlier occurrence.
Final Answer:
When target is found, continue searching left half to find earlier occurrence -> Option B
Quick Check:
Find first occurrence by searching left after match [OK]
Hint: On match, move left to find first occurrence [OK]
Common Mistakes:
Stopping search immediately on first match
Searching right half after finding target
Switching to linear search unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.