← IBM Interview Insights

IBM·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

IBM software engineer round, one algorithmic question that looked simple until you actually had to implement it correctly. The sliding window stuff sounds like textbook material but the 'exactly k distinct' constraint has a subtle wrinkle that I did not handle cleanly on the first pass.

Questions Asked (1)

Q1

Given an integer array and an integer k, find the minimum length of a contiguous subarray containing exactly k distinct integer values. Return -1 if none exists.

Algorithms & Data Structures
Author's notes

I jumped straight to a sliding window and got something working for 'at least k' before realizing the question said 'exactly.' Had to backtrack and rethink the shrink logic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window (two-pointer) technique to maintain a window with at most k distinct integers, and when the window has exactly k distinct, try to shrink it from the left to find the minimum length. Track the minimum length seen and return -1 if no valid window exists.

Pro tip: Clarify edge cases upfront (e.g., k <= 0, k > distinct elements) and mention that the sliding window approach runs in O(n) time, which is optimal for this problem.

1. Understand the problem and edge cases

Restate the problem to ensure clarity: find the smallest contiguous subarray with exactly k distinct integers. Discuss edge cases such as k <= 0, k greater than the number of distinct elements in the array, or empty array.

2. Choose the sliding window approach

Explain that a brute-force solution would be O(n^2) or worse, so a sliding window (two-pointer) technique is optimal. Maintain a window [left, right] and a frequency map of elements in the window.

3. Expand and contract the window

Expand the right pointer to include new elements. When the window contains exactly k distinct integers, update the minimum length and then shrink from the left while maintaining exactly k distinct integers to find the smallest valid window.

4. Track the minimum length

Keep a variable to store the minimum length found. After processing all possible windows, return the minimum length or -1 if no valid window was found.

5. Analyze complexity and test

State that the time complexity is O(n) because each element is added and removed at most once, and space complexity is O(k) for the frequency map. Walk through a small example to verify correctness.

Key Points to Mention

  • Sliding window technique with two pointers (left and right).
  • Use a hash map (or dictionary) to count frequencies of elements in the current window.
  • Maintain a count of distinct elements in the window.
  • When distinct count equals k, update minimum length and shrink window from left while distinct count remains k.
  • Time complexity O(n) and space complexity O(k).
  • Handle edge cases: k <= 0, k > total distinct elements, empty array.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.