Back to the live walkthrough

Littlepay engineering exercise: taps to priced trips

This submission has these parts:

Running the tests

You need Java 21. Nothing else needs installing, because the Gradle wrapper downloads Gradle on first use.

./gradlew test

The run prints each test as it passes. There are 79 tests. An HTML report is written to build/reports/tests/test/index.html.

What the code does

TripPricer pricer = new TripPricer(FareTable.sample());
PricingResult result = pricer.price(taps);

result.trips();       // priced trips, in time order
result.anomalies();   // taps that were not charged, with the reason

For the exercise’s sample, trip.toCsvLine() gives:

22-01-2026 09:20:00, , , Stop3, , $7.30, Company1, Bus36, 4111111111111111, INCOMPLETE
22-01-2026 13:00:00, 22-01-2026 13:05:00, 300, Stop1, Stop2, $3.25, Company1, Bus37, 5500005555555559, COMPLETED
23-01-2026 08:00:00, 23-01-2026 08:02:00, 120, Stop1, Stop1, $0.00, Company1, Bus37, 4111111111111111, CANCELLED

It also reports tap 6 as an anomaly, TAP_OFF_WITHOUT_TAP_ON, for reasons explained below.

The pricer works in four steps:

  1. Keep one copy of a tap that was resent unchanged. Reject every copy of a tap ID that was reused with different content.
  2. Group the remaining taps by card, then sort each card’s taps by time, using the tap ID to break ties.
  3. Walk each card’s taps in order. A tap on followed by a tap off on the same bus is a trip. Anything else closes the tap on as an incomplete trip.
  4. Report, rather than price, any trip that touches a stop missing from the fare table.

The pricer is a pure function. It gives the same trips whatever order the taps come in, and however many times a tap is repeated. That property is what the Part 2 design is built on.

Built to change

The brief’s fares are one table, but real operators keep adding stops, raising fares and doing things their own way. The code is shaped so those changes are data, not edits to the pricer:

Code layout

Class Responsibility
Tap, TapType A tap event as reported by a reader
Pan The card number. toString() masks it, so it cannot leak into logs
FareTable Fares between pairs of stops, in either direction. It derives the highest fare from each stop
Tariff One immutable version of an operator’s pricing: a fare table plus its rules, with an effectiveFrom
FareCatalogue Every operator’s tariffs over time. It resolves the tariff in force for a tap on
IncompleteTripPolicy What a missing tap off costs. The exercise’s rule is maximum fare; a flat penalty is the other example
TripPricer Deduplication, grouping, matching and pricing
Trip, TripStatus A priced trip, with toCsvLine() in the exercise’s output layout
PricingResult, Anomaly The trips, plus every tap that was not charged and why

Assumptions

The brief leaves several rules open. These are the decisions I made. Every behavioural one has a test that pins it.

  1. The sample input is not in time order. Tap 3 (09:20) comes after taps 1 and 2 (13:00), so taps are always sorted by their own timestamp. The order of the list is never relied on. Taps with identical timestamps are ordered by ID.
  2. A tap off with no tap on before it is not charged. Tap 6 in the sample is a tap off two days after card 5500… already completed a trip. There is nothing to price it against. Charging it a maximum fare would bill a passenger for a trip we cannot show they took, so it is reported as an anomaly for review instead. In a running system the same tap is held, not discarded. If its tap on arrives late, re-pricing pairs them.
  3. A tap on followed by another tap on closes the first trip as incomplete. It is charged the maximum fare from its stop. In the sample, tap 3 (on at Stop3) is followed the next day by tap 4 (on at Stop1), so tap 3 is incomplete at $7.30, the larger of $5.50 and $7.30.
  4. A tap on and a tap off only form a trip if they are on the same bus of the same operator. A tap off on another bus means the passenger changed vehicles. The tap on becomes an incomplete trip and the tap off is reported. Transfers would be a separate fare rule.
  5. There is no time limit between tap on and tap off because the brief does not set one. A real system would close a trip after the operator’s maximum journey time. That is how the Part 2 design handles it, with a timer per open tap on.
  6. A cancelled trip still records its tap off details. It has a finish time, a duration and a destination, and is charged $0.00.
  7. An incomplete trip leaves the fields it does not have empty: Finished, DurationSecs and ToStopId.
  8. The highest fare from a stop is derived from the fare table, not configured separately, so a fare change can never leave the two out of step. The table is passed in rather than hard-coded, because every operator has its own stops and fares.
  9. Duplicates are handled two ways. A tap resent with identical content is kept once and the extra copies are reported as DUPLICATE_TAP. If two taps share an ID but differ in content, neither is trusted, and both are reported as CONFLICTING_TAP_ID. Keeping “the first one received” would make the price depend on arrival order. In this core, the trip’s tap on is then priced as incomplete. In the Part 2 design, a card with an unresolved conflict is held back from charging until someone reviews it.
  10. A trip that touches a stop missing from the fare table is reported, not priced. This applies in both directions: an unknown stop on the tap on or on the tap off. The tap at the unknown stop is reported as UNKNOWN_STOP and its partner as PAIRED_WITH_UNKNOWN_STOP. In particular, a tap off at an unknown stop does not leave the passenger paying the maximum fare.
  11. Money is handled exactly. Amounts are BigDecimal with two decimal places, and floating point is never used. Fares with more than two decimals are rejected. Output is shown as $3.25.
  12. Times are UTC. That applies on input and output, both in the dd-MM-yyyy HH:mm:ss format.
  13. The PAN appears in the output because the exercise asks for it, so a card can be identified. Everywhere else it is masked (550000******5559). The tests only use published test card numbers.
  14. The output is sorted by start time, then by PAN.

How I checked it is correct

What I would do next