FastPrepSubarray Sum Equals K
Problems/Subarray Sum Equals K

Subarray Sum Equals K

ServiceNow logoServiceNow● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In the second round of a ServiceNow SSE (IC3) loop in Bengaluru, the interviewer told a long story that boiled down to counting the subarrays whose sum equals a target. The candidate linked it to the well-known version of that task.

The post does not share the story, examples or limits. This practice version uses the plain array form: up to 2 * 10^4 values from -1000 to 1000, and a target from -10^7 to 10^7.

The judged task matches the post at about 85%.

You are given an integer array nums and an integer k. Return how many subarrays of nums have a sum equal to k.

A subarray is a non-empty run of elements that are next to each other in nums. Two subarrays at different positions count separately, even if they hold the same values.

Function

subarraySum(nums: int[], k: int) → int

Examples

Example 1

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

Four subarrays add up to 3: [1, 2], [3], [2, 3, -2] and [3, -2, 2].

Example 2

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

The first two 2 values and the last two 2 values are two different subarrays, so the answer is 2.

Example 3

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

The subarrays with sum 0 are [1, -1] at the start, [-1, 1] in the middle, [1, -1] at the end, and the whole array. That is 4.

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • -1000 <= nums[i] <= 1000
  • -10^7 <= k <= 10^7

More ServiceNow interview questions

  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximum Subarray Sum with One DeletionPHONE SCREEN · Seen Nov 2026
  • Find Trapped ZonesPHONE SCREEN · Seen Nov 2026
  • Find Max Minimum Value Using K Elements 🐴OA · Seen Mar 2024
  • Maximum Sum of Balanced SubsequenceOA · Seen Mar 2024

Similar questions at other companies

  • Count Subarrays with Equal Even and Odd CountsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Maximum Subarray Sum with Equal EndpointsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Count Subarrays with Sum KMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
  • Subarray Sum Equals KApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Longest Equal-Zero-One SubarrayMotive · PHONE SCREEN · ONSITE INTERVIEWMotive interview questions
  • Find and Measure the Longest Target-Sum SubarrayMeta · ONSITE INTERVIEWMeta interview questions
  • Longest Subarray with a Target SumPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
  • Count Subarrays Whose Sum Is KExpedia · ONSITE INTERVIEWExpedia interview questions
  • Maximum Sum Desirable SubarrayGroww · PHONE SCREEN
  • Maximum Size Subarray Sum Equals KeBay · PHONE SCREENeBay interview questions
All ServiceNow interview questionsSee ServiceNow hiring insights
Saved
class Solution {
    public int subarraySum(int[] nums, int k) {
        // Write your code here.
    }
}
Checking account…