← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Google SWE coding round, one algorithmic problem on subarrays. Pretty standard stuff but the follow-up on optimization is where it gets interesting.

Questions Asked (1)

Q1

Given an integer array and an integer k, find the length of the shortest contiguous subarray that contains at least k distinct integers. Return -1 if none exists.

Algorithms & Data Structures
Author's notes

Started with brute force because I panicked a little and needed to say something.

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 that contains at most k distinct integers, and whenever the window has exactly k distinct integers, shrink it from the left to find the shortest valid subarray. Track the minimum length seen. If no such subarray exists, return -1.

Pro tip: Clarify edge cases upfront: if k is greater than the number of distinct integers in the array, return -1 immediately. Also, discuss time and space complexity (O(n) time, O(k) space) to demonstrate efficiency awareness.

1. Understand the problem and edge cases

Restate the problem to ensure clarity. Identify edge cases: k <= 0, k > distinct count, empty array, and array length less than k.

2. Choose the right algorithm

Select sliding window with a hash map to track frequencies of elements in the current window. This allows O(n) time by expanding and shrinking the window dynamically.

3. Implement the sliding window

Initialize left and right pointers, a frequency map, and a counter for distinct elements. Expand right, update map, and when distinct count equals k, shrink left while maintaining k distinct, updating the minimum length.

4. Handle termination and return result

After iterating through the array, if no valid window was found, return -1; otherwise, return the minimum length recorded.

5. Analyze complexity and test

State time complexity O(n) and space complexity O(k). Walk through a small example to verify correctness, and consider potential optimizations or alternative approaches.

Key Points to Mention

  • Sliding window technique with two pointers
  • Hash map to track frequency of elements in the window
  • Maintaining a count of distinct integers in the window
  • Shrinking the window when distinct count equals k to find minimum length
  • Time complexity O(n) and space complexity O(k)
  • Edge cases: k > distinct count, empty array, k <= 0

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