Keyboard-Aware Edit Distance for Ranking Autocorrect Suggestions

Last updated: November 29, 2026

Quick Overview

Compute the cost to transform a typed word into a candidate word using insertions and deletions costing 1 each, and substitutions costing 1 if the two letters are neighbors on a QWERTY keyboard and 2 otherwise. Return the minimum cost.

Apple
Coding & Algorithms
Software Engineer
Apple
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Compute the cost to transform a typed word into a candidate word using insertions and deletions costing 1 each, and substitutions costing 1 if the two letters are neighbors on a QWERTY keyboard and 2 otherwise. Return the minimum cost.

iOS keyboard autocorrect ranks candidates by how plausible a mistype is, so adjacent-key errors should be cheap. This onsite question tests 2D dynamic programming with a custom cost function and clean keyboard geometry.

What the Interviewer Expects
  • Set up the standard edit distance table
  • Plug in a substitution cost based on key adjacency
  • Model the staggered keyboard rows correctly
  • Reduce memory to two rows
  • Explain how this would rank multiple candidates
Key Topics to Cover
dynamic programming
edit distance
2D DP
string algorithms
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
  • Add transposition of adjacent letters such as 'teh' to 'the' at cost 1.
  • Rank 50,000 dictionary words quickly for each keystroke.
  • Use touch coordinates instead of discrete keys.
  • Learn costs per user from their correction history.
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

Input: lowercase strings typed and target. Output: minimum weighted edit cost. Same letters cost 0. Characters outside the letter keys are treated as non-adjacent to everything....

High-Level Design

Place each key at coordinates: row index, and column plus a stagger offset of 0, 0.5 and 1 for the three rows. Two keys are neighbors if their Euclidean distance is at most 1.2, which captures same-ro...


Submit Your Answer
Markdown supported