Skip to content
Katabench
Try free

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

What treasury sees

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.

Log excerpt

Text
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

What we know so far

  • On-call's money is on the bank file writer reading its rows twice, since it was the first thing to warn.
  • Ledger.Post writes 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.

Your job

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.

Working the incident

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

C#
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.

C# coding challenges →

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