Apply sliding window techniques
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Apply sliding window techniques states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Minimum Size Subarray Sum
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^4
- 1 <= s <= 10^9
- All integers in nums are positive (this is what makes the shrink-while-valid invariant correct).
Examples
Input: ([2, 3, 1, 2, 4, 3], 7)
Expected Output: 2
Explanation: The subarray [4, 3] has sum 7 >= 7 and length 2, which is the minimum.
Input: ([1, 4, 4], 4)
Expected Output: 1
Explanation: A single element 4 already reaches the target, so the minimal length is 1.
Input: ([1, 1, 1, 1, 1, 1, 1, 1], 11)
Expected Output: 0
Explanation: The total sum is 8, which is less than 11, so no valid subarray exists; return 0.
Input: ([], 5)
Expected Output: 0
Explanation: Empty array has no subarray reaching the target; return 0.
Input: ([5], 5)
Expected Output: 1
Explanation: The single element equals the target exactly, so the minimal length is 1.
Input: ([1, 2, 3, 4, 5], 11)
Expected Output: 3
Explanation: The subarray [3, 4, 5] sums to 12 >= 11 with length 3; no length-2 window reaches 11.
Input: ([2, 3, 1, 2, 4, 3], 100)
Expected Output: 0
Explanation: The whole array sums to 15 < 100, so no subarray qualifies; return 0.
Hints
- Use two pointers forming a window. Move the right pointer to include elements and grow the running sum.
- Whenever the running sum is >= s, the current window is valid: record its length, then shrink from the left to look for a shorter valid window.
- Because all numbers are positive, removing from the left strictly decreases the sum, so the while-shrink loop is safe and the total pointer movement is O(n).
Longest Substring with At Most K Distinct Characters
Constraints
- 0 <= s.length <= 10^5
- 0 <= k <= 50 (or up to the size of the character set)
- s consists of arbitrary characters; an empty string or k = 0 should return 0.
Examples
Input: ("eceba", 2)
Expected Output: 3
Explanation: The substring "ece" uses exactly 2 distinct characters and has length 3, the longest possible.
Input: ("aa", 1)
Expected Output: 2
Explanation: Only one distinct character, so the whole string of length 2 qualifies.
Input: ("abcadcacacaca", 3)
Expected Output: 11
Explanation: The substring "cadcacacaca" uses the 3 distinct characters a, c, d and has length 11.
Input: ("", 2)
Expected Output: 0
Explanation: Empty string has no characters, so the answer is 0.
Input: ("abc", 0)
Expected Output: 0
Explanation: With k = 0, no character is allowed, so the longest valid substring has length 0.
Input: ("aabbcc", 1)
Expected Output: 2
Explanation: With only 1 distinct character allowed, the best windows are "aa", "bb", or "cc", each of length 2.
Input: ("a", 5)
Expected Output: 1
Explanation: k exceeds the number of distinct characters, so the entire string of length 1 qualifies.
Hints
- Maintain a window [left, right] and a hash map from character to its count inside the window.
- Extend the right edge each step. When the map has more than k distinct keys, shrink from the left, decrementing counts and deleting a key when its count hits zero.
- After every adjustment the window is valid (at most k distinct), so update the best length with right - left + 1. Handle k = 0 and the empty string by returning 0.