Find the Celebrity
Suppose you are at a party with $n$ people labeled from 0 to $n-1$. A celebrity is someone who is known by everyone else, but he/she does not know anyone else. Find the celebrity in $O(n)$.
Why Interviewers Ask This
Uber interviewers use this problem to assess your ability to optimize brute-force solutions and think critically about constraints. They specifically evaluate if you can identify the underlying graph structure, recognize that a celebrity must have an in-degree of n-1 and out-degree of 0, and apply a two-pointer or elimination strategy to achieve O(n) time complexity instead of the naive O(n^2).
How to Answer This Question
Key Points to Cover
- Explicitly define the celebrity constraints before writing code to avoid ambiguity.
- Identify the flaw in O(n^2) brute force approaches immediately.
- Explain the logic of how a single comparison eliminates one candidate from consideration.
- Describe the two-phase algorithm: elimination followed by verification.
- Confirm the final candidate meets both criteria (in-degree n-1, out-degree 0).
Sample Answer
Common Mistakes to Avoid
- Skipping the verification phase, assuming the last remaining candidate is always the answer without checking.
- Implementing an adjacency matrix or full graph traversal, leading to O(n^2) space or time complexity.
- Failing to handle edge cases where no celebrity exists at all.
- Confusing the direction of the 'knows' relationship, leading to incorrect elimination logic.
Practice This Question with AI
Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.