277. Find the Celebrity - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Imagine there is a party with n people labeled from 0 to n-1. A celebrity is defined as someone who:

  • Is known by everyone else.
  • Does not know any other person.

You are given a helper function:

boolean knows(int a, int b);

which returns true if person a knows person b, and false otherwise.

Task:
Find the celebrity if one exists; otherwise, return -1.

Example 1:

  • Input: n = 3, with the following relationship (where a 1 indicates that person i knows person j):
    • Person 0: knows [0, 1, 0]
    • Person 1: knows [0, 0, 0] (celebrity candidate, because they know no one)
    • Person 2: knows [1, 1, 0]
  • Output: 1
  • Explanation: Everyone (0 and 2) knows person 1, and person 1 does not know anyone else.

Example 2:

  • Input: n = 3, with relationships:
    • Person 0: knows [0, 1, 0]
    • Person 1: knows [1, 0, 0]
    • Person 2: knows [0, 1, 0]
  • Output: -1
  • Explanation: There is no person who is known by everyone and who knows no one.

Constraints:

  • n is in the range [1, 100].
  • There is at most one celebrity in the party.
  • Minimize the number of calls to knows(a, b) since each call is considered expensive.

Hints to Approach the Problem

  1. Elimination Idea:
    Notice that if person A knows person B, then A cannot be a celebrity. Conversely, if A does not know B, then B cannot be a celebrity. Use this observation to eliminate candidates.

  2. Candidate Selection:
    You can start with a candidate (say, person 0) and iterate over the rest of the people. If the current candidate knows person i, then candidate cannot be a celebrity and you set candidate to i. After one pass, you will have a potential candidate; then, verify this candidate against all people.

Approaches to Solve the Problem

Approach 1: Brute Force

Idea:
For each person, check if:

  • They do not know anyone else.
  • Every other person knows them.

Method:
For every candidate i:

  • For every other person j (j ≠ i):
    • If knows(i, j) is true or knows(j, i) is false, then i cannot be the celebrity.

Drawbacks:

  • The brute-force method requires checking each pair, leading to O(n²) calls to knows().

Approach 2: Two-Pass Candidate Elimination (Optimal)

Idea:

  1. First Pass (Candidate Selection):
    • Start with candidate = 0.
    • Iterate through all persons from 1 to n-1.
    • If candidate knows i, then candidate cannot be a celebrity. Set candidate = i.
  2. Second Pass (Validation):
    • Verify that the candidate does not know anyone else.
    • Ensure every other person knows the candidate.

Advantages:

  • This approach reduces the number of calls to knows() to O(n) in both passes.

Code Implementation

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Step-by-Step Walkthrough (Visual Example)

Consider a party with n = 3 persons labeled 0, 1, and 2.

  1. Candidate Selection:

    • Start with candidate = 0.
    • For i = 1:
      • Check if person 0 knows person 1.
      • If yes, candidate becomes 1.
    • For i = 2:
      • Check if candidate (person 1) knows person 2.
      • If person 1 does not know person 2, candidate remains 1.
  2. Candidate Verification:

    • For each person i (i ≠ candidate):
      • Ensure candidate (person 1) does not know i.
      • Ensure every person i knows the candidate.
    • If these conditions are met, person 1 is the celebrity; otherwise, return -1.

Complexity Analysis

  • Time Complexity:

    • The first pass makes O(n) calls.
    • The second pass also makes O(n) calls.
    • Overall, the algorithm runs in O(n) time.
  • Space Complexity:

    • The algorithm uses O(1) additional space.

Common Mistakes and Edge Cases

Common Mistakes:

  • Not verifying the candidate:
    After finding a candidate, it’s essential to verify that the candidate meets both conditions (does not know anyone and is known by everyone). Failing to do this may lead to an incorrect answer.

  • Assuming multiple celebrities:
    The problem guarantees at most one celebrity, so if a candidate fails verification, the correct answer is -1.

Edge Cases:

  • Single Person Party:
    When n = 1, the single person is trivially the celebrity.

  • No Celebrity Exists:
    Ensure that if any check fails in the second pass, the function returns -1.

Variations:

  • Modified Conditions:
    What if the celebrity also had to satisfy additional properties? You might need to adapt the verification step accordingly.
  • Graph Problems:
    Problems that involve directed graphs and elimination strategies.

  • Two-Sum Variations:
    Although a different topic, similar elimination logic and pairwise checking techniques can be applied.

TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;