You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.6 KiB
74 lines
2.6 KiB
6 years ago
|
---
|
||
|
id: trie
|
||
3 years ago
|
title: Trie cheatsheet for coding interviews
|
||
|
description: Trie study guide for coding interviews, including practice questions, techniques, time complexity, and recommended resources
|
||
|
keywords:
|
||
|
[
|
||
|
trie coding interview study guide,
|
||
|
trie tips for coding interviews,
|
||
|
trie practice questions,
|
||
|
trie useful techniques,
|
||
|
trie time complexity,
|
||
|
trie recommended study resources,
|
||
|
]
|
||
|
sidebar_label: Trie
|
||
3 years ago
|
toc_max_heading_level: 2
|
||
6 years ago
|
---
|
||
|
|
||
3 years ago
|
<head>
|
||
|
<meta property="og:image" content="https://www.techinterviewhandbook.org/social/algorithms/algorithms/algorithms-trie.png" />
|
||
|
</head>
|
||
|
|
||
3 years ago
|
## Introduction
|
||
6 years ago
|
|
||
3 years ago
|
Tries are special trees (prefix trees) that make searching and storing strings more efficient. Tries have many practical applications, such as conducting searches and providing autocomplete. It is helpful to know these common applications so that you can easily identify when a problem can be efficiently solved using a trie.
|
||
6 years ago
|
|
||
3 years ago
|
Be familiar with implementing from scratch, a `Trie` class and its `add`, `remove` and `search` methods.
|
||
6 years ago
|
|
||
3 years ago
|
## Learning resources
|
||
|
|
||
|
- Readings
|
||
|
- [Trying to Understand Tries](https://medium.com/basecs/trying-to-understand-tries-3ec6bede0014), basecs
|
||
|
- [Implement Trie (Prefix Tree)](https://leetcode.com/articles/implement-trie-prefix-tree/), LeetCode
|
||
|
- Additional (only if you have time)
|
||
|
- [Compressing Radix Trees Without (Too Many) Tears](https://medium.com/basecs/compressing-radix-trees-without-too-many-tears-a2e658adb9a0), basecs
|
||
|
|
||
3 years ago
|
## Time complexity
|
||
6 years ago
|
|
||
3 years ago
|
`m` is the length of the string used in the operation.
|
||
|
|
||
|
| Operation | Big-O | Note |
|
||
|
| --------- | ----- | ---- |
|
||
|
| Search | O(m) | |
|
||
|
| Insert | O(m) | |
|
||
|
| Remove | O(m) | |
|
||
|
|
||
|
## Corner cases
|
||
|
|
||
|
- Searching for a string in an empty trie
|
||
|
- Inserting empty strings into a trie
|
||
|
|
||
|
## Techniques
|
||
|
|
||
|
Sometimes preprocessing a dictionary of words (given in a list) into a trie, will improve the efficiency of searching for a word of length k, among n words. Searching becomes O(k) instead of O(n).
|
||
6 years ago
|
|
||
3 years ago
|
## Essential questions
|
||
|
|
||
|
_These are essential questions to practice if you're studying for this topic._
|
||
6 years ago
|
|
||
|
- [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree)
|
||
3 years ago
|
|
||
|
## Recommended practice questions
|
||
|
|
||
|
_These are recommended questions to practice after you have studied for the topic and have practiced the essential questions._
|
||
|
|
||
6 years ago
|
- [Add and Search Word](https://leetcode.com/problems/add-and-search-word-data-structure-design)
|
||
3 years ago
|
- [Word Break](https://leetcode.com/problems/word-break)
|
||
6 years ago
|
- [Word Search II](https://leetcode.com/problems/word-search-ii/)
|
||
3 years ago
|
|
||
|
## Recommended courses
|
||
|
|
||
|
import AlgorithmCourses from '../\_courses/AlgorithmCourses.md'
|
||
|
|
||
|
<AlgorithmCourses />
|