Algorithms and Big-O for coding interviews – Server Side interview prep

261 interview-style multiple-choice questions on Algorithms and Big-O for coding interviews, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 55 key terms with the flashcards first, then test yourself.

Take the practice test · Flashcards · All study tools

What it covers

  1. 18.1 Big-O and complexity analysis
  2. 18.2 Searching and binary search
  3. 18.3 Sorting algorithms
  4. 18.4 Recursion and divide and conquer
  5. 18.5 Dynamic programming
  6. 18.6 Greedy algorithms and intervals
  7. 18.7 Backtracking and combinatorial search
  8. 18.8 Two pointers, sliding windows and prefix sums
  9. 18.9 Interview problem-solving and common patterns

Key terms

18.1 Big-O and complexity analysis

Big-O
Upper bound on growth rate
O(log n)
Logarithmic growth, as in halving or doubling
O(n log n)
Typical of efficient comparison sorts
Amortized cost
Average cost over many operations
Big-Θ
A tight bound, above and below
Auxiliary space
Extra memory beyond the input
Recurrence
A cost defined in terms of smaller inputs

18.2 Searching and binary search

Lower bound
First index ≥ target
Upper bound
First index > target
Binary search on the answer
Bisecting a monotonic feasibility check
Monotonic predicate
False, then true from some point
Exponential search
Doubling to find a range, then bisecting

18.3 Sorting algorithms

Binary search
Halving search on sorted data
Merge sort
Stable divide-and-conquer sort
Quicksort
Partition-based sort with O(n log n) average
Stable sort
Keeps the order of equal elements
Timsort
Hybrid merge and insertion sort
Introsort
Quicksort falling back to heapsort
Radix sort
Sorts digit by digit
External sort
Sorting data bigger than memory

18.4 Recursion and divide and conquer

Base case
Condition that stops recursion
Dynamic programming
Reusing overlapping subproblem results
Memoization
Caching results of function calls
Greedy algorithm
Choosing the locally best option each step
Divide and conquer
Split, solve, combine
Master theorem
Solves T(n) = aT(n/b) + f(n)
Tail call
A call that's the function's last action
Fast exponentiation
Squaring to compute powers

18.5 Dynamic programming

Optimal substructure
Best solution built from best sub-solutions
Overlapping subproblems
The same subproblems recur
Tabulation
Bottom-up DP table filling
Knapsack
Choosing items under a weight limit
Edit distance
Minimum insert, delete or substitute steps
Kadane's algorithm
Maximum subarray in O(n)

18.6 Greedy algorithms and intervals

Exchange argument
Shows swapping in the greedy choice is safe
Interval scheduling
Choosing non-overlapping intervals
Sweep line
Processing sorted events in order
Earliest deadline first
Orders jobs by deadline

18.7 Backtracking and combinatorial search

Backtracking
Build candidates step by step, undoing dead ends
Pruning
Skipping branches that can't succeed
Branch and bound
Prunes using a bound on the best value
Constraint satisfaction
Assigning values that satisfy constraints
Meet in the middle
Solve halves separately and combine

18.8 Two pointers, sliding windows and prefix sums

Slow and fast pointers
One index writes, one reads
Difference array
Range updates in O(1)
Dutch national flag
Three-way partition in one pass

18.9 Interview problem-solving and common patterns

Two pointers
Moving two indices through data to avoid nested loops
Prefix sum
Running totals for fast range sums
Brute force
Trying all possibilities directly
Edge case
Unusual input at the boundaries of a problem
Constraint hint
Input size suggests the complexity needed
Boyer–Moore vote
Majority element in O(1) space
Fisher–Yates
Uniform in-place shuffle
Online algorithm
Processes input as it arrives
Coordinate compression
Maps values to small indexes

Sample practice questions

  1. What does Big-O notation describe?

    1. An upper bound on how time or memory grows with input size
    2. The number of lines of code an algorithm needs
    3. The amount of memory the computer has installed
    4. The exact number of seconds a program takes to finish

    Answer: A. An upper bound on how time or memory grows with input size O(2n) and O(n) are the same class.

  2. What does binary search require, and what is its complexity?

    1. A linked list as input; O(log n)
    2. A sorted collection; O(log n)
    3. A hash table as input; O(n)
    4. Any collection at all; O(1)

    Answer: B. A sorted collection; O(log n) Binary search on a linked list loses its advantage because there's no O(1) indexing.

  3. What is the lower bound for comparison-based sorting in the worst case?

    1. Ω(n log n)
    2. Ω(n²)
    3. Ω(n)
    4. Ω(log n)

    Answer: A. Ω(n log n) Counting and radix sort beat it by not comparing elements.

  4. What does every correct recursive function need?

    1. A loop inside the function body, around the call
    2. At least two parameters, one being a counter
    3. A base case, and a step that moves toward it
    4. A global variable to track the recursion depth

    Answer: C. A base case, and a step that moves toward it Without a base case it overflows the stack.

  5. Why is naive recursive Fibonacci slow? ```python def fib(n): return n if n < 2 else fib(n - 1) + fib(n - 2) ```

    1. Adding very large numbers together is extremely slow
    2. It secretly runs a nested loop on every recursive call
    3. It recomputes the same subproblems, taking exponential time
    4. Most languages can't handle recursion more than a few levels deep

    Answer: C. It recomputes the same subproblems, taking exponential time Memoization or a bottom-up loop makes it O(n).

  6. What is a greedy algorithm?

    1. One that always sorts its input before doing anything
    2. One taking the locally best choice each step, hoping it's globally best
    3. One that uses as much memory as possible to save time
    4. One that tries every possible combination exhaustively

    Answer: B. One taking the locally best choice each step, hoping it's globally best Greedy coin change works for US coins but not for every coin system.

  7. What is backtracking?

    1. Building candidates stepwise and abandoning dead-end paths early
    2. Running an algorithm in reverse from its output to its input
    3. Sorting an array starting from its last element instead
    4. Undoing a series of commits in Git to restore older code

    Answer: A. Building candidates stepwise and abandoning dead-end paths early Used for permutations, N-Queens and Sudoku solvers.

  8. Which technique finds a pair summing to a target in a sorted array in O(n) time and O(1) space?

    1. Two pointers from each end, moving inward by the sum
    2. Sorting the array again in descending order first
    3. Nested loops that check every possible pair of values
    4. A recursive search through every possible subset

    Answer: A. Two pointers from each end, moving inward by the sum Move the left pointer up if the sum is too small, the right one down if too big.

  9. You need the k most frequent words in a large list. Which approach is efficient?

    1. Compare every word against every other word
    2. Count in a hash map, then keep a size-k heap
    3. Sort all the words alphabetically, then scan
    4. Binary search the unsorted list for each word

    Answer: B. Count in a hash map, then keep a size-k heap Counting is O(n); the heap step is O(n log k).

  10. What is the time complexity of this code? ```python for i in range(n): for j in range(n): total += i * j ```

    1. O(log n)
    2. O(n²)
    3. O(n)
    4. O(2n)

    Answer: B. O(n²) Two nested loops of n iterations each give n × n steps.

Practice all 261 questions

More Server Side