Best Reward Path Through Delivery Stops With a Maximum Skip Distance
Last updated: November 29, 2026
Quick Overview
A driver may serve a subsequence of stops along a route; stop i has reward r[i] (possibly negative because of tolls or delays). Consecutive chosen stops must be at most k positions apart, and at least one stop must be chosen. Return the maximum total reward.
Amazon
November 29, 20260
0
0 solved
A driver may serve a subsequence of stops along a route; stop i has reward r[i] (possibly negative because of tolls or delays). Consecutive chosen stops must be at most k positions apart, and at least one stop must be chosen. Return the maximum total reward.
An Amazon onsite hard question combining DP with a sliding window maximum. The interviewer wants the O(n * k) DP first, then the observation that each state needs the max over the last k states, which a monotonic deque supplies in O(1) amortized.
What the Interviewer Expects
- Defines dp[i] as the best sum of a valid chain ending at i
- Writes dp[i] = r[i] + max(0, max(dp[i-k..i-1]))
- Maintains a decreasing deque of dp indices
- Evicts indices that fall out of the window
- Returns max(dp), not dp[-1]
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- The driver must start at stop 0 and end at stop n - 1.
- Return which stops are chosen.
- What if the skip limit is in miles rather than positions?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list r of integers (length up to 10^5) and 1 <= k <= n. Output: max sum of a non-empty subsequence where adjacent chosen indices differ by at most k....
High-Level Design
Let dp[i] be the best total of a valid chain that ends by choosing stop i. The previous chosen stop is one of i - k to i - 1, or there is none, so dp[i] = r[i] + max(0, max of dp over that window). Th...