class WordDictionary:
def __init__(self):
# Write your solution here
pass
def addWord(self, word: str) -> None:
pass
def search(self, word: str) -> bool:
pass
class WordDictionary {
public WordDictionary() {
// Write your solution here
}
public void addWord(String word) {
// Write your solution here
}
public boolean search(String word) {
// Write your solution here
return false;
}
}
#include <string>
using namespace std;
class WordDictionary {
public:
WordDictionary() {
// Write your solution here
}
void addWord(string word) {
// Write your solution here
}
bool search(string word) {
// Write your solution here
return false;
}
};
class WordDictionary {
constructor() {
// Write your solution here
}
addWord(word) {
// Write your solution here
}
search(word) {
// Write your solution here
return false;
}
}
Coming soon
0/10
Common Bugs to Avoid
Wrong: true for search('') when no empty word addedNot handling empty search string explicitly, returning true or error✅ Add explicit check: if search word is empty and no empty word added, return false
Wrong: false for search with all '.' wildcards when matching words existSearch does not explore all children nodes for '.' wildcard✅ Implement DFS that explores all children nodes when '.' is encountered
Wrong: true for search of longer word than any addedSearch does not enforce exact length match, allowing prefix matches✅ Ensure search returns false if search word length exceeds any added word length
Wrong: false for search with multiple '.' wildcards when matches existGreedy search stops early, not exploring all branches for '.'✅ Use full DFS backtracking to explore all possible children for each '.'
Wrong: TLE on large inputsUsing brute force linear search over all words instead of trie✅ Implement trie with DFS wildcard search to achieve O(L) average time
✓
Test Cases
Focus on handling edge cases like empty strings and duplicates to solidify your base implementation.
Prepare to catch subtle bugs like greedy search traps and length boundary errors.
Optimize your solution to handle large inputs efficiently using trie and DFS.
After adding 'bad', 'dad', and 'mad', searching 'pad' returns false as it was not added. Searching 'bad' returns true. Searching '.ad' matches any word ending with 'ad', so returns true. Searching 'b..' matches any word starting with 'b' and two letters after, so returns true.
💡 Think about how to store words to allow fast prefix and wildcard searches.
💡 Use a trie data structure and DFS to handle '.' wildcard characters.
💡 Implement recursive search that explores all children nodes when '.' is encountered.
Why it failed: Basic case failed - likely your search does not handle '.' wildcards correctly. Fix by adding DFS to explore all children nodes when '.' is found.
✓ Core trie insertion and wildcard search logic is correct.
Added 'apple' and 'apply'. Searching 'apple' returns true. 'appl.' matches 'apple' and 'apply' so true. 'a....' matches any 5-letter word starting with 'a' so true. '.....' matches any 5-letter word so true. 'ap..e' does not match any added word so false.
💡 Check how your search handles multiple '.' wildcards in different positions.
💡 Ensure your DFS explores all possible children for each '.' in the search word.
💡 Return true if any path matches fully; do not stop at first mismatch.
Why it failed: Search failed on multiple wildcards - your DFS likely stops early or does not explore all children. Fix by fully exploring all branches for '.' characters.
Adding duplicate words 'test' twice should not affect search results; searching 'test' returns true.
💡 Check if adding duplicates causes errors or changes search behavior.
💡 Ensure your data structure can handle duplicate insertions gracefully.
💡 Duplicates should not create multiple nodes or affect search correctness.
Why it failed: Duplicate addWord calls cause incorrect search results or errors. Fix by allowing duplicates without altering trie structure incorrectly.
✓ Duplicate words handled correctly without affecting search.
1. Given the following Trie implementation, after inserting the words ['cat', 'car', 'dog'], what is the value of node.isEnd after processing the last character of 'car' in the insert method?
easy
A. false, because 'car' shares prefix with 'cat' and 'dog'
B. true, but only if 'car' was inserted before 'cat'
C. false, because isEnd is only set after inserting all words
D. true, because 'car' is a complete inserted word
Solution
Step 1: Trace insertion of 'car'
For each character 'c', 'a', 'r', nodes are created or reused. After last character 'r', node.isEnd is set to true.
Step 2: Confirm isEnd flag meaning
isEnd marks the end of a complete word. Since 'car' was inserted, node.isEnd after 'r' is true.
Final Answer:
Option D -> Option D
Quick Check:
isEnd is true after inserting 'car' [OK]
Hint: isEnd is set true after inserting each complete word [OK]
Common Mistakes:
Assuming isEnd is false if prefix overlaps with other words
2. What is the time complexity of the sum(prefix) operation in the optimal MapSum implementation using a trie with array children, assuming average key length is k and n keys inserted?
medium
A. O(n * k) because we must scan all keys to sum prefix matches.
B. O(k) because we traverse the trie nodes corresponding to the prefix length.
C. O(k * 26) because we check all children at each prefix node.
D. O(log n) because trie nodes are balanced like a BST.
Solution
Step 1: Identify sum operation steps
Sum traverses trie nodes along prefix characters only, no scanning of all keys.
Step 2: Analyze traversal cost
Each character leads to one child node via array index, so traversal is O(k) where k is prefix length.
Final Answer:
Option A -> Option A
Quick Check:
Sum is O(k) due to direct trie traversal [INCORRECT]
Hint: Sum only traverses prefix length nodes, not all keys [INCORRECT]
Common Mistakes:
Confusing sum with brute force scanning
Thinking all children checked at each node
Assuming trie is balanced BST
3. What is the time complexity of the optimal trie-based solution for finding shortest unique prefixes for n words each of maximum length m?
medium
A. O(n^2 * m) because each prefix is compared with all other words
B. O(n * m) because each word is inserted and traversed once in the trie
C. O(n * m^2) due to nested prefix checks during insertion
D. O(n * log n * m) because sorting is involved before trie insertion
Solution
Step 1: Identify operations per word
Each word of length m is inserted into the trie once, updating frequency counts along the path.
Step 2: Analyze total complexity
Insertion and prefix search both take O(m) per word, so total is O(n * m). No sorting or nested loops over words occur.
Final Answer:
Option B -> Option B
Quick Check:
Trie insertion and search scale linearly with total characters [OK]
Hint: Trie operations scale with total characters, not pairwise comparisons [OK]
Common Mistakes:
Confusing trie insertion with pairwise prefix checks
Assuming sorting is required
4. What is the time complexity of the optimal Word Break solution that uses iterative DP combined with a reversed trie for prefix checks, where n is the length of the string and m is the maximum length of words in the dictionary?
medium
A. O(n * log m) due to binary search in trie children
B. O(n^2) because of nested loops over the string
C. O(n * m) because for each position, trie traversal is bounded by max word length
D. O(n * m^2) because each trie traversal can take up to m steps and each step checks children
Solution
Step 1: Identify loops and trie traversal cost
Outer loop runs n times. Inner loop traverses backwards up to m characters (max word length). Trie traversal per character is O(1) on average due to hashmap children.
Step 2: Combine to get total complexity
Total time is O(n * m). Nested loops do not run n^2 because inner loop breaks early when no trie child exists.
Final Answer:
Option C -> Option C
Quick Check:
Trie traversal bounded by max word length m [OK]
Hint: Trie traversal limits inner loop to max word length [OK]
Common Mistakes:
Assuming inner loop always runs n times
Confusing trie traversal cost with binary search
Ignoring early breaks in inner loop
5. If the problem is modified so that words can be concatenated using the same shorter word multiple times (reuse allowed), which change to the Trie + DFS + memoization approach is necessary to correctly find all concatenated words?
hard
A. Keep memoization but ensure that the base case allows concatenations with repeated words
B. Remove memoization to allow repeated exploration of the same indices
C. No change needed; current memoization handles reuse correctly
D. Modify DFS to allow revisiting the same start index multiple times without memoization
Solution
Step 1: Understand reuse means words can be concatenated multiple times
Memoization caches results per start index; reuse is allowed if base case and DFS logic permit repeated words.
Step 2: Adjust base case and DFS to allow repeated words but keep memoization for efficiency
Memoization should remain to avoid exponential calls; base case must allow concatenations with repeated words.
Final Answer:
Option A -> Option A
Quick Check:
Memoization remains; base case and DFS logic adapt to reuse [OK]
Hint: Memoization is key; only base case and DFS logic need adjustment for reuse [OK]
Common Mistakes:
Removing memoization causes TLE
Ignoring base case changes for reuse
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.