Need Help With This DSA Problem Asked in My NetApp Interview
I was asked a coding question during Round 1 of my NetApp Full Stack Engineer interview, and I wasn't able to figure out the complete approach or write the solution. I understood what the question was asking, but I got stuck while trying to handle the duplicate elements and calculate the maximum sum efficiently.
I'd appreciate it if someone could help me understand the approach and explain the solution step by step.
Problem Statement
Given an integer array nums and an integer k, find the maximum sum of a contiguous subarray containing exactly k elements, such that all the elements in that subarray are distinct.
If no valid subarray exists, return 0.
A valid subarray must satisfy two conditions:
It must contain exactly
kconsecutive elements.All elements within the subarray must be unique.
Example 1
Input:
nums = [4, 1, 3, 8, 5, 2, 6, 3]
k = 3Output:
16Explanation:
We need to examine every contiguous subarray of length 3 and find the one with distinct elements and the maximum sum.
[4, 1, 3]→ Sum = 8[1, 3, 8]→ Sum = 12[3, 8, 5]→ Sum = 16[8, 5, 2]→ Sum = 15[5, 2, 6]→ Sum = 13[2, 6, 3]→ Sum = 11
The subarray [3, 8, 5] contains three distinct elements and has the highest sum, which is 16.
Example 2
Input:
nums = [6, 6, 2, 2, 8, 8]
k = 2Output:
10Explanation:
Some subarrays contain duplicate elements, such as [6, 6], [2, 2], and [8, 8], so they cannot be considered.
However, [6, 2] and [2, 8] contain distinct elements. Their sums are 8 and 10, respectively. Therefore, the maximum valid sum is 10.
Example 3
Input:
nums = [5, 5, 5, 5]
k = 3Output:
0Explanation:
Every contiguous subarray of length 3 contains duplicate elements. Since no valid subarray exists, the answer is 0.
What I'm Struggling With
I tried to think of a way to check each subarray, but I'm not sure how to optimize the solution.
Recommended Reads
Apple SDE 3 Interview Experience | Java Spring Boot
Company: Apple Role: Software Development Engineer 3 (SDE 3) Tech Stack: Java, Spring Boot, Kafka, Redis Round 1: Java Coding and Problem-Solving In the first r
FamPay SDE-2 | ₹30 LPA Offer | 4 YOE
I recently interviewed for an SDE-2 position at FamPay in Bengaluru and received an offer of ₹30 LPA. I have around 4 years of experience at a leading fintech c
Kotak Mahindra Bank SDE 3 Interview Experience | Hyderabad |
My interview experience at Kotak Mahindra Bank for the SDE 3 role in Hyderabad involved a mix of practical coding and problem-solving. Bar Raiser - Low-Level De
Flipkart SDE-2 Interview Experience | 2026 | Selected
Hi everyone, Recently interviewed with Flipkart for an SDE-2 role with 4.3 years of experience and successfully received an offer. I’m sharing my experience acr
JLL Software Engineer Interview Experience (2026) | Campus Placement
I appeared for the JLL (Jones Lang LaSalle) campus placement process for the Software Engineer role. The selection process included resume shortlisting, a compa
To solve this problem efficiently, the optimal approach is to use the Sliding Window technique combined with a Hash Map (or Hash Set) to keep track of element frequencies and a running sum. In the loop you can keep updating maximum value whenever you reach the window size k and all distinct element (i.e. size of map equals k).