Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the first element as minimum if array is not rotated.
DSA Java
if (nums[0] < nums[nums.length - 1]) { return nums[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning nums[1] instead of nums[0]
Returning the last element nums[nums.length - 1]
✗ Incorrect
If the first element is less than the last, the array is not rotated, so the minimum is the first element nums[0].
2fill in blank
mediumComplete the code to calculate the middle index correctly.
DSA Java
int mid = [1] + (right - left) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using right instead of left as base
Using 0 or nums.length which are incorrect indices
✗ Incorrect
The middle index is calculated by adding left to half the distance between right and left to avoid overflow.
3fill in blank
hardFix the error in the condition to decide which half to search next.
DSA Java
if (nums[mid] [1] nums[right]) { right = mid; } else { left = mid + 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <
Using == or != which are incorrect for this logic
✗ Incorrect
If nums[mid] is less than nums[right], the minimum is in the left half including mid, so we move right to mid.
4fill in blank
hardFill both blanks to complete the while loop condition and return statement.
DSA Java
while ([1] < [2]) { int mid = left + (right - left) / 2; if (nums[mid] < nums[right]) { right = mid; } else { left = mid + 1; } } return nums[left];
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid in the loop condition
Using nums.length which is out of index
✗ Incorrect
The loop continues while left is less than right, narrowing down the search. Finally, nums[left] is the minimum.
5fill in blank
hardFill all three blanks to complete the binary search method to find minimum in rotated sorted array.
DSA Java
public int findMin(int[] nums) {
int [1] = 0, [2] = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < nums[right]) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[[3]];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using mid or right in return instead of left
Incorrect initialization of left or right
✗ Incorrect
Initialize left and right pointers, use binary search to narrow down, and return nums[left] as minimum.
