Quick Overview

Test grid reachability through row or column jumps whose word endpoints match, using BFS, cycle prevention, and optional character-based adjacency indexes.

Reach the Bottom-Right Cell Through Row or Column Word Matches

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: HR Screen

A rectangular grid contains nonempty strings. From a cell, you may jump to another cell in the same row or column when the destination string's first character equals the current string's last character. Determine whether the bottom-right cell is reachable from the top-left cell. ### Constraints & Assumptions The source's wording refers to first and last letters; this practice formulation interprets cells as nonempty strings. Jumps may span any distance within the same row or column, matching is case-sensitive, and there are no obstacles beyond the match rule. Confirm these details if the original grid representation differs. ### Clarifying Questions to Ask - May a jump skip intervening cells, or must it be adjacent? - Are cell values strings, and is matching based on first/last character exactly? ```hint Treat each cell as a graph vertex The next valid cells depend on the current word's final character, not on physical adjacency alone. ``` ### What a Strong Answer Covers - Graph reachability with a visited set to prevent cycles. - Checking every matching destination in the current row and column. - Empty-grid and one-cell behavior, with a justified complexity bound. ### Follow-up Questions - How could row/column indexes keyed by first character reduce repeated scans? - What extra state is needed if each cell may be used only a limited number of times?

Overview: Test grid reachability through row or column jumps whose word endpoints match, using BFS, cycle prevention, and optional character-based adjacency indexes.

Given a rectangular `grid` of nonempty strings, determine whether the bottom-right cell is reachable from the top-left cell. From the current cell, you may jump to a different cell at any distance in the same row or column if the destination string's first character equals the current string's last character. Compare characters as Unicode code points, with case-sensitive matching. Return false if the grid has no cells. A one-cell grid is already at the destination.

Constraints

  • The grid is rectangular. Each dimension and the total cell count fit a signed 32-bit length.
  • Each cell is a nonempty, well-formed Unicode string.
  • Matching is exact and case-sensitive. A jump may land only within the same row or column.

Examples

Input: ([["ab", "xx", "bc"], ["zz", "uv", "cd"]],)

Expected Output: True

Explanation: Jump across the first row from ab to bc, then down to cd.

Input: ([["ab", "zx"], ["xy", "bc"]],)

Expected Output: False

Explanation: The bottom-right word starts with b, but it is diagonal from the start and no first jump is possible.

Hints

  1. When tracing a route, list which destinations share a row or column with the current cell.

Reach the Bottom-Right Cell Through Row or Column Word Matches

mediumGoogleSoftware Engineer

Given a rectangular grid of nonempty strings, determine whether the bottom-right cell is reachable from the top-left cell. From the current cell, you may jump to a different cell at any distance in the same row or column if the destination string's first character equals the current string's last character. Compare characters as Unicode code points, with case-sensitive matching.

Return false if the grid has no cells. A one-cell grid is already at the destination.

View full question (all parts)

A rectangular grid contains nonempty strings. From a cell, you may jump to another cell in the same row or column when the destination string's first character equals the current string's last character. Determine whether the bottom-right cell is reachable from the top-left cell.

Constraints & Assumptions

The source's wording refers to first and last letters; this practice formulation interprets cells as nonempty strings. Jumps may span any distance within the same row or column, matching is case-sensitive, and there are no obstacles beyond the match rule. Confirm these details if the original grid representation differs.

Clarifying Questions to Ask

  • May a jump skip intervening cells, or must it be adjacent?
  • Are cell values strings, and is matching based on first/last character exactly?

What a Strong Answer Covers

  • Graph reachability with a visited set to prevent cycles.
  • Checking every matching destination in the current row and column.
  • Empty-grid and one-cell behavior, with a justified complexity bound.

Follow-up Questions

  • How could row/column indexes keyed by first character reduce repeated scans?
  • What extra state is needed if each cell may be used only a limited number of times?

Examples

Example 1
Input
([["ab", "xx", "bc"], ["zz", "uv", "cd"]],)
Output
True
Notes
Jump across the first row from ab to bc, then down to cd.
Example 2
Input
([["ab", "zx"], ["xy", "bc"]],)
Output
False
Notes
The bottom-right word starts with b, but it is diagonal from the start and no first jump is possible.

Constraints

  • •The grid is rectangular. Each dimension and the total cell count fit a signed 32-bit length.
  • •Each cell is a nonempty, well-formed Unicode string.
  • •Matching is exact and case-sensitive. A jump may land only within the same row or column.

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 can_reach_word_grid_end(grid: list[list[str]]) -> bool:
Sample call
can_reach_word_grid_end([["ab", "xx", "bc"], ["zz", "uv", "cd"]])
Language
Case 1
Input
([["ab", "xx", "bc"], ["zz", "uv", "cd"]],)
Expected
True
Case 2
Input
([["ab", "zx"], ["xy", "bc"]],)
Expected
False

Browse More Questions

Show the approach

Approach

Index every cell twice, once by its row and first character and once by its column and first character. Start a graph search at the top-left cell. For each reached cell, use its last character to fetch the two buckets of possible destinations. Mark a destination before adding it to the search, which prevents cycles from revisiting cells.

A bucket can be removed after its first scan. Every cell in it is already reached or can be reached immediately from the scanning cell, aside from the scanning cell itself, which is already marked. Later visits to the same bucket cannot discover a new destination. The search therefore follows every outgoing jump that could reach a new cell, and it succeeds exactly when the bottom-right cell is reached.

Each cell occurs in exactly two buckets. Every reached cell is processed at most once, and each bucket is scanned at most once. Reading the first or last code point takes constant time in the corresponding Unicode representation.

Time complexity:
O(rows × columns) average traversal time with hash lookup, excluding decoding the input itself.
Space complexity:
O(rows × columns) auxiliary space.