First Subarray with the Largest Sum
Updated
Problem statement
You are given an integer array nums that can hold positive numbers, negative numbers and zeros. A subarray is a run of one or more consecutive elements of nums. Find the largest sum that any subarray has, and return the first subarray with that sum.
When several subarrays have the largest sum, the first one is the one that starts at the smallest index. If more than one of those starts at that index, it is the shortest of them.
Function
firstLargestSumSubarray(nums: int[]) → int[]Examples
Example 1
nums = [-7,4,-2,5,3,-6,8,-8]return = [4,-2,5,3,-6,8]The subarray from index 1 to index 6 has the sum 4 - 2 + 5 + 3 - 6 + 8 = 12. No other subarray has a sum of 12 or more, so this subarray is returned.
Example 2
nums = [5,-3,-4,-2,6,-4,1,3]return = [6]The largest sum is 6, and two subarrays have it: [6] and [6,-4,1,3]. Both start at index 4, so the shorter one, [6], is returned.
Example 3
nums = [-3,-1,-2,-1]return = [-1]Every number is negative, so adding another number to a subarray only lowers its sum, and the best subarray holds one number. The largest number is -1, at index 1 and index 3. The subarray [-1] at index 1 starts first, so it is returned.
Example 4
nums = [1,-1,2]return = [1,-1,2]The largest sum is 2. The whole array has it, starting at index 0, and so does [2], starting at index 2. The whole array starts first, so it is returned even though it is longer.
Constraints
1 <= nums.length <= 3 * 104-104 <= nums[i] <= 104
More Zillow Onsite Interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
- Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
- Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Tic-Tac-Toe Winner on a Huge BoardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2016
- Most Frequent Three-Page SequencesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2016
- Maximum Path Sum in a GridONSITE INTERVIEW · Seen Apr 2016
Similar questions at other companies
- Card Game: Best First-Player ScoreShopify · OAShopify interview questions
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Distinct Subset SumsGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- 0/1 KnapsackConfluent · OAConfluent interview questions
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions