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 of 3 in sorted array
What is the output of the following C++ code that finds the first and last occurrence of the number 3 in a sorted array?
DSA C++
#include <iostream> #include <vector> using namespace std; int firstOccurrence(const vector<int>& arr, int target) { int low = 0, high = arr.size() - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; high = mid - 1; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return result; } int lastOccurrence(const vector<int>& arr, int target) { int low = 0, high = arr.size() - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; low = mid + 1; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return result; } int main() { vector<int> arr = {1, 2, 3, 3, 3, 4, 5}; int target = 3; cout << firstOccurrence(arr, target) << " " << lastOccurrence(arr, target) << endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that the array is sorted and contains multiple 3s. The first occurrence is the smallest index where 3 appears, and the last occurrence is the largest index.
✗ Incorrect
The first occurrence of 3 is at index 2 (0-based), and the last occurrence is at index 4. The binary search narrows down to these indices by adjusting the search space when the target is found.
❓ Predict Output
intermediate2:00remaining
Output when element is not present
What will be the output of the following code when searching for element 6 in the array?
DSA C++
#include <iostream> #include <vector> using namespace std; int firstOccurrence(const vector<int>& arr, int target) { int low = 0, high = arr.size() - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; high = mid - 1; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return result; } int lastOccurrence(const vector<int>& arr, int target) { int low = 0, high = arr.size() - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; low = mid + 1; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return result; } int main() { vector<int> arr = {1, 2, 3, 3, 3, 4, 5}; int target = 6; cout << firstOccurrence(arr, target) << " " << lastOccurrence(arr, target) << endl; 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 firstOccurrence and lastOccurrence return -1, indicating the element is absent.
🔧 Debug
advanced2:00remaining
Identify the bug in lastOccurrence function
The following code is intended to find the last occurrence of a target in a sorted array. What is the bug causing incorrect output?
DSA C++
int lastOccurrence(const vector<int>& arr, int target) { int low = 0, high = arr.size() - 1, result = -1; while (low < high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; low = mid + 1; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return result; }
Attempts:
2 left
💡 Hint
Check the loop condition carefully to ensure all elements are checked.
✗ Incorrect
Using 'low < high' causes the loop to miss checking when low equals high, potentially skipping the last occurrence. Changing to 'low <= high' ensures all elements are checked.
❓ Predict Output
advanced2:00remaining
Output of first and last occurrence in array with single element
What is the output of the code when the array contains only one element which is the target?
DSA C++
#include <iostream> #include <vector> using namespace std; int firstOccurrence(const vector<int>& arr, int target) { int low = 0, high = arr.size() - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; high = mid - 1; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return result; } int lastOccurrence(const vector<int>& arr, int target) { int low = 0, high = arr.size() - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; low = mid + 1; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return result; } int main() { vector<int> arr = {7}; int target = 7; cout << firstOccurrence(arr, target) << " " << lastOccurrence(arr, target) << endl; return 0; }
Attempts:
2 left
💡 Hint
With only one element equal to target, both first and last occurrence are at index 0.
✗ Incorrect
The single element matches the target, so both first and last occurrence indices are 0.
🧠 Conceptual
expert2:00remaining
Why binary search is efficient for first and last occurrence?
Why is binary search preferred over linear search to find the first and last occurrence of an element in a sorted array?
Attempts:
2 left
💡 Hint
Think about how many elements are checked in each method.
✗ Incorrect
Binary search halves the search space each time, leading to logarithmic time complexity O(log n). Linear search checks each element one by one, leading to O(n) time.