PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's competency in array algorithms and reasoning about contiguous subarray sums, including correct handling of negative and zero values.

  • hard
  • Apple
  • Coding & Algorithms
  • Software Engineer

Count subarrays with sum equals k

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Given an integer array `nums` (may contain negative numbers and zeros) and an integer `k`, return the **number of contiguous subarrays** whose sum is **exactly** `k`. ### Requirements - The subarray must be contiguous. - Your algorithm should handle negative values correctly. ### Input / Output - **Input:** `nums: int[]`, `k: int` - **Output:** `count: int` (number of contiguous subarrays with sum `k`) ### Example - `nums = [1, 2, 3], k = 3` → output `2` because subarrays `[1,2]` and `[3]` sum to 3.

Quick Answer: This question evaluates a candidate's competency in array algorithms and reasoning about contiguous subarray sums, including correct handling of negative and zero values.

Given an integer array nums that may contain positive numbers, negative numbers, and zeros, and an integer k, return the number of non-empty contiguous subarrays whose sum is exactly k. A subarray must use consecutive elements from the original array.

Constraints

  • 0 <= nums.length <= 20000
  • -1000 <= nums[i] <= 1000
  • -10000000 <= k <= 10000000
  • Subarrays must be non-empty and contiguous
  • nums may contain negative numbers and zeros

Examples

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

Expected Output: 2

Explanation: The subarrays [1, 2] and [3] both sum to 3.

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

Expected Output: 3

Explanation: The subarrays [1, -1], [1, -1, 0], and [0] sum to 0.

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

Expected Output: 6

Explanation: All non-empty contiguous subarrays sum to 0. There are 3 single-element, 2 two-element, and 1 three-element subarrays.

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

Expected Output: 4

Explanation: The qualifying subarrays are [3, 4], [7], [7, 2, -3, 1], and [1, 4, 2].

Input: ([], 0)

Expected Output: 0

Explanation: There are no non-empty subarrays in an empty array.

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

Expected Output: 2

Explanation: The subarrays [-1, 2] and [1] sum to 1.

Hints

  1. A sliding window is not reliable when negative numbers are allowed because the sum may increase or decrease unpredictably.
  2. Use prefix sums: if current_prefix - previous_prefix = k, then the subarray between those two prefix sums has sum k.
Last updated: Jun 23, 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 Common Prefix Across Strings - Apple (easy)
  • Find Minimum Processing Rate - Apple
  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Rotate a Matrix In Place - Apple (medium)