← Questions

Word search in a straight line

0
UberCodingLast asked May 2026

A coding interview question asked in Uber's coding round, last asked May 2026. It tests matrix, strings and search.

Given an m x n grid of uppercase letters board and a string word, decide whether word appears in the grid along a straight line.

A match starts at some cell and reads len(word) consecutive cells in one of the 8 directions: left, right, up, down, or one of the four diagonals. The direction may not change partway through the word, and the line may not wrap around the edge of the grid. (Because the direction is fixed, no cell is used twice.)

Implement:

  • exists(board, word) -> bool: whether the word appears anywhere.
  • find_all(board, word) -> list[(row, col, (dr, dc))]: every starting cell and direction where the word appears, in row-major order. Report a one-letter word once per matching cell.

Constraints

  • 1 <= m, n <= 200
  • 0 <= len(word) <= 200
  • An empty word counts as found.

Example

P L A N
E O R K
N I D S
S M T Y
  • exists(board, "PEN") returns true: column 0, going down.
  • exists(board, "NAL") returns true: row 0, going left.
  • exists(board, "POD") returns true: the diagonal from the top-left corner.
  • exists(board, "POLE") returns false. The letters are adjacent, but the path turns.
  • find_all(board, "POD") returns [(0, 0, (1, 1))].

Expect follow-ups: allow the path to turn (without reusing a cell), search for many words at once, or answer many queries against one large grid.

Clarifying questions

  • Is the word always uppercase, like the board, or should the match ignore case?
  • A palindrome matches from one cell in two directions. Should find_all report both? In which order are directions listed for the same cell?
  • What should find_all return for an empty word?
  • How many words are searched against one board?

What a strong answer covers

  • For each cell that holds word[0] and each of the 8 directions, checks that the last cell is inside the grid before comparing, then compares letter by letter and stops at the first mismatch. Worst case O(m·n·8·L), usually much less.
  • Agrees on the order of directions in find_all, and reports a one-letter word once per cell, not eight times.
  • Edge cases: the empty word, a word longer than both sides, a one-row grid, and palindromes found in two directions.
  • Tests the example, including "POLE" (false, the path turns) and "NAL" (read right to left).

Follow-ups

  • The path may turn but never reuse a cell: depth-first search with backtracking.
  • Search for many words at once with a trie over the words.
  • Many queries against one large grid: index the positions of each letter, or hash every line in each direction.

Where it's asked

  • Uber · Coding · Last asked May 202660 min · Two rounds; one is often object-oriented design (a parking lot, a meeting scheduler, an expression evaluator) · No AI, except a newer AI-assisted debugging round in some senior loops

What it tests

matrix · strings · search

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding