What does 'First Occurrence of Element' mean in an array?
It means finding the index of the first time a given element appears in the array, starting from the left (index 0).
Click to reveal answer
beginner
What does 'Last Occurrence of Element' mean in an array?
It means finding the index of the last time a given element appears in the array, starting from the left (index 0).
Click to reveal answer
intermediate
Why is it useful to find the first and last occurrence of an element?
It helps to know the range where the element appears, useful for counting how many times it occurs or for searching efficiently.
Click to reveal answer
beginner
What is a simple way to find the first occurrence of an element in an array?
Start from the beginning of the array and check each element until you find the target element. Return that index.
Click to reveal answer
intermediate
How can binary search help in finding first and last occurrences in a sorted array?
Binary search can be modified to find the first or last position of an element by adjusting the search boundaries when the element is found.
Click to reveal answer
What index does the first occurrence of an element represent?
AThe middle index of the array
BThe largest index where the element appears
CThe smallest index where the element appears
DThe last index of the array
✗ Incorrect
The first occurrence is the smallest index where the element is found.
If an element does not exist in the array, what should the function return?
A0
BThe length of the array
CThe first index
D-1
✗ Incorrect
Returning -1 indicates the element was not found.
Which search method is best for finding first and last occurrences in a sorted array?
ABinary search
BLinear search
CDepth-first search
DBreadth-first search
✗ Incorrect
Binary search is efficient for sorted arrays.
What is the time complexity of linear search for first occurrence?
AO(1)
BO(n)
CO(n log n)
DO(log n)
✗ Incorrect
Linear search checks each element, so it takes O(n) time.
In binary search for first occurrence, what do you do when you find the element?
ASearch left half to find earlier occurrence
BReturn immediately
CSearch right half to find later occurrence
DStop searching
✗ Incorrect
To find the first occurrence, continue searching the left half.
Explain how to find the first and last occurrence of an element in a sorted array using binary search.
Think about how to move left or right when you find the element.
You got /5 concepts.
Describe a simple approach to find the first and last occurrence of an element in an unsorted array.
Check elements one by one from start and end.
You got /3 concepts.
Practice
(1/5)
1. What does the "First and Last Occurrence of Element" problem ask you to find in an array?
easy
A. The number of unique elements in the array
B. The sum of all elements in the array
C. The maximum and minimum elements in the array
D. The first and last positions where a given element appears
Solution
Step 1: Understand the problem statement
The problem asks to find the first and last positions of a specific element in an array.
Step 2: Differentiate from other array queries
It is not about sums, max/min, or counting unique elements, but about positions of a target element.
Final Answer:
The first and last positions where a given element appears -> Option D
Quick Check:
Positions of element = first and last occurrence [OK]
Hint: Look for positions of target element, not values or counts [OK]
Common Mistakes:
Confusing with finding max or min element
Trying to sum elements instead of finding positions
Counting unique elements instead of occurrences
2. Which of the following C# code snippets correctly initializes an array to find the first and last occurrence of an element?
easy
A. int arr = {1, 2, 3, 4, 5};
B. int arr = new int[5] {1, 2, 3, 4, 5};
C. int[] arr = new int[] {1, 2, 3, 4, 5};
D. int arr[] = new int[] {1, 2, 3, 4, 5};
Solution
Step 1: Check array initialization syntax in C#
Correct syntax is: int[] arr = new int[] {1, 2, 3, 4, 5};
Step 2: Identify incorrect options
int arr = new int[5] {1, 2, 3, 4, 5}; has wrong syntax (cannot assign array with size and initializer like that), A is missing 'new int[]', D uses C-style syntax invalid in C#.
Final Answer:
int[] arr = new int[] {1, 2, 3, 4, 5}; -> Option C
Hint: Use 'int[] arr = new int[] { ... }' for array initialization [OK]
Common Mistakes:
Using C/C++ style array declarations
Omitting 'new int[]' keyword
Assigning array size with initializer incorrectly
3. What will be the output of the following C# code?
int[] arr = {5, 3, 7, 3, 9};
int target = 3;
int first = -1, last = -1;
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == target) {
if (first == -1) first = i;
last = i;
}
}
Console.WriteLine($"{first} {last}");
medium
A. 3 1
B. 1 3
C. 0 4
D. -1 -1
Solution
Step 1: Trace the loop to find first and last occurrence of 3
Hint: Track first when found, update last every time [OK]
Common Mistakes:
Swapping first and last indices
Not updating last occurrence correctly
Initializing first and last with wrong values
4. Identify the error in the following code that tries to find the first and last occurrence of an element:
int[] arr = {2, 4, 2, 5, 2};
int target = 2;
int first = 0, last = 0;
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == target) {
first = i;
last = i;
}
}
Console.WriteLine($"{first} {last}");
medium
A. first and last are initialized incorrectly, causing wrong output
B. Loop condition should be i <= arr.Length
C. Target value is not assigned properly
D. Console.WriteLine syntax is incorrect
Solution
Step 1: Analyze initialization of first and last
Both first and last start at 0. Inside loop, first and last are set to i every time target is found, so first will be overwritten and end up as last occurrence index.
Step 2: Identify correct approach
First should be set only once when first occurrence is found (e.g., if first == -1). Last should update every time target is found.
Final Answer:
first and last are initialized incorrectly, causing wrong output -> Option A
Quick Check:
Initialize first to -1 and update only once [OK]
Hint: Initialize first to -1 and update only once [OK]
Common Mistakes:
Overwriting first occurrence index
Using wrong loop boundary
Incorrect Console.WriteLine format
5. Given a sorted array with possible duplicates, how can you efficiently find the first and last occurrence of a target element using C#?
hard
A. Use two binary searches: one for first occurrence, one for last occurrence
B. Traverse the array twice: once from start, once from end
C. Use a dictionary to store element counts
D. Sort the array again and find the target
Solution
Step 1: Understand the array is sorted with duplicates
Sorted array allows binary search to find positions efficiently.
Step 2: Use binary search twice
One binary search finds the first occurrence by moving left when target found, another finds last occurrence by moving right.
Step 3: Avoid full traversal or extra sorting
Traversing twice or sorting again is inefficient. Dictionary counts do not give positions.
Final Answer:
Use two binary searches: one for first occurrence, one for last occurrence -> Option A
Quick Check:
Binary search twice = efficient first and last occurrence [OK]
Hint: Binary search twice for first and last occurrence in sorted array [OK]
Common Mistakes:
Using linear search on large sorted arrays
Sorting array again unnecessarily
Using dictionary counts instead of positions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.