Minimum Cost to Batch Consecutive Orders Into Delivery Runs

Last updated: November 29, 2026

Quick Overview

Orders from one store become ready in sequence, each with a drop-off distance. Group them into contiguous runs of at most cap orders, where a run departs when its last order is ready and costs a fixed amount, plus twice its farthest distance, plus a penalty per minute each order waits; minimize the total cost and return the runs.

DoorDash
Coding & Algorithms
Software Engineer
DoorDash
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Orders from one store become ready in sequence, each with a drop-off distance. Group them into contiguous runs of at most cap orders, where a run departs when its last order is ready and costs a fixed amount, plus twice its farthest distance, plus a penalty per minute each order waits; minimize the total cost and return the runs.

Batching (one Dasher carrying several orders) is a core DoorDash efficiency lever, traded against food sitting on the counter. This partition DP checks that you define the state over prefixes, compute each block's cost incrementally, and can reconstruct the decision.

What the Interviewer Expects
  • Define dp[i] as the best cost for the first i orders
  • Enumerate the last block length up to cap
  • Maintain the running max distance and use prefix sums for waiting time
  • Reconstruct the chosen blocks with a cut array
  • State O(n * cap) and discuss larger cap values
Key Topics to Cover
dynamic programming
partition DP
prefix sums
batching
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you drop the contiguity requirement, letting runs pick any orders?
  • How would you add a hard limit that no order waits more than 10 minutes?
  • How would you speed this up if cap equals n?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: ready (non-decreasing), dist, cap, fixed, wait_penalty.
  • A run is a contiguous block j..i-1 of at most cap orders, departing at ready[i-1].
  • Cost: `fixed + 2 * max(dist[j..i...
High-Level Design

Let dp[0] = 0 and dp[i] be the best cost to dispatch orders 0..i-1. For each i, try every last block start j from i-1 down to i-cap. Walking j downward lets us update the block's farth...


Submit Your Answer
Markdown supported