PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates array manipulation, cumulative-sum reasoning, and algorithmic complexity analysis for counting contiguous subarrays that equal a target value.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Count subarrays equal to target

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given an integer array nums and an integer k, return the number of contiguous subarrays whose sum equals k. Provide an algorithm better than O(n^ 2), explain time and space complexity, and describe how your approach handles negative numbers, zeros, very large arrays, and integer overflow concerns.

Quick Answer: This question evaluates array manipulation, cumulative-sum reasoning, and algorithmic complexity analysis for counting contiguous subarrays that equal a target value.

Given an integer array `nums` and an integer `k`, return the number of contiguous (non-empty) subarrays whose elements sum to exactly `k`. A subarray is a contiguous slice of the array. Two subarrays at different start/end positions are counted separately even if their contents are identical. Your algorithm must run better than O(n^2). The intended solution uses prefix sums and a hash map: keep a running prefix sum `s` while scanning left to right, and for each position add the number of earlier prefix sums equal to `s - k` (initializing the count of prefix sum 0 to 1 so that subarrays starting at index 0 are counted). This is O(n) time and O(n) space. The approach naturally handles negative numbers and zeros (a sliding window would NOT, because the running sum is not monotonic). For very large arrays it stays linear, and the running/prefix sums should use a 64-bit integer type to avoid overflow when many large values accumulate. Example 1: nums = [1, 1, 1], k = 2 -> 2 (the subarrays [1,1] at indices 0-1 and 1-2). Example 2: nums = [1, 2, 3], k = 3 -> 2 (the subarrays [1,2] and [3]). Example 3: nums = [0, 0, 0], k = 0 -> 6 (every one of the C(3,2)+3 = 6 contiguous slices sums to 0).

Constraints

  • 1 <= nums.length <= 2 * 10^4 (an empty array yields 0)
  • -1000 <= nums[i] <= 1000
  • -10^7 <= k <= 10^7
  • Prefix sums can reach magnitudes around 2 * 10^7, well within 32-bit range, but use a 64-bit accumulator if element bounds are widened

Examples

Input: ([1, 1, 1], 2)

Expected Output: 2

Explanation: The subarrays [1,1] at indices 0-1 and at indices 1-2 each sum to 2.

Input: ([1, 2, 3], 3)

Expected Output: 2

Explanation: [1,2] sums to 3 and the single element [3] sums to 3.

Input: ([1, -1, 0], 0)

Expected Output: 3

Explanation: [1,-1], [1,-1,0], and the lone [0] all sum to 0 — negatives and zeros are handled by the prefix-sum map.

Input: ([], 0)

Expected Output: 0

Explanation: Empty array has no subarrays, so the answer is 0.

Input: ([5], 5)

Expected Output: 1

Explanation: Single-element array where that element equals k.

Input: ([3, 4, 7, 2, -3, 1, 4, 2], 7)

Expected Output: 4

Explanation: [3,4], [7], [7,2,-3,1], and [1,4,2] each sum to 7, demonstrating overlapping matches with negatives.

Input: ([-1, -1, 1], 0)

Expected Output: 1

Explanation: Only [-1,1] (indices 1-2) sums to 0; all-negative prefixes are still tracked correctly.

Input: ([0, 0, 0], 0)

Expected Output: 6

Explanation: Every contiguous slice sums to 0: three single zeros, two length-2 slices, and one length-3 slice = 6. Repeated zero prefix sums in the map drive the count up.

Hints

  1. A sliding window does not work here because negative numbers and zeros make the running sum non-monotonic — shrinking the window can both decrease and increase the sum.
  2. Define prefix[i] = nums[0] + ... + nums[i-1]. A subarray (i, j] sums to k exactly when prefix[j] - prefix[i] = k, i.e. prefix[i] = prefix[j] - k.
  3. Scan left to right keeping a running prefix sum and a hash map from prefix-sum value to how many times it has occurred. For each position add map[running_sum - k] to the answer, then record the current running_sum.
  4. Seed the map with {0: 1} before the loop so that subarrays starting at index 0 (where prefix[i] = 0) are counted.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)