Level 1 Match by memo
A business sends invoices and receives payments. Reconciling means working out which payment pays which invoice. Both arrive as text lines:
- an invoice is
"id,due_date,amount", like"INV-7,2026-04-30,250.00"; - a payment is
"id,amount,memo", like"PAY-1,250.00,Paying INV-7, thanks". The memo is free text written by the payer. It can be empty and it can contain commas, so only the first two commas separate fields.
Amounts are above zero and always have exactly two decimals. Invoice ids are distinct and made of letters, digits and -.
Write match_by_memo(invoices, payments) -> list[str]. Go through the payments in order. Every invoice starts open.
- Split the memo into words: the longest runs of letters, digits and
-. So"#INV-7."holds the wordINV-7, andINV-70is notINV-7. - The payment goes to the invoice named by the first word that is the id of an open invoice. Matching is case-sensitive.
- That invoice is then closed. At this level the amount doesn't matter: any match closes it.
- Return, for each payment, the id of its invoice, or
"UNMATCHED".
invoices = ["INV-1,2026-03-01,100.00", "INV-2,2026-03-15,250.00", "INV-10,2026-02-01,75.50"]
payments = ["PAY-1,250.00,for INV-2", "PAY-2,75.50,Invoice INV-10, March",
"PAY-3,100.00,INV-2 again", "PAY-4,20.00,no reference"]
match_by_memo(invoices, payments)
# ["INV-2", "INV-10", "UNMATCHED", "UNMATCHED"] INV-2 is already closed for PAY-3
Up to 10^5 invoices and 10^5 payments. ⭐ Bonus: the speed test earns a star if each memo word is looked up in O(1).