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 in Grid
What is the output of the Rust program that checks if the word "CAT" exists in the given 3x3 grid using backtracking?
DSA Rust
fn exist(board: &Vec<Vec<char>>, word: &str) -> bool { fn backtrack(board: &Vec<Vec<char>>, word: &[char], i: usize, x: usize, y: usize, visited: &mut Vec<Vec<bool>>) -> bool { if i == word.len() { return true; } if x >= board.len() || y >= board[0].len() || board[x][y] != word[i] || visited[x][y] { return false; } visited[x][y] = true; let directions = [(1,0), (-1,0), (0,1), (0,-1)]; for &(dx, dy) in &directions { let nx = x as isize + dx; let ny = y as isize + dy; if nx >= 0 && ny >= 0 && (nx as usize) < board.len() && (ny as usize) < board[0].len() && backtrack(board, word, i+1, nx as usize, ny as usize, visited) { return true; } } visited[x][y] = false; false } let word_chars: Vec<char> = word.chars().collect(); let mut visited = vec![vec![false; board[0].len()]; board.len()]; for i in 0..board.len() { for j in 0..board[0].len() { if backtrack(board, &word_chars, 0, i, j, &mut visited) { return true; } } } false } fn main() { let board = vec![ vec!['C', 'A', 'T'], vec!['X', 'Y', 'Z'], vec!['A', 'B', 'C'], ]; let word = "CAT"; println!("{}", exist(&board, word)); }
Attempts:
2 left
💡 Hint
Think about whether the word "CAT" can be found by moving horizontally or vertically in the grid.
✗ Incorrect
The word "CAT" can be found in the first row of the grid moving from left to right. The backtracking function returns true when it finds the complete word.
🧠 Conceptual
intermediateUnderstanding Backtracking Directions
In the backtracking approach for word search in a grid, which directions are typically checked to find the next character?
Attempts:
2 left
💡 Hint
Consider the problem constraints that usually allow moving only horizontally or vertically.
✗ Incorrect
The standard word search problem allows moving only up, down, left, or right to find the next character.
🔧 Debug
advancedIdentify the Bug in Backtracking Visited Reset
What is the bug in this backtracking snippet for word search that causes incorrect results?
```rust
visited[x][y] = true;
for &(dx, dy) in &directions {
let nx = x as isize + dx;
let ny = y as isize + dy;
if nx >= 0 && ny >= 0 && backtrack(board, word, i+1, nx as usize, ny as usize, visited) {
return true;
}
}
// Missing visited[x][y] = false here
false
```
Attempts:
2 left
💡 Hint
Think about what happens if you don't mark a cell as unvisited after exploring it.
✗ Incorrect
Not resetting visited[x][y] to false after exploring causes the algorithm to think the cell is permanently visited, blocking valid paths.
❓ Predict Output
advancedOutput of Word Search for Non-Existent Word
What is the output of the Rust program when searching for the word "DOG" in the following grid?
[['C', 'A', 'T'],
['X', 'Y', 'Z'],
['A', 'B', 'C']]
DSA Rust
fn main() {
let board = vec![
vec!['C', 'A', 'T'],
vec!['X', 'Y', 'Z'],
vec!['A', 'B', 'C'],
];
let word = "DOG";
println!("{}", exist(&board, word));
}
fn exist(board: &Vec<Vec<char>>, word: &str) -> bool {
fn backtrack(board: &Vec<Vec<char>>, word: &[char], i: usize, x: usize, y: usize, visited: &mut Vec<Vec<bool>>) -> bool {
if i == word.len() {
return true;
}
if x >= board.len() || y >= board[0].len() || board[x][y] != word[i] || visited[x][y] {
return false;
}
visited[x][y] = true;
let directions = [(1,0), (-1,0), (0,1), (0,-1)];
for &(dx, dy) in &directions {
let nx = x as isize + dx;
let ny = y as isize + dy;
if nx >= 0 && ny >= 0 && (nx as usize) < board.len() && (ny as usize) < board[0].len() && backtrack(board, word, i+1, nx as usize, ny as usize, visited) {
return true;
}
}
visited[x][y] = false;
false
}
let word_chars: Vec<char> = word.chars().collect();
let mut visited = vec![vec![false; board[0].len()]; board.len()];
for i in 0..board.len() {
for j in 0..board[0].len() {
if backtrack(board, &word_chars, 0, i, j, &mut visited) {
return true;
}
}
}
false
}Attempts:
2 left
💡 Hint
Check if the letters D, O, G appear in sequence in the grid.
✗ Incorrect
The word "DOG" does not appear in the grid in any path moving up, down, left, or right.
🚀 Application
expertMinimum Steps to Form Word in Grid
Given a grid and a word, what is the minimum number of steps required to form the word by moving horizontally or vertically from one letter to the next? Assume you can start at any cell containing the first letter. Which approach best solves this problem?
Attempts:
2 left
💡 Hint
Think about exploring all possible paths but stopping early when paths are longer than current minimum.
✗ Incorrect
Backtracking with pruning allows exploring all valid paths while avoiding unnecessary work, enabling finding the minimum steps.
