Design a Transit System to Find Minimum Bus Changes Between Source and Destination

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2024-12-19

Problem Statement

Design and implement a system that models a city’s bus network and allows users to query the minimum number of bus changes required to reach a destination from a given source.

Each bus runs on a fixed circular route (i.e., it loops through its route infinitely).

🎯 Functional Requirements

You must implement the following operations:

1. addBusRoute(busId, List<String> stops)

•Adds a circular bus route to the system.
•Each bus has a unique busId and visits stops in the given order, then loops back to the first stop.

2. int getMinimumBusChanges(String sourceStop, String destinationStop)

•Returns the minimum number of bus changes required to go from sourceStop to destinationStop.
•If unreachable, return -1.

🧠 Assumptions

•Stops are identified by unique string names.
•A rider can board or get off at any stop on a bus's route.
•Bus transfers can occur only at common stops.
•All buses loop infinitely — availability is not a constraint.
•Bus routes may overlap at one or more stops.


Solution

Comments