Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return -1 if the array is empty.
DSA Go
if len(nums) == 0 { return [1] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 which could be confused with a valid index.
Returning length of the array which is out of index range.
✗ Incorrect
Returning -1 indicates the target is not found when the array is empty.
2fill in blank
mediumComplete the code to calculate the middle index correctly.
DSA Go
mid := [1] + (high - low) / 2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using high directly which can cause index out of range.
Using 0 which ignores the current search range.
✗ Incorrect
The middle index is calculated by adding low to half the distance between high and low.
3fill in blank
hardFix the error in the condition to check if the left half is sorted.
DSA Go
if nums[[1]] <= nums[mid] {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using high or mid index incorrectly in the condition.
Using 0 which may not be the current low index.
✗ Incorrect
We compare nums[low] with nums[mid] to check if the left half is sorted.
4fill in blank
hardFill both blanks to check if the target is in the left sorted half.
DSA Go
if target >= nums[[1]] && target < nums[[2]] { high = mid - 1 } else { low = mid + 1 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using high instead of mid in the second comparison.
Using 0 which may not be the current low index.
✗ Incorrect
We check if target is between nums[low] and nums[mid] to decide the search range.
5fill in blank
hardFill all three blanks to check if the target is in the right sorted half.
DSA Go
if target > nums[[1]] && target <= nums[[2]] { [3] = mid + 1 } else { high = mid - 1 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using low instead of mid or high in comparisons.
Updating wrong variable in the assignment.
✗ Incorrect
We check if target is between nums[mid+1] and nums[high], then update low to mid+1.