Remove Every Consecutive Run of Ledger Entries That Nets to Zero
Last updated: November 29, 2026
Quick Overview
A Google Pay ledger is a singly linked list of signed integer amounts. Repeatedly delete consecutive sequences of nodes that sum to zero until none remain, and return the head of the result.
0
0
0 solved
A Google Pay ledger is a singly linked list of signed integer amounts. Repeatedly delete consecutive sequences of nodes that sum to zero until none remain, and return the head of the result.
A Google onsite question that rewards seeing the prefix-sum structure behind the list. The interviewer wants a two-pass hash map solution and a convincing argument that one round of deletions is enough.
What the Interviewer Expects
- Relate zero-sum runs to repeated prefix sums
- Use a dummy node with prefix 0 so runs starting at the head are found
- Record the last node for each prefix sum in pass one
- In pass two, link each node to the successor of the last node with the same prefix
- Explain why the result has no remaining zero-sum run
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return which original nodes were removed.
- Remove runs whose sum is a multiple of m.
- Do it in a single pass, and is it worth it?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: head of a list of integers (may include 0 and negatives).
- Output: a list with no consecutive run summing to 0.
- O(n) time....
High-Level Design
If the prefix sum after node i equals the prefix sum after node j (i < j), then nodes i+1..j sum to zero. Start with a dummy node of value 0 so the empty prefix is 0. Pass one walks the list and store...