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

0

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
Monotonic deque
Prefix sums
Sliding window
Arrays
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
  • 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 Problems
Sample Answer
Requirements

Input: list of ints (may be negative), integer K. Output: minimum length or -1. n up to 10510^5, so O(n2)O(n^2) 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, ...


Submit Your Answer
Markdown supported