├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 xianzhez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Coding-Interview-101 2 | > Author: @xianzhez 3 | > 4 | > Last updated: 2020.11.26 5 | > 6 | > 7 | > Remark: 8 | > 9 | > For data structures and algorithms in this document 10 | > 11 | > - in **bold**: you must know the concept and complexity, and can implement in real code. 12 | > - normal: you should know the concept and complexity, but pseudo-code is fine. 13 | > - in *italic*: you should know the general idea. If you encounter such a question in an interview for entry-level position, that company might not be hiring actively. 14 | 15 | ## Preface 16 | 17 | This post is summarizing the data structure and algorithm fundamentals you might need in a coding interview and the corresponding implementations in different languages (Currently Python, Java, and C++). 18 | 19 | You may or may not know some of them. It's totally fine. You could easily find videos, blogs, and source code about these data structures and algorithms online. 20 | 21 | I tried to make it accurate and correct. But I might be still missing something important or writing something wrong. If you find any mistake or have good advice, you are welcome to contact me. If you find it useful, welcome to star, fork, and share this repo. Please also provide the reference if you are going to refer to this repo. 22 | 23 | Coding classic algorithms may not represent your actual ability and specialization. Don't feel discouraged if you cannot solve it in limited time or fail an interview. It takes some time! 24 | 25 | I will keep updating this doc and make it more helpful. I hope everyone could land a dream job eventually. Keep coding! 26 | 27 | ## Prerequisite 28 | 1. Learn a programming language; 29 | 2. Learn data structures and APIs; 30 | 3. Understand the underlying implementation of data structures; 31 | 4. Understand complexity analysis; 32 | 5. Learn classical algorithms and advanced data structures; 33 | 34 | ## Language 35 | - C++: [cplusplus.com - The C++ Resources Network](https://www.cplusplus.com/) 36 | - Python: [Python Tutorial](https://docs.python.org/3/tutorial/) 37 | - Java: [Java Documentation](https://docs.oracle.com/javase/tutorial/) 38 | ## Data Structures 39 | > - Cheatsheet(ALGS4)(Java): [Algorithms and Data Structures Cheatsheet](https://algs4.cs.princeton.edu/cheatsheet/) 40 | > - [Big-O Algorithm Complexity](https://www.bigocheatsheet.com/) 41 | 42 | 43 | > Python: [Data Structures — Python 3.8.6 documentation](https://docs.python.org/3/tutorial/datastructures.html#), [Common Python Data Structures (Guide) – Real Python](https://realpython.com/python-data-structures/) 44 | > Java: [Lesson: Interfaces (The Java™ Tutorials > Collections)](https://docs.oracle.com/javase/tutorial/collections/interfaces/index.html) 45 | > C++: [Containers - C++ Reference](https://www.cplusplus.com/reference/stl/) 46 | 47 | ### Basic 48 | * **Array**: 49 | 50 | > Python: list 51 | > Java: ArrayList 52 | > C++: std::vector 53 | 54 | * **LinkedList**: 55 | 56 | > Python: list or customized 57 | > Java: LinkedList 58 | > C++: std::list 59 | 60 | * **HashTable**: 61 | 62 | > Python: dict(), collections.defaultdict() 63 | > Java: HashMap 64 | > C++: std::unordered_map 65 | 66 | * **HashSet** 67 | 68 | > Python: set() 69 | > Java: HashSet() 70 | > C++: std::unordered_set 71 | 72 | ### Advanced 73 | * **Tree** 74 | * **Graph** 75 | * **Stack** 76 | 77 | > Python: list, deque 78 | > Java: Stack 79 | > C++: std::stack 80 | 81 | * **Deque** 82 | 83 | > Python: collections.deque() 84 | > Java: LinkedList, ArrayDeque 85 | > C++: std::deque 86 | 87 | * **PriorityQueue** (heap) 88 | 89 | > Python: heapq, collections.PriorityQueue (thread safe) 90 | > Java: PriorityQueue 91 | > C++: std::priority_queue 92 | 93 | * BinarySearchTree (map) 94 | 95 | > Python: None (try to use [bisect](https://docs.python.org/3/library/bisect.html), but big O is different) 96 | > Java: TreeMap 97 | > C++: std::map 98 | 99 | * BinarySearchTree (set) 100 | 101 | > Python: None (try to use [bisect](https://docs.python.org/3/library/bisect.html)], but big O is different) 102 | > Java: TreeSet 103 | > C++: std::set 104 | 105 | * Trie: a map of key, map pairs; 106 | 107 | > - `T = lambda: collections.defaultdict(T)` 108 | > - [Implement Trie (Prefix Tree) - LeetCode](https://leetcode.com/problems/implement-trie-prefix-tree/) 109 | 110 | * UnionFind 111 | > You can implement by yourself in an interview, here is a very concise and brilliant template (path compression has been included): 112 | > ```py 113 | > uf = {i:i for i in range(len(M))} 114 | > def find(p): 115 | > if uf[p] != p: 116 | > uf[p] = find(uf[p]) 117 | > return uf[p] 118 | > 119 | > def union(p1, p2): 120 | > a1, a2 = find(p1), find(p2) 121 | > uf[a2] = a1 122 | > ``` 123 | > WARNING: this implementation has some limitation, such as you need to traverse the `uf` by calling `find` for every element with a `set` to count the number of unions, this operation is O(n) since the length of the path for every element will be no more than 2. 124 | > 125 | >Time complexity for union find is a little bit tricky, the union and find operation will take log*n time. Please check this [wiki](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) to get a better understanding. 126 | 127 | ### Ultimate 128 | * *Red-black tree* 129 | * *KD-Tree* 130 | * *B-tree* 131 | * *Segment Tree* 132 | 133 | ## Algorithms 134 | 135 | > Cheatsheet(ALGS4) (Java): [Algorithms and Data Structures Cheatsheet](https://algs4.cs.princeton.edu/cheatsheet/) 136 | 137 | ### Basics 138 | * **DFS** 139 | * **BFS** 140 | * **BackTracking** 141 | * **Binary Search** 142 | * **Two pointers** 143 | * Fast and slow pointers 144 | * (Thinking from Reverse Order) 145 | 146 | ### Advanced 147 | * **Topological sort** 148 | * Greedy: e.g. Huffman coding 149 | * Divide and Conquer 150 | * UnionFind 151 | * Cycle detection in undirected graph 152 | * Cycle detection in directed graph 153 | * Find SCC in directed Graph 154 | * Lowest Common Ancestor 155 | * [Lowest Common Ancestor of a Binary Tree - LeetCode](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)[Lowest Common Ancestor Binary Tree - YouTube](https://www.youtube.com/watch?v=13m9ZCB8gjw&list=RDCMUCZLJf_R2sWyUtXSKiKlyvAw&index=8&frags=pl%2Cwn) 156 | * Dijkstra: single source shortest path, O(nlogn + m ) 157 | * Bellman-Floyd: single source with negative weights, O(mn) 158 | * Floyd-Warshall: all pairs shortest path; O(n^3) 159 | * Reservoir Sampling 160 | * KMP [Implement strStr() - LeetCode](https://leetcode.com/problems/implement-strstr/) 161 | * *Manacher* 162 | * *Morris* 163 | 164 | ### Ultimate 165 | * *Minimum Spanning Tree: Prim’s, Kruskal* 166 | * *minimum s-t cut: Ford-Fulkerson* 167 | * *global min cut: Karger’s, Karger Stein* 168 | 169 | 170 | ### Dynamic Progamming 171 | > Recipe for DP: 172 | > 1. Identify optimal substructure 173 | > 2. Find a recursive formulation for the optimal solution 174 | > 3. Use dynamic programming to find optimal solution 175 | > 4. If needed, keep track of some additional info so that the algorithm from step 3 can find the actual solution 176 | 177 | Classical Problems: 178 | 179 | * LCS: longest common subsequence [Longest Common Subsequence - LeetCode](https://leetcode.com/problems/longest-common-subsequence/) 180 | * unbounded knapsack 181 | * 0/1 knapsack 182 | 183 | LeetCode 184 | LC322M: [Coin Change - LeetCode](https://leetcode.com/problems/coin-change/) 185 | 186 | Complexity: reduce time complexity from exponential with brutal force to polynomial. 187 | 188 | ## Resources 189 | ### Blogs and Repos 190 | > 1. [GitHub - Tech-Interview-Cheat-Sheet](https://github.com/TSiege/Tech-Interview-Cheat-Sheet): An interview cheatsheet. 191 | > 2. [Data Structures - GeeksforGeeks](https://www.geeksforgeeks.org/data-structures/): data structure basics 192 | > 3. [fucking-algorithm](https://github.com/labuladong/fucking-algorithm/tree/english): algorithms (available in both English and Chinese) 193 | > 4. [fuck-coding-interviews](https://github.com/vinta/fuck-coding-interviews): data structure and algorithms implementation, as well as solutions for leetcode questions sorted by categories. 194 | 195 | ### Videos 196 | > These videos and Youtuber might be helpful. 197 | 198 | > You can also take some online courses or watch some famous courses online to learn data structures and algorithms systematically if you have enough time. This might be time consuming but useful. Such as CS106B@Stanford, CS161@Stanford, 6.006@MIT, etc. 199 | 200 | > Some textbooks you can refer to but not required: 201 | > 202 | > - [Introduction to Algorithms](https://www.google.com/books/edition/Introduction_to_Algorithms/i-bUBQAAQBAJ?hl=en&gbpv=0) 203 | > - [Algorithms](https://algs4.cs.princeton.edu/home/) 204 | 205 | #### Algorithms 206 | - @[Tushar Roy - Coding Made Simple](https://www.youtube.com/user/tusharroy2525/featured) 207 | - [Graph Algorithms @ Tushar Roy - Coding Made Simple](https://www.youtube.com/playlist?list=PLrmLmBdmIlpu2f2g8ltqaaCZiq6GJvl1j): I found these videos very useful to understand and implement graph algorithms. Tushar also provide the source code. 208 | 209 | - [Dynamic Programming @ Tushar Roy - Coding Made Simple](https://www.youtube.com/playlist?list=PLrmLmBdmIlpsHaNTPP_jHHDx_os9ItYXr) 210 | 211 | - @[Abdul Bari](https://www.youtube.com/channel/UCZCFT11CWBi3MHNlGf019nw/playlists) 212 | - [Algorithms](https://www.youtube.com/playlist?list=PLDN4rrl48XKpZkf03iYFl-O29szjTrs_O): most of algorithms mentioned in this blog are covered. 213 | 214 | #### Interview tips 215 | - @[Clément Mihailescu](https://www.youtube.com/channel/UCaO6VoaYJv4kS-TQO_M-N_g) 216 | - [Google Coding Interview With A College Student](https://www.youtube.com/watch?v=3Q_oYDQ2whs) 217 | - [Google Coding Interview With A High School Student](https://www.youtube.com/watch?v=qz9tKlF431k&t=4s) 218 | 219 | - @[TechLead](https://www.youtube.com/channel/UC4xKdmAXFh4ACyhpiQ_3qBw) 220 | - [How to solve coding interview problems ("Let's leetcode") @ TechLead](https://www.youtube.com/watch?v=dIrS31CCITM) 221 | 222 | ### Takeaway 223 | 224 | #### Before the interview 225 | - Practice on some platforms such as LeetCode, Google KickStart, HackerRank; 226 | - LeetCode Premium is very useful, especially you can filter questions with tags and companies, and sort problems by frequency. 227 | - Participating in the weekly contest on LeetCode is also a good way to practice interivew. 228 | - Find someone to do mock interview. 229 | 230 | #### In the interview 231 | - Correctness matters, find a workable solutions at least. 232 | - Time matters, spending too much on a simple solution you have known is not a good option. 233 | - Communication matters, think loud and let the interviewer know what you are thinking about. Then the interviewer can know how to give you hints. 234 | - Handle edge cases, you can skip some parts handling edge cases and complete them later, but you must let the interviewer know that you have noticed these edge cases. You can add some TODOs or move it to a function to implement later. 235 | - Time complexity and space complexity are necessary. Even the interviewer forgets to ask you about the complexity analysis, you should address them. Because it is an important part of the feedback the interviewer will submit. 236 | 237 | --------------------------------------------------------------------------------