Design a flight booking system with cheapest-route and fewest-hops queries

Quick Overview

Design a flight booking class that stores directed, priced flights between cities and returns both the cheapest route and the route with the fewest hops between any two cities. It tests graph modeling, shortest-path search on weighted and unweighted graphs, and deterministic tie-breaking by lexicographic path order.

Design a flight booking system with cheapest-route and fewest-hops queries

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a flight booking system that keeps a directed, weighted graph of flights between cities and answers routing queries. Flights are added over time, and each one has a source city, a destination city and an integer price. The system keeps its state between calls, so every query sees all flights added before it. For any two cities it must find the cheapest route (minimum total price) and the route with the fewest hops (minimum number of flights). Implement a class `FlightBookingSystem` with these methods: - `addFlight(source: str, destination: str, price: int)` registers a direct flight from `source` to `destination`. If a flight between the same ordered pair already exists, keep the cheaper price. - `findCheapestRoute(src: str, dst: str) -> list` returns `[path, totalPrice]`, where `path` is the list of cities on a cheapest route from `src` to `dst`, both ends included. - `findFewestHopsRoute(src: str, dst: str) -> list` returns `[path, hopCount]`, where `hopCount` is the number of flights on a route from `src` to `dst` with the fewest hops. Both queries follow the same rules: - If no route exists, return `[[], -1]`. A city that appears in no flight has no route, even to itself. - A query from a known city to itself returns `[[src], 0]`. - When several routes tie on the quantity being minimized, return the lexicographically smallest path, comparing paths city by city as sequences of names (the way Python compares two lists of strings). A first session: ```text fbs = FlightBookingSystem() fbs.addFlight("A", "B", 50) fbs.addFlight("B", "C", 50) fbs.addFlight("A", "C", 200) fbs.findCheapestRoute("A", "C") -> [["A", "B", "C"], 100] fbs.findFewestHopsRoute("A", "C") -> [["A", "C"], 1] fbs.findCheapestRoute("A", "A") -> [["A"], 0] ``` Going through B costs 50 + 50 = 100, which beats the direct fare of 200. The direct flight is one hop against two. A city needs no flight to reach itself. A session with ties: ```text fbs = FlightBookingSystem() fbs.addFlight("X", "M", 10) fbs.addFlight("M", "Y", 20) fbs.addFlight("X", "K", 10) fbs.addFlight("K", "Y", 20) fbs.addFlight("X", "Y", 40) fbs.findCheapestRoute("X", "Y") -> [["X", "K", "Y"], 30] fbs.addFlight("X", "Y", 30) fbs.findCheapestRoute("X", "Y") -> [["X", "K", "Y"], 30] fbs.findFewestHopsRoute("X", "Y") -> [["X", "Y"], 1] fbs.findCheapestRoute("Y", "X") -> [[], -1] ``` At first the routes through K and M both cost 30, and "K" sorts before "M". Once the direct fare drops to 30, three routes tie. The comparison is decided at the second city, where "K" < "M" < "Y", so the route through K still wins even though the direct flight has fewer hops. No flight leaves Y, so there is no route from Y to X. ```hint Count the ties Pushing whole paths into a priority queue breaks ties correctly. Before settling on it, count how many equal-cost routes a chain of small diamonds can produce. ``` ```hint Work backward from the destination Suppose you already knew, for every city, the cheapest price from that city to `dst`. How would that tell you which next city keeps a route optimal? ``` ### Constraints & Assumptions - At most 1,000 cities. - Prices are integers from 1 to 1,000,000. - City names are non-empty strings of at most 20 characters. - At most 10,000 calls in total across all methods. - Flights are directed: a flight from A to B says nothing about B to A. ### Clarifying Questions to Ask - Are city names compared by code point (case-sensitive) for the tie-break, or case-insensitively? - What should a query return when a city has never appeared in a flight, including the case where source and destination are the same? - Is the workload dominated by queries or by additions, and does a query have to reflect a flight added just before it? - Can flights ever be removed or become more expensive, or do prices only go down? ### What a Strong Answer Covers - A graph representation that supports adding flights, keeps the cheaper price for a repeated pair, and answers queries without rebuilding. - A correct method for minimum total price and one for minimum hop count, with a reason each is correct when every price is positive. - A tie-break that provably returns the lexicographically smallest optimal path, and how many equal-cost paths that tie-break may have to consider. - The edge cases: unknown cities, source equal to destination, an unreachable destination, and repeated flights. - Time and space complexity of each method under the stated limits. ### Follow-up Questions - How would you support a limit of at most K intermediate stops on the cheapest route? - If queries far outnumber additions, what would you cache, and when would `addFlight` invalidate it? - How would the design change if flights could be cancelled or their prices could go up?

