FastPrepSubarray Sum Equals K
Problems/Subarray Sum Equals K

Subarray Sum Equals K

Google logoGoogle● MediumNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names a well-known problem, Subarray Sum Equals K. We use a 64-bit count for large arrays. Our version is about 98% the same as the named problem.

Given an integer array nums and an integer k, count the nonempty sections of consecutive array elements whose sum equals k, and return that count.

Return the exact count as a signed 64-bit integer.

Function

countSubarraysWithSum(nums: int[], k: int) → long

Examples

Example 1

nums = [1,1,1]k = 2return = 2

The first two and last two elements each sum to 2.

Example 2

nums = [1,2,3]k = 3return = 2

Subarrays [1,2] and [3] qualify.

Example 3

nums = [1,-1,0]k = 0return = 3

[1,-1], [0], and the whole array sum to zero.

Constraints

  • 0 <= nums.length <= 200000.
  • -10^9 <= nums[i], k <= 10^9.

More Google Phone Screen questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Maximum Sum Desirable SubarrayGroww · PHONE SCREEN
  • Maximum Size Subarray Sum Equals KeBay · PHONE SCREENeBay interview questions
  • Detect a Contiguous Subarray with Target SumMeta · PHONE SCREENMeta interview questions
  • Count Subarrays With Target SumAmazon · PHONE SCREEN
  • Count Subarrays with Sum KByteDance · PHONE SCREENByteDance interview questions
  • Subarray Sum Equals KApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Longest Equal-Zero-One SubarrayMotive · PHONE SCREEN · ONSITE INTERVIEWMotive interview questions
  • Shortest Subarray with an Exact Target SumPeopleStrong · PHONE SCREENPeopleStrong interview questions
  • Count Subarrays That Add Up to kAdobe · PHONE SCREENAdobe interview questions
  • Find a Target-Sum SubarrayMorgan Stanley · PHONE SCREENMorgan Stanley interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public long countSubarraysWithSum(int[] nums, int k) {
        // Write your code here.
    }
}
Checking account…