Longest Order Stretch With Prep Time Spread Within Tolerance

Last updated: November 29, 2026

Quick Overview

Given the prep times of a store's consecutive orders, find the longest contiguous stretch in which the difference between the maximum and minimum prep time is at most a tolerance, returning its start and end indices.

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

0

0

0 solved


Given the prep times of a store's consecutive orders, find the longest contiguous stretch in which the difference between the maximum and minimum prep time is at most a tolerance, returning its start and end indices.

Stable prep times make Dasher dispatch timing predictable, so DoorDash cares about how consistent a merchant is. This question tests monotonic deques for sliding window max and min, a step beyond simple sliding windows, and the interviewer expects an O(n) argument.

What the Interviewer Expects
  • Explain why a single running max or min fails when the window shrinks
  • Maintain a decreasing deque for max and an increasing deque for min
  • Shrink from the left while the spread exceeds the tolerance
  • Pop stale indices from the deque fronts
  • Prove amortized O(n)
Key Topics to Cover
monotonic queue
sliding window
deque
amortized analysis
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 answer this for every store in a stream of orders?
  • What if the tolerance is a percentage of the median rather than an absolute value?
  • How would you find the number of windows that satisfy the condition?
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: list of prep minutes and tolerance tol >= 0.
  • Find the longest window with max - min <= tol. Ties: earliest.
  • Output: (start, end) inclusive, or None for empty input....
High-Level Design

Keep maxq holding indices with decreasing values (front is the window max) and minq with increasing values (front is the window min). When adding right, pop from the back any index whose value i...


Submit Your Answer
Markdown supported