Skip to content

Software interviews

Coding and complexity practice, with checked answers

A coding round asks you to find a method and say how fast it runs. This page explains how to reason about running time and gives eight original problems with exact answers that a program re-derives.

Your first hour with George Li (60 minutes by video, $250) ends with a written plan within one working day.

Book your first hour ($250)

What the round tests

A coding problem has two parts: a method that gives the right answer and an argument about how its running time grows with the input. Interviewers ask for both. A method that works only on small input is a start, and the next question is usually how to do better. Saying that out loud, before you are asked, shows how you think.

Reasoning about running time

Running time is counted in basic steps as a function of the input size n. You drop constants and lower terms and keep the growth. Checking every pair of items takes about n squared steps. A single pass takes about n. Halving the range at each step takes about log n. Sorting takes about n log n.

  • Nested loops multiply, unless the inner loop’s range depends on the outer variable, in which case you sum the inner counts. Problem 2 is an example in which a pair of loops is faster than it looks.
  • A loop that halves or doubles its variable runs about log n times, as in problem 3.
  • A recursion that splits the work in two and does linear work to combine, as in problem 6, runs in n log n.

Trade memory for time

Counting pairs that satisfy a condition looks like a quadratic job. Often a table of counts turns it into a single pass: sort items into groups by the value that matters, such as a remainder, and count combinations of groups. Problem 1 works this way. When you see “how many pairs”, ask what you can group by.

Dynamic programming in three questions

  1. What is the smallest piece of the answer? For a longest increasing subsequence, it is the best length that ends at one position.
  2. How does a bigger piece follow from smaller ones? Write the rule as an equation, as in the staircase problem.
  3. In what order do you fill the table so that every piece you need is already there?

The algorithm families, and how to tell which one a question wants

Most coding questions come from a short list of algorithm families. A candidate who can name the family from the wording of the problem has already done half the work, because each family has a standard state, a standard cost and a standard way to fail. Learn the list as a set of signals, not as a set of solutions to memorize.

Algorithm families, the signal in the question and where to practice
FamilySignal in the questionTypical costPractice page
Depth-first searchExplore every path, connected regions, tree propertiesVisits each node and edge onceGraphs, BFS and DFS, trees
Breadth-first searchFewest steps, level by levelVisits each node and edge onceGraphs, BFS and DFS
Binary searchSorted data, or a yes or no test that flips onceAbout log n testsBinary search
SortingOrder helps: duplicates, intervals, closest pairsAbout n log n for the sortSorting, intervals
Dynamic programmingBest, count or possible, over overlapping choicesStates times the work per stateDynamic programming
GreedyA local choice that never needs undoingOften one sort and one passGreedy algorithms
BacktrackingBuild every arrangement, prune the bad onesGrows with the number of arrangementsBacktracking
Divide and conquerSplit in two, solve each half, combineDepends on the combine stepSorting

Two families are easy to confuse because both ask for a best answer. The NIST Dictionary of Algorithms and Data Structures defines a greedy algorithm, read 2026-11-24, as one that always takes the best immediate, or local, solution, and says it finds the globally optimal answer for some optimization problems but may find a worse one for others. It defines dynamic programming, read the same day, as solving an optimization problem by caching subproblem solutions rather than recomputing them. In short, greedy never goes back, and dynamic programming keeps every smaller answer so it can compare choices. Greedy is simpler when it works, and a small counterexample shows when it does not.

A counterexample for greedy, with coins of 1, 3 and 4 (computed by code)
MethodFewest coins for 6How it chooses
Greedy3Takes the largest coin that fits: 4, then 1, then 1
Dynamic programming2Compares every coin at each amount and finds 3 and 3

Greedy works for other problems, and the test is whether you can show that the local choice is safe. For activity selection, take activities as [start, end] pairs: [1, 10], [2, 3], [3, 5], [5, 7], [7, 9]. Always taking the activity that finishes earliest fits 4 activities. Always taking the one that starts earliest fits 1, because the first one chosen runs from 1 to 10 and blocks the rest. The choice rule matters as much as the idea of being greedy.

In an interview, say the family, say why the signal points to it and say what would make you change your mind. If you are unsure between greedy and dynamic programming, try the small counterexample first and the table second.

Eight practice problems

Each problem has a short answer you can compute by hand, and the working names the method and its running time. Try to name the method first.

Problem 1. Given the array [3, 4, 10, 11, 14, 7, 0, 21, 5, 2, 9, 12], how many pairs of positions i < j have a[i] + a[j] divisible by 7? Give an integer.

Show the answer and the working

Answer: 14

  1. Reduce each value mod 7: 3, 4, 3, 4, 0, 0, 0, 0, 5, 2, 2, 5.
  2. Residue counts: 0 appears 4 times, 3 twice, 4 twice, 5 twice, 2 twice.
  3. Pairs whose residues add to 0 mod 7: C(4,2) = 6 from residue 0, 2 x 2 = 4 from 3 with 4, 2 x 2 = 4 from 5 with 2.
  4. The total is 14, found in O(n) with a count table instead of checking all n^2 pairs.

