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?
1
in the flights
matrix signify?
i
to location j
.source == dest
, we can return True
because no travel is required.HAPPY CASE
Input:
flights = [
[0, 1, 0], # Flight 0
[0, 0, 1], # Flight 1
[0, 0, 0] # Flight 2
]
source = 0
dest = 2
Output:
True
Explanation: There is a path from location 0 to 2 through location 1.
EDGE CASE
Input:
flights = [
[0, 0],
[0, 0]
]
source = 0
dest = 1
Output:
False
Explanation: There is no flight available from location 0 to location 1.
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: Treat the flight routes as a graph represented by the adjacency matrix flights
. Use DFS to recursively explore the paths from the source
location and check if we can reach dest
. If we find the destination, return True
; otherwise, return False
after all paths have been explored.
1) Create a visited list to track locations we have already checked.
2) Define a recursive DFS function:
a) If the current location is `dest`, return True.
b) Mark the current location as visited.
c) Explore all neighbors of the current location.
d) If we find the destination through one of the neighbors, return True.
3) If the DFS function completes without finding the destination, return False.
4) Call the DFS function starting from `source`.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def can_rebook(flights, source, dest):
n = len(flights) # Number of locations (nodes)
visited = [False] * n
def dfs(current):
# If we reach the destination, return True
if current == dest:
return True
visited[current] = True
# Explore neighbors
for neighbor in range(n):
if flights[current][neighbor] == 1 and not visited[neighbor]:
if dfs(neighbor):
return True
return False # No path found
# Start DFS from the source
return dfs(source)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
flights1 = [
[0, 1, 0], # Flight 0
[0, 0, 1], # Flight 1
[0, 0, 0] # Flight 2
]
flights2 = [
[0, 1, 0, 1, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
print(can_rebook(flights1, 0, 2)) # True
print(can_rebook(flights2, 0, 2)) # False
True
False
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(V^2)
where V
is the number of locations (vertices). This is because for each location, we check all other locations in the adjacency matrix.O(V)
for the visited
list and the recursion stack in DFS.