Find Maximum Length Subarray
Updated
Problem statement
You have n magical fruits in a row. The power of fruit i is a[i]. You are also given a number k.
A subarray is a stretch of fruits that sit next to each other. Its sum is the total power of those fruits.
Find the largest length L such that every subarray of length L has a sum strictly less than k, and return L. L can be anything from 0 to n. If a single fruit already has power k or more, no length works except 0, so return 0.
Complete the function findMaximumLengthSubarray in the editor.
findMaximumLengthSubarray has the following parameters:
int[] a: the power of each fruitlong k: the sum of a subarray must stay strictly below this number
The function returns an int: the largest length L.
Function
findMaximumLengthSubarray(a: int[], k: long) → intExamples
Example 1
a = [3, 1, 2, 4]k = 8return = 3The subarrays of length 3 are [3, 1, 2] with sum 6 and [1, 2, 4] with sum 7. Both are less than 8. The only subarray of length 4 is the whole array, with sum 10, which is not less than 8. So the answer is 3.
Constraints
1 ≤ n ≤ 2 * 105, wherenisa.length1 ≤ a[i] ≤ 1091 ≤ k ≤ 1018
More Uber interview questions
- Random Bingo Card GeneratorONSITE INTERVIEW · Seen Dec 2026
- Travelers per Consecutive City TripletONSITE INTERVIEW · Seen Dec 2026
- Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Max Points on a LinePHONE SCREEN · Seen Oct 2026
- Online Meeting Scheduler with Fixed RoomsONSITE INTERVIEW · Seen Oct 2026
- Convex Function MinimizationPHONE SCREEN · Seen Oct 2026
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Merge Meeting IntervalsPHONE SCREEN · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
- Choose ContainersIMC Trading · OAIMC Trading interview questions
- Largest Sub-GridMathWorks · OAMathWorks interview questions
- Minimum Size Subarray SumMicrosoft · OAMicrosoft interview questions
- Minimum Operations to Make All Array Elements EqualIBM · OAIBM interview questions
- Calculate Total Distance TravelledAmazon · OA
- TikTok Metrics AnalysisTikTok · OATikTok interview questions
- Largest Square Sub-Grid Within a Sum LimitC3.ai · OAC3.ai interview questions
- Maximum Distinct Items Within a BudgetAdobe · OAAdobe interview questions