FastPrepSubarray with a Given Sum
Problems/Subarray with a Given Sum

Subarray with a Given Sum

Expedia logoExpedia● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In a 2023 write-up of Expedia's campus hiring for summer interns, this was the opening coding task of the first face-to-face interview. The post names it only through a link to a well-known exercise: find a stretch of neighboring values that adds up to a target. The author began by checking all start and end pairs, then sped it up.

The post has no statement, limits or rule for multiple answers. We chose these. Values are never negative, the target is at least 1, and positions count from 1. [-1] means no answer. If several stretches work, the earliest start wins, then the earliest end. Sizes are ours too: up to 10^5 values from 0 to 10^9, and a target from 1 to 10^14.

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

You are given an array arr of non-negative integers and a positive integer target. Find a subarray (one or more neighboring elements) whose values add up to exactly target.

Positions are counted from 1, so the first element is at position 1. Return [start, end], the first and last positions of the subarray.

If several subarrays work, return the one with the smallest start. If several of those share that start, return the one with the smallest end. If no subarray works, return [-1].

Function

findSubarrayWithSum(arr: int[], target: long) → int[]

Examples

Example 1

arr = [1,2,3,7,5]target = 12return = [2,4]

Positions 2 to 4 hold 2 + 3 + 7 = 12. Positions 4 to 5 also work, because 7 + 5 = 12, but position 2 is the smaller start, so the answer is [2, 4].

Example 2

arr = [4,0,0,3,0,2]target = 3return = [2,4]

No subarray starting at position 1 works, because it already holds 4. From position 2, the sums are 0, 0, 3, so positions 2 to 4 add up to 3. Positions 2 to 5 also add up to 3, but position 4 is the earlier end. The answer is [2, 4], not the shorter [4, 4], because position 2 starts earlier.

Example 3

arr = [1,2,3]target = 7return = [-1]

The whole array adds up to only 6, so no subarray reaches 7. The answer is [-1].

Constraints

  • 1 <= arr.length <= 10^5
  • 0 <= arr[i] <= 10^9
  • 1 <= target <= 10^14

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 Hotspot Playback WindowsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
  • Longest Binary Window with at Most K OnesLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Trim a Telemetry Buffer to a Target SumAmazon · ONSITE INTERVIEW
  • Trapping Rain WaterInMobi · ONSITE INTERVIEWInMobi interview questions
  • Subarray Product Less Than KApple · PHONE SCREENApple interview questions
  • Subarray Product Less Than KAngel One · PHONE SCREEN
  • Subarray Product Less Than KThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
  • Trapping Rain WaterGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • Count Subarrays With Product Equal to KSalesforce · OASalesforce interview questions

Interview reports that mention this question

  • Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)

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 SWE Intern Final Round: 126 Experiences (Part 1 of 6)
All Expedia interview questionsSee Expedia hiring insights
Saved
class Solution {
    public int[] findSubarrayWithSum(int[] arr, long target) {
        // Write your code here.
    }
}
Checking account…