Codepath

Finding Direct Flights

Unit 10 Session 1 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 15-20 mins
  • 🛠️ Topics: Graphs, Adjacency Matrix, Neighbors

1: U-nderstand

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?
  • Q: What does each value in the adjacency matrix represent?
    • A: Each value n[i][j] == 1 indicates that there is a direct flight from destination i to destination j, and n[i][j] == 0 means no flight exists.
  • Q: What do we need to return?
    • A: A list of all destinations reachable directly from the source destination.
  • Q: Can there be a source with no outgoing flights?
    • A: Yes, in such a case, the result would be an empty list.
HAPPY CASE
Input: flights = [
                   [0, 1, 1, 0],
                   [1, 0, 0, 0],
                   [1, 1, 0, 1],
                   [0, 0, 0, 0]
                 ], source = 2
Output: [0, 1, 3]
Explanation: From destination 2, there are direct flights to destinations 0, 1, and 3.

EDGE CASE
Input: flights = [
                   [0, 1, 1, 0],
                   [1, 0, 0, 0],
                   [1, 1, 0, 1],
                   [0, 0, 0, 0]
                 ], source = 3
Output: []
Explanation: Destination 3 has no outgoing flights, so the result is an empty list.

2: M-atch

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:

  • Adjacency Matrix: This problem is a classic example of how to work with an adjacency matrix to find neighbors.
  • Graph Traversal: We are not required to traverse the entire graph, but simply check the row corresponding to the source node to find all direct connections.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: We are given an adjacency matrix and asked to return all direct flights from a given source destination. This means checking the row corresponding to source in the matrix and finding all columns where the value is 1, which represents a direct flight.

1) Create an empty list `direct_flights` to store the result.
2) Iterate over each destination (column) in the `flights[source]` row.
3) If `flights[source][destination] == 1`, append `destination` to `direct_flights`.
4) Return the list `direct_flights`.

⚠️ Common Mistakes

  • Forgetting to account for destinations with no outgoing flights.
  • Confusing rows and columns in the adjacency matrix when looking for connections.

4: I-mplement

Implement the code to solve the algorithm.

def get_direct_flights(flights, source):
    direct_flights = []
    n = len(flights)  # Number of destinations (matrix is n x n)
    
    # Iterate over all possible destinations from the source
    for destination in range(n):
        if flights[source][destination] == 1:  # If there is a direct flight
            direct_flights.append(destination)
    
    return direct_flights

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Input:
    flights = [
                [0, 1, 1, 0],
                [1, 0, 0, 0],
                [1, 1, 0, 1],
                [0, 0, 0, 0]
              ]
    print(get_direct_flights(flights, 2))
    print(get_direct_flights(flights, 3))
  • Output:
    [0, 1, 3]
    []
  • Explanation:
    • From destination 2, there are direct flights to destinations 0, 1, and 3.
    • From destination 3, there are no outgoing flights.

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(n), where n is the number of destinations. We only need to check one row of the adjacency matrix.
  • Space Complexity: O(n) as we store the list of direct flights.
Fork me on GitHub