Magicsheet logo

Alphabet Board Path

Medium
25%
Updated 8/1/2025

Asked by 1 Company

Alphabet Board Path

What is this problem about?

The "Alphabet Board Path interview question" involves navigating a 2D grid that represents a keyboard. The board is a 5imes55 imes 5 grid containing 'a' through 'z' in order, with 'z' being alone on the last row. Given a target string, you need to find a sequence of moves ('U', 'D', 'L', 'R') and an '!' (to select a character) that spells out the string starting from 'a'.

Why is this asked in interviews?

Google uses the "Alphabet Board Path coding problem" to test a candidate's ability to map 1D data (the alphabet) to 2D coordinates and handle specific edge cases in a simulation. It evaluates coordinate geometry skills and the ability to think through ordering—specifically why moving to the letter 'z' requires a different sequence of moves than moving away from it.

Algorithmic pattern used

This problem follows the Coordinate Geometry and Simulation pattern.

  1. Mapping: Convert each character to a coordinate (row,col)(row, col). For example, 'a' is (0,0)(0, 0), 'b' is (0,1)(0, 1), and 'z' is (5,0)(5, 0). The formula is row = charCode / 5 and col = charCode % 5.
  2. Pathfinding: Calculate the difference between your current (r1,c1)(r1, c1) and target (r2,c2)(r2, c2).
  3. Edge Case Management (The 'z' rule): Because 'z' is at (5,0)(5, 0) and there are no characters at (5,1)(5, 1) through (5,4)(5, 4), you must be careful:
    • When moving to 'z', move Left first, then Down.
    • When moving away from 'z', move Up first, then Right. This prevents the path from stepping into the "empty" space on the last row.

Example explanation

Target: "bz"

  1. Start at 'a' (0,0)(0, 0). Target 'b' (0,1)(0, 1).
    • Move: Right ('R'), then '!'. Current: 'b'.
  2. Target 'z' (5,0)(5, 0).
    • Row diff: +5 (Down), Col diff: -1 (Left).
    • Order: Move Left ('L'), then Down ('DDDDD'), then '!'. Total Path: R!LDDDDD!

Common mistakes candidates make

  • Incorrect 'z' handling: Crashing the simulation by moving "Down" into the empty space next to 'z' before moving "Left."
  • Coordinate calculation: Using 1-based indexing instead of 0-based, which complicates the modulo math.
  • Inefficient pathing: Trying to use BFS for the path when simple coordinate differences (dx,dydx, dy) are much faster and sufficient.

Interview preparation tip

When dealing with grids, always check for "irregular" shapes. The Alphabet Board is almost a rectangle but has a single character in the last row. This is a huge hint that the order of 'U/D' and 'L/R' moves will matter.

Similar Questions