Bird
Raised Fist0
DSA Cprogramming~30 mins

Word Search in Trie in DSA C - Build from Scratch

Choose your learning style10 modes available
Word Search in Trie
📖 Scenario: Imagine you have a list of words and you want to quickly check if a certain word exists in that list. A Trie is a special tree-like data structure that helps you do this efficiently.In this project, you will build a simple Trie and then search for a word in it.
🎯 Goal: You will create a Trie with some words, then write code to search for a specific word in the Trie and print whether it exists or not.
📋 What You'll Learn
Create a Trie node structure
Insert words into the Trie
Search for a word in the Trie
Print the search result
💡 Why This Matters
🌍 Real World
Tries are used in autocomplete systems, spell checkers, and IP routing to quickly find words or prefixes.
💼 Career
Understanding Tries helps in software engineering roles that involve text processing, search engines, and efficient data retrieval.
Progress0 / 4 steps
1
Create the Trie Node Structure and Insert Words
Define a struct called TrieNode with an array of 26 TrieNode* pointers called children and an int called isEndOfWord. Then create a function called getNode that returns a new TrieNode* with all children set to NULL and isEndOfWord set to 0. Finally, create a function called insert that takes a TrieNode* root and a char* word and inserts the word into the Trie.
DSA C
Hint

Think of the TrieNode as a box with 26 slots for letters and a flag to mark the end of a word.

The insert function walks through the word letter by letter, creating new nodes if needed.

2
Create the Trie and Insert Sample Words
Create a TrieNode* variable called root and set it to the result of getNode(). Then insert the words "cat", "car", and "dog" into the Trie using the insert function.
DSA C
Hint

Start by creating the root node with getNode().

Then call insert for each word exactly as shown.

3
Write the Search Function for the Trie
Write a function called search that takes a TrieNode* root and a char* word and returns 1 if the word exists in the Trie and 0 otherwise. Use a loop to check each character's child node and verify isEndOfWord at the end.
DSA C
Hint

Walk through each letter of the word. If a letter's node is missing, return 0.

If you reach the end, check if isEndOfWord is 1.

4
Search for a Word and Print the Result
In the main function, call search with root and the word "car". If it returns 1, print "Found". Otherwise, print "Not Found".
DSA C
Hint

Use search(root, "car") and check if it returns 1.

Print "Found" if yes, else "Not Found".

Practice

(1/5)
1. What is the main purpose of using a Trie data structure in word search?
easy
A. To efficiently store and search words by their prefixes
B. To sort words alphabetically
C. To store words in a hash table
D. To perform arithmetic operations on words

Solution

  1. Step 1: Understand Trie structure

    A Trie stores characters of words in a tree-like structure where each node represents a character.
  2. Step 2: Identify Trie usage

    It allows quick prefix-based search by traversing nodes matching characters in order.
  3. Final Answer:

    To efficiently store and search words by their prefixes -> Option A
  4. Quick Check:

    Trie = prefix search [OK]
Hint: Trie is for prefix-based word search, not sorting [OK]
Common Mistakes:
  • Confusing Trie with hash tables
  • Thinking Trie sorts words
  • Assuming Trie stores whole words in one node
2. Which of the following is the correct way to check if a word exists in a Trie in C?
easy
A. Use a hash function on the word and check hash table
B. Check only the root node for the word
C. Sort the Trie nodes and do binary search
D. Traverse nodes for each character; if path ends early, word not found

Solution

  1. Step 1: Understand Trie search logic

    Search each character by moving through child nodes; if any character node missing, word not found.
  2. Step 2: Eliminate incorrect options

    Hash and sorting are unrelated to Trie search; root node alone can't hold whole word.
  3. Final Answer:

    Traverse nodes for each character; if path ends early, word not found -> Option D
  4. Quick Check:

    Trie search = character traversal [OK]
Hint: Check each character node in order to find word [OK]
Common Mistakes:
  • Using hash or sorting instead of Trie traversal
  • Checking only root node
  • Assuming word is stored in one node
3. What will be the output of the following C code snippet that searches for the word "cat" in a Trie containing words "cat", "car", "dog"?
bool search(TrieNode* root, const char* word) {
    TrieNode* node = root;
    for (int i = 0; word[i] != '\0'; i++) {
        int index = word[i] - 'a';
        if (!node->children[index]) return false;
        node = node->children[index];
    }
    return node->isEndOfWord;
}

// Assume Trie is built with words "cat", "car", "dog"
printf("%d", search(root, "cat"));
medium
A. 1
B. 0
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Analyze search function logic

    Function traverses Trie nodes for each character; returns true if last node marks end of word.
  2. Step 2: Check if "cat" is in Trie

    "cat" was inserted, so traversal succeeds and isEndOfWord is true, returning 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Word found = 1 [OK]
Hint: If word inserted, search returns 1 (true) [OK]
Common Mistakes:
  • Assuming search returns 0 for found words
  • Confusing isEndOfWord flag
  • Ignoring null checks in traversal
4. Identify the bug in this Trie search function in C:
bool search(TrieNode* root, const char* word) {
    TrieNode* node = root;
    for (int i = 0; word[i] != '\0'; i++) {
        int index = word[i] - 'a';
        if (node->children[index] == NULL) return false;
        node = node->children[index];
    }
    return true;
}
medium
A. It uses wrong index calculation for characters
B. It does not check for NULL root pointer
C. It returns true without checking if node marks end of word
D. It should return false at the end instead of true

Solution

  1. Step 1: Review return condition after loop

    Function returns true after traversal without verifying if current node marks end of a valid word.
  2. Step 2: Understand Trie word end marking

    Words end at nodes with isEndOfWord true; missing this check causes false positives.
  3. Final Answer:

    It returns true without checking if node marks end of word -> Option C
  4. Quick Check:

    Must check isEndOfWord before returning true [OK]
Hint: Always check isEndOfWord after traversal [OK]
Common Mistakes:
  • Skipping isEndOfWord check
  • Not handling NULL root
  • Incorrect index calculation
5. Given a Trie containing the words: "apple", "app", "ape", "bat", "bath", which of the following words will the search function return true for? Assume the search function correctly checks isEndOfWord.
hard
A. "ap" only
B. "bat", "bath", and "apple" only
C. "bat", "bathroom", and "apple" only
D. "bat" and "apple" only

Solution

  1. Step 1: Identify words inserted in Trie

    Words inserted are "apple", "app", "ape", "bat", "bath"; only these have isEndOfWord true at last character.
  2. Step 2: Check each option word

    "ap" is prefix but not a word; "bat", "bath", "apple" are present; "bathroom" not inserted.
  3. Final Answer:

    "bat", "bath", and "apple" only -> Option B
  4. Quick Check:

    Search true only for inserted words [OK]
Hint: Search true only for complete inserted words [OK]
Common Mistakes:
  • Assuming prefixes are valid words
  • Including words not inserted
  • Ignoring isEndOfWord flag
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard