The Find the Celebrity interview question is a classic logic and graph problem. In a party of people, a "celebrity" is defined as someone who:
knows(a, b) which returns true if knows . Your task is to find the celebrity (if one exists) using the minimum number of calls to the knows function.Microsoft, Google, and Meta ask the Find the Celebrity coding problem to evaluate a candidate's ability to optimize a search. A naive solution takes calls, but an solution exists. It tests your logical deduction skills—specifically, how one bit of information (a knows b) can eliminate one person from being a celebrity.
This problem is solved using the Two Pointers or Elimination pattern.
knows(candidate, i) is true, then the current candidate cannot be a celebrity (they know someone). Update candidate = i.knows(candidate, i) is false, then i cannot be a celebrity (they are not known by the candidate). The candidate remains the same.People: {0, 1, 2}. 1 is the celebrity.
knows(0, 1)? True. Person 0 is not celebrity. Candidate = 1.knows(1, 2)? False. Person 2 is not celebrity. Candidate = 1.knows for every possible pair, which uses calls.knows(a, b) results and eliminating the wrong person.Think of this as a tournament. Every time you ask a question, someone is "disqualified." This "Elimination interview pattern" is a powerful way to reduce complexity from to .
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find Positive Integer Solution for a Given Equation | Medium | Solve | |
| Minimum Number of Vertices to Reach All Nodes | Medium | Solve | |
| Find Champion II | Medium | Solve | |
| Paths in Maze That Lead to Same Room | Medium | Solve | |
| Maximal Network Rank | Medium | Solve |