Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the mid index in binary search.
DSA C
int mid = low [1] (high - low) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes mid to be less than low.
Using '*' or '/' directly without proper parentheses causes wrong calculation.
✗ Incorrect
The mid index is calculated by adding half the distance between low and high to low, so '+' is correct.
2fill in blank
mediumComplete the condition to check if mid element is less than its next element.
DSA C
if (nums[mid] [1] nums[mid + 1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' reverses the logic and leads to wrong search direction.
Using '==' or '!=' does not help decide the search direction.
✗ Incorrect
If nums[mid] is less than nums[mid + 1], we move right to find the peak.
3fill in blank
hardFix the error in updating the high index when moving left.
DSA C
else { high = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to mid + 1 causes infinite loop or wrong search.
Using low + 1 or high - 1 unrelated to mid causes incorrect boundaries.
✗ Incorrect
When moving left, high should be set to mid - 1 to exclude mid from next search.
4fill in blank
hardFill both blanks to complete the binary search loop and return statement.
DSA C
while (low [1] high) { int mid = low + (high - low) / 2; if (nums[mid] < nums[mid + 1]) { low = mid + 1; } else { high = mid - 1; } } return [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' in loop condition may miss the peak at the boundary.
Returning high instead of low may give wrong index.
✗ Incorrect
The loop runs while low is less than or equal to high, and the peak index is at low after the loop.
5fill in blank
hardFill all three blanks to complete the function signature, variable declaration, and return statement.
DSA C
[1] findPeakElement([2] nums[], int numsSize) { int low = 0, high = numsSize - 1; while (low <= high) { int mid = low + (high - low) / 2; if (nums[mid] < nums[mid + 1]) { low = mid + 1; } else { high = mid - 1; } } return [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type causes compilation error.
Returning high instead of low may give wrong peak index.
✗ Incorrect
The function returns an int index, takes an int array, and returns low as the peak index.
