hash_table.state
Input
Hash table
Current look-up
Match / window
Stored entry
What is Longest Subarray with Sum K
Same prefix identity as “count subarrays”, but we want the longest span. Store the first index where each prefix appeared. If pref − k was seen at index j, the window (j+1 … i) is a candidate of length i − j.
0 → -1 so a prefix equal to k starts at index 0.10, 5, 2, 7, 1, 9, k = 15. Prefixes reveal [5,2,7,1] of length 4.Interactive trace
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Each prefix | O(1) expected |
| Full scan | O(n) |
| Space | O(n) |
Python implementation