Let's play a game at a party! Imagine there are n
people at the party, labeled from 0 to n - 1
. Among these n
people, at most one is a celebrity. A celebrity is defined as someone who is known by everyone else at the party, but they don't know anyone else. So, everyone knows the celebrity, but the celebrity doesn't know anyone.
Your task is to find out who the celebrity is, if they exist. You are only allowed to ask one type of question: "Does person A know person B?" Luckily, there's a function called knows(a, b)
which tells you whether person A knows person B. This function is already implemented. Your code should call this helper function as few times as possible.
Write a function int findCelebrity(n)
that takes the number of people n
as input and returns the label of the celebrity if there is one, or -1 if there is no celebrity. Assume 1 <= n <= 100
and there will be at most one celebrity.
For example, consider the following scenario:
n = 3
knows = [
[1,1,0],
[0,1,0],
[1,1,1]
]
Here, knows[i][j]
means whether person i
knows person j
. Based on the definition of a celebrity:
Therefore, findCelebrity(3)
should return -1
.
Another example:
n = 2
knows = [
[1,0],
[1,1]
]
Therefore, findCelebrity(2)
should return -1
.
Can you implement the findCelebrity
function efficiently?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding a celebrity means checking every single person to see if they fit the celebrity requirements. We treat each person as a potential celebrity and systematically verify if they meet the conditions.
Here's how the algorithm would work step-by-step:
def find_celebrity_brute_force(number_of_people, knows):
for potential_celebrity in range(number_of_people):
is_actually_celebrity = True
# Check if everyone else knows the potential celebrity
for other_person in range(number_of_people):
if potential_celebrity == other_person:
continue
if not knows(other_person, potential_celebrity):
is_actually_celebrity = False
break
# Check if the potential celebrity knows anyone else
if is_actually_celebrity:
# Must verify potential celebrity knows no one
for other_person in range(number_of_people):
if potential_celebrity == other_person:
continue
# Found someone the potential celebrity knows
if knows(potential_celebrity, other_person):
is_actually_celebrity = False
break
# Found a celebrity
if is_actually_celebrity:
return potential_celebrity
return -1
The problem is to find a 'celebrity' in a group, where a celebrity is someone everyone knows, but who doesn't know anyone else. The efficient strategy is to eliminate candidates who cannot be the celebrity, quickly narrowing down to a single potential celebrity, and then verifying if this person truly fits the celebrity criteria.
Here's how the algorithm would work step-by-step:
def find_celebrity(number_of_people):
potential_celebrity = 0
for i in range(1, number_of_people):
if knows(potential_celebrity, i):
#If potential knows i, potential is not celebrity
potential_celebrity = i
# Verify if potential_celebrity is actually a celebrity.
for i in range(number_of_people):
if i != potential_celebrity:
if not knows(i, potential_celebrity) or knows(potential_celebrity, i):
#Must be known by everyone, and know nobody
return -1
#Last step to verify
if knows(potential_celebrity, potential_celebrity):
return -1
return potential_celebrity
Case | How to Handle |
---|---|
n is zero or negative | Return -1 immediately as a celebrity cannot exist in an empty or nonsensical room size. |
n is one | Person 0 is a celebrity if they know nobody (knows(0,x) is always false for all x). |
No celebrity exists | The algorithm should correctly identify and return -1 if no one satisfies the celebrity criteria. |
Multiple potential candidates after the first loop | The second verification loop must filter down to ensure only a true celebrity is returned. |
The 'knows' function is implemented incorrectly or inconsistently. | Assume the 'knows' function is correctly implemented according to the problem description. |
Integer overflow when calculating indices or using large n | The index should not overflow as long as it does not exceed the integer limit, assuming the 'knows' function accepts integer types, but large n can affect the algorithm's runtime complexity. |
Input 'n' exceeding available memory | The solution must be space-efficient to avoid out-of-memory errors for large 'n', and should scale linearly. |
Candidate celebrity knowing themselves (knows(candidate, candidate) is true) | This should be handled during the verification phase, returning -1 if this impossible condition occurs. |