Unit 10 Session 1 Standard (Click for link to problem statements)
Unit 10 Session 1 Advanced (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?
boarding_passes
list represent?
(departure, arrival)
represents a flight from departure
to arrival
.HAPPY CASE
Input: boarding_passes = [
(""JFK"", ""ATL""),
(""SFO"", ""JFK""),
(""ATL"", ""ORD""),
(""LAX"", ""SFO"")]
Output: ['LAX', 'SFO', 'JFK', 'ATL', 'ORD']
Explanation: The correct itinerary starts at LAX, then proceeds to SFO, JFK, ATL, and finally ORD.
EDGE CASE
Input: boarding_passes = [
(""LAX"", ""DXB""),
(""DFW"", ""JFK""),
(""LHR"", ""DFW""),
(""JFK"", ""LAX"")]
Output: ['LHR', 'DFW', 'JFK', 'LAX', 'DXB']
Explanation: The correct itinerary starts at LHR, proceeds to DFW, JFK, LAX, and finally DXB.
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 Traversal problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will first create a mapping from each departure to its corresponding arrival. Then, we identify the starting point of the itinerary, which is the airport that never appears as an arrival. Finally, we trace the entire itinerary in the correct order.
1) Create a dictionary `flight_map` where the key is the departure airport and the value is the arrival airport.
2) Identify the starting airport by finding a departure airport that never appears in the list of arrival airports.
3) Starting from this airport, build the itinerary by following the flights using the `flight_map`.
4) Continue following the flights until the last airport (which has no outgoing flights).
5) Return the constructed itinerary.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def find_itinerary(flights):
# Step 1: Create a mapping from departure to arrival
flight_map = {}
all_arrivals = set()
for departure, arrival in flights:
flight_map[departure] = arrival
all_arrivals.add(arrival)
# Step 2: Find the starting point (departure that is not an arrival)
start = None
for departure, arrival in flights:
if departure not in all_arrivals:
start = departure
break
# Step 3: Trace the itinerary
itinerary = []
while start:
itinerary.append(start)
start = flight_map.get(start)
return itinerary
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
boarding_passes_1 = [
(""JFK"", ""ATL""),
(""SFO"", ""JFK""),
(""ATL"", ""ORD""),
(""LAX"", ""SFO"")]
print(find_itinerary(boarding_passes_1))
Output: ['LAX', 'SFO', 'JFK', 'ATL', 'ORD']
Input:
boarding_passes_2 = [
(""LAX"", ""DXB""),
(""DFW"", ""JFK""),
(""LHR"", ""DFW""),
(""JFK"", ""LAX"")]
print(find_itinerary(boarding_passes_2))
Output: ['LHR', 'DFW', 'JFK', 'LAX', 'DXB']
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(n)
, where n
is the number of boarding passes. We iterate through the list of flights once to build the flight map and once more to trace the itinerary.O(n)
for storing the flight map and the itinerary.