PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Apple

Count subarrays with sum equals k

Last updated: Mar 29, 2026

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.

Related Interview Questions

  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Rotate a Matrix In Place - Apple (medium)
  • Encode and Rebuild a Binary Tree - Apple (hard)
  • Wrap Matching Substrings in Bold Tags - Apple (medium)
Apple logo
Apple
Feb 11, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
8
0
Loading...

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.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Apple•More Software Engineer•Apple Software Engineer•Apple Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ 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.