← TikTok Interview Insights

TikTok·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

TikTok software engineer coding round, one question the whole time. Pretty standard binary search territory but the O(log n) constraint is what makes it interesting, you can't just linear scan and call it a day.

Questions Asked (1)

Q1

Given a sorted integer array and a target value, return the first and last positions of the target in the array. Must run in O(log n) time. Return [-1, -1] if the target isn't found.

Algorithms & Data Structures
Author's notes

The naive approach jumps to mind immediately and that's the problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use two separate binary searches: one to find the leftmost occurrence of the target and another to find the rightmost occurrence. Modify the standard binary search to continue searching even after finding the target, adjusting the search space based on whether you're looking for the first or last position. This ensures O(log n) time complexity.

Pro tip: Clarify with the interviewer whether the array can contain duplicates and if the target is guaranteed to be present. Also, mention that you can optimize by first finding any occurrence and then expanding, but that would be O(n) in the worst case, so the two-binary-search approach is optimal.

1. Clarify requirements and edge cases

Confirm the array is sorted, may contain duplicates, and the target may or may not be present. Discuss edge cases like empty array, target smaller than all elements, or larger than all elements.

2. Design binary search for first occurrence

Modify binary search to find the leftmost index: when nums[mid] == target, record the index and continue searching in the left half (high = mid - 1) to find an earlier occurrence.

3. Design binary search for last occurrence

Similarly, modify binary search to find the rightmost index: when nums[mid] == target, record the index and continue searching in the right half (low = mid + 1) to find a later occurrence.

4. Implement and handle not found

Implement both searches, initializing result to [-1, -1]. If the first search fails, return [-1, -1] immediately; otherwise, run the second search and return the results.

5. Analyze complexity and test

State that each binary search takes O(log n) time, so overall O(log n) time and O(1) space. Walk through test cases like target at boundaries, single element, and duplicates.

Key Points to Mention

  • Time complexity: O(log n) because two binary searches each take O(log n).
  • Space complexity: O(1) iterative implementation.
  • Handling duplicates: binary search must continue after finding a match to locate boundaries.
  • Edge cases: empty array, target not present, target at start/end, all elements equal to target.
  • Comparison with linear scan: linear scan would be O(n), which is not acceptable.
  • Potential optimization: if first search returns -1, skip second search.

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