Challenge - 5 Problems
Word Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Word Search Result for Simple Grid
What is the output of the following code that checks if the word "CAT" exists in the grid?
DSA Javascript
const board = [ ['C', 'A', 'T'], ['X', 'Y', 'Z'], ['A', 'B', 'C'] ]; function exist(board, word) { const rows = board.length; const cols = board[0].length; const visited = Array.from({ length: rows }, () => Array(cols).fill(false)); function backtrack(r, c, index) { if (index === word.length) return true; if (r < 0 || c < 0 || r >= rows || c >= cols) return false; if (visited[r][c] || board[r][c] !== word[index]) return false; visited[r][c] = true; const res = backtrack(r + 1, c, index + 1) || backtrack(r - 1, c, index + 1) || backtrack(r, c + 1, index + 1) || backtrack(r, c - 1, index + 1); visited[r][c] = false; return res; } for (let i = 0; i < rows; i++) { for (let j = 0; j < cols; j++) { if (backtrack(i, j, 0)) return true; } } return false; } console.log(exist(board, "CAT"));
Attempts:
2 left
💡 Hint
Think about whether the letters C, A, T appear consecutively in the grid horizontally or vertically.
✗ Incorrect
The word "CAT" appears in the first row of the grid from left to right, so the function returns true.
❓ Predict Output
intermediateOutput of Word Search for Word Not Present
What is the output of the code when searching for the word "ODG" in the given grid?
DSA Javascript
const board = [ ['D', 'O', 'G'], ['A', 'B', 'C'], ['E', 'F', 'G'] ]; function exist(board, word) { const rows = board.length; const cols = board[0].length; const visited = Array.from({ length: rows }, () => Array(cols).fill(false)); function backtrack(r, c, index) { if (index === word.length) return true; if (r < 0 || c < 0 || r >= rows || c >= cols) return false; if (visited[r][c] || board[r][c] !== word[index]) return false; visited[r][c] = true; const res = backtrack(r + 1, c, index + 1) || backtrack(r - 1, c, index + 1) || backtrack(r, c + 1, index + 1) || backtrack(r, c - 1, index + 1); visited[r][c] = false; return res; } for (let i = 0; i < rows; i++) { for (let j = 0; j < cols; j++) { if (backtrack(i, j, 0)) return true; } } return false; } console.log(exist(board, "ODG"));
Attempts:
2 left
💡 Hint
Check if the letters O, D, G appear consecutively in the grid horizontally or vertically without reusing cells.
✗ Incorrect
The word "ODG" is not present in the grid in consecutive adjacent cells in the required order, so the function returns false.
❓ Predict Output
intermediateOutput of Word Search for Word "GDO" Not Present
What is the output of the code when searching for the word "GDO" in the given grid?
DSA Javascript
const board = [ ['D', 'O', 'G'], ['A', 'B', 'C'], ['E', 'F', 'G'] ]; function exist(board, word) { const rows = board.length; const cols = board[0].length; const visited = Array.from({ length: rows }, () => Array(cols).fill(false)); function backtrack(r, c, index) { if (index === word.length) return true; if (r < 0 || c < 0 || r >= rows || c >= cols) return false; if (visited[r][c] || board[r][c] !== word[index]) return false; visited[r][c] = true; const res = backtrack(r + 1, c, index + 1) || backtrack(r - 1, c, index + 1) || backtrack(r, c + 1, index + 1) || backtrack(r, c - 1, index + 1); visited[r][c] = false; return res; } for (let i = 0; i < rows; i++) { for (let j = 0; j < cols; j++) { if (backtrack(i, j, 0)) return true; } } return false; } console.log(exist(board, "GDO"));
Attempts:
2 left
💡 Hint
Check if the letters G, D, O appear consecutively in the grid horizontally or vertically without reusing cells.
✗ Incorrect
The word "GDO" is not present in the grid in consecutive adjacent cells in the required order, so the function returns false.
🔧 Debug
advancedIdentify the Bug in Word Search Backtracking Code
The following code is intended to find if a word exists in a grid using backtracking. However, it sometimes returns incorrect results. What is the bug?
DSA Javascript
function exist(board, word) {
const rows = board.length;
const cols = board[0].length;
const visited = Array.from({ length: rows }, () => Array(cols).fill(false));
function backtrack(r, c, index) {
if (index === word.length) return true;
if (r < 0 || c < 0 || r >= rows || c >= cols) return false;
if (visited[r][c]) return false;
if (board[r][c] !== word[index]) return false;
visited[r][c] = true;
const res = backtrack(r + 1, c, index + 1) ||
backtrack(r - 1, c, index + 1) ||
backtrack(r, c + 1, index + 1) ||
backtrack(r, c - 1, index + 1);
visited[r][c] = false;
return res;
}
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (backtrack(i, j, 0)) return true;
}
}
return false;
}Attempts:
2 left
💡 Hint
Think about what happens after exploring one path and backtracking to try another.
✗ Incorrect
The visited cells must be marked as unvisited after exploring a path to allow other paths to use those cells. Without resetting visited[r][c] to false, the function wrongly blocks valid paths.
🚀 Application
expertMinimum Steps to Form Word in Grid with Backtracking
Given a grid and a word, you want to find the minimum number of steps to form the word by moving horizontally or vertically to adjacent cells. Each step moves to one adjacent cell. You can start at any cell matching the first letter. Which approach best solves this problem?
Attempts:
2 left
💡 Hint
Think about shortest path in a grid and how to explore multiple starting points efficiently.
✗ Incorrect
BFS is suitable for shortest path problems in grids. Starting BFS from all cells matching the first letter simultaneously finds the minimum steps to form the word efficiently.
