KatInterview

Billing Reconciliation: Match Payments to Invoices in Three Stages

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-mapheapsegment-tree

Last updated

Tests incremental lookup design by matching payments to invoices via explicit ID, then earliest-dated exact-amount fallback, then a forgiveness range, with closed-invoice tracking throughout.

Billing Reconciliation: Match Payments to Invoices in Three Stages

You are building the reconciliation engine for an accounts-receivable system. Given a collection of invoices and a batch of payments, your task is to determine which invoice each payment settles — or report that no match was found.

Data Formats

All inputs are Python dicts. Fields and types are fixed.

An invoice dict has:

  • "id" — a unique string identifier
  • "amount" — a non-negative integer (minor currency units, e.g. cents)
  • "date" — an ISO-8601 date string ("YYYY-MM-DD")

A payment dict has:

  • "id" — a string identifier for this payment
  • "amount" — a non-negative integer
  • "invoice_id" (optional) — the string ID of the invoice this payment intends to settle

Matching Rules

Payments are processed in order, one at a time, and each decision is final. An invoice that has been matched is closed and cannot be matched again.

Apply the following rules in priority order for each payment:

Rule 1 — Explicit invoice ID. If the payment includes an "invoice_id" key, look up that invoice by ID and match it, regardless of whether the payment amount equals the invoice amount. If no invoice with that ID exists, or the invoice is already closed, report no match for this payment. Do not fall back to Rule 2 in either failure case.

Rule 2 — Amount fallback with forgiveness. If the payment has no "invoice_id", find every open invoice whose amount is within the closed interval [payment_amount − f, payment_amount + f], where f is the non-negative integer forgiveness parameter. Among all such candidates, match the one with the earliest date. If two candidates share the earliest date, break the tie by choosing the one whose invoice ID comes first in lexicographic order.

Setting f = 0 makes Rule 2 require an exact amount match.

Return Value

Return a list of results, one per payment, in the same order. Each result is either:

  • the string ID of the matched invoice, or
  • None if no match was found.

Constraints

  • 0 ≤ len(invoices) ≤ 10^4
  • 0 ≤ len(payments) ≤ 10^4
  • Invoice IDs are unique strings.
  • 0 ≤ amount ≤ 10^9 for both invoices and payments.
  • 0 ≤ forgiveness ≤ 10^9
  • date strings are valid ISO-8601 dates and compare correctly as plain strings.
  • A payment with "invoice_id" present but set to an ID that does not exist in the invoice list yields None — no fallback.
  • If no invoices are given, every payment yields None.

Examples

Example 1

Input: reconcile_payments([{'id': 'inv-1', 'amount': 1000, 'date': '2024-01-02'}, {'id': 'inv-2', 'amount': 1000, 'date': '2024-01-01'}, {'id': 'inv-3', 'amount': 1200, 'date': '2024-01-03'}], [{'id': 'p1', 'amount': 999, 'invoice_id': 'inv-1'}, {'id': 'p2', 'amount': 1000}, {'id': 'p3', 'amount': 1100}, {'id': 'p4', 'amount': 1200, 'invoice_id': 'missing'}], 100)

Output: ['inv-1', 'inv-2', 'inv-3', None]

Example 2

Input: reconcile_payments([{'id': 'a', 'amount': 500, 'date': '2024-02-01'}, {'id': 'b', 'amount': 500, 'date': '2024-01-01'}], [{'id': 'p1', 'amount': 500}, {'id': 'p2', 'amount': 500}, {'id': 'p3', 'amount': 500}], 0)

Output: ['b', 'a', None]

Example 3

Input: reconcile_payments([{'id': 'x', 'amount': 700, 'date': '2024-01-01'}], [{'id': 'p1', 'amount': 699, 'invoice_id': 'x'}, {'id': 'p2', 'amount': 700}], 10)

Output: ['x', None]

Hints

Hint 1

For Rule 1, build a dict mapping invoice ID → invoice index before processing any payments so each lookup is O(1).

Hint 2

For Rule 2, sort the unique invoice amounts and store a min-heap per amount bucket (keyed by (date, invoice_id, index)). Binary search gives you the bucket range that falls inside the forgiveness window, then you need the earliest still-open entry across that range.

Hint 3

To query the minimum across a contiguous range of buckets efficiently, layer a segment tree on top of the per-amount heaps. Each segment-tree node caches the current minimum of its subtree, and you update it whenever a matched invoice is removed from a heap.