Quick Overview

Given an integer array and an integer k, return the k-th largest value among the sums of all non-empty contiguous subarrays, counting equal sums from different subarrays separately. Tests reasoning about the quadratic number of subarrays, efficient selection of an order statistic, and correct handling of negative values and duplicate sums.

K-th Largest Sum Among All Contiguous Subarrays

Company: DRW

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Given an integer array `nums` and an integer `k`, consider every non-empty contiguous subarray of `nums` together with its sum. Return the `k`-th largest of these sums. ### Function Signature ```python def kth_largest_subarray_sum(nums: list[int], k: int) -> int: ``` ### Rules - A subarray is a non-empty run of consecutive elements `nums[i..j]` with `0 <= i <= j < n`; an array of length `n` has `n * (n + 1) / 2` of them. - Subarrays with different `(i, j)` are different even when their sums are equal, so each sum is counted once per subarray that produces it. - List all subarray sums in descending order; the answer is the value at position `k`, counting from 1. `k = 1` asks for the largest sum. - Return that sum as a single integer. ### Constraints - `1 <= n <= 1000`, where `n = len(nums)` - `-10^4 <= nums[i] <= 10^4`; elements may be negative, zero or positive - `1 <= k <= n * (n + 1) / 2` - Every subarray sum lies in `[-10^7, 10^7]`, well within 32-bit range ### Examples **Example 1** ```text Input: nums = [3, -1, 2], k = 4 Output: 2 ``` The subarrays starting at index 0 have sums 3, 2 and 4; those starting at index 1 have sums -1 and 1; the one starting at index 2 has sum 2. In descending order the sums are 4, 3, 2, 2, 1, -1. The two 2s come from different subarrays and both count, so the 4th largest is 2. **Example 2** ```text Input: nums = [1, 2, 3], k = 2 Output: 5 ``` The sums in descending order are 6, 5, 3, 3, 2, 1; the second largest is 5, from `[2, 3]`. **Example 3** ```text Input: nums = [-2, -3], k = 3 Output: -5 ``` The three sums are -2, -3 and -5, so the third largest is -5.

Overview: Given an integer array and an integer k, return the k-th largest value among the sums of all non-empty contiguous subarrays, counting equal sums from different subarrays separately. Tests reasoning about the quadratic number of subarrays, efficient selection of an order statistic, and correct handling of negative values and duplicate sums.

Read the full DRW Software Engineer interview experience this question came from

Given an integer array `nums` and an integer `k`, consider every non-empty contiguous subarray of `nums` together with its sum. Return the `k`-th largest of these sums. Implement `kth_largest_subarray_sum(nums, k)`. **Rules** - A subarray is a non-empty run of consecutive elements `nums[i..j]` with `0 <= i <= j < n`; an array of length `n` has `n * (n + 1) / 2` of them. - Subarrays with different `(i, j)` are different even when their sums are equal, so each sum is counted once per subarray that produces it. - List all subarray sums in descending order; the answer is the value at position `k`, counting from 1. `k = 1` asks for the largest sum. - Return that sum as a single integer. **Constraints** - `1 <= n <= 1000`, where `n = len(nums)` - `-10^4 <= nums[i] <= 10^4`; elements may be negative, zero or positive - `1 <= k <= n * (n + 1) / 2` - Every subarray sum lies in `[-10^7, 10^7]`, well within 32-bit range: no value exceeds 2^31 - 1, so a 32-bit `int` is enough in Java and C++ **Example 1** ```text Input: nums = [3, -1, 2], k = 4 Output: 2 ``` The subarrays starting at index 0 have sums 3, 2 and 4; those starting at index 1 have sums -1 and 1; the one starting at index 2 has sum 2. In descending order the sums are 4, 3, 2, 2, 1, -1. The two 2s come from different subarrays and both count, so the 4th largest is 2. **Example 2** ```text Input: nums = [-2, -3], k = 3 Output: -5 ``` The three sums are -2, -3 and -5, so the third largest is -5.

Constraints

  • 1 <= n <= 1000, where n = len(nums)
  • -10^4 <= nums[i] <= 10^4; elements may be negative, zero or positive
  • 1 <= k <= n * (n + 1) / 2
  • Every subarray sum lies in [-10^7, 10^7], well within 32-bit range

Examples

Input: ([3, -1, 2], 4)

Expected Output: 2

