NetApp Full Stack Engineer

374
Oct 10, 2026

Round 1

Interview Question — Maximum Sum of Unique Subarray

Given an integer array nums and an integer k, write a function to identify the highest possible sum of a subarray within nums, where:

  1. The subarray must have exactly k elements.
  2. All elements in the subarray must be unique.
  3. If no such subarray exists, return 0.

Example 1

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

Output:
20

Explanation:

The valid subarray with the highest sum is:

[3, 4, 6, 7]

Its sum is:

3 + 4 + 6 + 7 = 20

Example 2

Input:
nums = [5, 5, 5, 5, 5]
k = 3

Output:
0

Explanation:

Every subarray of length 3 contains duplicate elements, so no valid subarray exists.


Additional Example

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

The solution should consider every contiguous subarray of length 4 and select the one with:

  • exactly 4 elements
  • no duplicate elements
  • maximum sum

Comments (3)