Problem 2. How many times does the statement count++ run in the following code with n = 100? Give an integer. for (i = 1; i <= n; i *= 2) { for (j = 0; j < i; j++) { count++; } }

Show the answer and the working

Answer: 127

  1. The outer loop runs with i = 1, 2, 4, 8, 16, 32, 64 (128 is above 100).
  2. The inner loop runs i times for each i, so the total is 1 + 2 + 4 + 8 + 16 + 32 + 64.
  3. That is 2^7 - 1 = 127. In general the work is below 2n, so the loop pair is O(n), not O(n log n).

Problem 3. A sorted array has 1000 distinct values. A binary search sets lo = 0 and hi = 999, repeats mid = floor((lo + hi) / 2) and compares a[mid] with the target (one probe per pass), and then moves lo to mid + 1 or hi to mid - 1 until it finds the target. For a target that is in the array, what is the largest number of probes any such target can need? Give an integer.

Show the answer and the working

Answer: 10

  1. Each probe at least halves the range, and a range of size r needs at most floor(log2(r)) + 1 probes.
  2. For r = 1000, floor(log2(1000)) = 9, since 2^9 = 512 and 2^10 = 1024.
  3. So the worst case is 10 probes.

Problem 4. What is the length of the longest strictly increasing subsequence (not necessarily contiguous) of [5, 2, 8, 6, 3, 6, 9, 7, 1, 10, 4, 11]? Give an integer.

Show the answer and the working

Answer: 6

  1. Let best(i) be the longest increasing subsequence ending at position i: 1, 1, 2, 2, 2, 3, 4, 4, 1, 5, 3, 6.
  2. The largest value is 6, reached at the final element 11.
  3. One such subsequence is 2, 3, 6, 9, 10, 11. The table takes O(n^2) time, and keeping the smallest tail for each length gives O(n log n).

Problem 5. A staircase has 15 steps. On each move you climb one of the step sizes 1, 3, 4, and two climbs are different if their sequences of step sizes differ. How many different climbs reach exactly step 15? Give an integer.

Show the answer and the working

Answer: 714

  1. Let w(n) be the number of ways to reach step n, with w(0) = 1 and w(n) = w(n-1) + w(n-3) + w(n-4), using only terms with a non-negative index.
  2. Values for n = 0 to 15: 1, 1, 1, 2, 4, 6, 9, 15, 25, 40, 64, 104, 169, 273, 441, 714.
  3. So w(15) = 441 + 169 + 104 = 714.

Problem 6. A divide and conquer routine on n items satisfies T(1) = 1 and T(n) = 2 T(n/2) + n for n a power of two. What is T(64)? Give an integer.

Show the answer and the working

Answer: 448

  1. Unrolling the recurrence k times gives 2^k T(n/2^k) + k n.
  2. With n = 64 = 2^6 and k = 6: 64 x T(1) + 6 x 64 = 64 + 384.
  3. T(64) = 448, which is n log2(n) + n and shows the O(n log n) growth.

Problem 7. What is the largest sum of a non-empty contiguous block of [4, -9, 3, -2, 5, -1, 6, -12, 7, 2, -3, 8]? Give an integer.

Show the answer and the working

Answer: 14

  1. Scan once keeping the best sum of a block that ends at the current position: it is max(current value, previous best + current value).
  2. Those running values are 4, -5, 3, 1, 6, 5, 11, -1, 7, 9, 6, 14.
  3. The largest running value is 14, from the block 7, 2, -3, 8. This takes O(n) time instead of checking all O(n^2) blocks.

Problem 8. Bubble sort repeatedly passes over an array, swapping each adjacent pair that is out of order (the left value is larger), until a pass makes no swap. How many swaps in total does it make on [4, 10, 1, 7, 3, 9, 2, 8, 6, 5]? Give an integer.

Show the answer and the working

Answer: 23

  1. Each adjacent swap removes exactly one inversion (a pair of positions in the wrong order), and a sorted array has none.
  2. So the number of swaps equals the number of inversions in the starting array.
  3. Counting for each value the smaller values after it: 3, 8, 0, 4, 1, 4, 0, 2, 1, 0, which sum to 23.

Habits worth building

  • Say your plan before you write code, and name the running time of the plan.
  • Test on a small example by hand, including an empty or one-item input.
  • After a working method, ask whether a table, a sort or a single pass would be faster.
  • Explain what you would change if the input were a hundred times larger.

Every answer on this page is recomputed by a separate program when the site is built, using exact fractions and by counting cases directly rather than by following the written steps. A wrong answer would stop the build. Read how the problems are checked.

These are our own practice problems, written for this site to train the reasoning that timed interviews reward. If you have signed an agreement about an assessment, keep its contents to yourself, including in a coaching session.

The software internship guide covers the calendar and the rounds around the coding problem, and the AI guide lists the machine learning topics to add. A coached session is a place to code aloud and have the reasoning questioned; you write all the code yourself.

Want help with your own applications? See internship interview coaching, a career coach for college internships, or resume review for internships.

Start with one hour

  • Your first purchase is refundable for unused time within 7 days. Nothing renews. Refund terms