Finding the Celebrity
A celebrity at a party is someone whom everyone knows, yet who knows no one. Suppose that you are at a party with
people. For any pair of people and , you can ask whether they know and receive an honest answer. Give a recursive algorithm that determines whether the party includes a celebrity, and if so who, by asking at most questions.
This problem comes from the Practice Problems for Note 3 in CS 70, Summer 2026. I found its solution particularly elegant because a seemingly global property can be determined through a simple process of elimination.
One Question, One Elimination
The key observation is that a single question allows us to rule out at least one of the two people involved. Suppose we ask
- If
knows , then cannot be a celebrity. - If
does not know , then cannot be a celebrity.
Regardless of the answer, we can safely eliminate one of them.
Finding the Only Remaining Candidate
Choose any person as the initial candidate, then compare that candidate with each remaining person. For a candidate c and another person p:
- If
cknowsp, eliminatecand makepthe new candidate. - If
cdoes not knowp, eliminatepand keepcas the candidate.
After
candidate = people[0]
for person in people[1:]:
if Ask(candidate, person):
candidate = person
Surviving this elimination phase does not prove that the candidate is a celebrity; it only proves that everyone else is not. We still need to verify that, for every other person p:
- the candidate does not know
p, and pknows the candidate.
There are
Making It Recursive
The same idea leads naturally to a recursive algorithm. Assume x. Since x cannot be a celebrity, we can recursively search among the remaining
If the recursive call returns no celebrity, then the original group has no celebrity either. Otherwise, suppose it returns a candidate c. The recursive call has already verified c against everyone except x, so only two relationships remain to be checked: x must know c, and c must not know x.
FindCelebrity(P):
if |P| == 2:
let P = {A, B}
ab = Ask(A, B)
ba = Ask(B, A)
if ab and not ba:
return B
if ba and not ab:
return A
return NONE
choose two distinct people A and B from P
if Ask(A, B):
x = A
else:
x = B
c = FindCelebrity(P without x)
if c == NONE:
return NONE
if Ask(x, c) and not Ask(c, x):
return c
return NONE
Counting the Questions
Let FindCelebrity on a group of
For
- one question to eliminate either
or , - one to determine whether
xknowsc, and - one to determine whether
cknowsx.
Therefore,
Applying this recurrence gives
Thus, the recursive algorithm satisfies the required bound.
Iteration and Recursion
The iterative and recursive approaches rely on the same elimination principle, but they organize verification differently:
- Iterative: eliminate everyone but one candidate, then verify that candidate.
- Recursive: eliminate one person, solve the smaller problem, then verify the returned candidate against the eliminated person.
The central idea is to use every answer to discard someone who cannot be the celebrity. Once the problem is viewed as elimination rather than exhaustive checking, both the iterative and recursive solutions—and the linear question bound—follow naturally.