Shortest Run of Days Reaching Net Revenue K Despite Refunds
Last updated: November 29, 2026
Quick Overview
Daily net revenue for a seller can be negative because of refunds. Given the daily values and a target K > 0, return the length of the shortest contiguous run of days whose total is at least K, or -1 if none exists.
Amazon
November 29, 20260
0
0 solved
Daily net revenue for a seller can be negative because of refunds. Given the daily values and a target K > 0, return the length of the shortest contiguous run of days whose total is at least K, or -1 if none exists.
An Amazon onsite hard question that deliberately breaks the two-pointer sliding window by allowing negatives. The interviewer wants prefix sums with a monotonic deque and the two pruning rules explained, not just recited.
What the Interviewer Expects
- Explains why a sliding window fails with negative values
- Uses prefix sums P with target P[j] - P[i] >= K
- Pops from the front when a valid run is found
- Pops from the back when a new prefix is smaller or equal
- Achieves O(n)
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
- Return the start and end days of the run.
- Shortest run with total at most K instead.
- Longest run with total at least K.
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 of integers (can be negative), length up to 10^5, and K >= 1. Output: min j - i over pairs i < j with P[j] - P[i] >= K, where P is the prefix sum array, or -1....
High-Level Design
With negatives, shrinking a window from the left can increase the sum, so two pointers is unsound. Instead keep a deque of prefix indices with increasing prefix values. For each j: while the front i s...