Find All Distinct Triples of Ledger Entries That Net to Zero

Last updated: November 29, 2026

Quick Overview

Given a list of integer ledger adjustments, return every distinct triple of values (not indices) that sums to zero, with no duplicate triples in the output.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a list of integer ledger adjustments, return every distinct triple of values (not indices) that sums to zero, with no duplicate triples in the output.

Asked in Google onsite coding rounds in the spirit of reconciling Google Pay ledger adjustments. The interviewer checks that you sort and use two pointers rather than a cubic scan, and especially that you skip duplicates correctly without a set of tuples as a crutch.

What the Interviewer Expects
  • Sorts the array and fixes the smallest element
  • Uses two pointers for the remaining pair
  • Skips duplicate values at all three positions
  • Explains why sorting enables early termination
  • States O(n^2) time and justifies it as optimal in practice
Key Topics to Cover
Two Pointers
Sorting
Deduplication
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
  • Count triples instead of listing them; can you do better?
  • Find the triple whose sum is closest to a target.
  • Generalize to k-sum; what is the recursion and complexity?
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 adj, may contain duplicates.
  • Output: list of triples [a, b, c] with a <= b <= c and a + b + c == 0; each distinct triple appears once.
  • n up to a few thousand, so ...
High-Level Design

Sort the list. For each index i as the smallest element, solve two-sum on the suffix with target -adj[i] using pointers l = i+1 and r = n-1. If the sum is too small, move l right; too large, m...


Submit Your Answer
Markdown supported