Quick Overview

Find a word in a character grid with backtracking, orthogonal adjacency, and no cell reuse, including repeated-letter cases and path-state restoration.

Find a Word Along Adjacent Grid Cells

Company: Reddit

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a rectangular grid of characters and a word, determine whether the word can be formed by a path through horizontally or vertically adjacent cells. A cell may be used at most once in a path. ### Constraints & Assumptions This is the standard single-word formulation of the named Word Search problem. Diagonal moves are excluded, matching is case-sensitive, and the empty word is considered found. ### Clarifying Questions to Ask - Are diagonal moves or cell reuse permitted? - Is the task one target word or many words sharing the same grid? ```hint Undo each tentative path A cell unavailable to one candidate path must become available again when exploring another starting point. ``` ### What a Strong Answer Covers - Starting searches at matching cells and advancing the target index. - Path-local visited state with correct backtracking. - Bounds, repeated letters, and early rejection when the word is too long. ### Follow-up Questions - How would a trie help when searching many words? - What letter-frequency check can rule out impossible targets early?

Overview: Find a word in a character grid with backtracking, orthogonal adjacency, and no cell reuse, including repeated-letter cases and path-state restoration.

Given a rectangular `grid` of characters and a `word`, return whether a path through the grid spells the word. Start at any cell. Each move must go to a horizontally or vertically adjacent cell, and a cell can be used at most once in the path. Match Unicode code points exactly and case-sensitively. An empty word is found even if the grid is empty. A nonempty word cannot be found in a grid with no cells.

Constraints

  • The grid is rectangular. Each dimension and the total number of cells fit a signed 32-bit length.
  • Each cell is a string representing one well-formed Unicode code point.
  • word is a well-formed Unicode string of at most 2^31 - 1 UTF-8 bytes.

Examples

Input: ([["C", "A", "T"], ["N", "R", "D"], ["O", "O", "G"]], "CARD")

Expected Output: True

Explanation: The path moves right to A, down to R, then right to D.

Input: ([["A", "S"], ["Z", "B"]], "AB")

Expected Output: False

Explanation: The only A and B are diagonal.

Hints

  1. Trace one tentative route and check which cells remain available after stepping back.

Find a Word Along Adjacent Grid Cells

mediumRedditSoftware Engineer

Given a rectangular grid of characters and a word, return whether a path through the grid spells the word. Start at any cell. Each move must go to a horizontally or vertically adjacent cell, and a cell can be used at most once in the path. Match Unicode code points exactly and case-sensitively.

An empty word is found even if the grid is empty. A nonempty word cannot be found in a grid with no cells.

View full question (all parts)

Given a rectangular grid of characters and a word, determine whether the word can be formed by a path through horizontally or vertically adjacent cells. A cell may be used at most once in a path.

Constraints & Assumptions

This is the standard single-word formulation of the named Word Search problem. Diagonal moves are excluded, matching is case-sensitive, and the empty word is considered found.

Clarifying Questions to Ask

  • Are diagonal moves or cell reuse permitted?
  • Is the task one target word or many words sharing the same grid?

What a Strong Answer Covers

  • Starting searches at matching cells and advancing the target index.
  • Path-local visited state with correct backtracking.
  • Bounds, repeated letters, and early rejection when the word is too long.

Follow-up Questions

  • How would a trie help when searching many words?
  • What letter-frequency check can rule out impossible targets early?

Examples

Example 1
Input
([["C", "A", "T"], ["N", "R", "D"], ["O", "O", "G"]], "CARD")
Output
True
Notes
The path moves right to A, down to R, then right to D.
Example 2
Input
([["A", "S"], ["Z", "B"]], "AB")
Output
False
Notes
The only A and B are diagonal.

Constraints

  • •The grid is rectangular. Each dimension and the total number of cells fit a signed 32-bit length.
  • •Each cell is a string representing one well-formed Unicode code point.
  • •word is a well-formed Unicode string of at most 2^31 - 1 UTF-8 bytes.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def word_exists_in_grid(grid: list[list[str]], word: str) -> bool:
Sample call
word_exists_in_grid([["C", "A", "T"], ["N", "R", "D"], ["O", "O", "G"]], "CARD")
Language
Case 1
Input
([["C", "A", "T"], ["N", "R", "D"], ["O", "O", "G"]], "CARD")
Expected
True
Case 2
Input
([["A", "S"], ["Z", "B"]], "AB")
Expected
False

Browse More Questions

Show the approach

Approach

Start a depth-first search at every cell matching the first character. Keep a stack of the cells along the current path. Each frame records which neighbor to try next. Mark a cell when pushing it, and unmark it when popping it after all its neighbors have been tried. Only push a cell that is adjacent, unmarked, and equal to the next character. Return true as soon as the stack matches the full word. A word longer than the number of cells is impossible.

The stack always spells a prefix of the word with adjacent, distinct cells. From each partial path, the search tries every way to extend that prefix by one character. Unmarking a cell on backtracking makes it available to later branches and starting cells. Therefore a path is returned exactly when one exists. Explicit frames let the search handle deep paths without depending on call-stack capacity.

Time complexity:
O(R × C × 3^L + L) worst case for an R by C grid and a nonempty word of L code points. The first move has at most four choices; subsequent moves cannot return to the previous cell.
Space complexity:
O(R × C + L) auxiliary space for visited cells, the search stack, and the indexed word representation.