Greedy Grid Word Occurrences with No Reused Cells
Updated
Problem statement
Find repeated occurrences of word in a rectangular character grid. Return their coordinates without using a cell in more than one selected occurrence.
No cell may be used by more than one completed occurrence. Each path starts anywhere and moves only to an adjacent cell to the right or directly below.
For this exercise, follow these steps:
- Find the paths that spell the whole word using only unused cells.
- Select the lexicographically smallest coordinate sequence.
- Mark every cell in that path as permanently used.
- Repeat until no complete path remains.
Compare coordinates by row then column, using zero-based indices. A failed exploratory branch never consumes a cell.
This greedy procedure is the required rule; it need not maximize the number of occurrences.
Return all selected coordinates in one int[][]. For this exercise, assume each consecutive block of word.length rows represents one path, and blocks appear in selection order. If no path exists, return an empty array.
Function
disjointWordPaths(grid: String[], word: String) → int[][]Examples
Example 1
grid = ["abab","abab"]word = "ab"return = [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3]]This example selects four consecutive two-cell paths and uses each cell once.
Example 2
grid = ["aa","aa"]word = "aa"return = [[0,0],[0,1],[1,0],[1,1]]This example selects the top horizontal path before the bottom horizontal path.
Example 3
grid = ["aaa","abb","aaa"]word = "aab"return = [[0,0],[0,1],[1,1]]This example selects [0,0],[0,1],[1,1] and stops. A different choice could produce two paths, but maximizing their number is not the stated rule.
Constraints
- For this exercise, assume grid height and width are each from
1through10, and row strings have equal length. 1 <= word.length <= height + width - 1.- All characters are lowercase English letters.
- No coordinate occurs twice in the result.
More Compass International Holdings Phone Screen questions
- Decode StringPHONE SCREEN · Seen Oct 2026
- Lowest Common Ancestors in a Parent DAGPHONE SCREEN · Seen Apr 2022
- Can One Dictionary Word Supply the Target Letters?PHONE SCREEN · Seen Apr 2022
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Apr 2022
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Mar 2022
- Word Path Moving Only Up or RightPHONE SCREEN · Seen Feb 2022
- Top Five Stock Records in a Date RangePHONE SCREEN · Seen Oct 2021
- Wrap Words from Existing Text LinesPHONE SCREEN · Seen Apr 2021
- All Common Ancestors in a Parent GraphPHONE SCREEN · Seen Jan 2021
- Nodes With Zero or Three ParentsPHONE SCREEN · Seen Jan 2021
Similar questions at other companies
- Classify Password StrengthHackerRank · OA · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Dasher Pay with Store Waits and Peak HoursDoorDash · PHONE SCREENDoorDash interview questions
- Fuzzy Word Lookup With One Misspelled LetterMeta · PHONE SCREENMeta interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Group Timestamps into Consecutive-Day IntervalsDuolingo · PHONE SCREENDuolingo interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Longest Substring Without Repeating CharactersAngel One · PHONE SCREEN