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?
HAPPY CASE
Input: Create an adjacency dictionary for a graph with nodes representing celebrities (Kevin Bacon, Meryl Streep, Idris Elba, Laverne Cox, Sofia Vergara) and their connections.
Output: {
'Kevin Bacon': ['Laverne Cox', 'Sofia Vergara'],
'Meryl Streep': ['Idris Elba', 'Sofia Vergara'],
'Idris Elba': ['Meryl Streep', 'Laverne Cox'],
'Laverne Cox': ['Kevin Bacon', 'Idris Elba'],
'Sofia Vergara': ['Kevin Bacon', 'Meryl Streep']
}
Explanation: Each celebrity is connected to other celebrities based on the graph.
EDGE CASE
Input: A graph with no connections between some celebrities.
Output: The adjacency list should reflect isolated nodes, e.g., `'Kevin Bacon': []` if there are no connections for Kevin Bacon.
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 need to construct an adjacency dictionary where each celebrity is a key, and the value is a list of other celebrities they know, based on the graph provided.
1) Create a dictionary with keys as the names of the celebrities.
2) For each key, create a list of the other celebrities they are connected to.
3) Return the adjacency dictionary representing the graph.
⚠️ Common Mistakes
A
knows B
, then B
knows A
).Implement the code to solve the algorithm.
hollywood_stars = {
'Kevin Bacon': ['Laverne Cox', 'Sofia Vergara'],
'Meryl Streep': ['Idris Elba', 'Sofia Vergara'],
'Idris Elba': ['Meryl Streep', 'Laverne Cox'],
'Laverne Cox': ['Kevin Bacon', 'Idris Elba'],
'Sofia Vergara': ['Kevin Bacon', 'Meryl Streep']
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
hollywood_stars
['Kevin Bacon', 'Meryl Streep', 'Idris Elba', 'Laverne Cox', 'Sofia Vergara']
[['Laverne Cox', 'Sofia Vergara'], ['Idris Elba', 'Sofia Vergara'], ['Meryl Streep', 'Laverne Cox'], ['Kevin Bacon', 'Idris Elba'], ['Kevin Bacon', 'Meryl Streep']]
['Laverne Cox', 'Sofia Vergara']
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
, where N
is the number of nodes (celebrities). We need to define the relationships for each celebrity.O(N)
for storing the adjacency dictionary.