21. Word Search
mediumAsked at Electronic ArtsSearch for a word in a 2D character grid using backtracking DFS, a direct analog to EA's tile-map traversal and game-world pathfinding interview problems.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
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.
Constraints
m == board.length, n == board[i].length1 <= m, n <= 61 <= word.length <= 15board and word consist of only lowercase and uppercase English letters
Examples
Example 1
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"trueExample 2
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"falseApproaches
1. Brute force DFS without marking
Try DFS from every cell, pass a visited set — correct but uses extra space.
- Time
- O(m*n*4^L)
- Space
- O(m*n)
// For each cell, DFS checking all 4 neighbors
// track visited set to avoid reuse
// Time: O(m*n*4^L), Space: O(m*n) for visited setTradeoff:
2. Backtracking with in-place marking
Mark visited cells by temporarily replacing with a sentinel character, recurse into neighbors, then restore. EA interviewers reward the in-place trick as it avoids allocating a visited set for each DFS branch.
- Time
- O(m*n*4^L)
- Space
- O(L)
function exist(board, word) {
const m = board.length, n = board[0].length;
function dfs(r, c, idx) {
if (idx === word.length) return true;
if (r < 0 || r >= m || c < 0 || c >= n || board[r][c] !== word[idx]) return false;
const tmp = board[r][c];
board[r][c] = '#';
const found = dfs(r+1,c,idx+1)||dfs(r-1,c,idx+1)||dfs(r,c+1,idx+1)||dfs(r,c-1,idx+1);
board[r][c] = tmp;
return found;
}
for (let r = 0; r < m; r++)
for (let c = 0; c < n; c++)
if (dfs(r, c, 0)) return true;
return false;
}Tradeoff:
Electronic Arts-specific tips
EA interviews cover game development data structures, pathfinding (A*, BFS on grids), spatial algorithms, and simulation problems. Grid/matrix manipulation and BFS on 2D arrays are very common.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Word Search and other Electronic Arts interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →