Delete Every Consecutive Run of Balance Changes Summing to Zero

Last updated: November 29, 2026

Quick Overview

A linked list holds account balance deltas. Repeatedly delete consecutive sequences of nodes whose sum is 0 until none remain, and return the head of the final list.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A linked list holds account balance deltas. Repeatedly delete consecutive sequences of nodes whose sum is 0 until none remain, and return the head of the final list.

A Meta technical-screen question that blends prefix sums with linked list surgery, framed around collapsing self-cancelling adjustments in a ledger (for example a charge followed by a full refund). The interviewer looks for the two-pass prefix-sum map approach and a clear argument that it removes everything in one go.

What the Interviewer Expects
  • Relate zero-sum runs to equal prefix sums
  • Use a dummy node with prefix sum 0
  • First pass: map each prefix sum to the last node with that sum
  • Second pass: link each node to the successor of the last node with the same sum
  • Argue why one pair of passes reaches the fixed point
Key Topics to Cover
Linked list
Prefix sum
Hash map
Dummy 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 how many nodes were deleted.
  • Only delete runs of length at most k.
  • Do it on an array in place.
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 in [-1000, 1000], length up to 1000 or more. Output: the head of a list with no consecutive zero-sum run. Any valid final answer is accepted in the classic version,...

High-Level Design

If the prefix sum after node u equals the prefix sum after a later node v, the nodes strictly after u through v sum to zero. Put a dummy node with value 0 in front so the prefix sum before the head is...


Submit Your Answer
Markdown supported