0
0
DSA Cprogramming~20 mins

Word Search in Grid Using Backtracking in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Word Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Word Search Result
What is the output of the following C code that checks if the word "CAT" exists in the given grid using backtracking?
DSA C
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define ROWS 3
#define COLS 4

bool dfs(char grid[ROWS][COLS], int r, int c, const char *word, int index, bool visited[ROWS][COLS]) {
    if (index == strlen(word)) return true;
    if (r < 0 || c < 0 || r >= ROWS || c >= COLS) return false;
    if (visited[r][c] || grid[r][c] != word[index]) return false;

    visited[r][c] = true;
    bool found = dfs(grid, r+1, c, word, index+1, visited) ||
                 dfs(grid, r-1, c, word, index+1, visited) ||
                 dfs(grid, r, c+1, word, index+1, visited) ||
                 dfs(grid, r, c-1, word, index+1, visited);
    visited[r][c] = false;
    return found;
}

bool exist(char grid[ROWS][COLS], const char *word) {
    bool visited[ROWS][COLS] = {false};
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (dfs(grid, i, j, word, 0, visited)) return true;
        }
    }
    return false;
}

int main() {
    char grid[ROWS][COLS] = {
        {'C', 'A', 'T', 'F'},
        {'B', 'G', 'E', 'S'},
        {'I', 'T', 'A', 'E'}
    };
    const char *word = "CAT";
    if (exist(grid, word)) {
        printf("Word found\n");
    } else {
        printf("Word not found\n");
    }
    return 0;
}
ARuntime error
BWord not found
CCompilation error
DWord found
Attempts:
2 left
💡 Hint
Check if the word "CAT" can be traced by moving up, down, left, or right in the grid.
Predict Output
intermediate
2:00remaining
Output of Word Search for Non-Existing Word
What is the output of the following C code that checks if the word "DOG" exists in the given grid using backtracking?
DSA C
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define ROWS 3
#define COLS 4

bool dfs(char grid[ROWS][COLS], int r, int c, const char *word, int index, bool visited[ROWS][COLS]) {
    if (index == strlen(word)) return true;
    if (r < 0 || c < 0 || r >= ROWS || c >= COLS) return false;
    if (visited[r][c] || grid[r][c] != word[index]) return false;

    visited[r][c] = true;
    bool found = dfs(grid, r+1, c, word, index+1, visited) ||
                 dfs(grid, r-1, c, word, index+1, visited) ||
                 dfs(grid, r, c+1, word, index+1, visited) ||
                 dfs(grid, r, c-1, word, index+1, visited);
    visited[r][c] = false;
    return found;
}

bool exist(char grid[ROWS][COLS], const char *word) {
    bool visited[ROWS][COLS] = {false};
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (dfs(grid, i, j, word, 0, visited)) return true;
        }
    }
    return false;
}

int main() {
    char grid[ROWS][COLS] = {
        {'C', 'A', 'T', 'F'},
        {'B', 'G', 'E', 'S'},
        {'I', 'T', 'A', 'E'}
    };
    const char *word = "DOG";
    if (exist(grid, word)) {
        printf("Word found\n");
    } else {
        printf("Word not found\n");
    }
    return 0;
}
AWord not found
BWord found
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Check if the word "DOG" can be formed by adjacent letters in the grid.
🔧 Debug
advanced
3:00remaining
Identify the Bug in Word Search Backtracking
The following C 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 C
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define ROWS 2
#define COLS 2

bool dfs(char grid[ROWS][COLS], int r, int c, const char *word, int index, bool visited[ROWS][COLS]) {
    if (index == strlen(word)) return true;
    if (r < 0 || c < 0 || r >= ROWS || c >= COLS) return false;
    if (visited[r][c] || grid[r][c] != word[index]) return false;

    visited[r][c] = true;
    bool found = dfs(grid, r+1, c, word, index+1, visited) ||
                 dfs(grid, r-1, c, word, index+1, visited) ||
                 dfs(grid, r, c+1, word, index+1, visited) ||
                 dfs(grid, r, c-1, word, index+1, visited);
    // Missing backtracking step here
    return found;
}

bool exist(char grid[ROWS][COLS], const char *word) {
    bool visited[ROWS][COLS] = {false};
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (dfs(grid, i, j, word, 0, visited)) return true;
        }
    }
    return false;
}

int main() {
    char grid[ROWS][COLS] = {
        {'A', 'B'},
        {'C', 'D'}
    };
    const char *word = "ABCD";
    if (exist(grid, word)) {
        printf("Word found\n");
    } else {
        printf("Word not found\n");
    }
    return 0;
}
AThe code uses strlen(word) inside dfs which is inefficient and causes wrong results.
BThe grid size is too small to find the word, so the code always returns false.
CThe code does not reset visited[r][c] to false after recursive calls, causing incorrect backtracking.
DThe code does not check diagonal neighbors, so it misses some valid paths.
Attempts:
2 left
💡 Hint
Backtracking requires undoing the visited mark after exploring neighbors.
🧠 Conceptual
advanced
2:00remaining
Why Use Backtracking for Word Search?
Why is backtracking the preferred method to solve the word search problem in a grid?
ABecause it sorts the grid letters first to speed up the search for the word.
BBecause it tries all possible paths and undoes choices when they don't lead to a solution, ensuring all options are explored.
CBecause it uses dynamic programming to store intermediate results and avoid repeated work.
DBecause it converts the grid into a graph and uses breadth-first search to find the word.
Attempts:
2 left
💡 Hint
Think about how the algorithm explores paths and handles dead ends.
Predict Output
expert
3:00remaining
Output of Word Search with Repeated Letters
What is the output of the following C code that searches for the word "ABA" in the grid using backtracking?
DSA C
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define ROWS 2
#define COLS 3

bool dfs(char grid[ROWS][COLS], int r, int c, const char *word, int index, bool visited[ROWS][COLS]) {
    if (index == strlen(word)) return true;
    if (r < 0 || c < 0 || r >= ROWS || c >= COLS) return false;
    if (visited[r][c] || grid[r][c] != word[index]) return false;

    visited[r][c] = true;
    bool found = dfs(grid, r+1, c, word, index+1, visited) ||
                 dfs(grid, r-1, c, word, index+1, visited) ||
                 dfs(grid, r, c+1, word, index+1, visited) ||
                 dfs(grid, r, c-1, word, index+1, visited);
    visited[r][c] = false;
    return found;
}

bool exist(char grid[ROWS][COLS], const char *word) {
    bool visited[ROWS][COLS] = {false};
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (dfs(grid, i, j, word, 0, visited)) return true;
        }
    }
    return false;
}

int main() {
    char grid[ROWS][COLS] = {
        {'A', 'B', 'A'},
        {'B', 'A', 'B'}
    };
    const char *word = "ABA";
    if (exist(grid, word)) {
        printf("Word found\n");
    } else {
        printf("Word not found\n");
    }
    return 0;
}
AWord found
BWord not found
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Check if the word "ABA" can be formed by adjacent letters without revisiting the same cell twice.