Data structures and algorithms: 50 Big-O flashcards
Big-O for arrays, hash maps, heaps, trees and graphs, sorting trade-offs, and the patterns behind interview problems: two pointers, sliding windows, BFS, Dijkstra and DP.
Practice now
Study without an account
Scheduled with the default Askesis timings. Progress is saved in this browser.
All 50 cards
- 1Access an array element by index
- 2Append to a dynamic array (Python list, JS array, Vec)AmortizedAn occasional resize copies everything, but doubling the capacity spreads that cost out.
- 3Insert at the front of an array, because every element shifts
- 4Search an unsorted array
- 5Access the k-th node of a linked list, you have to walk from the head
- 6Insert or delete at the head of a linked list
- 7Hash map lookup, insert and deleteon average, in the worst case (many collisions)
- 8Two ways hash tables resolve collisionsChaining (a list per bucket) and open addressing (probe for another slot)
- 9Binary heap: push, pop and peekPush and pop , peek at the min/max
- 10Build a heap from n unsorted itemswith bottom-up heapifyPushing items one by one would take .
- 11Balanced BST search, insert, deleteAn unbalanced BST can degrade to , like a linked list.
- 12What order does an in-order traversal of a BST visit keys in?Sorted (ascending) order
- 13Trie lookup for a word of length L, independent of how many words are stored
- 14Space of an adjacency list vs an adjacency matrixList ; matrix
- 15Time of BFS or DFS on a graph (adjacency list)
- 16Shortest path in an unweighted graphBreadth-first search from the source
- 17Merge sort: time, space, stable?in every case, extra space, stable
- 18Quicksort: average and worst caseAverage , worst (bad pivots); not stable
- 19Heapsort: time, space, stable?, extra space, not stable
- 20Insertion sort: best and worst caseBest on nearly sorted input, worst ; stable
- 21Lower bound for comparison-based sortingcomparisons in the worst case
- 22Counting sort complexityfor keys in a range of size ; not comparison-based
- 23What does “stable sort” mean?Elements that compare equal keep their original relative order
- 24Which algorithm do Python’s
sortedand Java’s object sort use?Timsort: stable, worst case, on already-sorted runs - 25Binary search time and precondition, and the array must be sorted
- 26Binary search midpoint that can’t overflow
mid = lo + (hi - lo) // 2In fixed-width integer languages,(lo + hi) / 2can overflow. - 27Two pointers: find a pair with a given sum in a sorted arrayStart at both ends; move left up if the sum is too small, right down if too big. time, space
- 28Sliding window: longest substring without repeating charactersGrow the right edge, shrink the left while a character repeats, track positions in a map.
- 29Detect a cycle in a linked list in O(1) spaceFloyd’s tortoise and hare: a slow and a fast pointer meet if there’s a cycle. time
- 30Dijkstra’s algorithm with a binary heap; requires non-negative edge weights
- 31Shortest paths with negative edge weightsBellman–Ford, ; it also detects negative cycles
- 32Topological sort: when does it exist, and how fast is it?Only for a DAG (no cycles). Kahn’s algorithm or DFS runs in
- 33Union-find with path compression and union by rankNearly constant amortized time per operation:
- 34Kruskal’s minimum spanning treeSort edges by weight and add them unless they form a cycle (checked with union-find).
- 35Memoization vs tabulationMemoization is top-down recursion with a cache; tabulation fills a table bottom-up
- 36Naive recursive Fibonacci vs memoizedNaive ; memoized or iterative
- 37Longest common subsequence of strings of lengths m and ntime with a DP table
- 380/1 knapsack with n items and capacity W: pseudo-polynomial, since is a value, not an input size
- 39Edit (Levenshtein) distance between strings of lengths m and ndynamic programming
- 40Space cost of recursionOne stack frame per level: , e.g. for naive recursion over a list
- 41What is amortized analysis?The average cost per operation over a worst-case sequence of operations, even if single operations are sometimes expensive
- 42Big-O vs Big-Θ vs Big-ΩO is an upper bound, Ω a lower bound, Θ a tight bound (both)
- 43Solve, as in merge sort (master theorem)
- 44Find the k largest of n itemsKeep a min-heap of size :Quickselect finds the k-th largest in on average.
- 45Design an LRU cache with O(1) get and putA hash map from key to node plus a doubly linked list ordered by recency
- 46Prefix sumsPrecompute running totals in , then answer any range sum inpython
prefix = [0] for x in nums: prefix.append(prefix[-1] + x) range_sum = prefix[r + 1] - prefix[l] - 47Next greater element for every itemA monotonic stack: , each item is pushed and popped once
- 48Time to generate all permutations of n items
- 49Deque operations at both endspush and pop at either end
- 50Height of a balanced binary tree with n nodes
Frequently asked questions
Is this enough for coding interviews?
It covers the complexities and patterns interviews test most. Pair it with solving problems so you can apply them.
Which language are the examples in?
The few snippets use Python; the ideas are language-independent.
Why do some answers say amortized?
Operations like appending to a dynamic array are usually O(1) but occasionally expensive; amortized cost averages that out over a sequence.
More in Computer science
Computer science
CS fundamentals: binary and hex, two’s complement, Big-O, stack vs heap, threads and deadlocks, CPU caches, compilers, UTF-8 and floating point.
50 cardsMorse code
Learn International Morse code: all 26 letters, the 10 digits, common punctuation and signals like SOS, each with its rhythm and a memory aid.
50 cardsDesign patterns
SOLID, DRY, KISS and YAGNI, plus the Gang of Four patterns used most: singleton, factory, builder, adapter, decorator, observer, strategy, command and more.
50 cardsSoftware testing
Software testing fundamentals: the test pyramid, unit, integration and E2E tests, TDD, mocks vs stubs vs fakes, coverage, mutation and property-based testing.
50 cardsAPI design
API design fundamentals: REST, HTTP methods, status codes, pagination, versioning, OAuth 2.0 and JWT, CORS, OpenAPI, GraphQL, gRPC and webhooks.
50 cardsSystem design
System design fundamentals: load balancers, caching, CDNs, replication and sharding, CAP, consistency, queues, rate limiting and back-of-the-envelope math.
50 cardsCryptography
Cryptography flashcards: AES modes, RSA, elliptic curves, Diffie–Hellman, hashing, HMAC, signatures, password hashing, TLS 1.3 and common pitfalls.
50 cards