← TikTok Interview Insights

TikTok·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026Remote

Summary

TikTok software engineering interview with a pretty gnarly algorithmic problem that had multiple layers to it. The O(log n) constraint is what makes it interesting and also what makes it easy to freeze up on.

Questions Asked (1)

Q1

Given a sorted integer array of length n, find all distinct values that appear strictly more than n/3 times. Your solution must run in O(log n) time with O(1) extra space. As part of your answer: prove there can be at most two such values, explain how you'd use binary search to identify and verify candidates, argue correctness, analyze complexity, and handle edge cases like empty arrays or all-equal elements.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The pigeonhole argument for 'at most two' came to me pretty fast, that part felt good.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by proving the at-most-two property using a counting argument, then use binary search to find the first and last occurrence of each candidate value (the elements at indices n/3 and 2n/3). Verify their counts exceed n/3, and handle edge cases like empty arrays and all-equal elements.

Pro tip: Mention that the O(log n) time bound is tight because any correct algorithm must at least read the candidate elements, and explicitly discuss how you'd handle the case where the array length is not a multiple of 3.

1. Prove at most two values

Use a counting argument: if three distinct values each appeared more than n/3 times, their total count would exceed n, which is impossible. Thus, at most two such values exist.

2. Identify candidates

In a sorted array, any value appearing more than n/3 times must include either the element at index floor(n/3) or the element at index floor(2n/3) (or both). Use these indices to pick at most two candidate values.

3. Verify candidates with binary search

For each candidate, use binary search to find its first and last occurrence, compute its frequency, and check if it exceeds n/3. This takes O(log n) time per candidate.

4. Handle edge cases

If the array is empty, return an empty list. If all elements are equal, the candidate will be that element and its count will be n, which exceeds n/3. Also handle small arrays (n < 3) where the condition may be trivially satisfied.

5. Analyze complexity and argue correctness

The algorithm uses O(1) extra space and O(log n) time (constant number of binary searches). Correctness follows from the candidate property and the verification step.

Key Points to Mention

  • Proof that at most two values can appear more than n/3 times.
  • The candidate indices: floor(n/3) and floor(2n/3) in the sorted array.
  • Binary search to find first and last occurrence of a candidate.
  • Time complexity O(log n) and space complexity O(1).
  • Edge cases: empty array, all elements equal, n < 3.
  • Why the O(log n) bound is optimal (must read candidates).

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