Follow a non-branching path through a grid and collect its letters in order

Quick Overview

Walk the single non-branching path of star and letter cells that starts at the top-left corner of a grid, and return the lowercase letters in the order the path reaches them. It tests grid traversal, bounds checks on neighbors and tracking visited cells so the walk never turns back.

Follow a non-branching path through a grid and collect its letters in order

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a 2D grid of characters. Starting from position `(0, 0)`, follow the unique path formed by `'*'` (star) characters and lowercase letters. At each step, move to an adjacent cell (up, down, left, right) that is part of the path and has not been visited yet. Collect all the lowercase letters encountered along the path in order and return the resulting string. Empty cells are represented by `'.'`. The cell at `(0, 0)` is always part of the path. The path is guaranteed to be unique (no branching), meaning at each step there is at most one unvisited neighbor that continues the path. ### Function Signature ```python def collect_path_letters(grid: list[list[str]]) -> str: ``` ### Rules - `grid` is a list of rows, and each row is a list of one-character strings. - Path cells are `'*'` and lowercase letters. A `'.'` cell is never entered. - The walk ends when the current cell has no unvisited path neighbor inside the grid. - A letter on `(0, 0)` counts. If the path holds no letters, return the empty string `""`. ### Constraints - `1 <= grid.length, grid[0].length <= 100` - Each cell is either `'.'`, `'*'`, or a lowercase English letter. - The path starting from `(0, 0)` is unique (no branching). - The cell `(0, 0)` is always part of the path. ### Examples ```text Input: grid = [["*","*",".","."],[".","h",".","."],[".","i","*","."],[".",".","*","."]] Output: "hi" ``` The path is `(0,0) -> (0,1) -> (1,1) -> (2,1) -> (2,2) -> (3,2)`. It meets `'h'` at `(1,1)` and then `'i'` at `(2,1)`. ```text Input: grid = [["a","*","b"],[".",".","*"],[".",".","c"]] Output: "abc" ``` The path is `(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2)`. The start cell itself holds `'a'`, followed by `'b'` at `(0,2)` and `'c'` at `(2,2)`. ```text Input: grid = [["*",".","a"],["*",".","b"],["*","*","*"]] Output: "ba" ``` The path goes down the left column, along the bottom row and up the right column, so it meets `'b'` at `(1,2)` before `'a'` at `(0,2)`.

Overview: Walk the single non-branching path of star and letter cells that starts at the top-left corner of a grid, and return the lowercase letters in the order the path reaches them. It tests grid traversal, bounds checks on neighbors and tracking visited cells so the walk never turns back.

Apple logo
Apple
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a 2D grid of characters. Starting from position (0, 0), follow the unique path formed by '*' (star) characters and lowercase letters. At each step, move to an adjacent cell (up, down, left, right) that is part of the path and has not been visited yet.

Collect all the lowercase letters encountered along the path in order and return the resulting string.

Empty cells are represented by '.'. The cell at (0, 0) is always part of the path. The path is guaranteed to be unique (no branching), meaning at each step there is at most one unvisited neighbor that continues the path.

Function Signature

def collect_path_letters(grid: list[list[str]]) -> str:

Rules

  • grid is a list of rows, and each row is a list of one-character strings.
  • Path cells are '*' and lowercase letters. A '.' cell is never entered.
  • The walk ends when the current cell has no unvisited path neighbor inside the grid.
  • A letter on (0, 0) counts. If the path holds no letters, return the empty string "" .

Constraints

  • 1 <= grid.length, grid[0].length <= 100
  • Each cell is either '.' , '*' , or a lowercase English letter.
  • The path starting from (0, 0) is unique (no branching).
  • The cell (0, 0) is always part of the path.

Examples

Input: grid = [["*","*",".","."],[".","h",".","."],[".","i","*","."],[".",".","*","."]]
Output: "hi"

The path is (0,0) -> (0,1) -> (1,1) -> (2,1) -> (2,2) -> (3,2). It meets 'h' at (1,1) and then 'i' at (2,1).

Input: grid = [["a","*","b"],[".",".","*"],[".",".","c"]]
Output: "abc"

The path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2). The start cell itself holds 'a', followed by 'b' at (0,2) and 'c' at (2,2).

Input: grid = [["*",".","a"],["*",".","b"],["*","*","*"]]
Output: "ba"

The path goes down the left column, along the bottom row and up the right column, so it meets 'b' at (1,2) before 'a' at (0,2).