KatInterview

Daily Ledger Reconciliation with FX Rates and Late Postings

SQL & DataModel answer
MediumModel answerSQL & Datasqldata-engineeringwindow-functions

Last updated

Build SQL reconciliation across two ledgers with as-of FX conversion, 48h late-arrival classification, an idempotent backfilling pipeline, and honest discrepancy reporting.

You own a daily job that reconciles a raw payment stream against two downstream ledgers. Postings routinely land late — up to 48 hours is normal, and anything later is an exception that still needs to be reported. Given these tables:

  • payments_raw(payment_id INT, user_id INT, amount DECIMAL(10,2), currency CHAR(3), created_at TIMESTAMP)
  • ledger_a(txn_id STRING, user_id INT, amount_usd DECIMAL(10,2), posted_at TIMESTAMP)
  • ledger_b(txn_id STRING, user_id INT, amount_usd DECIMAL(10,2), posted_at TIMESTAMP)
  • fx_rates(rate_date DATE, currency CHAR(3), usd_rate DECIMAL(10,6)) — USD per one unit of the currency

Sample data:

payments_raw
1 | 101 | 100.00 | USD | 2025-08-30 10:05:00
2 | 102 |  90.00 | EUR | 2025-08-30 16:40:00
3 | 101 |  50.00 | USD | 2025-08-31 23:30:00
4 | 103 | 100.00 | EUR | 2025-09-01 01:20:00

fx_rates
2025-08-30 | USD | 1.000000
2025-08-30 | EUR | 1.100000
2025-08-31 | USD | 1.000000
2025-08-31 | EUR | 1.120000
2025-09-01 | USD | 1.000000
2025-09-01 | EUR | 1.150000

ledger_a: A-1(101, 100.00, 08-30 10:06), A-2(102, 99.00, 08-30 18:00), A-3(103, 115.00, 09-01 08:00)
ledger_b: B-1(101, 100.00, 09-02 11:00), B-2(101, 50.00, 09-01 00:10), B-3(102, 99.01, 08-30 18:02)

Deliver four things:

  1. One SQL query producing a per-payment report with columns payment_id, user_id, amount_usd_from_fx, a_txn_id, b_txn_id, match_status, late_by_hours, abs_amount_diff. Rules: (a) convert amount to USD using the most recent FX rate on or before created_at's date, rounded to cents; (b) each payment matches at most one txn per ledger, requiring same user_id, amount within $0.01, and a posting lag within 48 hours; a lag beyond 48 hours (but otherwise matching) becomes late_beyond_48h with late_by_hours populated; if only one ledger matches, emit missing_in_a / missing_in_b for the other; if both ledgers have candidates but amounts differ by more than $0.01, emit amount_mismatch. Break ties by smallest absolute time difference from created_at, deterministically. late_by_hours is NULL when not late.

  2. A Python ETL design outline that partitions by created_at::date, keeps a rolling 72-hour backfill window, writes a snapshot table keyed deterministically, and delivers exactly-once downstream effects — including how late-beyond-48h corrections get merged without duplicate rows.

  3. A discrepancy chart. Produce a pie chart of counts by match_status, then argue why a bar chart with percentages and 95% confidence intervals is the better visual, and sketch how you'd build it.

  4. Justify Python over R (or vice versa) for this pipeline, naming concrete libraries, runtime/deployment concerns, and interoperability with the broader data stack.

State your assumptions about ambiguous rules before writing code.

Hints

Hint 1

The FX lookup is an as-of join: pick the latest rate_date <= the payment date per currency, not an equality join.

Hint 2

Do the ledger candidate generation as a UNION ALL of both ledgers, then a ROW_NUMBER per (payment_id, source) so each payment gets at most one txn per side — order by amount-tolerance first, then |time delta|, then txn_id so reruns are stable.

Hint 3

Rows in one ledger must never satisfy the match for the other ledger; keep the per-source flag through to the final CASE, and make the classification order explicit (missing vs mismatch vs late vs matched).