← LinkedIn Interview Insights

LinkedIn·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

LinkedIn SWE interview with a classic algorithm puzzle. Nothing too wild, but the O(n) constraint is what makes it interesting.

Questions Asked (1)

Q1

In a group of n people labeled 0 to n-1, find the 'celebrity': someone everyone else knows, but who knows nobody. You have access to a knows(a, b) API. Return the celebrity's label, or -1 if none exists. Do it in O(n) API calls.

Algorithms & Data StructuresAPI & Integrations
Author's notes

The naive approach is obvious and wrong, O(n^2) calls if you just check every pair.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-phase elimination approach: first, find a candidate celebrity by iterating through all people and eliminating non-celebrities based on knows(a, b) calls. Then, verify the candidate by checking that everyone knows them and they know no one, using additional API calls. This ensures O(n) calls.

Pro tip: Emphasize that the elimination phase uses exactly n-1 calls, and verification uses at most 2(n-1) calls, so total is O(n). Also, mention edge cases like n=0 or n=1 and how the algorithm handles them.

1. Initialize candidate

Start with candidate = 0. This will be the potential celebrity after elimination.

2. Elimination phase

For each person i from 1 to n-1, if knows(candidate, i) is true, then candidate cannot be a celebrity (since they know someone), so set candidate = i. Otherwise, i cannot be a celebrity (since they are not known by candidate), so keep candidate unchanged. This uses n-1 calls.

3. Verification phase

Verify that the candidate is indeed a celebrity: check that for every other person i, knows(candidate, i) is false and knows(i, candidate) is true. If any check fails, return -1.

4. Handle edge cases

If n is 0, return -1. If n is 1, the single person is trivially a celebrity (assuming no self-knows calls), so return 0 after verification (which is trivial).

5. Analyze complexity

Explain that elimination uses n-1 calls, verification uses at most 2(n-1) calls, so total O(n) calls, meeting the requirement.

Key Points to Mention

  • The elimination phase reduces the candidate set by one with each API call, ensuring linear time.
  • The verification phase is necessary because the elimination only guarantees a candidate, not a true celebrity.
  • The total number of API calls is at most 3n-3, which is O(n).
  • Edge cases: n=0, n=1, and multiple potential celebrities (only one can exist if any).
  • The algorithm does not require storing a graph or adjacency matrix, saving space.
  • The knows(a, b) API is assumed to be O(1) and reliable.

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