If you're interviewing in Python, learn how to code the following data structures / algorithms in python.
lists: a = []
or a = list()
A -> B -> C
linked lists or A <-> B <-> C
doubly linked lists.
Optimizes insertion/deletion as O(1). Sucks at indexing/searching O(n).
- Indexing: Linked Lists: O(n)
- Search: Linked Lists: O(n)
- Insertion: Linked Lists: O(1)
- Indexing: Linear array: O(1)
- Search: Linear array: O(n)
- Optimized Search: Linear array: O(log n)
a = (1,2,3)
Nice property is it can be hashed for use in hashmaps/dictionaries.
Numpy arrays if allowed:
import numpy as np
a = np.array([1,2,3,4])
a = np.arange(4)
a = np.zeros(4)
...
- Indexing: Linear array: O(1)
- Search: Linear array: O(n)
- Optimized Search: Linear array: O(log n)
a = {}
or a = dict()
or a = dict(name='john', age=123)
or a = {'name':'john', age:123'}
You can have dictionary keys return a default value using a defaultdict
. Very useful for counting or building multiple lists of things.
import collections
a = collections.defaultdict(int)
for char in 'thisisatest':
a[char] += 1
- Indexing: Hash Tables: O(1)
- Search: Hash Tables: O(1)
- Insertion: Hash Tables: O(1)
If interviewer allows, a Counter does counting for you: collections.Counter('thisisatest').most_common(1)
-> [('s', 3)]
O(N) time and O(N) space worse case for N characters. Worth pointing out in an interview anyhow.
TODO
Unlike Binary tree, elements are ordered by some comparison (<=, >, alphabetic etc.)
- Indexing: Binary Search Tree: O(log n)
- Search: Binary Search Tree: O(log n)
- Insertion: Binary Search Tree: O(log n)
TODO
TODO
TODO
BFS guarantees optimal path, but will search all nodes in radius # steps. DFS can get to 'an' answer very quickly, but not necessarily optimal, for an infinite tree it may never end.
TODO
BFS/DFS: O(|V| + |E|)
- E is number of edges
- V is number of vertices
You don't have to memorize how to code it fully, but be able to explain how it functions, and do simplified versions of it.
For example, given an index, how to move all values less than value at index to the left inplace. Or given two sorted lists of ints, how to merge them.
-
Best Case Sort: Merge Sort: O(n)
-
Average Case Sort: Merge Sort: O(n log n)
-
Worst Case Sort: Merge Sort: O(n log n)
-
Best Case Sort: Quick Sort: O(n)
-
Average Case Sort: Quick Sort: O(n log n)
-
Worst Case Sort: Quick Sort: O(n^2)
You're almost definitely going to get an interview question where you'll write a recursive algorithm.
Understand stack level and how it's bad (every time you recurse, that information is being stored in the stack...)
You're definitely going to get an interview question where you'll write an iterative algorithm.
Know how to iterate through a string and do stuff like tell if a string is a palindrome or there is a substring with a palindrome for example.
Definitely know about two pointer iterative algorithms, like having a pointer on the start and on the end and iterating both to check if a string is a palindrome if we're allowed to change/add/remove one letter.
Recursion | Iteration
----------------------------------|----------------------------------
recursive_method(array, n) | iterative_method(array)
if array[n] is not nil | for n from 0 to size of array
print(array[n]) | print(array[n])
recursive_method(array, n+1) |
TODO