← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Got a string manipulation problem at Google, pretty straightforward premise but the edge cases tripped me up a bit.

Questions Asked (1)

Q1

Given two strings, write a function that returns true if you can change exactly 2 characters in the first string to make it equal to the second string, and false otherwise.

Algorithms & Data Structures
Author's notes

Seemed simple at first glance.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify edge cases: strings must be of equal length, and exactly two characters must differ. Then, iterate through both strings simultaneously, counting mismatches; return true only if the count is exactly 2.

Pro tip: Mention that if the strings are identical, the answer is false because zero changes are needed, not exactly two. Also, handle unequal lengths immediately by returning false.

1. Clarify requirements and edge cases

Confirm that 'change exactly 2 characters' means the strings must differ in exactly two positions, and that the strings must be of equal length. Discuss edge cases like empty strings, identical strings, and strings with more than two differences.

2. Check length equality

If the lengths of the two strings are not equal, return false immediately, as it's impossible to make them equal by changing characters.

3. Count mismatches

Iterate through both strings simultaneously, comparing characters at each index. Increment a mismatch counter whenever characters differ.

4. Return based on mismatch count

After the loop, return true if the mismatch count is exactly 2, otherwise return false. Optionally, you can early-exit if the count exceeds 2 to optimize.

5. Test with examples

Walk through test cases: identical strings (false), one difference (false), two differences (true), three differences (false), and different lengths (false).

Key Points to Mention

  • Time complexity: O(n) where n is the length of the strings, as we only need a single pass.
  • Space complexity: O(1) since we only use a counter variable.
  • Edge case: strings of different lengths should return false immediately.
  • Edge case: identical strings have zero mismatches, so return false.
  • Early termination: if mismatches exceed 2, we can return false early to save time.
  • The function should return a boolean value, not the modified string.

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