Secret Celebrity - codepath/compsci_guides GitHub Wiki
Unit 10 Session 1 Standard (Click for link to problem statements)
Unit 10 Session 1 Advanced (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Medium
- ⏰ Time to complete: 15-20 mins
- 🛠️ Topics: Graphs, Trust Relationships, Counting
1: U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- Q: What does the array
trust
represent?- A: Each element
trust[i] = [a, b]
means contestanta
trusts contestantb
.
- A: Each element
- Q: How can we identify a celebrity?
- A: A celebrity trusts no one, but everyone else trusts the celebrity. This means the celebrity will have
n-1
people trusting them and will trust no one.
- A: A celebrity trusts no one, but everyone else trusts the celebrity. This means the celebrity will have
- Q: What should we return if there is no celebrity?
- A: Return
-1
if no contestant satisfies the celebrity conditions.
- A: Return
HAPPY CASE
Input: trust = [1, 2](/codepath/compsci_guides/wiki/1,-2), n = 2
Output: 2
Explanation: Contestant 2 is trusted by 1, but trusts no one, making them the celebrity.
EDGE CASE
Input: trust = [1, 3], [2, 3], [3, 1](/codepath/compsci_guides/wiki/1,-3],-[2,-3],-[3,-1), n = 3
Output: -1
Explanation: There is no celebrity because contestant 3 trusts contestant 1.
2: M-atch
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Graph Traversal problems, we want to consider the following approaches:
- Counting Relationships: We can use two arrays, one to count how many people each contestant trusts, and another to count how many people trust each contestant. If a contestant is trusted by
n-1
people but trusts no one, they are the celebrity. - Graph Representation: The problem can be represented as a directed graph where a trust relationship is an edge from
a
tob
.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will maintain two arrays to keep track of:
- How many people trust each contestant (
trust_count
). - How many people each contestant trusts (
trusted_by
).
The celebrity will be the one who:
- Is trusted by exactly
n-1
people. - Trusts no one (trusted by 0 people).
1) Initialize two arrays `trust_count` and `trusted_by` of size `n + 1` to count the number of people each contestant trusts and the number of people who trust each contestant.
2) Iterate over the `trust` list and update `trust_count` and `trusted_by` for each contestant.
3) After populating the arrays, iterate through all contestants.
a) If a contestant is trusted by `n-1` people and trusts no one, return the contestant's number as the celebrity.
4) If no contestant satisfies the conditions, return `-1`.
⚠️ Common Mistakes
- Forgetting to handle the case where no one trusts the potential celebrity.
- Incorrectly assuming that if one person is trusted by many, they must be the celebrity without checking that they trust no one.
4: I-mplement
Implement the code to solve the algorithm.
def identify_celebrity(trust, n):
# Step 1: Initialize trust counters
trust_count = [0] * (n + 1) # To track how many people trust each person
trusted_by = [0] * (n + 1) # To track how many people each person trusts
# Step 2: Populate trust_count and trusted_by based on trust relationships
for a, b in trust:
trust_count[b] += 1 # b is trusted by a
trusted_by[a] += 1 # a trusts b
# Step 3: Identify the celebrity
for i in range(1, n + 1):
if trust_count[i] == n - 1 and trusted_by[i] == 0:
return i # This person is the celebrity
return -1 # No celebrity found
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Input:
trust1 = [1, 2](/codepath/compsci_guides/wiki/1,-2)
trust2 = [1, 3], [2, 3](/codepath/compsci_guides/wiki/1,-3],-[2,-3)
trust3 = [1, 3], [2, 3], [3, 1](/codepath/compsci_guides/wiki/1,-3],-[2,-3],-[3,-1)
print(identify_celebrity(trust1, 2)) # Output: 2
print(identify_celebrity(trust2, 3)) # Output: 3
print(identify_celebrity(trust3, 3)) # Output: -1
- Output:
2
3
-1
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity:
O(n + t)
wheren
is the number of contestants andt
is the number of trust relationships. We iterate over thetrust
list and then over the contestants to find the celebrity. - Space Complexity:
O(n)
for storing thetrust_count
andtrusted_by
arrays.