Bird
Raised Fist0
Interview PrepbacktrackinghardAmazonFacebookGoogleBloomberg

Word Search II (Trie + Backtrack)

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Build Trie - insert 'eat'

Insert word 'eat' into the trie. Root → e(id=1) → a(id=2) → t(id=3, word='eat'). Three nodes created along the path.

💡 The trie stores all search words as paths from root. Each character gets its own node.
Line:for w in words: node = root for c in w: if c not in node.children: node.children[c] = TrieNode() node = node.children[c] node.word = w
💡 Inserting 'eat' creates nodes for e→a→t with word='eat' at the leaf.
📊
Word Search II - Watch the Trie + Backtracking Execute, Step by Step
The trie acts as a combined dictionary and prefix checker - instead of checking all words per cell, we follow one trie path that matches the board. When the trie has no child for a board character we prune immediately.
Step 1/11
·Active fillAnswer cell
insert
eat
eat*
insert
oath
eoath*
search
t
Search: false
search
e
e
search
ea
ea
search
e
e
search
eat
eat*
Pairs found: 1
Search: true
search
o
o
Pairs found: 1
search
oa
oa
Pairs found: 1
search
oat
oat
Pairs found: 1
find_pairs
oath
oh*
Pairs found: 2
Search: true

Key Takeaways

Building a trie from all words allows prefix-based pruning - if no word starts with a board character the DFS is skipped entirely.

Without a trie you'd check each word against each board path separately, which is much slower.

Marking cells '#' during DFS prevents revisiting the same cell in one path, ensuring valid board paths only.

The restore step (board[r][c] = letter) after backtracking lets other DFS paths reuse the cell.

Pruning trie nodes with no children after finding a word avoids re-finding the same word and shrinks future search space.

This optimization is subtle but powerful - the trie gets smaller as words are found.

Practice

(1/5)
1. You are given a numeric string and a target integer. You need to insert binary operators (+, -, *) between digits to form expressions that evaluate to the target. Which algorithmic approach guarantees finding all valid expressions efficiently while handling operator precedence and avoiding invalid operands like those with leading zeros?
easy
A. Backtracking with pruning and careful operand parsing to explore all operator insertions
B. Breadth-first search enumerating all possible expressions without pruning
C. Dynamic programming that stores sub-expression results without operator precedence handling
D. Greedy approach that inserts operators at fixed intervals without backtracking

Solution

  1. Step 1: Understand problem requirements

    The problem requires exploring all ways to insert operators to reach a target, respecting operator precedence and avoiding invalid operands like those with leading zeros.
  2. Step 2: Identify suitable algorithm

    Backtracking with pruning and operand parsing systematically explores all valid expressions, correctly handles precedence (especially multiplication), and prunes invalid paths early.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Backtracking explores all valid expressions with pruning [OK]
Hint: Backtracking explores all operator insertions with pruning [OK]
Common Mistakes:
  • Assuming greedy or DP can handle operator precedence and invalid operands
2. You need to generate all combinations of balanced parentheses pairs for a given number n. Which algorithmic approach guarantees generating only valid sequences without generating invalid ones first?
easy
A. Brute force: generate all possible sequences of '(' and ')' of length 2n, then filter valid ones
B. Backtracking with pruning: build sequences by adding '(' or ')' only when it keeps the sequence valid
C. Greedy approach: always add '(' until n is reached, then add ')' to close all opened parentheses
D. Dynamic programming: count the number of valid sequences using Catalan number formula

Solution

  1. Step 1: Understand problem constraints

    We want to generate all valid parentheses sequences without generating invalid ones first.
  2. Step 2: Analyze approaches

    Brute force generates all sequences including invalid ones, greedy fails to generate all valid sequences, DP counts sequences but does not generate them. Backtracking with pruning adds '(' or ')' only when valid, thus generating only valid sequences.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Backtracking with pruning avoids invalid sequences [OK]
Hint: Backtracking prunes invalid sequences early [OK]
Common Mistakes:
  • Thinking greedy can generate all valid sequences
  • Confusing counting with generating sequences
3. You need to count all distinct ways to place n queens on an nxn chessboard so that no two queens threaten each other. Which algorithmic approach guarantees finding all valid solutions efficiently by exploring partial placements and pruning invalid ones early?
easy
A. Backtracking that tries all column placements per row and abandons invalid partial boards
B. Greedy algorithm that places queens row by row choosing the first safe column
C. Dynamic programming that stores counts of valid queen placements per row without conflict checks
D. Breadth-first search enumerating all board states level by level

Solution

  1. Step 1: Understand problem constraints

    The problem requires counting all valid queen placements with no conflicts, which demands exploring all possibilities.
  2. Step 2: Identify suitable algorithm

    Backtracking systematically tries each column per row and abandons partial solutions that violate constraints, ensuring correctness and pruning search space.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Backtracking explores all valid configurations efficiently [OK]
Hint: Counting all valid placements requires backtracking [OK]
Common Mistakes:
  • Assuming greedy placement always works
  • Believing DP without conflict checks suffices
4. Given the following code for palindrome partitioning, what is the final output when input string s = "aab" is passed to partition(s)?
def partition(s):
    def is_palindrome(left, right):
        while left < right:
            if s[left] != s[right]:
                return False
            left += 1
            right -= 1
        return True

    def backtrack(start, path):
        if start == len(s):
            result.append(path[:])
            return
        for end in range(start, len(s)):
            if is_palindrome(start, end):
                path.append(s[start:end+1])
                backtrack(end+1, path)
                path.pop()

    result = []
    backtrack(0, [])
    return result
easy
A. [["a", "a", "b"]]
B. [["a", "ab"], ["aa", "b"]]
C. [["a", "a", "b"], ["aa", "b"]]
D. [["aab"]]

Solution

  1. Step 1: Trace backtrack calls for s="aab"

    Start=0, path=[]; check substrings: "a" (palindrome), "aa" (palindrome), "aab" (not palindrome). Explore "a" -> backtrack(1, ["a"]), then "a" -> backtrack(2, ["a", "a"]), then "b" -> backtrack(3, ["a", "a", "b"]) adds ["a", "a", "b"] to result.
  2. Step 2: Explore "aa" substring

    From start=0, choose "aa" -> backtrack(2, ["aa"]), then "b" -> backtrack(3, ["aa", "b"]) adds ["aa", "b"] to result.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Both partitions contain only palindromes and cover entire string [OK]
Hint: Trace recursion and palindrome checks carefully [OK]
Common Mistakes:
  • Including non-palindromic substrings like "ab"
  • Missing one valid partition
  • Returning partial partitions
5. You are given a 2D grid of characters and a target string. You need to determine if the string can be formed by sequentially adjacent cells (horizontally or vertically) without revisiting any cell. Which algorithmic approach best guarantees an optimal solution for this problem?
easy
A. Backtracking with in-place marking and exploring all four directions
B. Dynamic Programming with memoization to store partial matches
C. Greedy search by always choosing the next matching character closest to the start
D. Breadth-first search (BFS) to explore all possible paths simultaneously

Solution

  1. Step 1: Understand problem constraints

    The problem requires checking if a word can be formed by adjacent cells without reuse, which is a classic backtracking scenario.
  2. Step 2: Identify suitable algorithm

    Backtracking with in-place marking efficiently explores all paths while preventing revisiting cells, guaranteeing correctness and optimal pruning.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Backtracking explores all valid paths with pruning [OK]
Hint: Backtracking fits adjacency and no-reuse constraints [OK]
Common Mistakes:
  • Assuming greedy or DP can solve adjacency constraints optimally