Coding Interview Cheat Sheet
Coding Interview Cheat Sheet
com
P.O. Box 70686
Sunnyvale, Ca 94086
Indexing O(1)
Search O(n)
2- Linked Lists
Linked Lists are not arrays. They are not stored in a contiguous block of memory.
Each element (node) in a linked list references the next node, and sometimes the previous node
too if it is a doubly linked list.
Stacks and Queues are two data structures that are implemented with linked lists.
A Stack is a last in, first out (LIFO) data structure whereas a Queue is a first in, first out data
structure.
Operation Big-O Complexity
Indexing O(n)
Search O(n)
Insertion O(1)
3- Hash tables
These are data structures that map keys to values.
A Hash function is a mathematical function that, given an input key, returns an output that is
very likely to be unique to that key.
Hash collisions are when a hash function returns the same output for two different keys.
Insertion O(1)
4- Binary Trees
A generic binary tree is a data structure where each node in a binary tree has two children (a
left child, and a right child). All of the children are descendants of one tree node called the root
node.
Search O(log n)
Insertion O(log n)
Second: Important Algorithms
1- Breadth First Search
BFS(initial node) {
Set all nodes to not visited;
q = new Queue();
q.enqueue(initial node);
while ( q ≠ empty ) do
{
x = q.dequeue();
if ( x has not been visited )
{
visited[x] = true;
for ( every edge (x, y))
if ( y has not been visited )
q.enqueue(y);
}
}
}
- Search complexity is O(|E| + |V|) where E is the number of edges and V is the number of
vertices.
3- Merge Sort
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
Scenario Complexity
Best case O(n)
Average case O(n log n)
Worst case O(n log n)
4- Quick Sort
quickSort(arr[], low, high) {
if (low < high) {
pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
partition (arr[], low, high) {
pivot = arr[high];
i = (low - 1)
for (j = low; j <= high- 1; j++) {
if (arr[j] <= pivot) {
i++;
swap arr[i] and arr[j]
}
}
swap arr[i + 1] and arr[high])
return (i + 1)
}
Scenario Complexity
Best case O(n)
Average case O(n log n)
Worst case O(n^2)