Skip to content
Katabench
Try free

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

What support sees

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.

Log excerpt

Text
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

What we know so far

  • 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.

Your job

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.

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

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