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.

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

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
linked list
prefix sums
hash map
sentinel node
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported