Subarray Sum Equals K (LeetCode 560): count contiguous subarrays whose sum equals k; must handle zeros and negative values.
Asked in the Round 1, 45-minute coding stage. The interviewer hinted to use a hash map, with a follow-up to optimize beyond storing all subarray sums.
Implement subarray_sum(nums, k). nums is a list of integers and k is an integer. Return the integer count of contiguous, non-empty subarrays whose elements sum to k.
def subarray_sum(nums, k):