Magicsheet logo

Keyboard Row

Easy
88.9%
Updated 6/1/2025

Keyboard Row

1. What is this problem about?

The Keyboard Row interview question asks you to filter a list of words. A word is valid if all its characters can be typed using only one row of a standard QWERTY keyboard. The three rows are:

  1. "qwertyuiop"
  2. "asdfghjkl"
  3. "zxcvbnm"

2. Why is this asked in interviews?

Companies like Apple and Microsoft ask the Keyboard Row coding problem to assess a candidate's basic string manipulation and set logic. It evaluations whether you can use Hash Table interview patterns to map characters to rows and efficiently validate a condition across all characters of a string.

3. Algorithmic pattern used

This problem follows the Pre-processing and Filtering pattern.

  1. Map Construction: Create a map or array of size 26 where each letter points to its row number (1, 2, or 3).
  2. Iterate Words: Loop through each word in the input array.
  3. Validate Word:
    • Take the row of the first character as the targetRow.
    • Check if all other characters in the word belong to the same targetRow.
  4. Collect: If valid, add the word to the result list.

4. Example explanation

words = ["Hello", "Alaska", "Dad", "Peace"]

  • "Hello": 'H' (Row 2), 'e' (Row 1). Mixed. Invalid.
  • "Alaska": 'A', 'l', 'a', 's', 'k', 'a' are all in Row 2. Valid.
  • "Dad": 'D', 'a', 'd' are all in Row 2. Valid. Result: ["Alaska", "Dad"].

5. Common mistakes candidates make

  • Case Sensitivity: Forgetting to handle uppercase and lowercase characters (either by normalizing to lowercase or mapping both).
  • Manual Comparison: Writing three nested loops or long if chains instead of using a single mapping structure.
  • Empty Strings: Not clarifying if an empty string should be included (though rare in this problem).

6. Interview preparation tip

Whenever you have items belonging to fixed categories, use a Lookup Table (Map or Array). This is a foundational String interview pattern that replaces complex logic with a simple O(1)O(1) check.

Similar Questions