cswork / libraryOpen practice workspace

Coding questions · NVIDIA

Apply sliding window techniques

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.

Explain the sliding window technique and apply it to solve: (1) Given an array of positive integers and a target S, return the minimal length of a contiguous subarray with sum ≥ S (or 0 if none); (2) Given a string and an integer K, return the length of the longest substring containing at most K distinct characters; Analyze time and space complexity and when to use fixed vs. dynamic windows.

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Minimum Size Subarray Sum

Given an array of positive integers nums and a positive integer s, return the minimal length of a contiguous subarray of which the sum is greater than or equal to s. If there is no such subarray, return 0.

This is the classic dynamic (variable-size) sliding window: expand the right edge to grow the window sum, and as soon as the sum reaches the target, shrink from the left while it still satisfies the constraint, recording the smallest valid window length seen.

Example:

  • nums = [2, 3, 1, 2, 4, 3], s = 7 -> 2 (the subarray [4, 3] has sum 7 with minimal length 2).

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).

Hints

  1. Use two pointers forming a window. Move the right pointer to include elements and grow the running sum.
  2. 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.
  3. 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

Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters.

This is a dynamic sliding window where the window grows greedily on the right; when the number of distinct characters exceeds k, shrink from the left until the constraint holds again. A hash map tracks the count of each character currently in the window so the distinct count can be maintained in O(1) per step.

Example:

  • s = "eceba", k = 2 -> 3 (the substring "ece" has 2 distinct characters and length 3).

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.

Hints

  1. Maintain a window [left, right] and a hash map from character to its count inside the window.
  2. 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.
  3. 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.

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

Explain the sliding window technique and apply it to solve: ( 1) Given an array of positive integers and a target S, return the minimal length of a contiguous subarray with sum ≥ S (or 0 if none); ( 2) Given a string and an integer K, return the length of the longest substring containing at most K distinct characters; Analyze time and space complexity and when to use fixed vs. dynamic windows.

Recommended Approach

Choose traversal based on the required output. DFS is natural for subtree computations, reconstruction, and range pruning; BFS is natural for level order or side views. Keep per-depth or per-position state when the output depends on columns, rows, or depths.

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

Most tree traversals are O(n) time and O(h) recursion stack for DFS or O(w) queue space for BFS, where h is height and w is maximum width.

Edge Cases and Tests

Empty tree, one node, skewed tree, duplicate values when reconstruction assumes uniqueness, deep recursion, and tie-breaking for same row/column nodes.