Count Time Windows Where Net Balance Change Equals a Target

Last updated: November 29, 2026

Quick Overview

Given an array of daily net balance changes (which may be negative) and a target k, count the contiguous ranges of days whose changes sum to exactly k.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given an array of daily net balance changes (which may be negative) and a target k, count the contiguous ranges of days whose changes sum to exactly k.

Framed around Google Pay ledger analysis, this tests prefix sums combined with a hash map. Negative values are the key twist: interviewers want to see you explain why a sliding window fails and why the prefix count map works.

What the Interviewer Expects
  • Explains that negatives break the two-pointer window approach
  • Uses prefix sums with a count of previously seen prefixes
  • Seeds the map with prefix 0 seen once
  • Updates the map after counting to avoid zero-length ranges
  • Gives O(n) time and O(n) space
Key Topics to Cover
Prefix Sums
Hash Map
Arrays
Counting
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 range instead of the count.
  • Count ranges whose sum is divisible by k.
  • The data is a 2D grid of regions by days; count submatrices summing to 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: integer array nums (positive, negative, or zero), integer k.
  • Output: number of pairs (i, j), i at most j, with sum(nums[i..j]) == k....
High-Level Design

Let P[j] be the sum of the first j elements. A range (i, j] sums to k exactly when P[j] - P[i] == k, so for each new prefix P we need the number of earlier prefixes equal to P - k. Walk the arra...


Submit Your Answer
Markdown supported