Algorithms Flashcards
time complexity
the number of elementary instructions a program executes
Big O
upper bound on time (asymptotic performance)
Big Ω
lower bound
Big θ
both O and Ω
3 ways to describe runtime of an algorithm
best case
worst case
expected case
space complexity
amount of memory or space required by an algorithm
data structures
Linked List Tree, Trie and Graph Stack and Queue Heap Vector/ArrayList HashTable
search and sort algorithms
breadth-first search depth-first search binary search merge sort quick sort
steps to handle a technical question
- Listen carefully
- Draw an example
- State a brute force
- Optimize
- Walk through
- Implement
- Test
Look for bottlenecks, unnecessary work and duplicated work.
Linked List
runner technique: two pointers, one runs ahead
many linked list problems rely on recursion
Stack
LIFO: most recent item added is the first to be removed. E.g. stack of dinner plates
Used in some recursive algorithms and to implement a recursive algorithm iteratively.
pop()
push(item)
peek()
isEmpty()
Queue
FIFO: first-in, first-out. E.g. ticket line
Often used in breadth-first search or caches.
add(item)
remove()
peek()
isEmpty()
Tree
data structure comprised of nodes. Has a root node with zero or more child nodes. Each child node has zero or more child nodes. Cannot contain cycles.
Binary Tree
a tree in which each node has up to two children
Binary Search Tree
a binary tree in which every node has a specific ordering property:
all left descendants <= n < all right descendants
Balanced Tree
a tree that is “not terribly imbalanced”. Left and right subtrees do not have to be exactly the same size.