Subarray Sum Equals K
Updated
Problem statement
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) → longExamples
Example 1
nums = [1,1,1]k = 2return = 2The first two and last two elements each sum to 2.
Example 2
nums = [1,2,3]k = 3return = 2Subarrays [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