Tries Flashcards

Learn (10 cards)

1
Q

How do you insert a word into a trie?

A

Add nodes for each character and mark the end of the word.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you mark the end of a word in a trie?

A

Use a special flag or indicator at the final character node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does startsWith return if a word with that prefix is in the trie?

A

True, because the prefix exists in at least one word.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you search for a word in a trie?

A

Follow character by character down the trie and check for word end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why are trie operations O(m)?

A

m is the length of the word or prefix, and you visit one node per letter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you count how many words pass through a node in a trie?

A

Add a counter to each node and increment it during word insertions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you delete a word from a trie?

A

Traverse the word, unmark the end, and remove unused nodes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are tries commonly used for?

A
  • Autocomplete
  • Spell-checking
  • Fast prefix matching
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If a trie has ‘CAT’ and ‘CAR’, how do you insert ‘CAN’?

A

Reuse the common prefix ‘CA’, then add a new node for ‘N’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you build autocomplete suggestions from a prefix in a trie?

A

Traverse to the end of the prefix, then explore all paths below it to collect full words.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly