Five problems the way a quant interview deepens: an order book, a steepest fall, a probability, trades with a limit and an expected score. A 125-minute round.
After the warm-up, a quant interview moves to problems that look like the work itself. Examples are an order book that changes all day, a risk number such as the steepest fall, a probability and an expected value. These are the same data structures and dynamic programs you have learned, applied to markets.
The habits from the warm-up still apply, and three more become important.
This is the second and last round of the quant set. The set is small: there were three medium problems and two hard ones, so one round holds all five, and the hard ones are the stretch.
Exercise · Predict the output: Steepest fall with a tie. An account has the daily values 100, 60, 200, 120 and 150. The fall on a day is its drop from the highest earlier value, as a share of that value. The steepest fall is kept, and the earliest day wins a tie. What does this print: the peak, then the value?
values = [100, 60, 200, 120, 150]
best_drop = 0
best_peak = 1
answer_peak = 0
answer_value = 0
peak = values[0]
for v in values:
if v > peak:
peak = v
drop = peak - v
if drop * best_peak > best_drop * peak:
best_drop = drop
best_peak = peak
answer_peak = peak
answer_value = v
print(answer_peak, answer_value)Each problem has one Academy idea at its core. The quant frame adds the exactness, the modulus and the question about size.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| best bid and best ask while orders are cancelled | two heaps and a live set, with cancelled orders thrown away at the top | Heaps and priority queues |
| the steepest fall as a share of the earlier high | a running peak, with falls compared by multiplying out | Kadane's algorithm |
| the chance that a total ends above its start | count the up-move subsets by their sum, then divide by 2 to the power n | Knapsack: take it or leave it |
| at most k round trips, one copy held at a time | a buy state and a sell state for each trip count | 1-D DP: choices along a line |
| the expected score of a repeated game with a streak | a distribution over the streak length, advanced round by round | Dynamic programming from recursion |
The probability problem hides its trap in one comparison. Each day the rate moves up or down by a known size. It ends above the start when twice the sum of the up moves is more than the total of all moves. Equal means it ended at the start, which does not count.
Exercise · Find the bug: Ending exactly at the start. Each day moves up or down by moves[i]. This counts the choices of days that go up for which the rate ends strictly above the start. It overcounts. Which line is wrong?
def count_above(moves):
total = sum(moves)
ways = [0] * (total + 1)
ways[0] = 1
for move in moves:
for s in range(total, move - 1, -1):
ways[s] += ways[s - move]
above = 0
for s in range(total + 1):
if 2 * s >= total:
above += ways[s]
return aboveEnding exactly at the start is not above it: the test twice the up sum against the total must be strict.
The order book and the two dynamic programs have the same shape of trap as the earlier rounds. The book needs lazy deletion: a cancel only leaves the live set, and the heap entry is discarded when it reaches the top. The trip counter needs a state for each trip count, so that holding a copy and being free are different states.
Exercise · Predict the output: Two trips. The daily prices are 3, 2, 6, 5, 0 and 3. A trader holds one copy at a time and may make at most 2 round trips. What does this print: the best total profit?
prices = [3, 2, 6, 5, 0, 3]
k = 2
NEG = -10 ** 9
buy = [NEG] * (k + 1)
sell = [0] * (k + 1)
for price in prices:
for j in range(1, k + 1):
buy[j] = max(buy[j], sell[j - 1] - price)
sell[j] = max(sell[j], buy[j] + price)
print(sell[k])A buy state and a sell state for each trip count give the best total of two trips in one pass.
The ladder rises from three medium problems to two hard ones. In one sitting you would meet two or three of them, so treat the five as the whole set.
Solve them in this order. The order book adds heaps, the fall adds exact fractions, the probability adds counting by sums, and the last two add states for trips and for streaks.
Quant interviewers push on size and on the next question, so prepare one for each problem.
The whole set is one long session: 125 minutes in all. Take it in two sittings with a break, and talk through each problem as if an interviewer were there.
The time boxes on the ladder are 20, 20, 25, 30 and 30 minutes. If the hard problems overrun, finish them and note which part of the state took longest to define.
Checkpoint · Quant core and stretch round
Q1/5 A probability is the fraction a over b, and the statement asks for it modulo a prime. What do you return?
Answer: B
Whole-number division loses the fraction. The modular inverse of b stands in for division in arithmetic modulo a prime.
Q2/5 A cancelled order is still inside a heap. What happens to it in the lazy deletion method?
Answer: B
A heap cannot remove a middle item cheaply. Checking the live set at the top keeps every operation fast.
Q3/5 A fall of 40 from a peak of 100, and a fall of 80 from a peak of 200. Which test shows they are equal without dividing?
Answer: B
Both products are 8,000, so the two fractions are equal. A strictly greater product is needed to replace the best, so a tie keeps the earlier day.
Q4/5 Two days each move up or down by 1. In how many of the four choices does the rate end strictly above the start?
Answer: B
Only both days up ends above the start. One up and one down, in either order, ends exactly at the start, and both down ends below it.
Q5/5 The buy and sell tables hold one entry for each trip count j. What does the sell entry for j mean?
Answer: B
The buy entry is the best balance while holding a copy. The sell entry is the best profit when nothing is held, and the answer is the sell entry for k.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.