Word Path Moving Only Right or Down
Updated
Problem statement
Find a path spelling word in a rectangular character grid. The word may start at any cell. Each next letter must be in the immediately adjacent cell to the right or directly below the current cell. The word is guaranteed to be present.
Return one coordinate [row,column] for each letter, in word order. For this exercise, assume zero-based coordinates and represent the grid as an array of equal-length row strings.
Several valid paths may exist. For this exercise, assume the lexicographically smallest complete coordinate path must be returned. Compare paths at the first differing coordinate, comparing row before column. This chooses starting cells in row-major order and prefers a feasible right continuation over a feasible down continuation.
For this exercise, assume all grid and word characters are lowercase English letters and both the grid and word are nonempty.
Function
gridPath(grid: String[], word: String) → int[][]Examples
Example 1
grid = ["cctnax","ccatnt","acnntt","tniipp","aoooaa","saaaoo","kaioki"]word = "catnip"return = [[1,1],[1,2],[1,3],[2,3],[3,3],[3,4]]The path spells catnip using only adjacent right and down moves.
Example 2
grid = ["cctnax","ccatnt","acnntt","tniipp","aoooaa","saaaoo","kaioki"]word = "cccc"return = [[0,0],[0,1],[1,1],[2,1]]Two paths spell the word. The lexicographically smallest path goes right from [0,0] before going down.
Constraints
- For this exercise, assume grid height and width are each from
1through30. - All row strings have the same length.
1 <= word.length <= height + width - 1.- At least one right/down path spells the word.
More Indeed Phone Screen questions
- Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Course Completion OrderPHONE SCREEN · Seen May 2025
- Recipes You Can PreparePHONE SCREEN · Seen May 2025
- Rotting Orange PropagationPHONE SCREEN · Seen May 2025
- Courses Shared by Every Student PairPHONE SCREEN · Seen Aug 2022
- First and Last Access Time per UserPHONE SCREEN · Seen Jul 2022
- Most Accessed Resource in Five MinutesPHONE SCREEN · Seen Jul 2022
- Resource Transition GraphPHONE SCREEN · Seen Jul 2022
- Count Disappearing Connected TilesPHONE SCREEN · Seen Jul 2022
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Jul 2022
Similar questions at other companies
- Enumerate Right-and-Down Matrix PathsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Word Path Moving Only Right or DownWayfair · PHONE SCREENWayfair interview questions
- Enumerate Right-and-Down Matrix PathsOracle · ONSITE INTERVIEWOracle interview questions
- Find a Word Path Moving Right or DownIntuit · OAIntuit interview questions
- Count Eight-Neighbor Unlock PatternsGoogle · ONSITE INTERVIEWGoogle interview questions
- Empty ShelfSquarepoint Capital · OASquarepoint Capital interview questions
- Count Complete Tic-Tac-Toe GamesDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions
- Unique Grid PathsMorgan Stanley · PHONE SCREENMorgan Stanley interview questions
- Count Starts Reaching a Grid MaximumDuolingo · PHONE SCREENDuolingo interview questions
- Find Robots by Blocker DistancesUber · PHONE SCREENUber interview questions