Unit 10 Session 1 Standard (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?
contacts
list represent?
[celebrity_a, celebrity_b]
represents a mutual relationship between celebrity_a and celebrity_b (undirected edge).HAPPY CASE
Input: contacts = [
[""Lupita Nyong'o"", ""Jordan Peele""],
[""Meryl Streep"", ""Jordan Peele""],
[""Meryl Streep"", ""Lupita Nyong'o""],
[""Greta Gerwig"", ""Meryl Streep""],
[""Ali Wong"", ""Greta Gerwig""]
]
celebrity = ""Lupita Nyong'o""
Output: ['Jordan Peele', 'Meryl Streep']
Explanation: Lupita Nyong'o is directly connected to Jordan Peele and Meryl Streep, so they are her closest friends.
EDGE CASE
Input: contacts = []
celebrity = ""Lupita Nyong'o""
Output: []
Explanation: There are no connections, so the result is an empty list.
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 Representation problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will create an adjacency list where each celebrity is a key, and the value is a list of their closest friends. Then, to get the closest friends of a given celebrity, we simply look up the value for that key in the adjacency list.
1) Create an empty dictionary `graph` to represent the adjacency list.
2) Iterate through the `contacts` list. For each contact `[a, b]`, do the following:
a) Add `b` to the adjacency list of `a`.
b) Add `a` to the adjacency list of `b` (because the graph is undirected).
3) Return the list of friends (value) for the given `celeb`.
⚠️ Common Mistakes
a
is connected to b
, then b
must also be connected to a
).Implement the code to solve the algorithm.
def get_close_friends(contacts, celeb):
# Step 1: Build the adjacency list
graph = {}
for contact in contacts:
a, b = contact
if a not in graph:
graph[a] = []
if b not in graph:
graph[b] = []
graph[a].append(b)
graph[b].append(a)
# Step 2: Return the friends of the given celebrity
return graph.get(celeb, [])
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
contacts = [
[""Lupita Nyong'o"", ""Jordan Peele""],
[""Meryl Streep"", ""Jordan Peele""],
[""Meryl Streep"", ""Lupita Nyong'o""],
[""Greta Gerwig"", ""Meryl Streep""],
[""Ali Wong"", ""Greta Gerwig""]
]
print(get_close_friends(contacts, ""Lupita Nyong'o""))
Output: ['Jordan Peele', 'Meryl Streep']
Input:
print(get_close_friends(contacts, ""Greta Gerwig""))
Output: ['Meryl Streep', 'Ali Wong']
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(n)
where n
is the number of contacts. We iterate through the contacts
list to build the adjacency list and then return the list of friends.O(n)
where n
is the number of contacts. We store the adjacency list in a dictionary.