← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Google SWE coding round, one main string problem with a follow-up. Pretty focused session, nothing too wild, but the edge case conversation took up more time than I expected.

Questions Asked (2)

Q1

Given a string and a target string, count how many substrings of the original string have the target as a prefix.

Algorithms & Data Structures
Author's notes

My first instinct was to just slide a window and check prefix matches, which works fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases, then propose an efficient algorithm. A good approach is to use a string matching algorithm like KMP to find all occurrences of the target in the original string, and for each occurrence at index i, the number of substrings starting at i is (n - i), where n is the length of the original string. Sum these counts to get the total.

Pro tip: Mention the trade-offs between different approaches (e.g., naive O(n*m) vs. KMP O(n+m)) and discuss how to handle overlapping occurrences. Also, consider if the target is empty or longer than the original string.

1. Clarify the problem

Ask clarifying questions: Are we counting distinct substrings or all substrings (including duplicates)? Should we consider overlapping occurrences? What are the constraints on string lengths and character set?

2. Discuss naive approach

Explain a simple solution: iterate over each starting index i, check if the substring from i of length equal to target matches the target. If yes, increment count by (n - i). This is O(n*m) time.

3. Optimize with string matching

Propose using an efficient string matching algorithm like KMP or Rabin-Karp to find all occurrences of the target in O(n+m) time. For each match at index i, add (n - i) to the count.

4. Handle edge cases

Consider cases: target is empty (then every substring has empty prefix, so count = n*(n+1)/2), target longer than original (count = 0), and overlapping matches (handled naturally by the algorithm).

5. Analyze complexity and test

State time and space complexity: O(n+m) time, O(m) space for KMP. Walk through a small example to verify correctness, e.g., original='aaaa', target='aa' -> matches at indices 0,1,2 -> counts: 4,3,2 -> total 9.

Key Points to Mention

  • Definition of substring and prefix
  • Naive approach and its O(n*m) complexity
  • KMP algorithm for efficient pattern matching
  • Handling overlapping occurrences
  • Edge cases: empty target, target longer than original
  • Time and space complexity analysis

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

Q2

Follow-up: find the minimum length of a substring that contains the target string.

Algorithms & Data Structures
Author's notes

This tripped me up more than it should have.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem: given a string S and a target string T, find the minimum length substring of S that contains all characters of T (considering duplicates). Use a sliding window with two pointers and a frequency map to track character counts, expanding the right pointer until all characters are matched, then shrinking the left pointer to find the minimal window.

Pro tip: After presenting the optimal solution, mention that if the target has many distinct characters, you can optimize the 'formed' check by tracking a counter of satisfied unique characters instead of comparing full frequency maps each time.

1. Clarify requirements and edge cases

Confirm whether the substring must contain the target as a subsequence or with exact character counts, and handle cases like empty target, target longer than source, or no valid window.

2. Choose sliding window approach

Explain that a brute-force check of all substrings is O(n^2 * m), so a sliding window with two pointers achieves O(n) time by maintaining a window that satisfies the target's character counts.

3. Define data structures and invariants

Use a frequency map for the target and a window frequency map, plus a 'formed' counter to track how many unique characters have met the required count. Maintain the invariant that the window is the smallest valid window ending at the right pointer.

4. Walk through the algorithm

Expand right pointer, update window counts and formed counter. When formed equals required unique characters, shrink left pointer while the window remains valid, updating the minimum length and start index.

5. Analyze complexity and test

State time complexity O(n + m) and space O(k) where k is unique characters in target. Walk through a small example to verify correctness, and discuss potential optimizations like early termination if minimum length equals target length.

Key Points to Mention

  • Sliding window technique with two pointers (left and right)
  • Frequency maps to track character counts in target and current window
  • Using a 'formed' counter to avoid comparing full maps each iteration
  • Time complexity O(n + m) and space complexity O(k) where k is unique characters
  • Handling edge cases: no valid window, target longer than source, duplicate characters
  • Potential optimization: early exit when minimum length equals target length

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