Alphabet Board Path
Updated
Problem statement
The 26 capital letters are laid out on a board in alphabet order, three letters per row:
A B C
D E F
G H I
J K L
M N O
P Q R
S T U
V W X
Y ZThe last row holds just Y and Z, under A and B. The spot to the right of Z is empty, and you can never step onto it.
You start on A. Given a word, spell it letter by letter. Each step moves you one cell up, down, left or right. When you reach the next letter of the word, you print that letter. If the next letter is the one you are already on, you print it again without moving.
Return everything you print as one string. Write each one-cell step as the word up, down, left or right, write each letter as itself, and put a single space between items.
To get from one letter to the next, first move left or right until you are in the next letter's column, then move up or down until you reach it. There is one exception: if you are on the last row and the next letter is in the right-hand column (C, F, I, ..., X), a step right would land on the empty spot, so move up to the next letter's row first, then right.
This order never wastes a step, so every word has exactly one correct answer.
Function
alphabetBoardPath(word: String) → StringExamples
Example 1
word = "BEEF"return = "right B down E E right F"From A, one step right reaches B, and one step down from there reaches E.
The next letter is E again, so it is printed with no step. One step right from E reaches F.
Example 2
word = "FIZZ"return = "right right down F down I left down down down down down down Z Z"Reaching F from A takes two steps right, then one step down. I is one row below F.
From I, one step left puts you in the column of Z, then six steps down reach Z. Moving down first would have run into the empty spot at the bottom of the right-hand column. The second Z needs no step.
Example 3
word = "YOU"return = "down down down down down down down down Y up up up up right right O down down U"Y is in the same column as A, eight rows lower, so it takes eight steps down.
O is in the right-hand column and you are on the last row, so the exception applies: four steps up reach M on the row of O, then two steps right reach O. U is two rows below O.
Constraints
1 <= word.length <= 100wordconsists of capital English lettersAtoZ.
More Zillow Onsite Interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
- Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
- Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Tic-Tac-Toe Winner on a Huge BoardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2016
- Most Frequent Three-Page SequencesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2016
Similar questions at other companies
- BitFont Part 1 - Render a CharacterStripe · ONSITE INTERVIEWStripe interview questions
- Cyclic Fuel Command SimulationWaymo · ONSITE INTERVIEWWaymo interview questions
- Simulate a Deterministic Finite AutomatonPoint72 · OAPoint72 interview questions
- Excel Training Data and Model Response AuditMicrosoft · PHONE SCREENMicrosoft interview questions
- Collect Letters Along a Unique Grid PathApple · OAApple interview questions
- Repair One Movement InstructionOpenAI · OAOpenAI interview questions
- Count Straight-Line Word Occurrences in a GridOptiver · OAOptiver interview questions
- Count Straight-Line Word Occurrences in a GridVisa · OAVisa interview questions
- Flip Rows and Columns, Count ZerosNutanix · OANutanix interview questions
- Hidden ArtifactsIMC Trading · OAIMC Trading interview questions