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?
[a, b]
indicates a bidirectional flight between cities a
and b
.HAPPY CASE
Input: flights = [['Cape Town', 'Addis Ababa'], ['Cairo', 'Lagos'], ['Lagos', 'Addis Ababa'], ['Nairobi', 'Cairo'], ['Cairo', 'Cape Town']]
Output: {
'Cape Town': ['Addis Ababa', 'Cairo'],
'Addis Ababa': ['Cape Town', 'Lagos'],
'Lagos': ['Cairo', 'Addis Ababa'],
'Cairo': ['Cape Town', 'Nairobi'],
'Nairobi': ['Cairo']
}
Explanation: The list of flights is converted into an adjacency dictionary, representing the bidirectional connections.
EDGE CASE
Input: flights = []
Output: {}
Explanation: There are no flights, so the output is an empty dictionary.
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 iterate through the list of edges and build the adjacency dictionary by adding connections for both cities a
and b
since the flight is bidirectional.
1) Create an empty dictionary `adj_dict`.
2) Iterate through the `flights` list. For each flight `(a, b)`:
a) Add `b` to `adj_dict[a]` if `a` is already in the dictionary, otherwise create a new entry for `a` and add `b`.
b) Add `a` to `adj_dict[b]` if `b` is already in the dictionary, otherwise create a new entry for `b` and add `a`.
3) Return the completed adjacency dictionary.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def get_adj_dict(flights):
adj_dict = {}
# Iterate through each flight (edge)
for a, b in flights:
# Add bidirectional flight from a to b
if a not in adj_dict:
adj_dict[a] = []
adj_dict[a].append(b)
# Add bidirectional flight from b to a
if b not in adj_dict:
adj_dict[b] = []
adj_dict[b].append(a)
return adj_dict
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
flights = [['Cape Town', 'Addis Ababa'], ['Cairo', 'Lagos'], ['Lagos', 'Addis Ababa'], ['Nairobi', 'Cairo'], ['Cairo', 'Cape Town']]
Expected Output:
{
'Cape Town': ['Addis Ababa', 'Cairo'],
'Addis Ababa': ['Cape Town', 'Lagos'],
'Lagos': ['Cairo', 'Addis Ababa'],
'Cairo': ['Cape Town', 'Nairobi'],
'Nairobi': ['Cairo']
}
Input: flights = []
Expected Output: {}
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(E)
, where E
is the number of edges (flights). We iterate through the list of flights once.O(V + E)
, where V
is the number of vertices (cities) and E
is the number of edges. We store the adjacency dictionary which holds all cities and their connections.