Dasher Payout Calculation

Dated Nov 19, 2026; last modified on Thu, 19 Nov 2026

Problem Statement

Design an API that calculates total payout for a driver based on their delivery events. Each delivery event has a status that’s one of accepted, delivered, or cancelled. The payout for completed deliveries is:

payout = base_rate * (1 + bonus_factor * concurrent_count)

… where concurrent_count is determined at the time of accepting the delivery request.

Solution

Parse, Don’t Validate

passes delivery events of the form:

{id: "D1", status: "accepted", time: "10:00"}

… and so parse them into appropriate types:

DeliveryEvent.cs
namespace DasherPayout;

internal enum DeliveryStatus { Accepted, Delivered, Cancelled }

internal record struct DeliveryEvent(string Id, DeliveryStatus Status, TimeOnly Time);
Converters.cs
using System.Globalization;

namespace DasherPayout;

internal static class Converters
{
    extension(External.DeliveryEvent deliveryEvent)
    {
        internal DeliveryEvent ToInternal()
        {
            DeliveryStatus status = Enum.Parse<DeliveryStatus>(deliveryEvent.Status, ignoreCase: true);
            if (!Enum.IsDefined(status))
            {
                throw new ArgumentException($"{status} is not a recognized event status");
            }

            var time = TimeOnly.Parse(deliveryEvent.Time, CultureInfo.InvariantCulture);
            return new(deliveryEvent.Id, status, time);
        }
    }
}

… where enum DeliveryStatus affords us stricter typing on the possible statuses, and TimeOnly Time gets us a sorting key, e.g., “8:00” is less than “10:00”.

The canonical input is to have a timestamp based on a pre-agreed epoch. Sending timestamps as strings is a weird choice on ’s part.

Using a double for money introduces rounding errors that are not appropriate for financial calculations, which require large numbers and no round-off errors. .NET’s Decimal fills this gap, supporting values in the range \([-(2^{96} - 1), 2^{96} - 1]\):

Console.WriteLine(0.1  + 0.2  == 0.3);  // False: double
Console.WriteLine(0.1m + 0.2m == 0.3m); // True: decimal

Find practice problems that demand an understanding of rounding off errors to further cement this.

Payout Calculation

I initially “reduced” this problem to a harder problem where I parsed the delivery events into a sequence of intervals:

private record struct Delivery(
  string Id, TimeOnly Start, TimeOnly End, DeliveryTerminalStatus Status);

… and then proceeded to sweep through the intervals in order, and extract concurrent counts.

Sweeping through intervals. Credits: GPT-6.1 Sol.

Sweeping through intervals. Credits: GPT-6.1 Sol.

However, the sweep ends up re-creating the original events, e.g., Delivery.Start is the original Delivery.Accepted event! Processing the DeliveryEvents in increasing Time order gets us there:

The failure mode here was having just enough information to trip myself up. I vaguely remembered some computational geometry algorithms from college, and rushed to assume that this was an instance of it.

The algorithm involved two instances of the Delivery lists, one sorted by Delivery.Start and the other sorted by Delivery.End. We then advanced a pointer in each list, while computing the overlapping intervals along the way.

Define the simultaneity contract when processing a sequence of events w.r.t. time. For example, define that a delivery is active over [accepted, closed). That way, if D2 ends at 11:00 and D3 starts at 11:00, then D3 payout will not be affected by when we processed D2’s end event.

using ExhaustiveMatching;

namespace DasherPayout;

public class DasherPayout
{
    public static decimal CalculatePayout(IEnumerable<External.DeliveryEvent> externalEvents, decimal baseRate, decimal bonusFactor) =>
        CalculatePayout(externalEvents.Select(e => e.ToInternal()), baseRate, bonusFactor);

    private static decimal CalculatePayout(IEnumerable<DeliveryEvent> events, decimal baseRate, decimal bonusFactor)
    {
        // Can't assume that events come in order. In a real system, network delays
        // might cause events to come out of order. Someone, e.g., our events store,
        // needs to guarantee that we're processing events in their correct order.
        IOrderedEnumerable<IGrouping<TimeOnly, DeliveryEvent>> sortedEvents =
            events.GroupBy(ev => ev.Time).OrderBy(g => g.Key);

        var concurrentCount = 0;
        Dictionary<string, int> multiplierByDeliveryId = [];
        decimal runningPayout = 0;

        HashSet<string> deliveryIdsPendingClosure = [];

        foreach (IGrouping<TimeOnly, DeliveryEvent> group in sortedEvents)
        {
            DeliveryEvent[] simultaneousEvents = [.. group];

            // Define `concurrentCount`'s delta independent of the original order in `events`.
            var newCount = group.Count(ev => ev.Status == DeliveryStatus.Accepted);
            concurrentCount += newCount;
            concurrentCount -= simultaneousEvents.Length - newCount;

            foreach (DeliveryEvent @event in simultaneousEvents)
            {
                switch (@event.Status)
                {
                    case DeliveryStatus.Accepted:
                        {
                            var added = deliveryIdsPendingClosure.Add(@event.Id);
                            if (!added)
                            {
                                throw new ArgumentException($"Multiple Accepted events for delivery {@event.Id}");
                            }

                            multiplierByDeliveryId[@event.Id] = concurrentCount;
                            break;
                        }

                    case DeliveryStatus.Delivered:
                        {
                            var removed = deliveryIdsPendingClosure.Remove(@event.Id);
                            if (!removed)
                            {
                                throw new ArgumentException($"Delivery {@event.Id} delivered w/o being accepted first.");
                            }

                            runningPayout += baseRate * (1 + (bonusFactor * multiplierByDeliveryId[@event.Id]));

                            break;
                        }

                    case DeliveryStatus.Cancelled:
                        {
                            var removed = deliveryIdsPendingClosure.Remove(@event.Id);
                            if (!removed)
                            {
                                throw new ArgumentException($"Delivery {@event.Id} cancelled w/o being accepted first.");
                            }

                            break;
                        }

                    default:
                        {
                            throw ExhaustiveMatch.Failed(@event.Status);
                        }
                }
            }
        }

        return runningPayout;
    }
}

References

  1. Design Dasher Payout Calculation System Based on Delivery Events | Hello Interview. www.hellointerview.com . Accessed Nov 19, 2026.
  2. blog/content/computer-science/low-level-design/exercises/dasher-payout/DasherPayout/DasherPayout.cs at b0a598c99f9611ca66fad95b53e6b2c25217caba · dchege711/blog. github.com . Accessed Nov 22, 2026.
  3. Decimal Struct (System) | Microsoft Learn. learn.microsoft.com . Accessed Nov 22, 2026.