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

0

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
prefix sums
hash map
subarray sum equals k
negative numbers
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
  • 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 Problems
Sample Answer
Requirements
  • Input: list of integers earnings (cents, may be negative), integer target.
  • Output: number of pairs (i, j) with i <= j and sum(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...


Submit Your Answer
Markdown supported