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 list for a graph with nodes representing airports (JFK, LAX, DFW, ATL) and flights between them.
Output: {
'JFK': ['LAX', 'DFW'],
'LAX': ['JFK'],
'DFW': ['ATL', 'JFK'],
'ATL': ['DFW']
}
Explanation: Each airport has flights to the airports listed in its adjacency list.
EDGE CASE
Input: A graph with no edges between airports.
Output: {
'JFK': [],
'LAX': [],
'DFW': [],
'ATL': []
}
Explanation: Airports have no connections.
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: Use a dictionary to represent the graph, where each airport is a key, and the values are lists of airports that have direct flights from the key airport.
1) Create a dictionary where each key is an airport and each value is a list of airports it has a direct flight to.
2) For each flight between two airports, add the destination to the adjacency list for the source and vice versa (because the graph is undirected).
3) Return the completed adjacency dictionary.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
flights = {
'JFK': ['LAX', 'DFW'],
'LAX': ['JFK'],
'DFW': ['ATL', 'JFK'],
'ATL': ['DFW']
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example:
print(list(flights.keys())) # ['JFK', 'LAX', 'DFW', 'ATL']
print(list(flights.values())) # [['LAX', 'DFW'], ['JFK'], ['ATL', 'JFK'], ['DFW']]
print(flights[""JFK""]) # ['LAX', 'DFW']
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(V + E)
where V
is the number of vertices (airports) and E
is the number of edges (flights). We need to process each vertex and each edge once.O(V + E)
as we store the adjacency list, which takes space proportional to the number of vertices and edges.