Reminders for Collected Parcels
Medium About 10 min Free, no account needed
Customers keep getting nightly reminders for parcels they collected days ago. Trace the failed pickup through the locker logs and fix it.
The brief
Reported by: Customer support
Impact: 38 tickets this week from customers who keep getting a nightly "your parcel is waiting" email after collecting it. The emails never stop on their own.
Code: PendingReminders in the parcel locker service
Every customer in the tickets really did collect their parcel, and the locker door sensor confirms each pickup. The reminder keeps arriving anyway, and it names the shelf the parcel was last on. Most customers never have this problem.
dock 08:12:40 scan parcel=4417
dock 08:12:44 scan parcel=5120
dock 08:12:51 scan parcel=6001
shelves 11:32:09 move parcel=4417 shelf=B2
lockers 14:51:33 pickup parcel=6001 door=4 customer=C-90114
lockers 14:51:33 waiting.remove parcel=6001 removed=True
lockers 16:05:11 pickup parcel=4417 door=17 customer=C-88213
lockers 16:05:11 waiting.remove parcel=4417 removed=False
notifier 02:00:03 reminder parcel=4417 shelf=B2
notifier 02:00:03 reminder parcel=5120 shelf=INTAKE- On-call suspects a race between the lockers and the notifier. The job replays the day's events in order on one thread.
- The lookup by id finds the right object. The reminder for 4417 shows shelf B2, which only that move could have set.
Fix PendingReminders so a collected parcel is never reminded about. Parcels still waiting get a reminder naming their current shelf, lowest ids first, up to 100 per run. A busy dock handles 60,000 parcels a day, so the job has to stay fast.
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 record Parcel(long Id)
{
public string Shelf { get; set; } = "INTAKE";
}
public class Solution
{
private const int MaxRemindersPerRun = 100;
// manifest: parcel ids scanned in at the dock. A parcel can be scanned more than once.
// events: "move <id> <shelf>" or "pickup <id>", oldest first, for parcels on the manifest.
// Returns "<id> <shelf>" for each reminder to send tonight, lowest ids first.
public string[] PendingReminders(long[] manifest, string[] events)
{
var parcels = new Dictionary<long, Parcel>();
var waiting = new HashSet<Parcel>();
foreach (var id in manifest)
{
if (parcels.ContainsKey(id))
{
continue;
}
var parcel = new Parcel(id);
parcels[id] = parcel;
waiting.Add(parcel);
}
foreach (var entry in events)
{
var parts = entry.Split(' ');
var parcel = parcels[long.Parse(parts[1])];
if (parts[0] == "move")
{
parcel.Shelf = parts[2];
}
else
{
waiting.Remove(parcel);
}
}
return waiting
.OrderBy(parcel => parcel.Id)
.Take(MaxRemindersPerRun)
.Select(parcel => $"{parcel.Id} {parcel.Shelf}")
.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
- 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.
- The Outage at 0% Errors Medium Free A route failed for 40 minutes while the dashboard showed 0% errors, so the alert never fired. Where did 412 failed requests go?
- 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?