FrontendInterviews.dev

Loading problem…

50. Word Search - Grid Search

Medium•
Acceptance: 75.00%
•
🔓3/3 Pro unlocks today

You're building a word search feature that needs to find if a word exists in a 2D grid of characters. This is useful for games, puzzles, and text search.

Given an m x n grid of characters board and a string word, return true if word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

Requirements

1. Basic Functionality

  • Search for word in 2D grid
  • Letters must be adjacent (horizontal or vertical)
  • Cannot reuse same cell
  • Return true if word found, false otherwise

Example Usage

const board = [
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
];
exist(board, "ABCCED"); // true
exist(board, "SEE"); // true
exist(board, "ABCB"); // false

Real-World Context

This problem models real search scenarios:

  • Word games: Find words in grid
  • Puzzle solving: Search patterns in grid
  • Text search: 2D text search
  • Path finding: Find paths in grid

Constraints

  • m == board.length
  • n = board[i].length
  • 1 <= m, n <= 6
  • 1 <= word.length <= 15
  • board and word consists of only lowercase and uppercase English letters