# Why does this trie search return true for prefixes that were never inserted as full words?

**URL:** <https://forum.kirupa.com/t/why-does-this-trie-search-return-true-for-prefixes-that-were-never-inserted-as-full-words/679951>\
**Category:** web dev\
**Created:** [April 3, 2026, 10:00pm UTC](https://forum.kirupa.com/t/why-does-this-trie-search-return-true-for-prefixes-that-were-never-inserted-as-full-words/679951 "2026-04-03T22:00:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [April 3, 2026, 10:00pm UTC](https://forum.kirupa.com/t/why-does-this-trie-search-return-true-for-prefixes-that-were-never-inserted-as-full-words/679951/1 "2026-04-03T22:00:10Z")

</div>

I’m implementing a small trie in TypeScript. Exact word lookup should only return true for inserted words, but prefixes like “car” become searchable after inserting “cart”. I think my traversal is fine, so I’m probably marking nodes incorrectly. What is the minimal fix here?

```ts
class TrieNode {
  children = new Map<string, TrieNode>();
  isWord = false;
}
function insert(root: TrieNode, word: string) {
  let node = root;
  for (const ch of word) {
    if (!node.children.has(ch)) node.children.set(ch, new TrieNode());
    node = node.children.get(ch)!;
    node.isWord = true;
  }
}

```

WaffleFries

---

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [April 3, 2026, 10:07pm UTC](https://forum.kirupa.com/t/why-does-this-trie-search-return-true-for-prefixes-that-were-never-inserted-as-full-words/679951/2 "2026-04-03T22:07:05Z")

</div>

You’re marking every traversed node as a word, so `car` gets flagged while inserting `cart`.

Quelly 😀
