CODING CHALLENGE · N°69

Re-Engineer a Legacy ETL

Medium FDEInterviewData

You inherit an aggregation nobody can explain and the totals are wrong. Three separate bugs — an exclude-list where the spec wants an include-list, a dedupe key missing a field, and pre-seeded customers that should never appear. The FDE re-engineering round, with hidden tests.

The problem

A customer’s reporting pipeline aggregates order rows into a per-customer revenue total. The rows come from a join that duplicates a row per shipment leg, so the same order_id can appear several times for one customer with the same amount. Implement summarise(rows) so that: only rows whose status is "shipped" or "delivered" count; each (customer, order_id) pair contributes its amount exactly once; and a customer with no qualifying orders does not appear in the result at all. The starting code is the version you inherited — it is wrong in three places.

EXAMPLE 1
Input the same order duplicated 3×
Output counted once
the join fans out; the money does not
EXAMPLE 2
Input status "pending"
Output excluded
only shipped and delivered count
EXAMPLE 3
Input a customer whose only order was cancelled
Output absent from the result
not present with a 0
CONSTRAINTS
  • Only "shipped" and "delivered" count. Every other status is excluded, including ones not seen in the fixtures.
  • Dedupe on the pair (customer, order_id) — the same order reference can legitimately appear under two customers.
  • A customer with no qualifying orders must be absent, not present with a total of 0.
  • Return a mapping of customer → total amount.
SOLVE IT YOURSELF

Your turn — write it

Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.

YOUR TASK

Read the inherited summarise(rows), find the three defects against the spec, and fix them one at a time.

HINTS — 4 IDEAS
  1. Bug 1 is the status test: it excludes one bad status instead of including the two good ones.
  2. Bug 2 is the dedupe key: order_id alone silently drops a legitimate order belonging to a different customer.
  3. Bug 3 is the pre-seeding loop: it puts every customer in the result before any filtering, so cancelled-only customers survive with 0.
  4. Fix and re-run after each change. Knowing which fix moved which test is the point of the exercise.
CPython · WebAssembly
Approach, complexity & discussion — open after you solve

The approach

Re-engineering is a reading exercise, not a rewriting one. Read the inherited function fully before you touch it, then check each line against the spec one at a time. Here three separate rules are broken: the status filter is an exclude-list where the spec says include-list, the dedupe key is missing the customer, and every customer is pre-seeded into the result before filtering so empty ones survive.

Fix them independently and re-run after each — a single sweeping rewrite hides which change fixed what, which is exactly what an interviewer is watching for.

Complexity

Time O(n) over the rows, space O(n) for the dedupe set.

Common mistakes

  • Rewriting from scratch instead of locating the defects — you lose the behaviour that was already correct.
  • Deduping on order_id alone: two customers can legitimately carry the same order reference from different upstream systems.
  • Excluding only cancelled. The spec names the statuses that count; anything else — pending, returned, a typo — must not.
  • Leaving customers with a zero total in the output. Downstream, a zero looks like a real reported value.

Where this shows up

This is the shape of a genuine FDE first week: an inherited pipeline whose numbers are subtly wrong, nobody who wrote it is still there, and the customer has been reporting from it for a year. The Palantir re-engineering round and Sierra’s planted-bug debugging round both test this directly. The skill is method — reproduce, narrow, fix one thing, verify — not speed.

Explore the topic

See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.