Challenge - 5 Problems
First and Last Occurrence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind first and last occurrence indexes
What is the output of the following Java code that finds the first and last occurrence of the number 3 in the array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {1, 3, 5, 3, 7, 3, 9}; int first = -1, last = -1; for (int i = 0; i < arr.length; i++) { if (arr[i] == 3) { if (first == -1) first = i; last = i; } } System.out.println("First: " + first + ", Last: " + last); } }
Attempts:
2 left
💡 Hint
Look for the first and last index where the value 3 appears in the array.
✗ Incorrect
The number 3 appears at indexes 1, 3, and 5. The first occurrence is at index 1 and the last at index 5.
🧠 Conceptual
intermediateUnderstanding first and last occurrence logic
Which statement correctly describes how to find the first and last occurrence of an element in an array?
Attempts:
2 left
💡 Hint
Think about how to track first and last positions in one pass.
✗ Incorrect
By traversing once, you can set the first occurrence when you find the element the first time and keep updating the last occurrence whenever you find it again.
❓ Predict Output
advancedOutput of binary search for first occurrence
What is the output of this Java code that uses binary search to find the first occurrence of 4 in a sorted array?
DSA Java
public class Main { public static int firstOccurrence(int[] arr, int x) { int low = 0, high = arr.length - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) { result = mid; high = mid - 1; } else if (arr[mid] < x) { low = mid + 1; } else { high = mid - 1; } } return result; } public static void main(String[] args) { int[] arr = {1, 2, 4, 4, 4, 5, 6}; System.out.println(firstOccurrence(arr, 4)); } }
Attempts:
2 left
💡 Hint
Binary search narrows down to the leftmost index where 4 appears.
✗ Incorrect
The first occurrence of 4 is at index 2 in the sorted array.
❓ Predict Output
advancedOutput of binary search for last occurrence
What is the output of this Java code that uses binary search to find the last occurrence of 4 in a sorted array?
DSA Java
public class Main { public static int lastOccurrence(int[] arr, int x) { int low = 0, high = arr.length - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) { result = mid; low = mid + 1; } else if (arr[mid] < x) { low = mid + 1; } else { high = mid - 1; } } return result; } public static void main(String[] args) { int[] arr = {1, 2, 4, 4, 4, 5, 6}; System.out.println(lastOccurrence(arr, 4)); } }
Attempts:
2 left
💡 Hint
Binary search moves right to find the last occurrence index.
✗ Incorrect
The last occurrence of 4 is at index 4 in the sorted array.
🧠 Conceptual
expertTime complexity of finding first and last occurrence
What is the time complexity of finding the first and last occurrence of an element in a sorted array using binary search?
Attempts:
2 left
💡 Hint
Consider that you do two binary searches independently.
✗ Incorrect
Each binary search takes O(log n), doing two searches is 2 * O(log n) which simplifies to O(log n).
