Three Extra Ledger Entries
Easy About 7 min Free, no account needed
Big payout runs reach the bank with three rows too many, and always the same three. Find out why before the next run settles.
The brief
Reported by: Treasury
Impact: Vendors paid twice on large payout runs, with $16,500.00 being recalled from this run alone
Code: RunPayouts in the payouts service
Run R-20931 had five requests and the bank file went out with eight rows. The extra rows pay NORTHWIND, CONTOSO and FABRIKAM a second time. Last week a run of forty requests also came out with exactly three extra rows, and they were again the first three requests in the file. Smaller runs have never had this problem.
payouts 17:02:11 run=R-20931 requests=5 reviewLimit=1000000
payouts 17:02:11 PAID 1000 NORTHWIND 900000
payouts 17:02:11 PAID 1001 CONTOSO 450000
payouts 17:02:11 PAID 1002 FABRIKAM 300000
payouts 17:02:11 PAID 1003 ADATUM 120000
payouts 17:02:11 PAID 1004 TAILSPIN 70000
payouts 17:02:11 REVIEW total=1840000 sample=1005,1006,1007
payouts 17:02:11 LEDGER entries=8 cents=3490000
bankfile 17:02:12 WARN wrote rows=8 expected=5- On-call's money is on the bank file writer reading its rows twice, since it was the first thing to warn.
Ledger.Postwrites exactly one entry per call. Its unit tests pass, and there is no retry logic anywhere in this service.- The requests were not duplicated on the way in. The source file has five rows.
Fix RunPayouts. Each request is ACCOUNT:CENTS, and zero-cent requests are dropped. A run whose paid total reaches reviewLimitCents gets a REVIEW line sampling the run's first receipts, so finance can spot-check them. Keep the report format exactly as it is.
Paying one vendor the same amount twice in a run is legitimate, so a repeated request is not an error.
The visible tests all pass, because this bug shipped. A hidden regression suite replays the incident and its neighbours. Custom input replays a log line, and anything you write with Console.WriteLine shows up in the results.
Starter code
using System.Collections.Generic;
using System.Linq;
public class Solution
{
private const int SampleSize = 3;
// requests: "ACCOUNT:CENTS". Requests of zero cents are dropped.
public string[] RunPayouts(string[] requests, long reviewLimitCents)
{
var ledger = new Ledger();
var payouts = PostAll(requests, ledger);
var report = new List<string>();
var total = 0L;
foreach (var payout in payouts)
{
report.Add($"PAID {payout.Receipt} {payout.Account} {payout.Cents}");
total += payout.Cents;
}
if (total >= reviewLimitCents)
{
report.Add(ReviewLine(total, payouts));
}
report.Add($"LEDGER entries={ledger.Entries} cents={ledger.PostedCents}");
return report.ToArray();
}
private static IEnumerable<Payout> PostAll(string[] requests, Ledger ledger) =>
requests
.Select(line => line.Split(':'))
.Select(parts => (Account: parts[0], Cents: long.Parse(parts[1])))
.Where(request => request.Cents > 0)
.Select(request => new Payout(
ledger.Post(request.Account, request.Cents), request.Account, request.Cents));
// Finance spot-checks the first few receipts of a large run before it settles.
private static string ReviewLine(long total, IEnumerable<Payout> payouts)
{
var sample = string.Join(",", payouts.Take(SampleSize).Select(payout => payout.Receipt));
return $"REVIEW total={total} sample={sample}";
}
}
public sealed class Ledger
{
private int _nextReceipt = 1000;
public int Entries { get; private set; }
public long PostedCents { get; private set; }
public int Post(string account, long cents)
{
Entries++;
PostedCents += cents;
return _nextReceipt++;
}
}
public sealed class Payout
{
public Payout(int receipt, string account, long cents)
{
Receipt = receipt;
Account = account;
Cents = cents;
}
public int Receipt { get; }
public string Account { get; }
public long Cents { get; }
}How it is graded
Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.
What it practices
- Debugging from symptoms
- Work from a production symptom and a log excerpt back to the cause, then fix it without breaking what already works.
Related katas
- Two Amounts of Tax on One Line Easy Free A customer rejected an invoice whose only line disagrees with its own total by a cent. How does one number come out as two?
- The Loss Listed Above the Gains Easy Free The treasury report put a ¥2.1B loss on top of the gains at month end, and every earlier close looks fine. What did October do differently?
- Reminders for Collected Parcels Medium Free Customers keep getting nightly reminders for parcels they collected days ago. Trace the failed pickup through the locker logs and fix it.
- One Sale, Two Business Days Medium Free During a terminal rollout, the same moment in the same shop landed on two different business days, and evening takings started drifting into tomorrow.