Preparing your design workspace

Loading the problem and your saved attempt…

Design and Implement a Train Route Fare Calculator

Model an appendable ordered train route, calculate its per-segment fares, and apply a deterministic weekend cap.

Design and implement a class that owns one ordered train route, supports dynamically appending stations, and calculates fares. The motivating route is G to U to H to K to I; each adjacent segment costs 1, so a weekday trip from G to I costs 4.

For this exercise, assume unique stop identifiers and immutable adjacent segments in one fixed currency, with non-negative configured per-segment fares; the reported route uses unit fares.

appendStop(stop, fareFromPrevious) adds a new unique stop after the current last stop and a new segment, preserving all existing stop positions and segment fares.

calculateFare(start, stop, travelDate) supports forward travel only, returns zero for the same known stop, sums every included segment, and then caps the total at a configured maximum on Saturday or Sunday.

Unknown stops, reverse travel, duplicate stop identifiers, negative segment fares and an invalid weekend cap are rejected explicitly before mutating route state.

For this exercise, assume one process and sequential calls; branching networks, deletion or reordering of stations, transfers, zones, ticket purchase, passenger discounts, time zones and persistence are out of scope.