KatInterview

Reconcile Payments Against Open Invoices in Three Tiers

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashinghash-mapsimulationmatching

Last updated

Process payments in order, pairing each with an invoice by ID plus amount, then by unique amount, then by closest amount within a percentage tolerance.

Reconcile Payments Against Open Invoices

A billing system receives a list of payments and a list of open invoices. Each record is a dictionary with an id (string) and an amount (non-negative number). Pair payments with invoices so that each invoice is used by at most one payment.

Write a function reconcile_payments(payments, invoices) that returns the pairs it finds.

Processing order

Handle the payments one at a time, in the order given. An invoice that has already been paired is no longer available to later payments. For each payment, try the following tiers in order and stop at the first one that selects an invoice.

Two amounts are treated as equal when they differ by at most 1e-9. Let eps = 1e-9.

  1. ID and amount match. Look at the available invoices whose id is identical to the payment's id and whose amount equals the payment's amount. If there are several, take the one that appears earliest in invoices.

  2. Unique amount match. Count the available invoices whose amount equals the payment's amount. If exactly one exists, pair the payment with it. If there are zero or several, move on to tier 3.

  3. Tolerance match. Consider every available invoice inv that satisfies

    |payment_amount - inv_amount| <= 0.03 * inv_amount + eps

    The tolerance is 3 percent of the invoice amount. Among these candidates, choose the one with the smallest |payment_amount - inv_amount|. If distances tie, choose the invoice with the lexicographically smaller id (string comparison). If that also ties, choose the one that appears earliest in invoices.

If no tier selects an invoice, the payment stays unmatched and is left out of the result.

Tier 3 also covers payments whose amount is shared by several available invoices. Those invoices are at distance 0, so the tie-break rules decide among them.

Output

Return a list of [payment_id, invoice_id] pairs (two-element lists of strings), one per matched payment, in the same order as the payments appear in the input.

Constraints

  • 0 <= len(payments), len(invoices) <= 10,000
  • Every id is a string. Invoice ids may repeat.
  • Amounts are non-negative numbers (integers or floats).
  • Use eps = 1e-9 for every amount-equality check, and 3% for the tolerance.
  • Unmatched payments do not appear in the result. Empty inputs give an empty list.

Examples

Example 1

Input: reconcile_payments([{'id': 'p1', 'amount': 100}, {'id': 'p2', 'amount': 205}], [{'id': 'i1', 'amount': 100}, {'id': 'i2', 'amount': 200}])

Output: [['p1', 'i1'], ['p2', 'i2']]

Hints

Hint 1

Keep a set of invoice positions that are still available, and remove a position as soon as a payment claims it.

Hint 2

Each payment runs through the three tiers independently and in order. In tier 2, count only available invoices whose amount equals the payment amount, and match only if the count is exactly 1.

Hint 3

In tier 3, build tuples of (distance, invoice id, position) for every available invoice that is within tolerance and take the minimum. The tuple ordering gives you the required tie-breaks.