Littlepay engineering exercise: taps to priced trips
This submission has these parts:
- Part 1: Java logic that turns a list of card taps into priced trips, with tests. It’s in
src/. - Part 2: a design note for running this continuously on AWS. It’s in
docs/DESIGN.md, with the diagram indocs/architecture.svganddocs/architecture.png.
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:
- Keep one copy of a tap that was resent unchanged. Reject every copy of a tap ID that was reused with different content.
- Group the remaining taps by card, then sort each card’s taps by time, using the tap ID to break ties.
- 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.
- 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:
- New fares: add a
Tariffwith a latereffectiveFrom. Tariffs are never edited. - The right fare for every trip: a trip is priced with the tariff of its tap-on operator in force at its tap-on time. A trip that starts at 23:55 on the night of a fare change keeps the old fare. A tap that arrives a week late is priced as it would have been on the day.
- Backward compatibility: a test prices January’s taps with and without February’s tariff in the catalogue and checks the results are identical. Every trip records its
tariffId, so any charge can be explained and reproduced later. - New operators: add their tariffs to the catalogue. Each one has its own stops, fares and incomplete-trip rule, and stops never leak between operators. A tap for an operator with no tariff yet is reported as
NO_TARIFF, not guessed at. - The original API still works:
new TripPricer(FareTable)is a catalogue with one tariff for everyone, and it gives exactly the output it always did.
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.
- 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.
- 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. - 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.
- 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.
- 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.
- A cancelled trip still records its tap off details. It has a finish time, a duration and a destination, and is charged
$0.00. - An incomplete trip leaves the fields it does not have empty:
Finished,DurationSecsandToStopId. - 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.
- 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 asCONFLICTING_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. - 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_STOPand its partner asPAIRED_WITH_UNKNOWN_STOP. In particular, a tap off at an unknown stop does not leave the passenger paying the maximum fare. - Money is handled exactly. Amounts are
BigDecimalwith two decimal places, and floating point is never used. Fares with more than two decimals are rejected. Output is shown as$3.25. - Times are UTC. That applies on input and output, both in the
dd-MM-yyyy HH:mm:ssformat. - 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. - The output is sorted by start time, then by PAN.
How I checked it is correct
- The exercise’s expected line is asserted character for character. That is the line taps 1 and 2 produce. The other three outcomes of the sample are asserted too.
- There is a test for each rule and behavioural assumption above. The rules covered are completed in both directions, cancelled, incomplete from each stop, tap on after tap on, tap off on another bus, a tap off with no tap on, an unknown stop, a resend and a conflicting ID.
- An order-independence test shuffles the input 25 times with fixed seeds and checks the result never changes. The input is the sample plus resent taps and a conflicting pair. This is the property that makes late and out-of-order taps safe.
- A re-pricing test shows a late tap off turning a $7.30 incomplete trip into a $3.25 completed one. The −$4.05 difference is what the Part 2 design would refund.
What I would do next
- Teach the pricer what time it is. Today a tap on with no tap off is priced as incomplete straight away, which is right for a finished batch. For continuous pricing the pricer would take an
asOftime and return a fourth status,OPEN, for a tap on that is still inside the operator’s maximum journey time. Only when that window has passed does it becomeINCOMPLETEand chargeable. This is the timer in the Part 2 design. - More rules on the same shape: a maximum journey time per operator, then daily and weekly capping, transfers between buses, and concession fares. Each is another policy on
Tariff, alongsideIncompleteTripPolicy. - Property-based tests, for example with jqwik. Random tap streams would check that results don’t depend on input order, that no trip is ever charged more than the maximum fare from its stop, and that every tap ends up in exactly one trip or anomaly.
- A thin adapter that reads and writes the CSV files, if a runnable tool is wanted.