Challenge - 5 Problems
First and Last Occurrence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find first and last occurrence indexes in a sorted array
What is the output of the following Go code that finds the first and last occurrence of the number 3 in a sorted array?
DSA Go
package main import "fmt" func firstAndLast(arr []int, target int) (int, int) { first, last := -1, -1 for i, v := range arr { if v == target { if first == -1 { first = i } last = i } } return first, last } func main() { arr := []int{1, 2, 3, 3, 3, 4, 5} f, l := firstAndLast(arr, 3) fmt.Printf("%d %d\n", f, l) }
Attempts:
2 left
💡 Hint
Look for the first and last index where the target appears in the array.
✗ Incorrect
The function scans the array and updates first when it finds the target for the first time, and keeps updating last as it finds more occurrences. For target 3, first is at index 2 and last at index 4.
❓ Predict Output
intermediate2:00remaining
Output of binary search for first occurrence
What is the output of this Go code that uses binary search to find the first occurrence of 5 in a sorted array?
DSA Go
package main import "fmt" func firstOccurrence(arr []int, target int) int { low, high := 0, len(arr)-1 result := -1 for low <= high { mid := (low + high) / 2 if arr[mid] == target { result = mid high = mid - 1 } else if arr[mid] < target { low = mid + 1 } else { high = mid - 1 } } return result } func main() { arr := []int{1, 3, 5, 5, 5, 7, 9} fmt.Println(firstOccurrence(arr, 5)) }
Attempts:
2 left
💡 Hint
Binary search moves left when it finds the target to find the first occurrence.
✗ Incorrect
The first occurrence of 5 is at index 2. The binary search updates result and moves left to find if there is an earlier occurrence.
🔧 Debug
advanced2:00remaining
Identify the error in last occurrence binary search
What error does the following Go code produce when trying to find the last occurrence of 4 in the array?
DSA Go
package main import "fmt" func lastOccurrence(arr []int, target int) int { low, high := 0, len(arr)-1 result := -1 for low <= high { mid := (low + high) / 2 if arr[mid] == target { result = mid low = mid + 1 } else if arr[mid] > target { high = mid - 1 } else { low = mid + 1 } } return result } func main() { arr := []int{1, 2, 4, 4, 4, 5, 6} fmt.Println(lastOccurrence(arr, 4)) }
Attempts:
2 left
💡 Hint
Check the loop conditions and index updates carefully.
✗ Incorrect
The code correctly updates low and high pointers and returns the last occurrence index 4 without errors.
❓ Predict Output
advanced2:00remaining
Output of combined first and last occurrence function
What is the output of this Go program that finds first and last occurrence of 7 in a sorted array?
DSA Go
package main import "fmt" func firstAndLastOccurrence(arr []int, target int) (int, int) { first, last := -1, -1 low, high := 0, len(arr)-1 // Find first occurrence for low <= high { mid := (low + high) / 2 if arr[mid] == target { first = mid high = mid - 1 } else if arr[mid] < target { low = mid + 1 } else { high = mid - 1 } } low, high = 0, len(arr)-1 // Find last occurrence for low <= high { mid := (low + high) / 2 if arr[mid] == target { last = mid low = mid + 1 } else if arr[mid] > target { high = mid - 1 } else { low = mid + 1 } } return first, last } func main() { arr := []int{1, 3, 5, 7, 7, 7, 9, 11} f, l := firstAndLastOccurrence(arr, 7) fmt.Printf("%d %d\n", f, l) }
Attempts:
2 left
💡 Hint
Binary search finds first and last indexes separately.
✗ Incorrect
The first 7 is at index 3 and last 7 is at index 5 in the array.
🧠 Conceptual
expert1:30remaining
Time complexity of first and last occurrence search
What is the time complexity of finding the first and last occurrence of an element in a sorted array using binary search?
Attempts:
1 left
💡 Hint
Binary search divides the search space in half each step.
✗ Incorrect
Each binary search (for first and last occurrence) takes O(log n), so combined still O(log n).