103. Dasher Payout Logic - Simple

Dasher Payout Logic - Simple
As part of migrating away from a monolith to a micro-service architecture your team has been tasked with building out a new Payments Service. Your project is to build out the Dasher payout logic. As part of your work you will get the relevant data in your constructor.

The service should support calculating payout for a Dasher for a given day from the sequence of delivery activities returned by the upstream dependency. The first version of the payment model is naively based on how much time the Dasher spends fulfilling each order.

Payment rules:
  • Base pay rate is 0.3 dollars per minute.
  • A dasher can make multiple deliveries parallelly.
  • Multi-order pay rate is fulfilled deliveries * 0.3 dollars per minute.
  • Payment is made only for orders that are fulfilled.

Input Encoding

The upstream delivery data is provided as a List<String>. Each string is of the form:

dasherId,deliveryId,timestamp,status

Example:
1,10,2023-03-31 18:15:00,ACCEPTED

Methods

Constructor

DasherPayoutService(List<String> deliveryActivities)
  • deliveryActivities contains all delivery activity records returned by the upstream dependency for one day.
  • Each record is encoded as dasherId,deliveryId,timestamp,status.
  • status is either ACCEPTED, FULFILLED, or CANCELLED.

Get Payout

double getPayout(int dasherId)
  • This method represents the POST payout endpoint.
  • Input is dasherId.
  • Output is the dollar amount the Dasher gets paid for that day.
  • Only deliveries whose final status is FULFILLED contribute to payout.
  • For each time interval between two consecutive activity timestamps for that Dasher, payout for that interval is:
    minutesInInterval * 0.3 * numberOfOngoingFulfilledDeliveries
  • Ignore seconds entirely and subtract only the minute fields.
    i.e. time between 4 min 16 sec and 6 min 12 seconds is simply 6-4 = 2 minutes.
  • If the Dasher has no fulfilled deliveries, return 0.0.

Rules

  • A delivery starts when its ACCEPTED record appears.
  • A delivery ends when its terminal record appears, which is either FULFILLED or CANCELLED.
  • A cancelled delivery adds 0 payout.
  • Only ongoing deliveries that eventually end in FULFILLED are counted in the multiplier for each interval.
  • Time difference is measured in whole minutes between consecutive timestamps.
  • Process records for each Dasher in timestamp order.

Constraints

  • 1 ≤ deliveryActivities.size() ≤ 10^5
  • 1 ≤ dasherId ≤ 10^4
  • 1 ≤ deliveryId ≤ 10^6
  • Each record string is non-empty and has exactly 4 comma-separated parts.
  • timestamp format is yyyy-MM-dd HH:mm:ss.
  • For a given deliveryId, there is exactly one ACCEPTED record and exactly one terminal record.
  • The terminal record for a delivery is either FULFILLED or CANCELLED.
  • For a given Dasher, records should be evaluated in non-decreasing timestamp order.

Examples

Example 1

DasherPayoutService( deliveryActivities = [ "1,1,2023-03-31 18:15:00,ACCEPTED", "1,2,2023-03-31 18:18:00,ACCEPTED", "1,1,2023-03-31 18:36:00,FULFILLED", "1,2,2023-03-31 18:45:00,FULFILLED" ] )

getPayout(dasherId = 1) = 14.4

Explanation:
  • 18:15 to 18:18: 1 ongoing fulfilled delivery → 3 * 0.3 * 1 = 0.9
  • 18:18 to 18:36: 2 ongoing fulfilled deliveries → 18 * 0.3 * 2 = 10.8
  • 18:36 to 18:45: 1 ongoing fulfilled delivery → 9 * 0.3 * 1 = 2.7
  • Total payout = 0.9 + 10.8 + 2.7 = 14.4

Example 2

DasherPayoutService( deliveryActivities = [ "1,1,2023-03-31 18:15:00,ACCEPTED", "1,2,2023-03-31 18:18:00,ACCEPTED", "1,1,2023-03-31 18:36:00,FULFILLED", "1,2,2023-03-31 18:45:00,CANCELLED" ] )

getPayout(dasherId = 1) = 6.3

Explanation:
  • Delivery 2 is cancelled, so it does not contribute to payout.
  • 18:15 to 18:18: 1 ongoing fulfilled delivery → 3 * 0.3 * 1 = 0.9
  • 18:18 to 18:36: only delivery 1 is payable → 18 * 0.3 * 1 = 5.4
  • 18:36 to 18:45: no ongoing fulfilled delivery → 0
  • Total payout = 0.9 + 5.4 = 6.3

Example 3

DasherPayoutService( deliveryActivities = [ "5,10,2023-03-31 09:00:00,ACCEPTED", "5,10,2023-03-31 09:12:00,CANCELLED" ] )

getPayout(dasherId = 5) = 0.0

Explanation:
  • The only delivery is cancelled.
  • Payment is made only for orders that are fulfilled.




Please use Laptop/Desktop or any other large screen to add/edit code.