Prepper

Quant coding interview questions in Python

Python coding questions from quant trading and research interviews: simulation, Sharpe ratios, moving averages, order books and option pricing, with solutions. 7 min readUpdated By the Prepper team

Coding rounds for quant roles are different from software engineering interviews. The problems are shorter and closer to the job: simulate something, clean a price series, compute a statistic, price a simple product. Python is accepted almost everywhere.

1. Simulate a probability

Estimate the probability that the sum of three dice is at least 15.

```python import random

def p_sum_at_least(target, dice=3, trials=200_000): hits = sum(sum(random.randint(1, 6) for _ in range(dice)) >= target for _ in range(trials)) return hits / trials ```

The exact answer is 20/216 = 0.0926. Interviewers like to see you check a simulation against a number you can compute.

2. Returns, volatility and Sharpe

```python import math

def sharpe(prices, periods=252): rets = [b / a - 1 for a, b in zip(prices, prices[1:])] mean = sum(rets) / len(rets) var = sum((r - mean) * 2 for r in rets) / (len(rets) - 1) return mean / math.sqrt(var) math.sqrt(periods) ```

Points to mention: n − 1 in the variance, and the √252 to annualise.

3. Moving average without recomputing

python def moving_average(xs, window): out, total = [], 0.0 for i, x in enumerate(xs): total += x if i >= window: total -= xs[i - window] if i >= window - 1: out.append(total / window) return out

This runs in linear time. Summing each window again would be slower, and noticing that is the point of the question.

4. Maximum drawdown

python def max_drawdown(prices): peak, worst = prices[0], 0.0 for p in prices: peak = max(peak, p) worst = max(worst, (peak - p) / peak) return worst

5. Best single trade

Given daily prices, find the largest profit from one buy followed by one sell.

python def best_trade(prices): low, best = prices[0], 0 for p in prices[1:]: best = max(best, p - low) low = min(low, p) return best

6. Match an order against a book

python def fill_buy(asks, qty): """asks: list of (price, size) sorted by price. Returns (filled, average price).""" cost = filled = 0 for price, size in asks: take = min(size, qty - filled) cost += take * price filled += take if filled == qty: break return filled, (cost / filled if filled else None)

7. Price an option on a binomial tree

python def binomial_call(s, k, up, down, steps, p): values = [max(s * up ** j * down ** (steps - j) - k, 0) for j in range(steps + 1)] for _ in range(steps): values = [p * values[j + 1] + (1 - p) * values[j] for j in range(len(values) - 1)] return values[0]

With zero interest rates the risk-neutral probability is p = (1 − down) / (up − down).

8. Expected value by recursion

You may roll a die up to three times and keep the last roll. What is the game worth?

python def roll_value(rolls): value = 3.5 for _ in range(rolls - 1): value = sum(max(face, value) for face in range(1, 7)) / 6 return value

This returns 4.25 for two rolls and 4.67 for three.

How to prepare

  • Write code by hand or in a plain editor. Many firms do not allow autocomplete.
  • Say the complexity of your solution without being asked.
  • Test with a tiny example before you say you are done.
  • Know the basics of pandas if the role is research: grouping, rolling windows, merging on dates, and avoiding look-ahead when shifting.

Frequently asked questions

Which language should I use in a quant coding interview?

Python is accepted almost everywhere for trading and research roles. Quant developer roles at low-latency firms often expect C++.

Are quant coding interviews like software engineering interviews?

They are shorter and closer to the work: simulation, statistics on price series and simple pricing. Developer roles do include standard data structure questions.

Keep reading