Unit 10 Session 1 Standard (Click for link to problem statements)
Unit 10 Session 1 Advanced (Click for link to problem statements)
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?
trust
represent?
trust[i] = [a, b]
means contestant a
trusts contestant b
.n-1
people trusting them and will trust no one.-1
if no contestant satisfies the celebrity conditions.HAPPY CASE
Input: trust = [[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]], n = 3
Output: -1
Explanation: There is no celebrity because contestant 3 trusts contestant 1.
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:
n-1
people but trusts no one, they are the celebrity.a
to b
.Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will maintain two arrays to keep track of:
1) How many people trust each contestant (trust_count
).
2) How many people each contestant trusts (trusted_by
).
The celebrity will be the one who:
n-1
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
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
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
trust1 = [[1, 2]]
trust2 = [[1, 3], [2, 3]]
trust3 = [[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
2
3
-1
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(n + t)
where n
is the number of contestants and t
is the number of trust relationships. We iterate over the trust
list and then over the contestants to find the celebrity.O(n)
for storing the trust_count
and trusted_by
arrays.