← LinkedIn Interview Insights

LinkedIn·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

LinkedIn SWE interview with a classic graph problem dressed up as a social scenario. The O(n) angle is what separates the good solutions from the brute-force ones, and I had to think carefully before I stopped fumbling around with nested loops.

Questions Asked (1)

Q1

Given n people at a party, find the 'celebrity' using only a knows(a, b) API. A celebrity is known by everyone but knows nobody. Return the celebrity's label or -1 if none exists, using as few API calls as possible.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

My first instinct was the n-squared brute force and I actually started explaining it before catching myself.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-phase elimination approach: first, find a candidate by iterating through people and eliminating one person per knows(a,b) call, then verify the candidate with at most 2(n-1) additional calls. This reduces the problem from O(n^2) to O(n) API calls.

Pro tip: Explicitly state the worst-case number of API calls (3n-4) and note that the elimination phase is optimal because each call can eliminate at most one person. This shows you understand both correctness and efficiency trade-offs.

1. Clarify the problem and constraints

Confirm that knows(a,b) returns true if a knows b, and that a celebrity knows no one and is known by everyone. Ask if there can be multiple celebrities (no, at most one) and if n=0 or n=1 edge cases matter.

2. Find a candidate via elimination

Initialize candidate = 0. For each person i from 1 to n-1, if knows(candidate, i) is true, then candidate knows someone, so candidate cannot be a celebrity; set candidate = i. Otherwise, i knows candidate, so i cannot be a celebrity; keep candidate. After one pass, candidate is the only possible celebrity.

3. Verify the candidate

Check that the candidate knows nobody: for each person i != candidate, if knows(candidate, i) is true, return -1. Also check that everyone knows the candidate: for each person i != candidate, if knows(i, candidate) is false, return -1. If both checks pass, return candidate.

4. Analyze complexity and optimize

The elimination phase uses n-1 calls, and verification uses at most 2(n-1) calls, totaling 3n-4 calls in the worst case. Explain that this is optimal because each call can eliminate at most one person, and at least n-1 eliminations are needed to identify a unique candidate.

5. Handle edge cases and discuss trade-offs

Mention edge cases: n=0 returns -1, n=1 returns 0 (the only person is trivially a celebrity). Discuss that while the algorithm is optimal in calls, it uses O(1) extra space and O(n) time.

Key Points to Mention

  • Two-phase approach: elimination to find a candidate, then verification.
  • Each knows(a,b) call can eliminate at least one person from being the celebrity.
  • Worst-case API calls: 3n-4, which is optimal.
  • At most one celebrity can exist in the group.
  • Edge cases: n=0, n=1, and no celebrity present.
  • Time complexity O(n) and space complexity O(1).

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