Programming
1. Python, C & pseudocode
- Trace by hand: keep a table of the variables after each line or loop pass; for recursion, draw the call tree.
- Lists:
append(x)adds one element (even a list),extend(L)adds each element ofL,insert(i, x)needs an index; slicingL[a:b]excludesb. - Mutability: a default argument like
lst=[]is created once and shared by every call that omits it; a closure keeps its own copy of the variables of the call that made it. - Sets:
A - B,A | B,A & B; tuple assignmentA, B, C = ...evaluates the whole right side first. - Division: in Python 3,
/gives a float and//an integer;%is the remainder. - Swapping:
D[i], D[j] = D[j], D[i]; swapping the ends and moving inwards reverses a list.
2. Data structures
- Stack (LIFO): push, pop. Queue (FIFO): enqueue, dequeue. Deque: insert and remove at both ends. All .
- Hash tables: average lookup. With open addressing and load factor , an unsuccessful search or an insertion takes at most probes on average; with linear probing a collision moves to the next free slot.
- Binary trees: (leaves, internal nodes); a tree of height has between and nodes.
- Traversals: preorder (root, left, right), inorder (left, root, right), postorder (left, right, root). Inorder with preorder or postorder determines the tree; for a full binary tree preorder with postorder does too.
- Heaps: a complete binary tree with parent children; insert and extract-min in .
- B trees: a node with pointers holds keys; an overflowing node splits and sends its middle key up.
3. Searching, sorting & complexity
- Big-O: .
- Binary search needs a sorted array (constant-time access): , ; at most comparisons.
- Bubble sort: passes of adjacent swaps; the number of swaps equals the number of inversions. Insertion sort: one swap per inversion, on nearly sorted input. Selection sort: puts the minimum in place each pass, at most swaps.
- Merge sort always. Quicksort: expected, worst (e.g. sorted input with the first or last element as pivot); expected time .
- Lower bound: comparison sorting needs comparisons.
4. Graph algorithms
- Representations: adjacency lists ( space) or an adjacency matrix ().
- BFS finds shortest paths in unweighted graphs; DFS explores as deep as possible first. Both run in and reach exactly the vertices reachable from the start.
- DFS edge types: tree, back, forward, cross; an undirected DFS has only tree and back edges, and a directed graph has a cycle iff DFS finds a back edge.
- Topological sort of a DAG: an order where every edge goes forward (Kahn's algorithm or reverse DFS finishing order).
- Shortest paths: Dijkstra for non-negative weights; a shortest path's subpaths are shortest, so an extra edge that would shorten it cannot exist.
- Reversed graph: is reachable from in iff is reachable from in .
5. Recursion, divide & conquer, greedy & DP
- Recursion: a base case and a smaller call. The number of calls of satisfies .
- Master theorem: is , or as .
- Divide and conquer: split, solve the parts, combine (merge sort, binary search, fast exponentiation).
- Greedy: take the best local choice; correct only with an exchange argument.
- Dynamic programming: store answers to overlapping subproblems (memoisation or a table); e.g. Fibonacci in , edit distance, knapsack.
6. Databases & SQL
- Keys: a superkey determines every attribute; a candidate key is a minimal superkey; a primary key is never NULL; a foreign key must match an existing key (or be NULL).
- Functional dependencies: compute the closure to test keys and derived dependencies. BCNF: every non-trivial has a superkey. A decomposition that loses dependencies needs joins to check them.
- Relational algebra: (select rows), (project columns), (natural join on equal common attributes), , and division (the paired with every in ).
- SQL:
SELECT ... FROM ... WHERE ... GROUP BY ... HAVING; a subquery can be compared with each row (correlated).GROUP BY CUBE(A, B)groups by every subset of . - Indexes: hash indexes serve equality; B trees serve equality and ranges.
- ER to relations: one relation per entity, one per many-to-many relationship, one per multi-valued attribute.
- OLAP: roll-up goes to coarser levels, drill-down to finer ones; with hierarchies of levels there are cuboids.