← Questions

Type a word on a grid keyboard with a jump limit

0
GoogleCoding

A coding interview question asked at Google. It tests grid, bfs and dynamic programming.

You are typing a word with one finger on an unusual keyboard: an m x n grid of characters. Moving the finger from cell (r1, c1) to cell (r2, c2) covers a Manhattan distance of |r1 - r2| + |c1 - c2|. The finger can never move more than k in a single jump.

Implement:

def can_type(keyboard: list[str], k: int, word: str) -> bool

Return True if word can be typed: the finger starts on any key holding word[0], and every move to the key for the next letter is at most k away. Staying on the same key (distance 0) is allowed.

Notes and constraints

  • keyboard has m rows of equal length n; 1 <= m, n <= 50.
  • A character may appear on more than one key. You may use any copy.
  • 0 <= k, and word has up to 1000 characters.
  • If a letter of word is not on the keyboard, the answer is False. An empty word is True.

Example 1

keyboard = ["qwertyuiop",
            "asdfghjkl;",
            "zxcvbnm,./"]
can_type(keyboard, 2, "were")  -> True   # every jump is 1
can_type(keyboard, 8, "quiz")  -> False  # i (0,7) to z (2,0) is 9
can_type(keyboard, 9, "quiz")  -> True

Example 2 (repeated keys)

keyboard = ["ba...a",
            ".....c"]
can_type(keyboard, 1, "bac") -> False

The a beside b is 5 away from c, and the a above c is 5 away from b. No choice of a works.

Aim for better than trying every combination of key copies.

Clarifying questions

  • Is the first letter free to place, with no starting key or cost?
  • Is the distance always Manhattan on the grid, ignoring what keys lie in between?
  • Is the comparison case-sensitive, and can the keyboard hold spaces or punctuation that the word uses?
  • Do we only need True or False, or also the keys to press?

What a strong answer covers

  • Tracks the set of keys the finger can be on after each letter. It starts with every copy of word[0], and for each next letter keeps the copies within k of some key in the current set.
  • Explains why trying every combination of copies grows exponentially, while this set-based DP stays polynomial.
  • Bounds the cost: comparing every pair of copies costs up to (mn)^2 per letter. A multi-source Manhattan distance transform over the grid makes each letter O(mn), so the total is O(len(word) x mn).
  • Stops early when the set becomes empty, and indexes key positions by character once.
  • Tests a missing letter, the empty word, k = 0 with repeated letters, and the repeated-key example where no single choice of a works.

Follow-ups

  • Return the minimum total distance needed to type the word.
  • Return the sequence of keys pressed.
  • Some cells are blocked and the finger moves only through free keys: switch to BFS distances.
  • Two fingers that can each press the next letter.

Where it's asked

What it tests

grid · bfs · dynamic programming

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding