Closest Friends - codepath/compsci_guides GitHub Wiki
Unit 10 Session 1 Standard (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10-15 mins
- 🛠️ Topics: Graphs, Adjacency List, Undirected Graphs
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 each element in the
contacts
list represent?- A: Each element
[celebrity_a, celebrity_b]
represents a mutual relationship between celebrity_a and celebrity_b (undirected edge).
- A: Each element
- Q: What is the goal of the problem?
- A: Return a list of a given celebrity's closest friends, which are directly connected nodes (neighbors) in the graph.
- Q: What happens if a celebrity has no connections?
- A: Return an empty list if the celebrity has no friends in the graph.
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.
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 Representation problems, we want to consider the following approaches:
- Adjacency List: This problem can be easily solved by creating an adjacency list that represents the relationships as a graph where each node is a celebrity and the edges represent mutual relationships.
3: P-lan
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
- Forgetting to add both directions for the undirected relationship (i.e., if
a
is connected tob
, thenb
must also be connected toa
). - Failing to handle the case where the celebrity has no connections.
4: I-mplement
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, [])
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Input:
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']
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity:
O(n)
wheren
is the number of contacts. We iterate through thecontacts
list to build the adjacency list and then return the list of friends. - Space Complexity:
O(n)
wheren
is the number of contacts. We store the adjacency list in a dictionary.