The Loss Listed Above the Gains
Easy About 7 min Free, no account needed
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?
The brief
Reported by: Treasury, while assembling the month-end pack
Impact: The October pack showed a ¥2.1B loss as the top line of "largest movements". It reached the CFO before anyone noticed.
Code: RankMovements in the treasury reporting job
The report ranks accounts by net movement for the day, largest first, with accounts in ordinal order when movements are equal. Every earlier close looks right. October 31 was the first close with the acquisition and its currency hedge on the books, and the consolidated report across all entities looks even stranger than the group one: each stretch of it is in order, as if someone cut the list in two and swapped the pieces.
treasury 18:00:04 rank-movements date=2026-09-30 accounts=3
treasury 18:00:04 1. SALES-APAC 1000000000
treasury 18:00:04 2. SUSPENSE 0
treasury 18:00:04 3. CAPEX -1000000000
treasury 18:00:02 rank-movements date=2026-10-30 accounts=3
treasury 18:00:02 1. SALES-EMEA 2000000000
treasury 18:00:02 2. SALES-US 1999000000
treasury 18:00:02 3. SALES-APAC 1500000000
treasury 18:00:03 rank-movements date=2026-10-31 accounts=5
treasury 18:00:03 1. FX-HEDGE -2100000000
treasury 18:00:03 2. ACQUISITION 2000000000
treasury 18:00:03 3. SALES-APAC 40000000
treasury 18:00:03 4. RENT -900000
treasury 18:00:03 5. PAYROLL -15000000
treasury 18:00:09 rank-movements date=2026-10-31 scope=consolidated accounts=40
treasury 18:00:09 1. ENT-05 -661905650
treasury 18:00:09 2. ENT-13 -741200528
treasury 18:00:09 ... 12 more, each a larger loss than the last ...
treasury 18:00:09 15. ENT-31 -1873580243
treasury 18:00:09 16. ENT-29 1987691551
treasury 18:00:09 17. ENT-30 1903378280
treasury 18:00:09 ... 22 more, descending ...
treasury 18:00:09 40. ENT-38 -418416462- The first guess in the incident channel was that someone changed the sort to use absolute values.
- The amounts are correct in the source system and on the report. Only the order is wrong. Movements are in whole yen.
- Nobody has changed this code this year.
Fix RankMovements. It returns "ACCOUNT YEN" for every account, largest movement first, with equal movements listing their accounts in ordinal order. A movement can be any int.
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 sealed class Movement
{
public Movement(string account, int yen)
{
Account = account;
Yen = yen;
}
public string Account { get; }
public int Yen { get; }
}
public class Solution
{
// Largest movement first. Equal movements list their accounts in ordinal order.
public string[] RankMovements(string[] accounts, int[] yen)
{
var movements = new List<Movement>(accounts.Length);
for (var i = 0; i < accounts.Length; i++)
{
movements.Add(new Movement(accounts[i], yen[i]));
}
movements.Sort((left, right) =>
{
var byYen = right.Yen - left.Yen;
return byYen != 0 ? byYen : string.CompareOrdinal(left.Account, right.Account);
});
return movements.Select(movement => $"{movement.Account} {movement.Yen}").ToArray();
}
}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
- Three Extra Ledger Entries Easy Free Big payout runs reach the bank with three rows too many, and always the same three. Find out why before the next run settles.
- 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?
- 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.