Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the middle index in the binary search function.
DSA C
int mid = low [1] (high - low) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition causes wrong mid calculation.
Using multiplication or division incorrectly changes the index.
✗ Incorrect
The middle index is calculated by adding low to half the distance between high and low: low + (high - low) / 2.
2fill in blank
mediumComplete the code to check if the middle element is equal to the target value.
DSA C
if (arr[mid] [1] target) { return mid; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '>' instead of '==' causes wrong condition.
Using '!=' checks for inequality, which is incorrect here.
✗ Incorrect
We check if the middle element equals the target using the equality operator '=='.
3fill in blank
hardFix the error in the recursive call to search the left half when the target is smaller than the middle element.
DSA C
return binarySearch(arr, low, [1], target);
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid + 1 searches the right half incorrectly.
Using high + 1 or low - 1 causes out-of-bound errors.
✗ Incorrect
To search the left half, we update high to mid - 1.
4fill in blank
hardFill both blanks to complete the recursive call for searching the right half when the target is greater than the middle element.
DSA C
return binarySearch(arr, [1], [2], target);
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping low and high values causes wrong search range.
Using mid - 1 for low is incorrect for right half.
✗ Incorrect
To search the right half, low is updated to mid + 1 and high remains the same.
5fill in blank
hardFill all three blanks to complete the base case that returns -1 when the target is not found.
DSA C
if ([1] > [2]) { return [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking high > low instead of low > high reverses logic.
Returning mid instead of -1 gives wrong result.
✗ Incorrect
The base case checks if low is greater than high, meaning target is not found, so return -1.
