Shortest Run of Trips Whose Net Earnings Reach a Target
Last updated: November 29, 2026
Quick Overview
A driver's per-trip net earnings can be negative (fuel, tolls, or a cancelled pickup after a long drive). Given the list and a target K, return the length of the shortest contiguous run of trips whose net earnings sum to at least K, or -1 if none exists.
Uber
November 29, 20260
0
0 solved
A driver's per-trip net earnings can be negative (fuel, tolls, or a cancelled pickup after a long drive). Given the list and a target K, return the length of the shortest contiguous run of trips whose net earnings sum to at least K, or -1 if none exists.
A Hard onsite problem in the style of Uber's driver earnings tooling. The usual two-pointer window fails because of negative values, and the interviewer wants the monotonic deque over prefix sums and an explanation of both pop rules.
What the Interviewer Expects
- Explains why a plain sliding window breaks with negatives
- Builds prefix sums
- Keeps a deque of indices with increasing prefix sums
- Pops from the front while a valid run is found
- Pops from the back when a smaller prefix makes older entries useless
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
- What if all earnings are non-negative, can you simplify?
- How would you return the start and end trip of the run?
- How would you answer this for many different targets 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 ints (may be negative), integer K. Output: minimum length or -1. n up to , so over all pairs of prefix sums is too slow....
High-Level Design
Let P[i] be the sum of the first i trips. We want the smallest j - i with P[j] - P[i] >= K. Scan j from 0 to n with a deque of candidate start indices. Front rule: while P[j] - P[front] >= K, ...