hash_table.state
Input
Hash table
Current look-up
Match / window
Stored entry
What is the Longest Zero-Sum Subarray
A subarray sums to 0 when two prefixes are equal — everything between them cancelled. To get the longest such span, store the first index of each prefix. When the prefix repeats at i, a candidate length is i − first[pref]. Seed 0 → −1 so a zero-sum prefix from the start is counted.
15, -2, 2, -8, 1, 7, 10, 23. Prefix 15 repeats after -2 2 -8 1 7, length 5.Interactive trace
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Each prefix | O(1) expected |
| Full scan | O(n) |
| Space | O(n) |
Python implementation