FastPrepCount Subarrays Whose Sum Is K
Problems/Count Subarrays Whose Sum Is K

Count Subarrays Whose Sum Is K

Expedia logoExpedia● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A post about Expedia's summer SDE intern hiring lists this task among the coding questions from the author's interview on September 20, 2021.

The post gives only the task's name, with no input format, examples or limits. Here, the array holds 1 to 2 x 10^4 integers from -1000 to 1000, so negative numbers and zeros are allowed. k is between -10^7 and 10^7. You return how many non-empty runs of neighboring elements add up to exactly k, counting runs at different positions separately.

Our version is about 90% the same as the reported question.

You are given an integer array nums and an integer k.

Return how many subarrays of nums add up to exactly k. A subarray is a non-empty run of neighboring elements. Subarrays at different positions are counted separately, even when they hold the same values.

The array can contain negative numbers and zeros.

Function

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

Examples

Example 1

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

Six subarrays add up to 3: [3], [-1, 4], [3, -1, 4, -3], [4, -3, 2], [2, 1] and [-1, 4, -3, 2, 1]. No other subarray does.

Example 2

nums = [0,6,0,0]k = 0return = 4

Each of the three zeros alone adds up to 0, and so does [0, 0] made of the last two elements. Every subarray that contains 6 adds up to 6, so the answer is 4.

Example 3

nums = [-4,9,2]k = 6return = 0

The six subarrays add up to -4, 9, 2, 5 ([-4, 9]), 11 ([9, 2]) and 7 (all three). None of them is 6.

Constraints

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

More Expedia Onsite Interview questions

  • Scramble StringONSITE INTERVIEW · Seen Sep 2026
  • Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
  • Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
  • Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
  • Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
  • Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
  • Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
  • Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
  • Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
  • Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 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
  • Maximum Sum Desirable SubarrayGroww · PHONE SCREEN
  • Maximum Size Subarray Sum Equals KeBay · PHONE SCREENeBay interview questions
  • Count Subarrays With Target SumAmazon · PHONE SCREEN

Expedia interview experiences

  • Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
  • Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
  • Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
  • Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
  • Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
  • Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)
All Expedia interview questionsSee Expedia hiring insights
Saved
class Solution {
    public int subarraySum(int[] nums, int k) {
        // Write your code here.
    }
}
Checking account…