Find the Celebrity - Problem

Imagine you're at an exclusive party with n people labeled from 0 to n-1. Among these party-goers, there might be exactly one celebrity!

A celebrity has a very specific definition:

  • ๐ŸŒŸ Everyone knows the celebrity (all other n-1 people)
  • ๐Ÿ™ˆ The celebrity knows nobody (not a single person at the party)

Your mission is to find this celebrity using the minimum number of questions possible. You can only ask questions in the format: "Hi A, do you know B?"

You have access to a helper function knows(a, b) that returns true if person a knows person b, and false otherwise.

Goal: Return the celebrity's label if one exists, or -1 if there's no celebrity at the party.

Challenge: Can you solve this in less than O(nยฒ) questions?

Input & Output

example_1.py โ€” Basic Celebrity Case
$ Input: n = 3, graph = [[1,1,0],[0,1,0],[1,1,1]]
โ€บ Output: 1
๐Ÿ’ก Note: Person 1 is the celebrity because everyone (0,2) knows person 1, but person 1 doesn't know anyone (knows(1,0)=false, knows(1,2)=false).
example_2.py โ€” No Celebrity
$ Input: n = 2, graph = [[1,1],[1,1]]
โ€บ Output: -1
๐Ÿ’ก Note: There's no celebrity because both people know each other. A celebrity should know nobody.
example_3.py โ€” Single Person
$ Input: n = 1, graph = [[1]]
โ€บ Output: 0
๐Ÿ’ก Note: Person 0 is automatically the celebrity since there's only one person at the party.

Visualization

Tap to expand
Celebrity Detection Tournament01234vsvs134vs1CELEBRITY!Verification Phase:โœ“ Does 1 know anyone? NOโœ“ Does everyone know 1? YESConfirmed Celebrity!Questions asked: 3 + 2(n-1) = 9 totalvs Brute Force: nยฒ = 25 questions
Understanding the Visualization
1
Tournament Bracket
Start with all people as potential celebrities
2
Head-to-Head Matches
Ask: 'Does person A know person B?'
3
Elimination Rules
If A knows B โ†’ A is eliminated. If A doesn't know B โ†’ B is eliminated
4
Final Candidate
The last remaining person is our celebrity candidate
5
Championship Verification
Verify the candidate actually deserves the celebrity title
Key Takeaway
๐ŸŽฏ Key Insight: Each elimination question removes exactly one person from consideration, making this approach optimal with just O(n) questions total!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

n-1 questions for elimination phase + 2(n-1) questions for verification = 3n-3 total questions

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using two pointers and a few variables for tracking

n
2n
โœ“ Linear Space

Constraints

  • n == graph.length == graph[i].length
  • 2 โ‰ค n โ‰ค 100
  • graph[i][j] is 0 or 1
  • graph[i][i] == 1 for all i (everyone knows themselves)
  • There will be exactly one celebrity in the party if they exist
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
43.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen