Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Subarray Sum Equals K

Python data engineering interview problem. Difficulty: intermediate. Pattern: Hashing. About 14 minutes. Part of the Pro drill bank.

Count subarrays whose sum equals k. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement subarray_sum(nums: list, k: int) -> int.

Constraints

  • Nums may include negatives and zeros.
  • Count all contiguous subarrays.

Examples

Input: subarray_sum([1, 1, 1], 2) Output: 2 Two windows of sum 2.

Topics: lakebench, python, prefix sum, hash map.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Subarray Sum Equals K

Interview-style drill: Count subarrays whose sum equals k.

Implement `subarray_sum(nums: list, k: int) -> int`. Example: `[1, 1, 1], 2` → `2`. Keep the harness.