Two-account transfer matching
Python data engineering interview problem. Difficulty: intermediate. Pattern: Hashing. About 15 minutes. Part of the Pro drill bank.
Two ledger lines should pair when their amounts add to a target transfer size. Accounting wants those two indices (or values) if they exist.
A reconciliation tool looks for two account entries that cancel each other out: one amount and its negative, such as +50 and -50. Find two entries in amounts that add up to zero. Return their positions as [i, j] with i < j. If there is no such pair, return []. Return the first pair you find when scanning from left to right. Write complementary_pair(amounts). In [1, -1, 2] the values 1 and -1 cancel, at positions 0 and 1, so the answer is [0, 1]. In [1, 2, 3] nothing cancels, so the answer is []. The starter file already has the function name and its print lines. Keep them and fill in the body.
Requirements
- Find two different positions whose values add to 0.
- Return [i, j] with the smaller position first.
- Scan left to right and return the first pair found.
- Return [] if there is no pair.
Constraints
- amounts is a list of numbers.
- A single entry cannot pair with itself.
Examples
Input: complementary_pair([1, -1, 2]) Output: [0, 1] This input follows the stated rules and produces this output.
Topics: lakebench, python, hash-based-problems.
More Python interview questions · All interview problems · Learn data engineering
Two-account transfer matching
Interview-style drill: Given a list of transfer amounts, find a pair that sums to exactly zero net movement.
Find two amounts that add to the target transfer (two-sum on the list).