← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

Meta SWE coding round, one sliding window problem about finding the shortest substring with exactly N unique characters. Pretty focused session, nothing crazy beyond that one question.

Questions Asked (1)

Q1

Given a string and an integer n, find the length of the shortest substring that contains exactly n unique characters. Return 0 if no such substring exists.

Algorithms & Data Structures
Author's notes

Sliding window felt like the right move and it was, but the 'exactly n' constraint tripped me up at first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window (two-pointer) technique to efficiently find the shortest substring with exactly n unique characters. Expand the right pointer to include characters, and when the window has exactly n unique characters, shrink from the left to find the minimal length. Track the minimum length and return 0 if no such substring exists.

Pro tip: Clarify edge cases upfront, such as n=0, n greater than the number of unique characters in the string, or empty string. Also, discuss how you would handle the case where the window has more than n unique characters—by moving the left pointer until the unique count drops to n.

1. Clarify requirements and edge cases

Confirm the definition of 'substring' (contiguous) and 'unique characters'. Discuss edge cases: n=0, n > total unique characters, empty string, and whether the string contains only lowercase letters or any characters.

2. Choose the sliding window approach

Explain that a brute-force check of all substrings would be O(n^2) or worse, so a sliding window with a hash map to count character frequencies achieves O(n) time.

3. Initialize pointers and data structures

Set left and right pointers to 0, use a hash map to track character counts in the current window, and initialize min_length to infinity.

4. Expand and contract the window

Move right to include characters. When the window has exactly n unique characters, update min_length and then move left to shrink the window while maintaining exactly n unique characters, updating min_length each time.

5. Return the result

After the loop, return min_length if it was updated, otherwise return 0 to indicate no valid substring exists.

Key Points to Mention

  • Sliding window technique with two pointers for O(n) time complexity.
  • Hash map (or array) to track character frequencies and count unique characters.
  • Handling the case when unique count exceeds n: move left pointer until unique count drops to n.
  • Updating the minimum length only when unique count equals n.
  • Edge cases: n=0, n > total unique characters, empty string.
  • Time and space complexity analysis: O(n) time, O(k) space where k is the number of unique characters.

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