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 of 3 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
#include <stdio.h> int firstOccurrence(int arr[], int n, int x) { int low = 0, high = n - 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; } int lastOccurrence(int arr[], int n, int x) { int low = 0, high = n - 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; } int main() { int arr[] = {1, 2, 3, 3, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int x = 3; int first = firstOccurrence(arr, n, x); int last = lastOccurrence(arr, n, x); printf("First: %d, Last: %d\n", first, last); return 0; }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and the array is sorted.
✗ Incorrect
The first occurrence of 3 is at index 2 and the last occurrence is at index 4 in the array.
❓ Predict Output
intermediateOutput when element is not present
What will be the output of the code when searching for element 6 in the array?
DSA C
#include <stdio.h> int firstOccurrence(int arr[], int n, int x) { int low = 0, high = n - 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; } int lastOccurrence(int arr[], int n, int x) { int low = 0, high = n - 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; } int main() { int arr[] = {1, 2, 3, 3, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int x = 6; int first = firstOccurrence(arr, n, x); int last = lastOccurrence(arr, n, x); printf("First: %d, Last: %d\n", first, last); return 0; }
Attempts:
2 left
💡 Hint
If the element is not found, the functions return -1.
✗ Incorrect
Since 6 is not in the array, both first and last occurrence functions return -1.
🔧 Debug
advancedIdentify the bug in lastOccurrence function
What is the bug in the following lastOccurrence function that causes incorrect output?
DSA C
int lastOccurrence(int arr[], int n, int x) { int low = 0, high = n - 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; }
Attempts:
2 left
💡 Hint
Check the loop condition carefully for binary search correctness.
✗ Incorrect
Using 'low < high' can miss checking the last element; 'low <= high' ensures all elements are checked.
🧠 Conceptual
advancedWhy binary search is suitable for finding first and last occurrence?
Why is binary search a good choice to find the first and last occurrence of an element in a sorted array?
Attempts:
2 left
💡 Hint
Think about how binary search divides the array.
✗ Incorrect
Binary search halves the search space each step, making it efficient to find positions in sorted arrays.
❓ Predict Output
expertOutput of first and last occurrence in array with all identical elements
What is the output of the code when the array contains all identical elements 7 and we search for 7?
DSA C
#include <stdio.h> int firstOccurrence(int arr[], int n, int x) { int low = 0, high = n - 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; } int lastOccurrence(int arr[], int n, int x) { int low = 0, high = n - 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; } int main() { int arr[] = {7, 7, 7, 7, 7, 7, 7}; int n = sizeof(arr) / sizeof(arr[0]); int x = 7; int first = firstOccurrence(arr, n, x); int last = lastOccurrence(arr, n, x); printf("First: %d, Last: %d\n", first, last); return 0; }
Attempts:
2 left
💡 Hint
All elements are the same, so first is start and last is end index.
✗ Incorrect
Since all elements are 7, first occurrence is at index 0 and last occurrence is at index 6.
