KatInterview

Reconcile Payments with Invoices Using Memo and Amount

StringsAuto-graded · 18 test cases
MediumAuto-gradedStringsstring-matchinghash-mapgreedy

Last updated

Tests multi-pattern substring search, hash-based indexing, and greedy reconciliation with priority-based tie-breaking across two matching strategies.

You are building a payment reconciliation system. Given two collections—a list of outstanding invoices and a list of incoming payments—your job is to pair each payment with at most one invoice using a two-phase matching strategy.

Each invoice contains:

  • id: a unique string identifier
  • amount: a numeric value
  • date: an ISO date string (YYYY-MM-DD)

Each payment contains:

  • amount: a numeric value
  • memo: a text field that may reference invoice identifiers
  • date: an ISO date string

Matching Rules

Process payments in the order given. For each payment, attempt to match it with an unmatched invoice using the following precedence:

Phase 1: Identifier Match

Search the payment's memo field for any invoice id as a substring. If one or more invoice identifiers appear and those invoices are still unmatched:

  • Select the invoice with the largest amount.
  • If multiple invoices share that largest amount, choose the one with the earliest date.
  • If still tied, choose the one with the lexicographically smallest id.

Phase 2: Exact Amount Match

If no unmatched invoice identifier was found in the memo, look for an unmatched invoice whose amount exactly equals the payment amount:

  • If multiple invoices have that amount, choose the one with the earliest date.
  • If still tied, choose the one with the lexicographically smallest id.

Output

Return a list of matched pairs [payment_index, invoice_id] in the order payments were processed. Payments that match no invoice are omitted from the result.

Once an invoice is matched, it becomes unavailable for subsequent payments.

Constraints

  • 1 ≤ number of invoices ≤ 10,000
  • 1 ≤ number of payments ≤ 10,000
  • Invoice id strings are unique and non-empty
  • Amounts are non-negative floats
  • Date strings follow ISO format; missing dates default to empty string
  • Memo strings may be empty or missing

Examples

Example 1

Input: reconcile_payments([{'id': 'INV-001', 'amount': 100, 'date': '2023-01-01'}, {'id': 'INV-002', 'amount': 200, 'date': '2023-01-02'}], [{'amount': 100, 'memo': 'Paid INV-001'}, {'amount': 200, 'memo': 'Final balance'}])

Output: [[0, 'INV-001'], [1, 'INV-002']]

Example 2

Input: reconcile_payments([{'id': 'A', 'amount': 50, 'date': '2024-02-01'}, {'id': 'B', 'amount': 100, 'date': '2024-01-01'}], [{'amount': 999, 'memo': 'A and B'}])

Output: [[0, 'B']]

Example 3

Input: reconcile_payments([{'id': 'X1', 'amount': 25, 'date': '2024-03-01'}, {'id': 'X2', 'amount': 25, 'date': '2024-01-01'}], [{'amount': 25, 'memo': 'no reference'}, {'amount': 25, 'memo': 'still no reference'}])

Output: [[0, 'X2'], [1, 'X1']]

Hints

Hint 1

For efficient substring matching of many patterns in each memo, consider using an Aho-Corasick automaton instead of checking each invoice ID individually.

Hint 2

Track which invoices have already been matched using a boolean array to ensure each invoice is used at most once.

Hint 3

A single comparison key of (-amount, date, id) handles tie-breaking for both phases: phase 1 selects the highest amount first, and phase 2 automatically uses date and id when amounts are equal.