├── LICENSE.md ├── README.md ├── intro_algorithms.pdf └── more_algorithms.pdf /LICENSE.md: -------------------------------------------------------------------------------- 1 | ## Creative Commons Attribution-ShareAlike 4.0 International Public License 2 | 3 | By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. 4 | 5 | ### Section 1 – Definitions. 6 | 7 | a. __Adapted Material__ means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. 8 | 9 | b. __Adapter's License__ means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. 10 | 11 | c. __BY-SA Compatible License__ means a license listed at [creativecommons.org/compatiblelicenses](http://creativecommons.org/compatiblelicenses), approved by Creative Commons as essentially the equivalent of this Public License. 12 | 13 | d. __Copyright and Similar Rights__ means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. 14 | 15 | e. __Effective Technological Measures__ means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. 16 | 17 | f. __Exceptions and Limitations__ means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. 18 | 19 | g. __License Elements__ means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution and ShareAlike. 20 | 21 | h. __Licensed Material__ means the artistic or literary work, database, or other material to which the Licensor applied this Public License. 22 | 23 | i. __Licensed Rights__ means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. 24 | 25 | j. __Licensor__ means the individual(s) or entity(ies) granting rights under this Public License. 26 | 27 | k. __Share__ means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. 28 | 29 | l. __Sui Generis Database Rights__ means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. 30 | 31 | m. __You__ means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning. 32 | 33 | ### Section 2 – Scope. 34 | 35 | a. ___License grant.___ 36 | 37 | 1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: 38 | 39 | A. reproduce and Share the Licensed Material, in whole or in part; and 40 | 41 | B. produce, reproduce, and Share Adapted Material. 42 | 43 | 2. __Exceptions and Limitations.__ For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. 44 | 45 | 3. __Term.__ The term of this Public License is specified in Section 6(a). 46 | 47 | 4. __Media and formats; technical modifications allowed.__ The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material. 48 | 49 | 5. __Downstream recipients.__ 50 | 51 | A. __Offer from the Licensor – Licensed Material.__ Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. 52 | 53 | B. __Additional offer from the Licensor – Adapted Material.__ Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapter’s License You apply. 54 | 55 | C. __No downstream restrictions.__ You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. 56 | 57 | 6. __No endorsement.__ Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i). 58 | 59 | b. ___Other rights.___ 60 | 61 | 1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. 62 | 63 | 2. Patent and trademark rights are not licensed under this Public License. 64 | 65 | 3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties. 66 | 67 | ### Section 3 – License Conditions. 68 | 69 | Your exercise of the Licensed Rights is expressly made subject to the following conditions. 70 | 71 | a. ___Attribution.___ 72 | 73 | 1. If You Share the Licensed Material (including in modified form), You must: 74 | 75 | A. retain the following if it is supplied by the Licensor with the Licensed Material: 76 | 77 | i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); 78 | 79 | ii. a copyright notice; 80 | 81 | iii. a notice that refers to this Public License; 82 | 83 | iv. a notice that refers to the disclaimer of warranties; 84 | 85 | v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; 86 | 87 | B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and 88 | 89 | C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. 90 | 91 | 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. 92 | 93 | 3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable. 94 | 95 | b. ___ShareAlike.___ 96 | 97 | In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply. 98 | 99 | 1. The Adapter’s License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-SA Compatible License. 100 | 101 | 2. You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material. 102 | 103 | 3. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply. 104 | 105 | ### Section 4 – Sui Generis Database Rights. 106 | 107 | Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: 108 | 109 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database; 110 | 111 | b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and 112 | 113 | c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database. 114 | 115 | For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. 116 | 117 | ### Section 5 – Disclaimer of Warranties and Limitation of Liability. 118 | 119 | a. __Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.__ 120 | 121 | b. __To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You.__ 122 | 123 | c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. 124 | 125 | ### Section 6 – Term and Termination. 126 | 127 | a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. 128 | 129 | b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates: 130 | 131 | 1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or 132 | 133 | 2. upon express reinstatement by the Licensor. 134 | 135 | For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. 136 | 137 | c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. 138 | 139 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. 140 | 141 | ### Section 7 – Other Terms and Conditions. 142 | 143 | a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. 144 | 145 | b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License. 146 | 147 | ### Section 8 – Interpretation. 148 | 149 | a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. 150 | 151 | b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. 152 | 153 | c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. 154 | 155 | d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. 156 | 157 | > Creative Commons is not a party to its public licenses. Notwithstanding, Creative Commons may elect to apply one of its public licenses to material it publishes and in those instances will be considered the “Licensor.” The text of the Creative Commons public licenses is dedicated to the public domain under the [CC0 Public Domain Dedication](https://creativecommons.org/publicdomain/zero/1.0/legalcode). Except for the limited purpose of indicating that material is shared under a Creative Commons public license or as otherwise permitted by the Creative Commons policies published at [creativecommons.org/policies](http://creativecommons.org/policies), Creative Commons does not authorize the use of the trademark “Creative Commons” or any other trademark or logo of Creative Commons without its prior written consent including, without limitation, in connection with any unauthorized modifications to any of its public licenses or any other arrangements, understandings, or agreements concerning use of licensed material. For the avoidance of doubt, this paragraph does not form part of the public licenses. 158 | > 159 | > Creative Commons may be contacted at creativecommons.org. 160 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # interview study sheet 2 | A quick study sheet I use as a refresher :smile: 3 | 4 | - [Data Structures](#data-structures) 5 | - [Algorithms](#algorithms) 6 | - [Other Concepts](#other-concepts) 7 | - [Math](#math) 8 | - [Common Problems](#common-problems) 9 | - [Just Python Things](#just-python-things) 10 | - [Java Cheatsheet](#java-cheatsheet) 11 | - [Programming Languages](#programming-languages) 12 | - [Problem-solving Strategies](#problem-solving-strategies) 13 | 14 | Also, there's much more to computer science than these simple topics! There are a multitude of online resources for broadening and deepening your core CS knowledge; https://teachyourselfcs.com/ is one such site. 15 | 16 | ## Data Structures 17 | ### Array 18 | * An *array* is a collection with specified size 19 | * Dynamic array: some languages' implementations automatically expand as you add elements 20 | * Access elements directly by index 21 | * Time complexity: 22 | * Access by index: `O(1)` 23 | * Search by value: `O(n)` 24 | * Insert: `O(n)` (need to shift values) 25 | * Delete: `O(n)` (need to shift values) 26 | 27 | ### Linked List 28 | * A *linked list* is a collection of nodes where each node has a value and a reference 29 | * **Singly linked list**: nodes have pointers to the next node 30 | * **Doubly linked list**: nodes have pointers to next and previous nodes 31 | * Time complexity: 32 | * Access by index: `O(n)` 33 | * Search by value: `O(n)` 34 | * Insert: `O(1)` 35 | * Delete: `O(1)` 36 | 37 | ### Stacks & Queues 38 | * **Stack**: last in, first out (LIFO) 39 | * Adding an element and popping the most recently added element are `O(1)` operations 40 | * **Queue**: first in, first out (FIFO) 41 | * Adding an element and popping the oldest element are `O(1)` operations 42 | * **Double-ended queue**: stack + queue combined 43 | * `push` adds elements & `pop` extracts elements 44 | 45 | ### Trees 46 | * A *tree* is an undirected, connected, acyclic graph 47 | * Has `v` vertices and `v-1` edges 48 | * Any two vertices are connected by a unique path 49 | * A **leaf** is a vertex of degree 1 50 | * One node is designated as the **root** 51 | * Each node has parent and/or children pointers 52 | * A node's height is the length of its path to the root 53 | * A **forest** has multiple distinct trees (a disjoint union) 54 | * An **n-ary tree** has at most `n` children per node 55 | 56 | #### Binary Tree 57 | * A *binary tree* has nodes with at most 2 children (designated left & right) 58 | * **Full**: every node has 0 or 2 children 59 | * Number of nodes is at most `2^(h+1)-1` 60 | * **Complete**: every level, except possibly the last, is filled, and the last level's nodes are as far left as possible 61 | * Number of internal nodes: `floor(n/2)` 62 | * **Balanced**: has the minimum possible maximum depth 63 | * Height is `ceil(lg(n+1))` 64 | * Traversals: 65 | * **Pre-order**: open current, visit left subtree, visit right subtree 66 | * **In-order**: visit left subtree, open current, visit right subtree (returns sorted list) 67 | * **Post-order**: visit left subtree, visit right subtree, open current 68 | * **Level-order**: breadth-first traversal, level by level 69 | 70 | #### Binary Search Tree 71 | * A *binary search tree* is an ordered binary tree 72 | * Satisfies the BST property: each node's value is greater than all keys stored in the left subtree and less than all keys stored in the right subtree 73 | * Designed to make searching faster--each comparison allows operations to skip about half the tree 74 | * Search: recursively search subtrees; takes `O(h)` 75 | * Insertion: like search, but insert node when a leaf is reached; takes `O(h)` 76 | * Deletion: more complicated; takes `O(h)` 77 | * If deleting a node with no children, just do it 78 | * If deleting a node with a single child, replace the node with its subtree 79 | * If deleting a node with two children, swap with minimum value in right subtree or maximum value in left subtree, then delete the node (which should now be a leaf) 80 | 81 | #### AVL Tree 82 | * An *AVL tree* is a self-balancing binary search tree 83 | * Pairs of subtrees differ in height by at most 1 84 | * Lookup, insertion, and deletion all take `O(log n)`, since height is at most `O(log n)` 85 | * Rotation balances the tree on update 86 | * Implement by adding a **balance factor** on each node (difference between subtree heights) 87 | 88 | #### Trie 89 | * A *trie* is a special tree that stores subsequences of values, also known as a prefix tree 90 | * Each node's descendants share a common prefix given by the node 91 | * Useful for autocomplete 92 | 93 | ### Hashing 94 | * A *hash function* is a function mapping an object to an integer such that if `a==b`, `H(a)==H(b)` 95 | * **Universal hashing**: a randomized way of drawing a hash function from some set of functions so that performance is good in expectation 96 | * **Perfect hashing**: has no collisions; usually only practical when the set of keys is roughly constant 97 | 98 | #### Hash Tables 99 | * A *hash table* is an array whose indices correspond to results from a hash function (implemented as a dictionary in Python) 100 | * Provides `O(1)` lookup, assuming load factor is small enough 101 | * **Load factor**: `n/k`, where `n` is number of entries and `k` is number of buckets 102 | * Collision resolution 103 | * Chaining (e.g. with linked lists) 104 | * Open addressing (e.g. with linear probing, quadratic probing, or double hashing) 105 | * **Table doubling**: choose a new hash function to map to the new size and insert elements from old table into new table 106 | * **Simple uniform hashing assumption (SUHA)**: a hash function maps to any slot with equal probability 107 | 108 | ### Heap 109 | * A *heap* is a special tree where nodes have higher (in the case of a min-heap) values than their parents 110 | * Binary heap: 111 | * Heapify in `O(n)` 112 | * Find min in `O(1)` 113 | * Extract min, increase key, insert, delete in `O(log n)` 114 | * Can implement as a list where a node at index `i` has children at `2i+1` and `2i+2` (0-indexed) 115 | 116 | ### Graph 117 | * A *graph* is a collection of nodes and edges and can be directed or undirected 118 | * **Cycle**: path that loops onto itself 119 | * **Topological sort**: linear ordering of vertices such that directional constraints are preserved in a directed acyclic graph (DAG) 120 | * Generate using DFS by prepending to output list 121 | * **Spanning tree**: a tree that includes all nodes in the graph 122 | * Minimum spanning tree: a spanning tree with minimum total edge weights 123 | * **Complete graph**: fully connected; every pair of nodes has an edge 124 | * **Bipartite graph**: split into two groups A and B where there are no edges within each groups 125 | * **Clique**: a complete subgraph 126 | 127 | 128 | ## Algorithms 129 | ### Binary Search 130 | * Given a sorted list, start at the midpoint and divide and conquer 131 | * **Exponential search** is like binary search but in one direction (e.g. can be used in infinite sequence) 132 | * `O(log n)` 133 | 134 | ### Sorting 135 | #### Insertion 136 | * Maintain a sorted sublist and insert new elements in it appropriately 137 | * Sorts in-place; stable 138 | * Best-case `O(n)`, average `O(n^2)`, worst `O(n^2)` 139 | 140 | #### Bubble 141 | * On each pass through the array, compare adjacent pairs of elements and swap if necessary 142 | * Sorts in-place; stable 143 | * Best-case `O(n)`, average `O(n^2)`, worst `O(n^2)` 144 | 145 | #### Selection 146 | * Exchange current element with smallest element to the right of the current element 147 | * Sorts in-place; unstable 148 | * Best-case `O(n^2)`, average `O(n^2)`, worst `O(n^2)` 149 | 150 | #### Merge 151 | * Recursively divide until sublists are size 1, then recursively merge the sublists 152 | * Requires `O(n)` space; stable 153 | * Best-case `O(n log n)`, average `O(n log n)`, worst `O(n log n)` 154 | 155 | #### Quick 156 | * Set some pivot element in the array; move elements smaller than pivot to its left and elements larger to the right 157 | * Recursively sort left and right sublists 158 | * Requires `O(log n)` space; stable 159 | * Best-case `O(n log n)`, average `O(n log n)`, worst `O(n^2)` 160 | 161 | #### Counting/Bucket 162 | * For lists whose elements' values are in a bounded, constant range 163 | * Not a comparison sort so best & average is `O(n+k)` and worst is `O(n^2)` (not bounded to `O(n log n)`) 164 | * Iterate through list and place items in buckets; can be stable 165 | 166 | #### Radix 167 | * Apply a stable counting sort to every place value in a number 168 | * Sort places from least to most significant 169 | * Requires `O(n+k)` space; `O(d(n+k))` time 170 | * Also not a comparison sort 171 | 172 | ### Graph Search 173 | * Given a graph, find a path from a start node to an end node 174 | * General strategy: expand a node, check to see if it's the goal node, add its children to the *search agenda* 175 | * In the case of weighted graphs, a **heuristic** may help find the shortest path faster 176 | * **Admissible**: heuristic's value for a node is less than actual distance from node to goal (`H(n,G) ≤ dist(n,G)` for all nodes `n`) 177 | * **Consistent**: heuristic follows triangle inequality (`|H(A)-H(B)| ≤ dist(A,B)` for all nodes `A,B`) 178 | 179 | #### Depth-first 180 | * Implement with a stack (add new paths to the front of the agenda) 181 | * Can use for cycle detection 182 | 183 | #### Breadth-first 184 | * Implement with a queue (add new paths to the end of the agenda) 185 | * In an unweighted graph, guaranteed to find shortest path 186 | 187 | #### Hill-climbing 188 | * Add new paths to the front of the agenda 189 | * Sort *new* paths by terminal node's heuristic 190 | 191 | #### Best-first 192 | * Add new paths to the front of the agenda 193 | * Sort *all paths* in agenda by terminal node's heuristic 194 | 195 | #### Branch and bound 196 | * Add new paths to the front of the agenda 197 | * Sort agenda by path length so far 198 | * Can also add a heuristic or extended set (or both) 199 | 200 | #### A* 201 | * Branch and bound with heuristic and extended set 202 | * Heuristic must be consistent 203 | 204 | #### Dijkstra's 205 | * Find shortest path between two nodes (branch and bound with extended set and without heuristic) 206 | * Can't handle negative edge weights 207 | * Using a Fibonacci heap, runtime is `O(|E|+|V|log|V|)` 208 | 209 | #### Bellman-Ford 210 | * Compute shortest paths from a single source to all other nodes in the graph 211 | * Can handle negative edge weights & detect negative-weight cycles 212 | * Worst-case runtime is `O(|V||E|)` 213 | 214 | #### Floyd-Warshall 215 | * Dynamic programming all-pairs shortest paths algorithm 216 | * `dp(i,j,k+1)=min(dp(i,j,k),dp(i,k+1,k)+dp(k+1,j,k))` 217 | 218 | ### Other Graph Algorithms 219 | #### Min Cut & Max Flow 220 | * The *min cut* problem asks for the minimum number of edges you can remove from a graph to disconnect a given source and sink 221 | * The *max flow* problem asks for the maximum flow from a given source to sink 222 | * **Karger's** randomized min-cut algorithm 223 | * **Ford-Fulkerson** computes max flow 224 | * Example of linear duality 225 | 226 | #### Minimum Spanning Tree 227 | * **Prim's** adds the smallest-weight connected edge that doesn't create a cycle 228 | * `O(|E|+|V|log|V|)` so use in dense graphs 229 | * **Kruskal's** adds the globally smallest edge and keeps a forest ( 230 | * `O(|E|log|V|)` 231 | 232 | ### Greedy Algorithms 233 | * Locally optimal choices lead to globally optimal solution 234 | * Be careful--this is usually rare! 235 | * Prim's, Kruskal's, interval scheduling, Huffman codes, Dijkstra's 236 | 237 | ### Dynamic Programming 238 | * A general method for solving a problem with optimal substructure by breaking it down into overlapping subproblems 239 | * **Top-down**: memoize (store) solutions to subproblems and solve problem recursively 240 | * **Bottom-up**: build up subproblems from base case up and avoid recursive overhead 241 | * Order subproblems by topologically sorting DAG of dependencies 242 | * Knapsack problem, longest common subsequence, coin change, edit distance, minimum number of jumps, longest palindrome substring, balanced partition 243 | 244 | ## Other Concepts 245 | ### General 246 | * Static/dynamic checking 247 | * Strongly/weakly typed 248 | * Compiled/interpreted 249 | * Shallow/deep copying 250 | * Immutable/mutable 251 | * Defensive copying 252 | * Pseudo-polynomial runtime 253 | 254 | ### Asymptotic Notation 255 | * Look [here](https://en.wikipedia.org/wiki/Big_O_notation#Formal_definition) for formal definitions 256 | * O - asymptotic upper bound 257 | * o - asymptotic upper bound, excluding same rate 258 | * Ω - asymptotic lower bound 259 | * ω - asymptotic lower bound, excluding same rate 260 | * Θ - same asymptotic growth 261 | * Exponential > polynomial > logarithmic > constant 262 | * Can ask for worst, best, or average case 263 | 264 | ### Object-oriented Programming 265 | Inspiration from [here](https://quizlet.com/22436874/oop-vocabulary-object-oriented-programming-flash-cards/) 266 | * *Abstract data type*: defined logically by set of values and set of operations 267 | * *Class*: basic concept in OOP, bundles data type information with actions 268 | * *Object*: runtime value which belongs to a class 269 | * *Encapsulation*: information hiding to ensure data integrity 270 | * *Hierarchy*: classes can have super- and subclasses 271 | * *Inheritance*: a subclass inherits data and methods from its parent classes 272 | * *Overriding*: a subclass inherits parent methods but may override them 273 | * *Polymorphism*: different classes in a program can respond to the same message in different ways; useful when an object's class can't be determined at compile time 274 | * *Identity*: checks whether two objects are the same location in memory 275 | * *Equality*: checks whether two objects are behaviorally equivalent 276 | 277 | ### Concurrency 278 | * Starting with a single-threaded program, threads can spawn new threads 279 | * *Data races*: bugs in concurrent programs resulting from concurrent access to shared objects 280 | * Ways to prevent data races: protect objects with locks so that only one thread can access an object at once, or use a special hyperobject 281 | 282 | ### Design Patterns 283 | * *Model-view-controller*: model stores data, controller updates model, view generates user interface 284 | * *Factory method*: use a factory object to create other objects rather than using a constructor 285 | * *Singleton*: restrict instantiation of a class to a single object 286 | * *Observer*: subjects notify observers of any state changes (usually by calling their methods); used in MVC 287 | * Lots more 288 | 289 | ### The Internet 290 | #### HTTP Methods 291 | * GET: used to retrieve data, no other effect on the data 292 | * POST: used to send data to the server (e.g. form) 293 | * PUT: replaces current representation of resource (idempotent) 294 | * DELETE: remove current representation resource 295 | 296 | #### HTTP Status Codes 297 | * 200 OK: success 298 | * 400 Bad Request: syntax could not be understood 299 | * 401 Unauthorized: request not fulfilled due to lack of authorization 300 | * 403 Forbidden: request understood but not fulfilled, authorization will not help 301 | * 404 Not Found: URI could not be matched 302 | * 408 Request Timeout: server did not receive a timely response from client 303 | * 418 I'm a teapot: the resulting entity body *may* be short and stout 304 | * 500 Internal Server Error: server exception 305 | * 503 Service Unavailable: server unable to handle the request (temporary) 306 | * 504 Gateway Timeout: server did not receive a timely response from upstream server 307 | 308 | #### Networking 309 | * [OSI Model](https://en.wikipedia.org/wiki/OSI_model) 310 | 311 | ### Recursion 312 | * [*Master theorem*](https://en.wikipedia.org/wiki/Master_theorem): is most work performed in the root node, in the leaves, or evenly distributed in the rows of the recursion tree? 313 | 314 | ### Terminal Commands 315 | * Basic commands: `ls`, `cd`, `mkdir`,`touch`, `cp`, `mv`, `rm`, `pwd`, `chmod`, `chown`, `man` 316 | * `ping`: ping a server, used for network diagnostics 317 | * `ps`: display info about processes running on the system 318 | * `grep`: searches through files for lines matching a given regular expression 319 | * `tar`, `zip`, `unzip`: make and open compressed archives 320 | * `curl`: send requests to web servers 321 | * `wget`: download files from the web (can do recursively) 322 | * `dig`: query over DNS 323 | * `crontab`: use Cron to schedule recurring tasks 324 | 325 | ### Git 326 | * `init`: creates/initializes `.git` folder in current directory 327 | * `clone`: clone repo into new directory 328 | * `pull`: fetch from another repo and integrate 329 | * `git pull` is same as `git fetch` then `git merge FETCH_HEAD` 330 | * `add`: add files to index of contents for next commit 331 | * `rm`: remove files from working tree and index 332 | * `commit`: record changes to the repo, along with a commit message 333 | * `rebase`: transplant changes on one branch to another, edit commit history 334 | * `branch`: list, create, or delete branches 335 | * `checkout`: switch branches (or just get a version of specific files) 336 | * `status`: show the working tree's status 337 | * `diff`: show changes between commits or the working tree 338 | * `log`: show commit logs in a repo 339 | * `remote`: manage tracked remote repos 340 | * `reset`: reset current HEAD to a different state (can do `--hard` or `--soft`) 341 | * Also cool things like `bisect`, `fixup` 342 | 343 | ## Math 344 | 345 | ### Combinatorics 346 | * `n(n-1)/2`: number of handshakes in a group 347 | * `n-1`: number of rounds in a knockout tournament 348 | * `2^k`: number of binary strings of length `k` 349 | * `n!/(n-k)!`: permutations of `n` items taken `k` at a time 350 | * `n!/(k!(n-k)!)`: combinations of `n` items taken `k` at a time 351 | 352 | ### Probability 353 | * **Bayes' theorem**: `P(A|B) = P(B|A)P(A)/P(B)` 354 | 355 | ## Common Problems 356 | Lots of these taken from [this blog](http://www.ardendertat.com/2012/01/09/programming-interview-questions/). 357 | * **Fibonacci sequence**: print the `n`th Fibonacci number 358 | * Optimally, do this recursively and cache the subproblem solutions 359 | * **Array pair sums**: given an array, output pairs which sum to a number `k` 360 | * Can do in `O(n)` with a set data structure. For each element in the array, check to see if `k-a[i]` is in the set, then add the element to a set. 361 | * **Reverse a linked list**: reverse a singly linked list 362 | * Track previous and current nodes; iterate through list and swap the direction of pointers. Time is `O(n)` and space is `O(1)`. 363 | * **Matrix region sum**: given multiple rectangular regions in a matrix, compute the sum of numbers in that region 364 | * Memoize sums of regions with the constraint that corners are at `m[0][0]` 365 | * **Word permutation**: find all permutations of a word 366 | 367 | ```python 368 | def permute(word): 369 | if len(word) == 1: 370 | return {word} 371 | else: 372 | result = set() 373 | permutations = permute(word[:-1]) 374 | letter = word[-1] 375 | for p in permutations: 376 | result.update([p[0:i]+letter+p[i:] for i in range(0,len(word)+1)]) 377 | return result 378 | ``` 379 | 380 | * **Median of number stream**: given a continuous stream of numbers, find the median of numbers so far at any time 381 | * Optimally, keep a max-heap of the smaller half of the numbers and a min-heap of the larger half of the numbers 382 | * **Infinite array search**: given a sorted, infinite-length array, find a given value 383 | * Modify binary search to start at the array's first element and exponentially increase the index you search at. Time is `O(log n)` 384 | * **Anagram pair**: determine if two words are anagrams 385 | * Comparison sort: sort the words in alphabetical order and check for equality. `O(n log n)`, where `n` is word length. 386 | * Count letters: use a hash table to track counts of letters in both words. `O(n)` runtime. 387 | * **Anagram dictionary**: determine which words in a list are anagrams of a given word 388 | * Check for the membership of every permutation of the input word in the dictionary 389 | * **Anagram list**: determine which sets of words in a dictionary are anagrams 390 | * Abstractly, hash each word and group by word. A hash can be a 26-digit string, or you can sort each word. 391 | * **Binary search tree verification**: verify whether a tree satisfies the binary search tree property 392 | * For each node, track its possible minimum and maximum values 393 | * Performing an inorder traversal should produce a sorted list 394 | * **Largest continuous sum**: in an array of integers, determine the subsequence with the largest sum 395 | * Track maximum sum encountered so far and check whether current sum is greater. Reset current sum when it becomes negative. Time is `O(n)` and space is `O(1)`. 396 | * **-1/0/1 array**: given an array where values are -1, 0, or 1, sort the array 397 | * Bucket sort (but this takes `O(n)` space) 398 | * Iterate through the list and track pointers for min and max index. If a value is -1, swap it with the element at the min index and increment min index. If a value is 1, swap it with the element at max index and decrement max index. Time is `O(n)` and space is `O(1)`. 399 | * **k-th largest element**: find the `k`th largest element in an unsorted array 400 | * Modify quicksort to recursively sort on pivots in left/right subarrays (average `O(n)`, worst-case `O(n^2)`) 401 | * Median of medians algorithm 402 | * **Find missing number**: given an array where every number except one appears an even number of times, find the number that appears an odd number of times 403 | * Optimally, bitwise XOR by numbers in the list (XORing an even number of times resets the number to its original value). Time is `O(n)` and space is `O(1)` 404 | * **Knapsack**: given a set of items each with weights and values, maximize value while keeping total weight under a limit 405 | * Dynamic programming: say weight limit is `W`. Create an array `m[w]` where each element is the maximum value of items with a weight limit `w≤W`. Optimize by dividing item weights and weight limit by their greatest common divisor. Runtime `O(nW)`. 406 | * **Balanced partition**: given a set of numbers, partition them so that the sums of the partitions are as close as possible 407 | * Greedy method: iterate through sorted list and add items to the smaller-sum partition 408 | * Dynamic programming: determine if a subset of the input sums to `n/2` (where `n` is the sum of the input numbers) 409 | * **LRU Cache**: implement a least-recently used cache 410 | * Use two data structures: queue (implemented using doubly linked list) and hash table. Queue contains pages in access order & hash map maps pages to queue node 411 | 412 | ## Just Python Things 413 | ### Strings 414 | * `s.center(w,[fillchar])`: returns centered string in string of width `w` 415 | * `s.count(sub[,start[,end]])`: returns count of non-overlapping occurences of substring 416 | * `sub in s`: returns `True` if `sub` is in `s` 417 | * `s.find(sub[,start[,end]])`: returns start index of substring or `-1` 418 | * `s.join(iter)`: join items in iterable, separated by `s` 419 | * `s.strip([chars])`: removing leading and trailing characters 420 | * `s.replace(old,new[,count])`: returns copy of `s` with `old` replaced by `new` 421 | * `s.isalpha()`: returns `True` if all characters in `s` are alphabetic 422 | * `s.isdigit()`: returns `True` if all characters in `s` are digits 423 | 424 | ### Lists 425 | * `l=[]`: initialize 426 | * `len(l)`: get size 427 | * `l.append(val)`: append a value 428 | * `l.insert(i,val)`: insert a value at position 429 | * `l.extend(lst)`: append all values in a list 430 | * `l.pop([i])`: remove an item and return it (defaults to last item) 431 | * `x in l`: check membership 432 | * `l.sort(cmp=None,key=None,reverse=False)`: sort in place 433 | * `sorted(iterable[, cmp[, key[, reverse]]])`: return a new stably sorted list 434 | * `l.reverse()`: reverse a list in place 435 | * `range(start,end)`: get a list with items from `start` (inclusive) to `end` (exclusive) 436 | * `[ for in if ]`: list comprehension 437 | * `listname[start:end:slice_size]`: slicing 438 | 439 | ### Sets 440 | * `set()` or `{l}`: initialize 441 | * `len(s)`: get cardinality 442 | * `x in s`: check membership 443 | * `s.update(other)`: add values of `other` to `s` 444 | * `s | other | ...`: return a union of sets 445 | * `s & other & ...`: return an intersection of sets 446 | * `s - other - ...`: return difference of sets 447 | * `s ^ other`: return set of elements uniquely in sets 448 | 449 | ### Dictionaries 450 | * `d={}`: initialize 451 | * `d[key]` or `d.get(key)`: get the value at `key` (the latter returns `None` if not found) 452 | * `len(d)`: get item count 453 | * `key in d`: check membership 454 | * `d.pop(key)`: remove and return a value in the dictionary 455 | * `del d[key]`: delete an item 456 | * `d.update(other)`: update/overwrite with keys & values from `other` 457 | * `d.items()`: return a list of `(key,value)` tuples 458 | * `d.keys()`: return a list of dicionary keys 459 | * `d.values()`: return a list of dictionary values 460 | * `{: for in if }`: list comprehension 461 | 462 | ### Error Handling 463 | * Common Python way to indicate error is to `raise Exception` (or a subclass of `Exception`) 464 | * Catch these with `try/except` blocks 465 | 466 | ### Classes 467 | ```Python 468 | class Node(ParentClass): 469 | def __init__(self, val, parent): 470 | self.val = val 471 | self.parent = parent 472 | self.children = [] 473 | def add_child(self, child): 474 | self.children.append(child) 475 | 476 | n = Node("root", None) 477 | ``` 478 | * Read about [Python metaclasses](https://jakevdp.github.io/blog/2012/12/01/a-primer-on-python-metaclasses/) 479 | * Inherit from `object` to use new-style classes 480 | * Note that Python supports multiple inheritance 481 | * Be cautious of method resolution order (`__mro__`) 482 | 483 | ### Non-Decimal Numbers 484 | * Binary numbers: preface with `0b`; use `bin(int)` to convert 485 | * Left and right shift: `<<` and `>>` 486 | * Bitwise AND, OR, XOR, NOT: `&`, `|`, `^`, `~` 487 | * [Bitmasks](https://en.wikipedia.org/wiki/Mask_(computing) 488 | 489 | ### File I/O 490 | * `f = open('filename', )`: open a file 491 | * `f.close()`: close file 492 | * `f.readline()`: read a line from the file 493 | * `for line in f`: iterate through lines in file 494 | * `f.write()`: write a string to the file 495 | 496 | ### Bitwise Operators 497 | * `x << y`: left shift by `y` bits 498 | * `x >> y`: right shift by `y` bits 499 | * `x & y`: bitwise AND 500 | * `x | y`: bitwise OR 501 | * `x ^ y`: bitwise XOR 502 | * `~x`: complement of `x` 503 | 504 | ### Magic Methods 505 | * `__init__(self,[...])`: initializer for a class 506 | * `__cmp__(self,other)`: return negative for `<`, 0 for `==`, positive for `>` 507 | * `__eq__(self,other)`: define behavior for `==` 508 | * Also `ne`, `lt`, `le`, `gt`, `ge` 509 | * `__str__(self)`: return string representation 510 | * `__repr__(self)`: return machine-readable representation 511 | * `__format__(self, formatstr)`: return new-style formatted string 512 | * `__hash__(self)`: return an integer such that `a==b` implies `hash(a)==hash(b)` 513 | * `__getitem__(self, key)`: defines what happens when you access `self[key]` 514 | * `__getattr__(self, key)`: defines what happens when you access `self.key` 515 | * `__contains__(self, item)`: defines behavior when using `in`/`not in` for membership checking 516 | 517 | ### Useful Packages 518 | * `copy` 519 | * `copy.copy(x)`: return shallow copy of `x` 520 | * `copy.deepcopy(x)`: return deep copy of `x` 521 | * `collections` (use `collections.deque`) 522 | * `dq.pop()`, `dq.popleft()`, `dq.appendleft(val)`, `dq.extendleft(lst)`, `dq.rotate(n)` 523 | * `heapq` 524 | * `heapq.push(heap,item)`: add an item 525 | * `heapq.pop(heap)`: pop an item 526 | * `heapq.heapify(l)`: make a list into a heap in linear time 527 | * `BeautifulSoup` 528 | * `scipy` 529 | * `numpy` 530 | * `scikit-learn` 531 | * `nltk` 532 | * `requests` 533 | * `unirest` 534 | * `networkx` 535 | * `pdb` 536 | * `pdb.set_trace()` sets a breakpoint at the current line and gives the user a CLI with which to inspect various objects and their values at runtime. Also allows you to continue code execution line by line. 537 | * `pprint`: Pretty Print 538 | * `pprint.pprint(iter)`: Print out a version of `iter` with JSON-like formatting. Useful for inspecting large, deeply nested objects. 539 | 540 | ### List Functionals 541 | * `zip(seq1 [,seq2 [...]])`: return list of tuples where each tuple contains the i-th element from each sequence. Truncated to length of shortest sequence (`[(seq1[0], seq2[0] ...), (...)]`) 542 | * `map(f, seq)`: return list of the results of `f` applied to each element of `seq` (`[f(seq[0]), f(seq[1]), ...]`) 543 | * `filter(f, seq)`: return list of items in `seq` for which `f(seq[i]) == True` 544 | * `reduce(f, seq)`: apply `f` to pairs of elements in `seq` until iterable is a single value 545 | 546 | ### Other 547 | * Infinity: `float("inf")` 548 | * Simultaneous assignment: `a,b = b,a` to swap 549 | * `lambda x: `: lambda function; don't need return statement (last value is return value) 550 | * Tuples are immutable lists; strings are also immutable 551 | * `zip()`: combine multiple lists into single list of tuples 552 | * Four numeric types: `int`, `long`, `float`, `complex` 553 | * **Logical operations**: `and`, `or`, `not` 554 | * `is` vs `==`: former for object identity, latter for object equality 555 | * **Falsey values**: 556 | * `None` 557 | * `False` 558 | * Zero of any numeric type 559 | * Empty sequences & mappings 560 | * When `__nonzero__()` returns `False` 561 | * When `__len__()` returns zero for a user-defined class 562 | * People like the word "Pythonic" 563 | 564 | ## Java Cheatsheet 565 | ### Program structure 566 | ```Java 567 | public class Program { 568 | // main 569 | public static void main(String[] args) { 570 | Hello h = new Hello("hi"); 571 | System.out.println(h); 572 | } 573 | } 574 | ``` 575 | ```Java 576 | public class Hello { 577 | private String text; // private instance variable 578 | // constructor 579 | public Hello(String helloText) { 580 | text = helloText; 581 | } 582 | public String toString() { 583 | return "Hello" + text; 584 | } 585 | } 586 | ``` 587 | ### Data Types 588 | * Primitive: `int`, `double`, `boolean`, `char`, `byte`, `short`, `long`, `float` 589 | * Also `Integer`, `Double`, `String` classes 590 | * Note that `char` literals have single quotes and `String` literals have double quotes 591 | * Arrays: use `[]` after type name (fixed length, `length` variable) 592 | * Interfaces -> concrete classes: 593 | * `List` -> `ArrayList`, `LinkedList` (variable length, `size()` method, can't store primitives) 594 | * `Set` -> `HashSet`, `LinkedHashSet`, `TreeSet` 595 | * `Map` -> `HashMap`, `LinkedHashMap`, `TreeMap` 596 | * `Collection` parent interface of `Set`, `List`, `Queue`, `Deque` 597 | * Others: `File`, `Math`, `Scanner`, `StringTokenizer` 598 | * `Object` class at the top of the hierarchy 599 | 600 | Inspired by http://introcs.cs.princeton.edu/java/11cheatsheet/ 601 | 602 | ## Programming Languages 603 | * Low level vs high level 604 | * Compiled vs interpreted 605 | * Imperative vs declarative (and functional) 606 | * Statically typed vs dynamically typed 607 | * See [Comparison of programming languages](https://en.wikipedia.org/wiki/Comparison_of_programming_languages) 608 | 609 | ### A Tiny Bit of C 610 | ```C 611 | #include 612 | int main() 613 | { 614 | printf("Hello, World!"); 615 | return 0; 616 | } 617 | ``` 618 | 619 | ### A Tiny Bit of C++ 620 | ```C++ 621 | #include 622 | using namespace std; 623 | 624 | class Hello { 625 | std::string name; 626 | public: 627 | Hello (std::string newName) { name = newName; } 628 | std::string hello () { return "Hello " + name; } 629 | }; 630 | 631 | int main () { 632 | Hello h ("world"); 633 | cout << h.hello(); 634 | return 0; 635 | } 636 | ``` 637 | 638 | ### A Tiny Bit of Ruby 639 | ```Ruby 640 | class HelloWorld 641 | def initialize(name) 642 | @name = name 643 | end 644 | def hello 645 | puts "Hello #{@name}!" 646 | end 647 | end 648 | 649 | h = HelloWorld.new("World") 650 | h.hello 651 | ``` 652 | 653 | ### A Tiny Bit of Go 654 | ```Go 655 | package main 656 | import "fmt" 657 | 658 | type hello struct { 659 | name string 660 | } 661 | func (h hello) hello() string { 662 | return "hello " + h.name 663 | } 664 | 665 | func main() { 666 | h := hello{name: "world"} 667 | fmt.Println(h.hello()) 668 | } 669 | ``` 670 | * Go has built-in support for parallel programming with goroutines and channels 671 | * [How Goroutines Work](http://blog.nindalf.com/how-goroutines-work/) 672 | 673 | 674 | ## Problem-solving Strategies 675 | General categories of problems 676 | * Straight-forward instruction following 677 | * String manipulation 678 | * Tree traversal 679 | * Graph search 680 | * Dynamic programming 681 | 682 | Approaching coding interview questions 683 | * Be thorough and verbalize your thought process (esp. if you're stuck!) 684 | * First, clarify the question and any assumptions you're making about input/output/behavior 685 | * Walk through potential solutions (if you can think of multiple with different runtime/space requirements, explain the tradeoffs and pick the one you'll implement) 686 | * Write out the function header & return value type 687 | * Implement the function body, explaining your code as you go & mentioning any invariants 688 | * When you're done, say so and walk through simple examples 689 | * Write out some test cases, esp edge cases 690 | * Talk about the runtime and space requirements of your solution 691 | 692 | See https://www.topcoder.com/community/data-science/data-science-tutorials/how-to-find-a-solution/ 693 | 694 | ## Final Thoughts 695 | Interviews can seem scary, but don't let them stress you out. 696 | Honestly, they can be really insightful experiences--I've learned so much on those occasions when interviewers take the time to have a conversation (about code or their work). 697 | Just be prepared and confident, and remember that good interviews try to assess your ability to learn and work with a team, not just your knowledge. 698 | And, if you get to the stage where you're looking at job offers (congrats!), check out [my other guide for things to consider when evaluating a role at a startup](https://github.com/kimberli/new-grad-offers)! 699 | -------------------------------------------------------------------------------- /intro_algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kimberli/interviews/dfa3db8fc11f0601bcf2cc7655e548bb279e173a/intro_algorithms.pdf -------------------------------------------------------------------------------- /more_algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kimberli/interviews/dfa3db8fc11f0601bcf2cc7655e548bb279e173a/more_algorithms.pdf --------------------------------------------------------------------------------