KatInterview

Minimum-Cost Flight Path with Departure Window

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphsdijkstragraphsshortest-path

Last updated

Tests constrained graph search: find the cheapest multi-leg flight route where only the first leg must depart within a given date range, connections must respect time ordering, and cycles are forbidden.

Minimum-Cost Flight Path with Departure Window

You are given a list of flights and must find the lowest-cost route from a starting airport to a target airport. Each flight is described by a list with six fields:

[flight_id, from_airport, to_airport, departure_time, arrival_time, price]
  • flight_id — a unique string identifier for the flight
  • from_airport / to_airport — airport codes (strings)
  • departure_time / arrival_time — ISO-8601 datetime strings, e.g. "2024-01-15T09:30"
  • price — a non-negative integer

Task

Return the minimum total price to travel from origin to destination, along with the ordered sequence of flight IDs that form that route.

The first leg of the route must have a departure_time that falls within the inclusive range [window_start, window_end]. Subsequent legs have no date restriction.

A valid connection between two consecutive flights requires that the second flight departs at or after the first flight arrives (same-minute connections are allowed).

No flight may appear more than once in a route.

Return value

  • If origin equals destination, return [0, []].
  • If a valid route exists, return [total_price, [flight_id_1, flight_id_2, ...]] where the flight IDs are listed in travel order.
  • If no valid route exists, return -1.

When multiple routes share the same minimum price, return the one whose flight ID list is lexicographically smallest.

How date/time comparisons work

All datetime and date strings are ISO-8601. Because ISO-8601 strings sort correctly as plain strings, you may compare them with the standard <, <=, >=, > operators. A date-only boundary like "2024-01-05" sorts before "2024-01-05T00:00", so a departure of "2024-01-05T10:00" would fall outside a window whose window_end is "2024-01-05".

Constraints

  • 0 <= len(flights) <= 500
  • All price values are non-negative integers.
  • departure_time < arrival_time for every flight (no zero-duration flights in the input).
  • All datetime strings are valid ISO-8601 format and are comparable as plain strings.
  • Flight IDs are unique across the entire input list.
  • Airport codes are non-empty strings; there is no fixed alphabet.
  • window_start <= window_end

Examples

Example 1

Input: cheapest_flight_path([['F1', 'A', 'B', '2024-01-01T08:00', '2024-01-01T10:00', 100], ['F2', 'B', 'D', '2024-01-01T11:00', '2024-01-01T13:00', 80], ['F3', 'A', 'D', '2024-01-01T09:00', '2024-01-01T12:00', 250]], 'A', 'D', '2024-01-01T00:00', '2024-01-02T00:00')

Output: [180, ['F1', 'F2']]

Example 2

Input: cheapest_flight_path([['A1', 'NYC', 'CHI', '2024-02-10T07:00', '2024-02-10T09:00', 120], ['A2', 'CHI', 'LAX', '2024-02-10T09:00', '2024-02-10T12:00', 100], ['A3', 'NYC', 'LAX', '2024-02-10T08:00', '2024-02-10T12:00', 250]], 'NYC', 'LAX', '2024-02-10T00:00', '2024-02-10T23:59')

Output: [220, ['A1', 'A2']]

Example 3

Input: cheapest_flight_path([['X1', 'A', 'B', '2024-03-02T10:00', '2024-03-02T11:00', 10], ['X2', 'B', 'C', '2024-03-02T12:00', '2024-03-02T13:00', 10]], 'A', 'C', '2024-03-03T00:00', '2024-03-04T00:00')

Output: -1

Hints

Hint 1

Instead of treating airports as the nodes in your search, consider making each individual flight a node. Two paths reaching the same airport can have different onward options depending on when they land there — flight-level state captures this.

Hint 2

Dijkstra's algorithm works here because all prices are non-negative. Store (total_cost, path_tuple, flight_index) in a min-heap. The path tuple in the key gives you a free lexicographic tie-break with no extra bookkeeping.

Hint 3

ISO-8601 strings compare correctly with plain Python string operators, so you don't need to parse dates. Use that for both the departure-window check on the first leg and the arrival/departure ordering check on every connection.