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
- 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.
- 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.
- Check what k = 1 and k = n * (n + 1) / 2 should return on an array whose elements are all negative.