Maximum Subarray Sum with Equal Endpoints
Updated
Problem statement
Given an integer array nums, choose indices i and j such that i <= j and nums[i] == nums[j].
The score of a pair is the inclusive subarray sum nums[i] + nums[i + 1] + ... + nums[j]. Return [i, j] for a pair with the maximum score.
If several pairs have the same maximum score, choose the one with the smallest i; if there is still a tie, choose the smallest j. A one-element subarray is valid, so an answer always exists.
Follow-up: remove the endpoint-equality rule
How would you return the indices of a maximum-sum nonempty subarray if its first and last values no longer had to be equal? Keep the same earliest-index tie-breaking rule. Aim for O(n) time and O(1) extra space. Discuss this as a separate function; the function above still requires equal endpoint values.
Function
maximumEqualEndpointSubarray(nums: int[]) → int[]Examples
Example 1
nums = [2,-5,2,4,2]return = [2,4]The endpoints at indices 2 and 4 are both 2, and their inclusive sum is 8, larger than every other valid pair.
Example 2
nums = [-4,3,-4]return = [1,1]The two equal -4 endpoints enclose sum -5. The single value 3 is a valid, higher-scoring choice.
Example 3
nums = [4,-2,4,-2,4]return = [0,4]The full array has equal endpoints and sum 8, which is the unique maximum.
Example 4
nums = [1,2,3,2,5]return = [1,3]The subarray [2,3,2] has equal endpoints and sum 7. Its zero-based endpoint indices are 1 and 3; no valid pair has a larger sum.
Constraints
1 <= nums.length <= 100000.-1000000000 <= nums[i] <= 1000000000.- Use 64-bit arithmetic for subarray sums.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 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
- Word BreakAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks 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
- Maximum Subarray SumCapital One · PHONE SCREEN · ONSITE INTERVIEWCapital One interview questions
- Longest Subarray with a Target SumPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
Interview reports that mention this question
- Google SWE Intern Extra Interview Round: 2 Experiences
- Google Intern Coding and OOP: 19 Experiences (Part 5 of 8)
- Google New Grad SWE Interview Questions Guide (Part 1 of 2)
- Google Early Career Onsite via Referral: 1 Experience
- Google New Grad Mountain View Onsite: 1 Experience
- Google SWE and PhD Intern Coding: 14 Experiences (Part 3 of 3)