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
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
dynamic programming
monotonic deque
sliding window maximum
subsequence
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
  • 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 Problems
Sample 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...


Submit Your Answer
Markdown supported