Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the first occurrence index of target in the sorted array.
DSA Go
func firstOccurrence(arr []int, target int) int {
low, high := 0, len(arr)-1
result := -1
for low <= high {
mid := low + (high - low) / 2
if arr[mid] == [1] {
result = mid
high = mid - 1
} else if arr[mid] < target {
low = mid + 1
} else {
high = mid - 1
}
}
return result
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with mid instead of target.
Using low or high instead of target in the comparison.
✗ Incorrect
We compare arr[mid] with the target value to check if we found the element.
2fill in blank
mediumComplete the code to find the last occurrence index of target in the sorted array.
DSA Go
func lastOccurrence(arr []int, target int) int {
low, high := 0, len(arr)-1
result := -1
for low <= high {
mid := low + (high - low) / 2
if arr[mid] == target {
result = mid
low = [1]
} else if arr[mid] < target {
low = mid + 1
} else {
high = mid - 1
}
}
return result
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Moving high instead of low after finding the target.
Setting low to mid - 1 which searches the left half incorrectly.
✗ Incorrect
To find the last occurrence, we move the low pointer to mid + 1 after finding the target.
3fill in blank
hardFix the error in the code to correctly find the first occurrence of target.
DSA Go
func firstOccurrence(arr []int, target int) int {
low, high := 0, len(arr)-1
result := -1
for low <= high {
mid := low + (high - low) / 2
if arr[mid] == target {
result = mid
high = [1]
} else if arr[mid] > target {
high = mid - 1
} else {
low = mid + 1
}
}
return result
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Moving high to mid + 1 which searches right half incorrectly.
Not updating high pointer after finding target.
✗ Incorrect
To find the first occurrence, after finding target, move high to mid - 1 to search left side.
4fill in blank
hardFill both blanks to create a function that returns first and last occurrence indices of target.
DSA Go
func firstAndLastOccurrence(arr []int, target int) (int, int) {
first, last := -1, -1
low, high := 0, len(arr)-1
for low <= high {
mid := low + (high - low) / 2
if arr[mid] == target {
first = mid
high = [1]
} else if arr[mid] < target {
low = mid + 1
} else {
high = mid - 1
}
}
low, high = 0, len(arr)-1
for low <= high {
mid := low + (high - low) / 2
if arr[mid] == target {
last = mid
low = [2]
} else if arr[mid] < target {
low = mid + 1
} else {
high = mid - 1
}
}
return first, last
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the updates of low and high pointers.
Not resetting low and high before second search.
✗ Incorrect
For first occurrence, move high to mid - 1; for last occurrence, move low to mid + 1.
5fill in blank
hardFill all three blanks to create a map of element to its first occurrence index from the array.
DSA Go
func mapFirstOccurrences(arr []int) map[int]int {
result := make(map[int]int)
for i, val := range arr {
if _, exists := result[[1]]; !exists {
result[[2]] = [3]
}
}
return result
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i as key instead of val.
Assigning result[i] = val which is incorrect.
✗ Incorrect
We check if val exists as key, then set result[val] = i for first occurrence index.