Count Order Windows Whose Daily Totals Hit a Revenue Target

Last updated: November 29, 2026

Quick Overview

Given daily order revenue for a seller (values may be negative due to refunds) and a target T, count the contiguous ranges of days whose total revenue equals exactly T.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given daily order revenue for a seller (values may be negative due to refunds) and a target T, count the contiguous ranges of days whose total revenue equals exactly T.

A prefix-sum plus hash map problem in the style of Amazon seller analytics screens. Because refunds make values negative, a sliding window does not work, and the interviewer wants to see you notice that and switch to counting prefix sums.

What the Interviewer Expects
  • Explain why two pointers fail with negative values
  • Use running prefix sums with a count map
  • Seed the map with prefix 0 seen once
  • Count before inserting the current prefix
  • Trace a case with zeros and negatives
Key Topics to Cover
Prefix Sums
Hash Map
Subarrays
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
  • How would you return the longest such range instead of the count?
  • What if you need ranges whose total is at least T?
  • How would you do this over a stream of days with bounded memory?
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

Revenue is a list of ints (can be negative or zero), possibly empty. T is an int. Return the number of (i, j) with i <= j such that sum(revenue[i..j]) == T....

High-Level Design

Let P be the prefix sum up to and including day j. A range ending at j sums to T exactly when some earlier prefix equals P - T. So keep a dictionary from prefix value to how many times it has occurred...


Submit Your Answer
Markdown supported