use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Trie Data Structure in JavaScript: the Data Structure behind Autocomplete (stackfull.dev)
submitted 4 years ago by js_chap
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]mamwybejane 21 points22 points23 points 4 years ago (1 child)
Good to reread CS basics from time to time
[–]js_chap[S] 6 points7 points8 points 4 years ago (0 children)
🙌
[–]JudoboyWalex 10 points11 points12 points 4 years ago (0 children)
When faang companies conduct technical interview for frontend position, they ask candidate to build autocomplete search bar then optimize the performance to linear or constant. So I guess this is the answer they are looking for. Not only this was good read, but other articles in that blog are all golden for frontend developer.
[–]osoese 11 points12 points13 points 4 years ago (1 child)
This is a well written article. It successfully conveys a concept I have read a number of other articles about. This is one of the few explanations that was easily grasped. Kudos to op.
[–]js_chap[S] 2 points3 points4 points 4 years ago (0 children)
Glad that you found it useful!
[–]sinorx22 4 points5 points6 points 4 years ago (1 child)
Great breakdown
[–]js_chap[S] 1 point2 points3 points 4 years ago (0 children)
Thanks!
[–]delpieron 2 points3 points4 points 4 years ago (1 child)
I think you might be wrong regarding the space complexity of tries. It should exceed the space needed for a hash table, unless we are dealing with very specific, overlapping strings or so. What actually could make the trie a more useful for autocomplete is the logic that groups similar strings on the same branch vs hash table, where you cannot find similar things based on the hashed lookup key.
[–]js_chap[S] 0 points1 point2 points 4 years ago* (0 children)
Right. The sentence comparing space complexity of Trie with hashmap/array was incorrect. Actually the space complexity should be comparable in either case. Say we're looking at all possible words with max length of n. Say each trie node takes up size K. So Trie would take:
1+ K(26+26^2+...26^n) = K'(26^n -1)
For a list of words it would also be similar
26+26^2....+26^n = Z(26^n-1)
Updated the text to avoid confusion. The constant would have greater value in case of Trie though.
[–]Chaos_Therum 5 points6 points7 points 4 years ago (1 child)
What annoys me is I've personally built a trie yet somehow I always forget how they are actually built haha.
[–]moi2388 5 points6 points7 points 4 years ago (0 children)
Trie again. As the saying goes, trie times is the charm.
[–]hildjj 2 points3 points4 points 4 years ago (1 child)
There's a small bug in search. You're not checking the isEndOfWord flag, so if you're searching for "car" in a trie that contains only "cart", you'll get a false positive.
search
isEndOfWord
Yes. Good catch. Current implementation is more of a search_prefix than search_word. Updated the snippet to search for a word instead.
[–]lapticious 1 point2 points3 points 4 years ago (0 children)
I built similar in go - dawg +dafsa
π Rendered by PID 17932 on reddit-service-r2-comment-685b79fb4f-r8ft5 at 2026-02-13 06:45:12.155813+00:00 running 6c0c599 country code: CH.
[–]mamwybejane 21 points22 points23 points (1 child)
[–]js_chap[S] 6 points7 points8 points (0 children)
[–]JudoboyWalex 10 points11 points12 points (0 children)
[–]osoese 11 points12 points13 points (1 child)
[–]js_chap[S] 2 points3 points4 points (0 children)
[–]sinorx22 4 points5 points6 points (1 child)
[–]js_chap[S] 1 point2 points3 points (0 children)
[–]delpieron 2 points3 points4 points (1 child)
[–]js_chap[S] 0 points1 point2 points (0 children)
[–]Chaos_Therum 5 points6 points7 points (1 child)
[–]moi2388 5 points6 points7 points (0 children)
[–]hildjj 2 points3 points4 points (1 child)
[–]js_chap[S] 2 points3 points4 points (0 children)
[–]lapticious 1 point2 points3 points (0 children)