Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return -1 if the array is empty.
DSA C
if (numsSize == [1]) return -1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for empty array size.
Returning numsSize instead of -1.
✗ Incorrect
If the size of the array is 0, it means the array is empty, so we return -1.
2fill in blank
mediumComplete the code to find the middle index between left and right.
DSA C
int mid = left + (right - left) [1] 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division.
Using addition instead of division.
✗ Incorrect
To find the middle index, we add half the distance between left and right to left using division by 2.
3fill in blank
hardFix the error in the condition to check if the target is in the left sorted part.
DSA C
if (nums[left] <= nums[mid] && target >= nums[left] && target [1] nums[mid]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <= causing wrong range check.
Using >= which reverses the condition.
✗ Incorrect
The target should be less than or equal to nums[mid] to be in the left sorted part.
4fill in blank
hardFill both blanks to update left and right pointers correctly.
DSA C
if (target [1] nums[mid]) { right = mid [2] 1; } else { left = mid + 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < in the comparison.
Adding 1 to right pointer instead of subtracting.
✗ Incorrect
If target is less than nums[mid], move right pointer left by one; else move left pointer right.
5fill in blank
hardFill all three blanks to complete the search loop and return the target index or -1.
DSA C
while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == [1]) return mid; if (nums[left] <= nums[mid]) { if (target >= nums[left] && target [2] nums[mid]) { right = mid [3] 1; } else { left = mid + 1; } } else { if (target > nums[mid] && target <= nums[right]) { left = mid + 1; } else { right = mid - 1; } } } return -1;
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing nums[mid] with wrong variable.
Using > instead of <= in range check.
Adding 1 to right pointer instead of subtracting.
✗ Incorrect
Check if nums[mid] equals target, then check if target is between nums[left] and nums[mid] using <=, and move right pointer left by 1.
