FastPrepFirst Subarray with the Largest Sum
Problems/First Subarray with the Largest Sum

First Subarray with the Largest Sum

Zillow logoZillow● MediumFULLTIMEOAONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Asked at Zillow in 2011 and 2021. In 2011 a candidate posted written answers to a set of Zillow questions; one asked, for an array of whole numbers, some above zero and some below, for the earliest run of neighboring values whose total is the greatest, with two worked examples. In 2021 a candidate going through a senior software engineer interview loop called the initial phone screen easy and named a maximum-sum subarray task as the hardest question they had been given.

The 2011 question asks for the run itself, not its total. We read its earliest run as the one starting at the smallest index; its second example settles a tie between two runs that start at the same place in favor of the shorter one. Your function gets the numbers as one integer array. Here a run holds at least one number, and the array has 1 to 3 x 10^4 values, each from -10^4 to 10^4. Both are our choices. We read the maximum-sum subarray task in the 2021 report as the same task as the 2011 question, which is our assumption. The 2011 answers were written up as files with runnable code, so we read that set as a take-home and place the task in an online assessment; the 2021 report calls the interviews a loop, so we place it in an onsite interview. Both placements are our reading.

Our version is about 90% the same as the reported question.

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
All Zillow interview questionsSee Zillow hiring insights
Saved
class Solution {
    public int[] firstLargestSumSubarray(int[] nums) {
        // Write your code here.
    }
}
Checking account…