Find and Measure the Longest Target-Sum Subarray
Updated
Problem statement
Given an integer array nums and a target k, consider every non-empty contiguous subarray whose elements sum to k.
Return [existsFlag, longestLength]. Set existsFlag to 1 if at least one qualifying subarray exists, or 0 otherwise. Set longestLength to the maximum number of elements in any qualifying subarray. If no qualifying subarray exists, return [0, 0].
Values may be positive, negative, or zero. A subsequence that skips elements does not count.
Function
findLongestTargetSubarray(nums: int[], k: int) → int[]Examples
Example 1
nums = [1,2,3,-2,2]k = 3return = [1,3]The first three values sum to 6, but [3,-2,2] sums to 3 with length 3; no longer matching subarray exists.
Example 2
nums = [1,-1,1,-1,1]k = 1return = [1,5]The entire array sums to 1 and is the longest possible qualifying subarray.
Example 3
nums = [2,4,6]k = 5return = [0,0]No non-empty contiguous subarray sums to 5.
Constraints
1 <= nums.length <= 20000.-1000 <= nums[i] <= 1000.-10000000 <= k <= 10000000.
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Subarray Sum Equals KServiceNow · ONSITE INTERVIEWServiceNow interview questions
- 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
- Longest Subarray with a Target SumPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Count Subarrays Whose Sum Is KExpedia · ONSITE INTERVIEWExpedia interview questions
- Number of Submatrices That Sum to TargetWhatNot · ONSITE INTERVIEWWhatNot interview questions
- Maximum Sum Desirable SubarrayGroww · PHONE SCREEN
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences