← IBM Interview Insights

IBM·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Did a technical screen for a software engineer role at IBM, pretty much just one coding problem the whole time. Nothing too wild but the window shrinking part tripped me up a bit.

Questions Asked (1)

Q1

Given an integer array and an integer k, find the minimum length of a contiguous subarray that contains exactly k distinct numbers. Return -1 if no valid subarray exists.

Algorithms & Data Structures
Author's notes

I knew it was a sliding window problem pretty fast, set up a hashmap to track frequencies and expanded the right pointer until I had k distinct values.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window (two pointers) to maintain a window with at most k distinct numbers, and when exactly k distinct numbers are present, shrink the window from the left to find the minimum length. Track the minimum length whenever the window has exactly k distinct numbers. If no such window is found, return -1.

Pro tip: Clarify whether the array can contain negative numbers or zeros, as this affects the distinct count logic. Also, discuss the trade-off between using a hash map versus an array for frequency counting based on the value range.

1. Understand the problem and edge cases

Restate the problem to ensure clarity: find the smallest contiguous subarray with exactly k distinct integers. Consider edge cases: k <= 0, k > number of distinct elements in the array, empty array, and array length less than k.

2. Choose the sliding window technique

Explain that a brute-force approach would be O(n^2) or worse, but a sliding window with two pointers can achieve O(n) time. Use a hash map to track the frequency of each element in the current window.

3. Expand and contract the window

Iterate the right pointer to expand the window, updating the frequency map. When the number of distinct elements exceeds k, move the left pointer to shrink the window until distinct count is <= k. When distinct count equals k, update the minimum length.

4. Optimize the shrinking step

To find the minimum length efficiently, after achieving exactly k distinct, try to shrink the window from the left as much as possible while still maintaining exactly k distinct. This ensures we capture the smallest valid window ending at the current right pointer.

5. Return the result and analyze complexity

After traversal, return the minimum length found or -1 if none. State the time complexity O(n) and space complexity O(k) for the frequency map.

Key Points to Mention

  • Sliding window technique with two pointers (left and right).
  • Hash map (or dictionary) to maintain frequency of elements in the current window.
  • Tracking the number of distinct elements in the window.
  • Updating the minimum length when the window has exactly k distinct elements.
  • Handling edge cases such as k <= 0, k > total distinct elements, and empty array.
  • Time complexity O(n) and space complexity O(k).

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