277. Find the Celebrity
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to identify a "celebrity" from a group of n people. A celebrity is defined as someone who is known by all other n-1 people but knows none of them. We are provided a function knows(a, b) which returns true if person a knows person b, and false otherwise. The goal is to find the celebrity with the minimum number of calls to knows, specifically in O(n) time.
Solution
Find the Celebrity in Python
# The knows API is already defined for you.
# return a bool, whether a knows b
# def knows(a: int, b: int) -> bool:
class Solution:
def findCelebrity(self, n: int) -> int:
# Step 1: Find a potential celebrity candidate.
# The logic is that if knows(a, b) is true, 'a' cannot be a celebrity.
# If it's false, 'b' cannot be a celebrity.
# We iterate through all people, eliminating one person at each step.
candidate = 0
for i in range(1, n):
if knows(candidate, i):
# `candidate` knows `i`, so `candidate` cannot be a celebrity.
# `i` might be, so `i` becomes the new candidate.
candidate = i
# else, `candidate` does not know `i`.
# So, `i` cannot be a celebrity because a celebrity is known by everyone.
# `candidate` remains a potential celebrity.
# Step 2: Verify if the candidate is truly a celebrity.
# We need to check if everyone else knows the candidate and the candidate knows no one.
for i in range(n):
if i == candidate:
continue
# A celebrity must not know anyone else.
# And everyone else must know the celebrity.
if knows(candidate, i) or not knows(i, candidate):
# If the candidate knows person `i`, or person `i` does not know the candidate,
# then our candidate is not a celebrity. Since there can be at most one,
# we can conclude no celebrity exists.
return -1
# If the candidate passed all checks, they are the celebrity.
return candidate
Find the Celebrity in Java
/* The knows API is defined in the parent class Relation.
boolean knows(int a, int b); */
public class Solution extends Relation {
public int findCelebrity(int n) {
// Step 1: Find a potential celebrity candidate.
// The logic is that if knows(a, b) is true, 'a' cannot be a celebrity.
// If it's false, 'b' cannot be a celebrity.
// We iterate through all people, eliminating one person at each step.
int candidate = 0;
for (int i = 1; i < n; i++) {
if (knows(candidate, i)) {
// `candidate` knows `i`, so `candidate` cannot be a celebrity.
// `i` might be, so `i` becomes the new candidate.
candidate = i;
}
// else, `candidate` does not know `i`.
// So, `i` cannot be a celebrity because a celebrity is known by everyone.
// `candidate` remains a potential celebrity.
}
// Step 2: Verify if the candidate is truly a celebrity.
// We need to check if everyone else knows the candidate and the candidate knows no one.
for (int i = 0; i < n; i++) {
if (i == candidate) {
continue;
}
// A celebrity must not know anyone else.
// And everyone else must know the celebrity.
if (knows(candidate, i) || !knows(i, candidate)) {
// If the candidate knows person `i`, or person `i` does not know the candidate,
// then our candidate is not a celebrity. Since there can be at most one,
// we can conclude no celebrity exists.
return -1;
}
}
// If the candidate passed all checks, they are the celebrity.
return candidate;
}
}
Find the Celebrity in C++
/* The knows API is defined for you.
bool knows(int a, int b); */
class Solution {
public:
int findCelebrity(int n) {
// Step 1: Find a potential celebrity candidate.
// The logic is that if knows(a, b) is true, 'a' cannot be a celebrity.
// If it's false, 'b' cannot be a celebrity.
// We iterate through all people, eliminating one person at each step.
int candidate = 0;
for (int i = 1; i < n; ++i) {
if (knows(candidate, i)) {
// `candidate` knows `i`, so `candidate` cannot be a celebrity.
// `i` might be, so `i` becomes the new candidate.
candidate = i;
}
// else, `candidate` does not know `i`.
// So, `i` cannot be a celebrity because a celebrity is known by everyone.
// `candidate` remains a potential celebrity.
}
// Step 2: Verify if the candidate is truly a celebrity.
// We need to check if everyone else knows the candidate and the candidate knows no one.
for (int i = 0; i < n; ++i) {
if (i == candidate) {
continue;
}
// A celebrity must not know anyone else.
// And everyone else must know the celebrity.
if (knows(candidate, i) || !knows(i, candidate)) {
// If the candidate knows person `i`, or person `i` does not know the candidate,
// then our candidate is not a celebrity. Since there can be at most one,
// we can conclude no celebrity exists.
return -1;
}
}
// If the candidate passed all checks, they are the celebrity.
return candidate;
}
};
Find the Celebrity in TypeScript
/**
* The knows API is defined in the parent class Relation.
* knows(a: number, b: number): boolean {
* ...
* };
*/
var solution = function(knows: any) {
return function(n: number): number {
// Step 1: Find a potential celebrity candidate.
// The logic is that if knows(a, b) is true, 'a' cannot be a celebrity.
// If it's false, 'b' cannot be a celebrity.
// We iterate through all people, eliminating one person at each step.
var candidate = 0;
for (var i = 1; i < n; i++) {
if (knows(candidate, i)) {
// `candidate` knows `i`, so `candidate` cannot be a celebrity.
// `i` might be, so `i` becomes the new candidate.
candidate = i;
}
// else, `candidate` does not know `i`.
// So, `i` cannot be a celebrity because a celebrity is known by everyone.
// `candidate` remains a potential celebrity.
}
// Step 2: Verify if the candidate is truly a celebrity.
// We need to check if everyone else knows the candidate and the candidate knows no one.
for (var i = 0; i < n; i++) {
if (i === candidate) {
continue;
}
// A celebrity must not know anyone else.
// And everyone else must know the celebrity.
if (knows(candidate, i) || !knows(i, candidate)) {
// If the candidate knows person `i`, or person `i` does not know the candidate,
// then our candidate is not a celebrity. Since there can be at most one,
// we can conclude no celebrity exists.
return -1;
}
}
// If the candidate passed all checks, they are the celebrity.
return candidate;
};
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.