Explanation: Source example 1: sums 4, 3, 2, 2, 1, -1; both 2s count, so the 4th largest is 2 (a distinct-sums answer would be 1).

Input: ([1, 2, 3], 2)

Expected Output: 5

Explanation: Source example 2: sums 6, 5, 3, 3, 2, 1; 1-based position 2 is 5, while 0-based index 2 would give 3.

Hints

  1. There are exactly n * (n + 1) / 2 subarrays, and equal sums from different (i, j) pairs each keep their own place in the descending list.
  2. The sum of nums[i..j] follows from the sum of nums[i..j-1] with one addition, or from the difference of two prefix sums.
  3. Check what k = 1 and k = n * (n + 1) / 2 should return on an array whose elements are all negative.

K-th Largest Sum Among All Subarrays

hardDRWSoftware Engineer

Given an integer array nums and an integer k, consider every non-empty contiguous subarray of nums together with its sum. Return the k-th largest of these sums.

Implement kth_largest_subarray_sum(nums, k).

Rules

  • A subarray is a non-empty run of consecutive elements nums[i..j] with 0 <= i <= j < n; an array of length n has n * (n + 1) / 2 of them.
  • Subarrays with different (i, j) are different even when their sums are equal, so each sum is counted once per subarray that produces it.
  • List all subarray sums in descending order; the answer is the value at position k, counting from 1. k = 1 asks for the largest sum.
  • Return that sum as a single integer.

Constraints

  • 1 <= n <= 1000, where n = len(nums)
  • -10^4 <= nums[i] <= 10^4; elements may be negative, zero or positive
  • 1 <= k <= n * (n + 1) / 2
  • Every subarray sum lies in [-10^7, 10^7], well within 32-bit range: no value exceeds 2^31 - 1, so a 32-bit int is enough in Java and C++

Example 1

Input:  nums = [3, -1, 2], k = 4
Output: 2

The subarrays starting at index 0 have sums 3, 2 and 4; those starting at index 1 have sums -1 and 1; the one starting at index 2 has sum 2. In descending order the sums are 4, 3, 2, 2, 1, -1. The two 2s come from different subarrays and both count, so the 4th largest is 2.

Example 2

Input:  nums = [-2, -3], k = 3
Output: -5

The three sums are -2, -3 and -5, so the third largest is -5.

Examples

Example 1
Input
([3, -1, 2], 4)
Output
2
Notes
Source example 1: sums 4, 3, 2, 2, 1, -1; both 2s count, so the 4th largest is 2 (a distinct-sums answer would be 1).
Example 2
Input
([1, 2, 3], 2)
Output
5
Notes
Source example 2: sums 6, 5, 3, 3, 2, 1; 1-based position 2 is 5, while 0-based index 2 would give 3.

Constraints

  • •1 <= n <= 1000, where n = len(nums)
  • •-10^4 <= nums[i] <= 10^4; elements may be negative, zero or positive
  • •1 <= k <= n * (n + 1) / 2
  • •Every subarray sum lies in [-10^7, 10^7], well within 32-bit range

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def kth_largest_subarray_sum(nums: list[int], k: int) -> int:
Sample call
kth_largest_subarray_sum([3, -1, 2], 4)
Language
Case 1
Input
([3, -1, 2], 4)
Expected
2
Case 2
Input
([1, 2, 3], 2)
Expected
5

Browse More Questions

Show the approach

Approach

Enumerate every subarray sum, then select by rank. For each start index i, extend the end index j from i to n - 1 while keeping a running total, so the sum of nums[i..j] is the sum of nums[i..j-1] plus nums[j]. Invariant: after processing start index i, the list holds exactly one entry for every subarray whose start is at most i. Each of the n * (n + 1) / 2 subarrays therefore contributes exactly one entry, so equal sums from different (i, j) pairs stay separate, as the rules require. Sorting does not change multiplicities, so in descending order the entry at 1-based position k (0-based index k - 1, or index total - k after an ascending sort) is the k-th largest sum by definition. Edge cases: n = 1 returns the only element; subarrays are non-empty, so on an all-negative array k = 1 returns the largest single element, not 0; k = n * (n + 1) / 2 returns the minimum subarray sum; every sum stays within [-10^7, 10^7], so 32-bit integers suffice. At n = 1000 the list holds 500,500 integers (about 2 MB as 32-bit values).

Time complexity:
O(n^2 log n)
Space complexity:
O(n^2)