← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Meta SWE coding round, one question on binary search applied to frequency counting. Pretty focused session, nothing too wild.

Questions Asked (1)

Q1

Given a sorted array and a target value, find how many times the target appears in the array using binary search. You need to locate both the leftmost and rightmost positions of the target.

Algorithms & Data Structures
Author's notes

The core idea clicked pretty fast since the array is sorted, two binary searches gets you there.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use binary search twice: once to find the leftmost occurrence of the target and once to find the rightmost occurrence. If the target is not found, return 0; otherwise, the count is rightmost - leftmost + 1. This approach runs in O(log n) time, which is optimal for a sorted array.

Pro tip: Clarify that you're assuming the array is sorted in ascending order and that duplicates are allowed. Also, mention that you can optimize by first checking if the target exists using a standard binary search, then finding the boundaries, but the two-pass approach is simpler and still O(log n).

1. Clarify the problem and constraints

Confirm the array is sorted, whether it's ascending or descending, and if duplicates are present. Ask about the expected time complexity and if the array can be empty.

2. Find the leftmost occurrence

Implement a modified binary search that continues searching left even after finding the target, to locate the first occurrence. Return -1 if not found.

3. Find the rightmost occurrence

Similarly, implement a modified binary search that continues searching right after finding the target, to locate the last occurrence. Return -1 if not found.

4. Compute the count

If either leftmost or rightmost is -1, return 0. Otherwise, return rightmost - leftmost + 1.

5. Analyze complexity and edge cases

State that time complexity is O(log n) and space is O(1). Discuss edge cases like empty array, target not present, all elements equal to target.

Key Points to Mention

  • Binary search modification to find boundaries (leftmost and rightmost).
  • Time complexity O(log n) and space complexity O(1).
  • Handling edge cases: empty array, target absent, all elements equal.
  • Avoid linear scan; emphasize logarithmic efficiency.
  • Use of integer mid calculation to prevent overflow (mid = left + (right - left) / 2).
  • Potential to combine both searches into one function with a boolean flag for leftmost/rightmost.

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