Count Stretches of Daily Earnings That Hit a Weekly Goal
Last updated: November 29, 2026
Quick Overview
Given a driver's daily net earnings (which can be negative after fees or adjustments) and a target amount, count the contiguous stretches of days whose total equals the target exactly.
Uber
November 29, 20260
0
0 solved
Given a driver's daily net earnings (which can be negative after fees or adjustments) and a target amount, count the contiguous stretches of days whose total equals the target exactly.
An Uber onsite coding round favors problems where the obvious sliding window breaks; here negative adjustments (refunds, fee clawbacks) rule out two pointers. The interviewer is checking that you notice that and switch to prefix sums with a hash map.
What the Interviewer Expects
- Explain why a sliding window fails with negative values
- Define prefix sums and the difference identity
- Seed the map with prefix 0 occurring once
- Count, not just detect, matching subarrays
- Analyze time and space
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 longest such stretch instead of the count
- How would you count stretches whose total is at least the target?
- What if the data is a stream of days and you need the running count?
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
earnings(cents, may be negative), integertarget. - Output: number of pairs
(i, j)withi <= jandsum(earnings[i..j]) == target. - Negative values are allowed, so...
High-Level Design
Let P[j] be the sum of the first j days. A stretch i..j sums to target exactly when P[j+1] - P[i] == target, that is, when an earlier prefix equals P[j+1] - target. Walk the array maintain...