Find a Target-Sum Subarray
Updated
Problem statement
Given an integer array nums and an integer target, find a nonempty contiguous subarray whose sum equals target.
For this exercise, assume values may be positive, negative or zero. Return its zero-based inclusive indices [left,right]. Choose the matching subarray with the smallest right; if several have that end, choose the smallest left. Return [] when no matching nonempty subarray exists.
Function
findSubarray(nums: int[], target: int) → int[]Examples
Example 1
nums = [4,-1,2,-2,3]target = 3return = [0,1]The interval [0,1] sums to 3 and ends before every other matching interval.
Example 2
nums = [0,2,-2,3]target = 3return = [0,3]The intervals [0,3] and [1,3] both sum to 3; choose the smaller start index 0.
Constraints
- For this exercise, assume 0 <= nums.length <= 2000.
- For this exercise, assume -1000000 <= nums[i] <= 1000000.
- For this exercise, assume -2000000000 <= target <= 2000000000.
More Morgan Stanley interview questions
- Unique Grid PathsPHONE SCREEN · Seen Oct 2026
- Reverse an IntegerPHONE SCREEN · Seen Jul 2024
- Second and Third Distinct Order ValuesPHONE SCREEN · Seen Jul 2024
- Binary Tree Preorder TraversalPHONE SCREEN · Seen Jan 2024
- Two SumPHONE SCREEN · Seen Jan 2024
- Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
- Three-Resource 0/1 KnapsackOA · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
- Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
Similar questions at other companies
- Maximum Sum Desirable SubarrayGroww · PHONE SCREEN
- Equal Sum Split After One RemovalGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- 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
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
- Morgan Stanley New Grad Technology Analyst: 2 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)