Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the left pointer to the start of the array.
DSA C
int left = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 1 skips the first element.
Using -1 causes invalid indexing.
Setting left to n goes beyond array bounds.
✗ Incorrect
The left pointer should start at index 0 to cover the whole array.
2fill in blank
mediumComplete the code to calculate the middle index between left and right.
DSA C
int mid = left + ([1] - left) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid inside its own calculation causes errors.
Using n is unrelated to midpoint calculation.
Using left twice does not find midpoint.
✗ Incorrect
We calculate mid as left plus half the distance to right to avoid overflow.
3fill in blank
hardFix the error in the condition to check if the middle element is greater than the right element.
DSA C
if (nums[mid] [1] nums[right]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= reverses the logic.
Using == does not help find minimum.
Using < leads to wrong half selection.
✗ Incorrect
If nums[mid] is greater than nums[right], the minimum is in the right half.
4fill in blank
hardFill both blanks to update pointers correctly when nums[mid] is greater than nums[right].
DSA C
if (nums[mid] > nums[right]) { left = [1]; } else { right = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting left = mid skips the minimum if it is mid + 1.
Setting right = mid - 1 may skip the minimum.
Using right - 1 is incorrect here.
✗ Incorrect
When nums[mid] > nums[right], minimum is right of mid, so left = mid + 1. Else, minimum is at mid or left, so right = mid.
5fill in blank
hardFill all three blanks to complete the function that finds the minimum in a rotated sorted array.
DSA C
int findMin(int* nums, int numsSize) {
int left = [1], right = numsSize - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = [2];
} else {
right = [3];
}
}
return nums[left];
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at numsSize causes out-of-bounds.
Setting left = mid skips minimum if it is mid + 1.
Setting right = mid - 1 may miss the minimum.
✗ Incorrect
Initialize left to 0. If nums[mid] > nums[right], move left to mid + 1. Else, move right to mid. Finally, return nums[left] as minimum.