Overview: Design a flight booking class that stores directed, priced flights between cities and returns both the cheapest route and the route with the fewest hops between any two cities. It tests graph modeling, shortest-path search on weighted and unweighted graphs, and deterministic tie-breaking by lexicographic path order.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a flight booking system that keeps a directed, weighted graph of flights between cities and answers routing queries.

Flights are added over time, and each one has a source city, a destination city and an integer price. The system keeps its state between calls, so every query sees all flights added before it. For any two cities it must find the cheapest route (minimum total price) and the route with the fewest hops (minimum number of flights).

Implement a class FlightBookingSystem with these methods:

  • addFlight(source: str, destination: str, price: int) registers a direct flight from source to destination . If a flight between the same ordered pair already exists, keep the cheaper price.
  • findCheapestRoute(src: str, dst: str) -> list returns [path, totalPrice] , where path is the list of cities on a cheapest route from src to dst , both ends included.
  • findFewestHopsRoute(src: str, dst: str) -> list returns [path, hopCount] , where hopCount is the number of flights on a route from src to dst with the fewest hops.

Both queries follow the same rules:

  • If no route exists, return [[], -1] . A city that appears in no flight has no route, even to itself.
  • A query from a known city to itself returns [[src], 0] .
  • When several routes tie on the quantity being minimized, return the lexicographically smallest path, comparing paths city by city as sequences of names (the way Python compares two lists of strings).

A first session:

fbs = FlightBookingSystem()
fbs.addFlight("A", "B", 50)
fbs.addFlight("B", "C", 50)
fbs.addFlight("A", "C", 200)
fbs.findCheapestRoute("A", "C")     -> [["A", "B", "C"], 100]
fbs.findFewestHopsRoute("A", "C")   -> [["A", "C"], 1]
fbs.findCheapestRoute("A", "A")     -> [["A"], 0]

Going through B costs 50 + 50 = 100, which beats the direct fare of 200. The direct flight is one hop against two. A city needs no flight to reach itself.

A session with ties:

fbs = FlightBookingSystem()
fbs.addFlight("X", "M", 10)
fbs.addFlight("M", "Y", 20)
fbs.addFlight("X", "K", 10)
fbs.addFlight("K", "Y", 20)
fbs.addFlight("X", "Y", 40)
fbs.findCheapestRoute("X", "Y")     -> [["X", "K", "Y"], 30]
fbs.addFlight("X", "Y", 30)
fbs.findCheapestRoute("X", "Y")     -> [["X", "K", "Y"], 30]
fbs.findFewestHopsRoute("X", "Y")   -> [["X", "Y"], 1]
fbs.findCheapestRoute("Y", "X")     -> [[], -1]

At first the routes through K and M both cost 30, and "K" sorts before "M". Once the direct fare drops to 30, three routes tie. The comparison is decided at the second city, where "K" < "M" < "Y", so the route through K still wins even though the direct flight has fewer hops. No flight leaves Y, so there is no route from Y to X.

Constraints & Assumptions

  • At most 1,000 cities.
  • Prices are integers from 1 to 1,000,000.
  • City names are non-empty strings of at most 20 characters.
  • At most 10,000 calls in total across all methods.
  • Flights are directed: a flight from A to B says nothing about B to A.

Clarifying Questions to Ask Guidance

  • Are city names compared by code point (case-sensitive) for the tie-break, or case-insensitively?
  • What should a query return when a city has never appeared in a flight, including the case where source and destination are the same?
  • Is the workload dominated by queries or by additions, and does a query have to reflect a flight added just before it?
  • Can flights ever be removed or become more expensive, or do prices only go down?

What a Strong Answer Covers Guidance

  • A graph representation that supports adding flights, keeps the cheaper price for a repeated pair, and answers queries without rebuilding.
  • A correct method for minimum total price and one for minimum hop count, with a reason each is correct when every price is positive.
  • A tie-break that provably returns the lexicographically smallest optimal path, and how many equal-cost paths that tie-break may have to consider.
  • The edge cases: unknown cities, source equal to destination, an unreachable destination, and repeated flights.
  • Time and space complexity of each method under the stated limits.

Follow-up Questions Guidance

  • How would you support a limit of at most K intermediate stops on the cheapest route?
  • If queries far outnumber additions, what would you cache, and when would addFlight invalidate it?
  • How would the design change if flights could be cancelled or their prices could go up?