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.
Return one coordinate [row,column] for every letter, in word order. For this exercise, assume zero-based coordinates and equal-length lowercase row strings. Do not modify the input.
In this exercise, return the smallest complete path in lexicographic order: compare the first coordinate where two paths differ, first by row and then by column. Try starting cells in row-major order and prefer right before down only when that continuation can complete the word.
For this exercise, assume the word may be absent. Return an empty coordinate array when no permitted path spells it.
Function
gridPath(grid: String[], word: String) → int[][]Examples
Example 1
grid = ["ab","bd"]word = "abd"return = [[0,0],[0,1],[1,1]]Both a-right-b-down-d and a-down-b-right-d work. The right continuation gives the lexicographically smaller coordinate path.
Example 2
grid = ["ax","bc"]word = "abc"return = [[0,0],[1,0],[1,1]]The path moves down and then right; the right neighbor of the start cannot supply b.
Example 3
grid = ["ab","cd"]word = "acb"return = []The letter a can move down to c, but no permitted next cell contains b, so no complete path exists.
Constraints
- For this exercise, assume grid height
Rand widthCare each from1through12, with all rows equally long. - For this exercise, assume
1 <= word.length <= 23. A target longer than any possible right/down path is valid and returns an empty result. - For this exercise, assume all grid and word characters are lowercase English letters.
More Wayfair interview questions
- Container With Most WaterPHONE SCREEN · Seen Sep 2026
- Assign Distinct Letter Values to Minimize XORPHONE SCREEN · Seen Sep 2026
- First Three Badge Times per UserPHONE SCREEN · Seen Apr 2022
- Time of Maximum Mall OccupancyPHONE SCREEN · Seen Apr 2022
- Longest Common Contiguous User ActionsPHONE SCREEN · Seen Apr 2022
- Count a Person’s Distinct FriendsPHONE SCREEN · Seen Mar 2022
- Tree Nodes with Zero or One ParentPHONE SCREEN · Seen Sep 2021
- UCB1 Multi-Armed Bandit TraceONSITE INTERVIEW · Seen Sep 2026
- Moving Cost by Volume and CategoryONSITE INTERVIEW · Seen Sep 2026
- DFS Subsequences of a String (For MLE)OA · Seen Sep 2024
Similar questions at other companies
- Word Path Moving Only Right or DownIndeed · PHONE SCREENIndeed interview questions
- Enumerate Right-and-Down Matrix PathsBloomberg LP · PHONE SCREENBloomberg LP 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