Tries Flashcards

(48 cards)

1
Q

What is a trie?

A

A tree-like data structure used to store strings, where each node represents a character of the string.

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

What is another name for a trie?

A

Prefix tree.

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

What is the primary use of a trie?

A

To store and retrieve strings efficiently, especially for prefix-based queries.

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

What is a typical application of a trie?

A

Autocomplete, spell checking, and dictionary word searches.

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

What is the time complexity of searching a string in a trie?

A

O(m), where m is the length of the string.

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

What is the time complexity of inserting a string in a trie?

A

O(m), where m is the length of the string.

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

What is the time complexity of deleting a string from a trie?

A

O(m), where m is the length of the string.

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

What is the space complexity of a trie?

A

O(n * m), where n is the number of words and m is the average length.

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

What is stored at each node of a trie?

A

A character and optionally a flag indicating the end of a word.

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

What is the root node of a trie?

A

An empty node that serves as the starting point for all insertions and lookups.

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

How do you check if a word exists in a trie?

A

Traverse the trie following the characters; check if the last node is marked as end-of-word.

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

What is an end-of-word marker in a trie?

A

A boolean flag indicating that a complete word ends at that node.

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

How do you handle shared prefixes in a trie?

A

Nodes are shared among words that have the same prefix.

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

What happens if two words share a prefix in a trie?

A

They branch from the common prefix node.

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

What is the difference between a trie and a binary search tree?

A

Tries store data character-by-character in paths, while BSTs compare entire strings.

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

How do you insert a word into a trie?

A

Traverse or create nodes for each character and mark the final node as an end-of-word.

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

Can tries store numbers?

A

Yes, if digits are treated as characters.

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

Can tries store non-alphabetic characters?

A

Yes, if the trie is designed to handle them.

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

What is a compressed trie?

A

A trie where chains of single-child nodes are compressed into one node.

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

What is a ternary search trie?

A

A trie that uses a binary search tree structure at each node to reduce space.

21
Q

What is a Patricia trie?

A

A compressed trie with binary branching used in networking and IP routing.

22
Q

What is the main benefit of tries over hash tables?

A

Tries provide efficient prefix-based operations and are ordered.

23
Q

How is a trie used in autocomplete systems?

A

By traversing to the node representing the prefix and exploring all descendant words.

24
Q

What is the advantage of tries in dictionary word lookup?

A

O(m) time search and supports prefix-based queries.

25
How do you implement a trie in code?
Use nodes with children stored in a hash map or array indexed by characters.
26
What is a node’s branching factor in a trie?
The number of children a node has.
27
What is a limitation of tries?
They can consume a lot of memory for sparse datasets.
28
How can you reduce memory usage in tries?
Use arrays for small alphabets or compression techniques.
29
What is the role of recursion in trie traversal?
To visit nodes depth-first or print all words.
30
What is a use case of tries in bioinformatics?
Storing DNA sequences and performing pattern matching.
31
What is the use of a trie in longest prefix matching?
To find the longest prefix of a query string present in the trie.
32
How do you print all words stored in a trie?
Use DFS to collect characters along the path and print at end-of-word nodes.
33
What is a multiway trie?
A trie where each node can have more than two children.
34
What is an adaptive radix trie?
A trie that adjusts branching based on input patterns to save space.
35
What is a practical limitation of using tries?
High space overhead due to many pointers in sparse tries.
36
What is the alphabet size in a trie?
The number of possible characters (e.g., 26 for lowercase English letters).
37
How do you check for a prefix in a trie?
Traverse characters and return true if all are found.
38
How do you count the number of words with a given prefix?
Traverse the prefix, then count all end-of-word nodes in the subtree.
39
Can a trie be used for regular expression matching?
Not directly, but it can help with certain fixed-pattern searches.
40
What is the difference between a trie and a suffix tree?
Tries store prefixes; suffix trees store all suffixes of a string.
41
How is a trie used in IP routing?
To match IP address prefixes efficiently.
42
What is a hybrid trie?
A trie combined with another structure (e.g., hash table or BST) to balance space and time.
43
What is the worst-case time for searching in a trie?
O(m), where m is the length of the key.
44
What is the best-case time for inserting in a trie?
O(1), if the key already exists.
45
Can tries handle case-insensitive matching?
Yes, by converting all characters to the same case during insert/search.
46
What is an edge-labeled trie?
A trie where edges store strings instead of single characters.
47
What is a digital search tree?
A trie that uses numeric data as input, typically for IP routing.
48
What happens if you delete a word from a trie?
Unset the end-of-word marker and remove unnecessary nodes.