Two Amounts of Tax on One Line
Easy About 6 min Free, no account needed
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 brief
Reported by: Customer success, then finance
Impact: A customer rejected invoice 88213 and is withholding payment. Finance has since found 214 of 9,300 invoices with the same problem, and the month-end close is blocked.
Code: RenderInvoice in the billing service
Invoice 88213
Consulting, 1 x 46.90 net 46.90 tax 2.35
TOTAL net 46.90 tax 2.34 due 49.24Their question was fair: how can a one-line invoice charge two different amounts of tax?
recon 02:14:07 invoice=88212 net=18.20 rate=5.00 line.tax=0.91 total.tax=0.91 ledger.tax=0.91 ok
recon 02:14:07 invoice=88213 net=46.90 rate=5.00 line.tax=2.35 total.tax=2.34 ledger.tax=2.35 DIFF
recon 02:14:07 invoice=88214 net=46.70 rate=5.00 line.tax=2.34 total.tax=2.34 ledger.tax=2.34 ok
recon 02:14:08 invoice=88216 net=46.89 rate=5.00 line.tax=2.34 total.tax=2.34 ledger.tax=2.34 ok
recon 02:14:08 invoice=88219 list=12.85 disc=50 line.net=6.43 total.net=6.42 ledger.net=6.43 DIFF
recon 02:14:09 credit=CN-311 net=-46.90 rate=5.00 line.tax=-2.35 total.tax=-2.34 ledger.tax=-2.35 DIFF- Customer success assumes the PDF template is stale and is showing old numbers.
- The general ledger is the system of record, so every printed amount has to match it.
- Nothing was deployed this month. The differences go back as far as finance has looked.
Fix RenderInvoice. Each row of lines is [quantity, unitPrice, discountPercent], and negative quantities are credit notes.
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;
using System.Collections.Generic;
using System.Globalization;
public class Solution
{
// lines: one row per invoice line, [quantity, unit price, discount percent].
// Returns the printed invoice: "LINE net tax" per line, then "TOTAL net tax due".
public string[] RenderInvoice(decimal[][] lines, decimal taxPercent)
{
var printed = new List<string>();
var net = 0m;
var tax = 0m;
foreach (var line in lines)
{
var rawNet = line[0] * line[1] * (100m - line[2]) / 100m;
var lineNet = Math.Round(rawNet, 2);
var rawTax = lineNet * taxPercent / 100m;
var lineTax = Math.Round(rawTax, 2);
printed.Add($"LINE {Money(rawNet)} {Money(rawTax)}");
net += lineNet;
tax += lineTax;
}
printed.Add($"TOTAL {Money(net)} {Money(tax)} {Money(net + tax)}");
return printed.ToArray();
}
private static string Money(decimal amount) => amount.ToString("F2", CultureInfo.InvariantCulture);
}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.
Related guides
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.
- 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.