Unit 10 Session 2 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?
collaborations
dictionary represent?
HAPPY CASE
Input:
```python
collaborations = {
""Chadwick Boseman"": [(""Lupita Nyong'o"", 2), (""Robert Downey Jr."", 3), (""Mark Ruffalo"", 2)],
""Lupita Nyong'o"": [(""Chadwick Boseman"", 2), (""Robert Downey Jr."", 1)],
""Robert Downey Jr."": [(""Chadwick Boseman"", 3), (""Lupita Nyong'o"", 1), (""Mark Ruffalo"", 3)],
""Mark Ruffalo"": [(""Chadwick Boseman"", 2), (""Robert Downey Jr."", 3)]
}
print(collaborations[""Chadwick Boseman""])
```
Output:
```markdown
[(""Lupita Nyong'o"", 2), (""Robert Downey Jr."", 3), (""Mark Ruffalo"", 2)]
```
EDGE CASE
Input:
```python
collaborations = {}
```
Output:
```markdown
[]
Explanation: If no actors or collaborations are present, the output 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 Weighted Graph problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: To build the adjacency dictionary, start with an empty dictionary. For each pair of actors and their number of collaborations, add the information to both actors' lists, since the graph is undirected.
1) Initialize an empty dictionary `collaborations` to store the adjacency list.
2) For each pair of actors and their number of collaborations:
a) If the actor is not already in the dictionary, add them with an empty list.
b) Add the tuple `(costar, num_collaborations)` to the actor's list.
c) Repeat the same for the costar (since the graph is undirected).
3) After processing all pairs, return the `collaborations` dictionary.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
# Example usage
collaborations = {
""Chadwick Boseman"": [(""Lupita Nyong'o"", 2), (""Robert Downey Jr."", 3), (""Mark Ruffalo"", 2)],
""Lupita Nyong'o"": [(""Chadwick Boseman"", 2), (""Robert Downey Jr."", 1)],
""Robert Downey Jr."": [(""Chadwick Boseman"", 3), (""Lupita Nyong'o"", 1), (""Mark Ruffalo"", 3)],
""Mark Ruffalo"": [(""Chadwick Boseman"", 2), (""Robert Downey Jr."", 3)]
}
print(collaborations[""Chadwick Boseman""])
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
collaborations = {
""Chadwick Boseman"": [(""Lupita Nyong'o"", 2), (""Robert Downey Jr."", 3), (""Mark Ruffalo"", 2)],
""Lupita Nyong'o"": [(""Chadwick Boseman"", 2), (""Robert Downey Jr."", 1)],
""Robert Downey Jr."": [(""Chadwick Boseman"", 3), (""Lupita Nyong'o"", 1), (""Mark Ruffalo"", 3)],
""Mark Ruffalo"": [(""Chadwick Boseman"", 2), (""Robert Downey Jr."", 3)]
}
print(collaborations[""Chadwick Boseman""])
[(""Lupita Nyong'o"", 2), (""Robert Downey Jr."", 3), (""Mark Ruffalo"", 2)]
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(E)
, where E
is the number of edges (collaborations) in the graph. Each edge (collaboration) is processed once for both actors.O(V + E)
, where V
is the number of vertices (actors) and E
is the number of edges (collaborations), to store the adjacency list.