Check Whether Two Search Queries Are Exactly One Edit Apart

Last updated: November 29, 2026

Quick Overview

Given two strings, return true if one can be turned into the other with exactly one insertion, deletion, or substitution of a single character.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given two strings, return true if one can be turned into the other with exactly one insertion, deletion, or substitution of a single character.

Framed around query spelling suggestions in Google Search, this question checks whether you avoid running full edit distance when a linear two-pointer scan suffices. Interviewers listen for a crisp case split on length difference and for attention to the exactly-one requirement.

What the Interviewer Expects
  • Rejects immediately when lengths differ by more than one
  • Uses a single two-pointer pass instead of O(nm) dynamic programming
  • Treats identical strings as zero edits, so false
  • Handles insertion and deletion symmetrically by swapping to make the first string shorter
  • Tests edge cases like empty strings
Key Topics to Cover
Strings
Two Pointers
Edit Distance
Spell Correction
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Generalize to at most k edits; what is the best complexity you can get?
  • How would you find all dictionary words within one edit of a query quickly?
  • What if transposition of adjacent characters also counts as one edit?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Inputs: two strings a, b of arbitrary characters.
  • Return true only if the edit distance is exactly 1.
  • Equal strings return false. Target O(n) time and O(1) extra space....
High-Level Design

If the lengths differ by more than 1, answer false. Make a the shorter (or equal) string. Walk both with indices i and j. On the first mismatch: if lengths are equal, it must be a substitution, ...


Submit Your Answer
Markdown supported