Maximum Sum of a Unique-Element Subarray
Reported by candidates from Tekion's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The example where [0,0,0] returns 0 tells you what this Tekion question is really about: a contiguous window where every value is unique, and you want the biggest sum. Tekion candidates reported it in July 2025. It's a sliding window problem wearing a hash set costume. The array can hit 10^5 elements, so anything quadratic dies. If you know the window pattern, you're done in ten minutes. If you blank, StealthCoder sits invisibly on your screen during the live OA and hands you the working solution as a safety net.
The problem
Given a non-empty integer array nums containing only non-negative values, return the maximum sum of a contiguous subarray whose elements are all distinct. Function maximumUniqueSubarraySum(nums: int[]) → int Examples Example 1 nums = [4,2,4,5,6] return = 17 The subarray [2,4,5,6] has distinct values and sum 17, which is maximal. Example 2 nums = [5,2,1,2,5,2,1,2,5] return = 8 A maximum-sum distinct subarray is [5,2,1], whose sum is 8. Example 3 nums = [0,0,0] return = 0 Any one-element subarray [0] is valid and has sum 0. Constraints 1 <= nums.length <= 10^5 0 <= nums[i] <= 10^4 The answer fits in a 32-bit signed integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a variable-size sliding window with a running sum. Keep a set (or a last-seen index map) of values in the window. Move the right pointer, and if nums[right] is already in the window, shrink from the left, subtracting values, until the duplicate is gone. Then add the new value and update the best sum. Each element enters and leaves once, so it's O(n). The common pitfall is resetting the sum to zero on a duplicate instead of shrinking, which throws away valid overlap. Another is forgetting that zeros are legal, so the answer can be 0. Since all values are non-negative, a longer distinct window never hurts, which is why the greedy shrink is safe. If your mind goes blank on the pointer logic during the live OA, StealthCoder is the hedge that gives you the code without the proctor seeing anything.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Maximum Sum of a Unique-Element Subarray cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximum erasure value. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Tekion's OA.
Tekion reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Sum of a Unique-Element Subarray FAQ
What's the trick for the Tekion unique-element subarray problem?+
Use a sliding window with a hash set and a running sum. Expand right, and when you hit a duplicate, shrink from the left until the window is distinct again. Track the max sum at each step. It runs in O(n) time with O(n) space for the set.
How hard is this problem really?+
It's medium at most. The pattern is standard sliding window, and the non-negative constraint removes the hard edge cases. If you've seen longest substring without repeating characters, this is the same idea with a sum instead of a length.
Why can't I use brute force?+
With n up to 10^5, checking every subarray for distinctness is O(n^2) or worse and will time out. The window approach processes each element at most twice, once when added and once when removed, which keeps it linear.
Should I use a set or a last-seen map?+
Either works. A set with a while-loop shrink is simpler to write correctly. A last-seen index map lets you jump the left pointer in one step, but you must use max(left, lastSeen+1) so the pointer never moves backward. Pick the one you can write without bugs.
How do I prepare for this in 48 hours?+
Write the sliding window with a set from scratch twice. Test it on [4,2,4,5,6], the all-zeros case, and a single-element array. Then do two or three similar window problems. Focus on the shrink loop and the running sum update, since that's where most mistakes happen.