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 in a sorted array
What is the output of the following C# code that finds the first and last occurrence of the number 3 in the array?
DSA C#
int[] arr = {1, 2, 3, 3, 3, 4, 5}; int first = Array.IndexOf(arr, 3); int last = Array.LastIndexOf(arr, 3); Console.WriteLine($"First: {first}, Last: {last}");
Attempts:
2 left
💡 Hint
Remember that Array.IndexOf returns the first index where the element appears, and Array.LastIndexOf returns the last index.
✗ Incorrect
The number 3 first appears at index 2 and last appears at index 4 in the array.
❓ Predict Output
intermediateOutput of binary search for first occurrence
What will be the output of this C# code that uses binary search to find the first occurrence of 5 in a sorted array?
DSA C#
int[] arr = {1, 3, 5, 5, 5, 7, 9}; int target = 5; int first = -1; int low = 0, high = arr.Length - 1; while (low <= high) { int 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; } } Console.WriteLine(first);
Attempts:
2 left
💡 Hint
Binary search narrows down to the first occurrence by moving high pointer.
✗ Incorrect
The first occurrence of 5 is at index 2.
🔧 Debug
advancedWhy does this code fail to find last occurrence?
This C# code tries to find the last occurrence of 7 in a sorted array but enters an infinite loop. What is the reason?
DSA C#
int[] arr = {2, 4, 7, 7, 7, 8, 10}; int target = 7; int last = -1; int low = 0, high = arr.Length - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == target) { last = mid; low = mid + 1; // <-- Problem here } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } Console.WriteLine(last);
Attempts:
2 left
💡 Hint
Think about how to move the search window to the right side after finding the target.
✗ Incorrect
To find the last occurrence, after finding target at mid, we must move low to mid + 1 to search right side. 'low = mid - 1' moves left, causing infinite loop or wrong result.
🧠 Conceptual
advancedTime 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
Binary search halves the search space each step.
✗ Incorrect
Binary search runs in logarithmic time, so finding first and last occurrence using it is O(log n).
🚀 Application
expertFind first and last occurrence in unsorted array efficiently
Given an unsorted array, which approach is the most efficient to find the first and last occurrence of a target element?
Attempts:
2 left
💡 Hint
Think about the minimum passes needed over the array.
✗ Incorrect
A single pass can track both first and last occurrence by updating indexes when target is found, making it O(n) time and O(1) space.
