What we discussed
Data Structures
- Lists
- ArrayList - list made using arrays, Advantage is random access is fast,
disadvantage is when scalling up access becomes slow
- LinkedList - list made using nodes, Advantage is scalling up is easy, disadvantage
is random access is slow
- Queues - FIFO First In First Out
- Stacks - LIFO Last In First Out
- Heaps/ Priority Queue - Special kind of tree, where root is always the max or min element
- Max Heap - Root node is always max
- Min Heap - Priority Queue - Root node is always min
- Trees
- Binary Tree
- Binary Search Tree - A special kind of tree where left node is always
smaller than root and right node is always greater than root
- Self Balancing trees
- AVL Tree - A special kind of tree where height of left and right subtree of
every node differs by atmost 1
- Red Black Tree - A special kind of tree where height of left and right
subtree of every node differs by atmost 1 and root is always black
- N-Arry Tree - A tree where each node can have atmost n children
6. Maps - Key Value Pair
- HashMap - A map where keys are stored in a hash table
- Tree Map - A map where keys are stored in a tree, so keys are always sorted, so we
can do range queries. Keys have to be comparable though
- Graphs - A set of nodes connected by edges
- Cyclic - A graph where there is a cycle
- Acyclic - A graph where there is no cycle
- Trie -- for dealing with string search - A special kind of tree where each node has k
children if our search space is k characters, basically one child for each character
- Segment Tree - A special kind of tree where each node stores the result of a range of
values
- Fenwik Trees / Binary Indexed Trees - A special kind of tree where each node stores the
result of a range of values, but it is more space efficient than segment tree because it uses an array
instead of a tree and we store the result of a range of values in a node only if that node is a power of 2
- Suffix Tree - A special kind of tree where each node stores a suffix of a string
- Suffix Array - A special kind of array where each element stores a suffix of a string
Algorithms
- Searching
- Linear Search- O(n) - search through each element one by one
- Binary Search - O(log(n)) - search by dividing the search space into half each time and
searching in the half where the element can be, only works on sorted arrays
- Sorting
- Bubble Sorts - O(n^2) - keep swapping adjacent elements if they are not in order
- Insertion Sort - O(n^2) - keep inserting elements in the right place.
basically pick an element and insert it into right spot
- Selection Sort - O(n^2) - keep selecting the smallest element and putting it in the
right place. Select a spot and pick right element
- Bucket Sort - O(n) - only works on integers, create buckets and put elements in the
right bucket, then sort each bucket
- counting Sort - O(n) - only works on integers, create buckets and put elements in
the right bucket, then empty the buckets
- Radix Sort - O(n*k) where k is number of digits - only works on integers, sort by
each digit from least significant to most significant using bucket sort
- Quick Sort - O(n^2) | avg case - O(nlog(n)) - pick a pivot and put all elements
smaller than pivot to the left and all elements greater than pivot to the right, then sort the left
and right subarrays
- Merge Sort - O(nlog(n)) - divide the array into two halves, sort each half and then
merge the two sorted halves
- heap sort - O(nlog(n)) - create a max heap, then keep removing the root and putting
it at the end of the array
- Graph
- BFS - O(V+E) - Breadth First Search - visit all nodes at a level before going to
the next level, where V is number of vertices and E is number of edges
- DFS - O(V+E) - Depth First Search - visit all nodes in a path before going to the
next path, where V is number of vertices and E is number of edges
- Topological Sort - O(V+E) - Visit nodes which have no incoming edges first, where V
is number of vertices and E is number of edges
- FloodFill - O(V+E) - Visit all nodes connected to a node, where V is number of
vertices and E is number of edges
- Shortest path algos
- **Dijkstra's **- O(V+E) - Find shortest path from a source to all other nodes, where V
is number of vertices and E is number of edges
- **Bellman Ford **- O(VE) - Find shortest path from a source to all other nodes, where V is
number of vertices and E is number of edges
- Union Find / Disjoint set union - O(log(n)) - Find if two nodes are connected or
not, where n is number of nodes
7.** Min Spanning Tree **- O(Elog(E)) - Find a tree which connects all nodes with minimum cost,
where E is number of edges
- **Prims/ Kruskal's **- O(Elog(E)) - Find a tree which connects all nodes with minimum
cost, where E is number of edges
For Tree Map
class Node:
Key
Value
Node left, right
For Trie
class TrieNode:
TrieNode[] children = new TrieNode[num-of-chars];
boolean isWordEnd;