← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Meta coding screen, pretty standard binary search territory. One question, clean setup, nothing tricky about the problem statement itself.

Questions Asked (1)

Q1

Given a sorted array, find the index of the first or last occurrence of a target number.

Algorithms & Data Structures
Author's notes

Classic binary search variant.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use binary search to find the first and last occurrences separately, modifying the standard binary search to continue searching even after finding the target. For the first occurrence, when the target is found, record the index and search the left half; for the last occurrence, search the right half. This ensures O(log n) time complexity.

Pro tip: Clarify with the interviewer whether to return the first or last occurrence, and handle edge cases like empty array or target not present. Also, mention that you can find both in one pass by finding the first occurrence and then the last occurrence starting from that index, but separate binary searches are simpler and still O(log n).

1. Clarify requirements and edge cases

Ask whether to find the first or last occurrence, and confirm the return value if the target is not found (e.g., -1). Discuss edge cases: empty array, single element, all elements equal to target.

2. Explain binary search modification

Describe how to adapt binary search: when nums[mid] == target, for first occurrence, set high = mid - 1 and update result; for last occurrence, set low = mid + 1 and update result. This continues searching for the boundary.

3. Walk through an example

Trace the algorithm on a small sorted array with duplicates, showing how the search narrows down to the first or last index. This demonstrates understanding and catches off-by-one errors.

4. Analyze complexity and trade-offs

State that time complexity is O(log n) and space is O(1). Mention that finding both first and last can be done with two binary searches, or one search for first then a second for last, still O(log n).

5. Write clean code and test

Implement the function with clear variable names and handle edge cases. If time permits, test with a few cases including target not present.

Key Points to Mention

  • Binary search modification to find boundaries
  • Time complexity O(log n) and space O(1)
  • Handling duplicates in sorted array
  • Edge cases: empty array, target not found, single element
  • Return -1 if target not present
  • Difference between finding first vs last occurrence

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.