Count Branch Pairs Whose Daily Cash Totals Hit a Target

Last updated: November 29, 2026

Quick Overview

Given a list of daily net cash totals for bank branches (integers, possibly negative or repeated), count the index pairs i < j whose totals sum exactly to a given target.

Infosys
Coding & Algorithms
Software Engineer
Infosys
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given a list of daily net cash totals for bank branches (integers, possibly negative or repeated), count the index pairs i < j whose totals sum exactly to a given target.

A short timed problem of the kind used to screen Systems Engineer candidates at Infosys, dressed as a treasury check that pairs branches whose surplus and deficit cancel out. The evaluator looks for moving from the quadratic brute force to a hash map and for correct counting when values repeat.

What the Interviewer Expects
  • State the brute force and its O(n^2) cost first
  • Use a frequency map built in one pass
  • Count pairs rather than return one pair
  • Handle duplicates and negative numbers correctly
  • Avoid counting an element with itself
Key Topics to Cover
hash map
frequency counting
two sum variant
duplicates handling
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 actual pairs instead of the count, and what is the output size worst case?
  • What changes if the list is already sorted and memory is tight?
  • How would you count triples that sum to the target?
  • If the totals arrive as a stream across days, how would you maintain the count incrementally?
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 totals of n integers and an integer target. Output: number of pairs (i, j) with i < j and totals[i] + totals[j] == target. Values can repeat and can be negative; n can be around $10^...

High-Level Design

Walk left to right keeping a count of every value seen so far. When we reach x, every earlier occurrence of target - x forms a valid pair with it, so add that count, then record x. Because we on...


Submit Your Answer
Markdown supported

Related Questions