Quant from Scratch by QuantWright

Introduction to Quant Finance · Lesson 25

Getting in, and what to learn next

Three classic puzzles of the kind quant interviews use, each worked out by hand and checked by simulation, then a map of what to study next.

Know the basics? Read the lesson notes

What this lesson is, and what it isn't

A quant (short for quantitative analyst) uses maths, statistics and code to answer questions about markets and money. The lesson What quants do maps the jobs. This last lesson works three puzzles of the kind their interviews use, then shows what to study next.

Please read this firstThis is a free course, not a qualification. It teaches ideas that quant interviews and jobs use, but it is not accredited (officially recognised as a qualification), it is not a substitute for a degree, and it won't get anyone a job by itself. Firms hire on their own criteria, which can include degrees, interviews and experience, and those criteria differ between firms and change. Neither this course nor QuantWright has any connection with any employer.

Interview guides often cover a few kinds of question. The mix differs by firm and by job. Turn the five over: this course has already used each one.

  1. Probability and averages

    How likely something is, and what it gives on average over many tries.

Problem 1: two heads in a row

A coin is fair when heads and tails each come up half the time, whatever came before. Flip one until you get two heads, one straight after the other. Then start again, and again, many times.

On average, how many flips does a round take? (The average: add up every round's flips and divide by the number of rounds.)

Keep your guess in mind. On the next screen you flip, then work it out, then check it.

Two heads: flip, reason, check

Flip until two heads in a row, three rounds. Your flips are the first three rounds of a seeded simulation: random draws by a computer from a fixed starting number, the seed, so every reader gets the same ones.

Hheads
Ttails

Round 1 of 3  ·  flips 0

    Your rounds took 7, 10 and 3 flips: 6.67 on average. Three rounds are far too few to settle an average (the Sample size lesson). So reason it out.

    Only two situations matter. Call E the average number of flips still needed at the start; a tail puts you back there, since it wipes out any head before it. Call EH the average still needed just after a head.

    At the start you make one flip. Half the time it is a head and you need EH more; half the time a tail and you need E more: E = 1 + ½EH + ½E. Just after a head you make one flip. A head finishes the round; a tail sends you back: EH = 1 + ½ × 0 + ½E.

    Put the second into the first: E = 1 + ½ × (1 + ½E) + ½E = 1.5 + ¾E. So ¼E = 1.5, and E = 6. Now check it.

    Flips needed in each of 10,000 rounds (simulated)

    Bars: how many rounds took 2, 3, 4… flips; the last bar holds 25 or more. Line: the rounds' average.

    Rounds10,000
    Average flips…
    Done in 2 flips…

    The 10,000 rounds averaged 6.00 flips, as worked out: the answer to the guess is C, 6. About a quarter of rounds ended at 2 flips (2,499), since two heads straight away has chance ½ × ½. Others ran long, one to 42 flips, and those pull the average above the most common count.

    Theory

    Average waiting time, one try at a time

    The method you just used is first-step analysis. Name the situations that decide what can happen next, the states. For each, the average waiting time is one try, plus each outcome's chance times the average waiting time from where it lands you. Then solve the equations. The answer is the expected waiting time: the average number of tries over very many rounds.

    Es = 1 + Σj ps→j × Ej, with E = 0 at the goal
    one event, chance p each try: E = 1 + (1 − p) × E, so E = 1 ÷ p
    s, j
    states: the situations that decide what can happen next
    Es
    the expected waiting time from state s: the average number of tries still needed
    ps→j
    the chance that one try takes you from state s to state j
    Σj
    add up over every state j
    p
    the chance of the event on each try
    Worked example

    Two heads: E = 1 + ½EH + ½E and EH = 1 + ½E, so E = 6; the simulation's 10,000 rounds averaged 6.00. One event: a die rolled until the first six has p = 1/6, so E = 1 ÷ (1/6) = 6 rolls.

    Holds when
    • each try's chances are fixed in advance;
    • the past matters only through the state you are in (a fair coin's earlier flips don't change the next one);
    • the goal is sure to be reached in the end.
    Breaks when

    Chances drift from try to try, or depend on more of the past than the states hold. Then the equations give a wrong answer unless more states are added. Real trades can drift like this: the Quant Research course, coming, is planned to cover this.

    New pattern. Flip a fair coin until a head is followed straight away by a tail. On average, how many flips?

    At the start: E = 1 + ½EH + ½E. Just after a head, a tail finishes and a head leaves you just after a head again: EH = 1 + ½EH + ½ × 0, so EH = 2. Then E = 1 + 1 + ½E, so E = 4. The seeded simulation's 10,000 rounds averaged 4.00.
    At the start: E = 1 + ½EH + ½E. Just after a head, a tail finishes and a head leaves you just after a head again: EH = 1 + ½EH + ½ × 0, so EH = 2. Then E = 1 + 1 + ½E, so E = 4. If you got 6, you sent a second head back to the start: here it keeps you one head in. The seeded simulation's 10,000 rounds averaged 4.00.

    Problem 2: a shared birthday

    A group of 30 people. Take every birthday to be one of 365 days, each day equally likely, with no twins in the group. What is the chance that at least two of the 30 share a birthday?

    This classic is the birthday problem. A simulation can check it directly, the way the lesson Monte Carlo answers a question: simulate it many times and count.

    Keep your guess in mind. The next screen works it out.

    Birthdays: count the pairs, then check

    About 8% is 30 ÷ 365, roughly the chance that someone shares your birthday. The question asks about any two people, and 30 people make 30 × 29 ÷ 2 = 435 pairs. Each pair is a chance of a match.

    For the exact answer, add people one at a time and work out the chance of no match. The second person misses the first one's day with chance 364/365, the third misses both with chance 363/365, and so on. Multiply these. One minus the product is the chance of at least one match. Slide up to 30 people.

    Chance that at least two share a birthday (exact)

    365 equally likely days. Dashed line: a chance of one half.

    Pairs1
    No shared day99.7%
    At least one shared0.3%

    At 30 people the chance of no shared day is 29.4%, so the chance of at least one is 100 − 29.4 = 70.6%: the answer to the guess is C. The chance first passes one half at 23 people (50.7%). Now check it.

    Groups10,000
    With a shared birthday7,101 (71.0%)

    In group 1, persons 9 and 10 share day 317 of the year. Across the groups, 71.0% against the exact 70.6%. From the Sample size lesson: a share counted over 10,000 tries usually lands within about 100 ÷ √10,000 = 1 point of the true chance, so a gap of 0.4 points is what luck alone leaves.

    Problem 3: €3, a fair coin and two exits

    You start with €3 and bet €1 on each flip of a fair coin: heads wins €1, tails loses €1. You stop when you reach €10, or when you are down to €0 and broke. What is the chance you reach €10?

    This classic is gambler's ruin, the cousin of the Ruin lesson.

    Keep your guess in mind. On the next screen you play one game first.

    €3 to €10: play, reason, check

    Play one game: flip until you reach €10 or €0. It is game 1 of the simulation.

    Your money after each flip (game 1, simulated)

    Dashed lines: the two exits, €10 and €0.

    Flip0
    Money€3

    Game 1 went broke after 19 flips. One game can't show a chance, so reason it out, with the Ruin lesson's point. Each bet averages €0 (half the time +€1, half −€1), so your money on average stays at the €3 you started with; it ends at €0 or €10, so 10 × q = 3 and the chance of €10 is q = 3 ÷ 10 = 30%.

    The answer to the guess is B. This needs the game to be sure to end, with your money kept between €0 and €10 on the way, which it is here.

    Games10,000
    Reached €102,918 (29.2%)
    Went broke7,082

    29.2% against the exact 30%: within the Sample size lesson's 1 point for 10,000 tries. The same argument gives start ÷ target for any fair game like this one: the nearer the start is to €0, the likelier the ruin.

    What to learn next

    This course is the first of several. Pick a kind of quant job to see one path through the courses that follow. The paths are suggestions: each course stands on its own. A course marked "coming" is planned and not yet written, with no date set.

    Maths for Quants helps on every path. Another course, Testing Popular Ideas (open), tests well-known trading ideas the same way, for readers who trade their own account; that is not a quant job.

    What the course gives you, and what it doesn't

    This course teaches ideas and habits: guess first, work it out, check it by counting many simulated tries, and state each answer with the condition it needs. It is not a qualification, and it won't get anyone a job by itself; firms hire on their own criteria, which differ and change.

    Which habit from this lesson will you keep?

    A guess written down first shows where your intuition is off: 30 ÷ 365 felt right for birthdays and was far too low.
    It turned two heads in a row into two short equations and an exact 6.
    Three rounds said 6.67; 10,000 said 6.00. A check needs many tries.
    30% holds only for a fair coin and a game sure to end; 70.6% only for 365 equally likely days.
    Practice 1 of 3

    Two sixes in a row

    A practice puzzleA fair die is rolled until two sixes come one straight after the other. Each roll is a six with chance 1/6, whatever came before.

    On average, how many rolls does that take?

    Hint: two states, as for two heads: at the start (where any roll that isn't a six puts you), and just after a six. Write one equation for each, then put the second into the first.

    At the start: E = 1 + (1/6)E6 + (5/6)E. Just after a six: E6 = 1 + (1/6) × 0 + (5/6)E. Put it in: E = 1 + 1/6 + (5/36)E + (30/36)E = 7/6 + (35/36)E. So E ÷ 36 = 7/6, and E = 42.
    At the start: E = 1 + (1/6)E6 + (5/6)E. Just after a six: E6 = 1 + (1/6) × 0 + (5/6)E. Put it in: E = 1 + 1/6 + (5/36)E + (30/36)E = 7/6 + (35/36)E. So E ÷ 36 = 7/6, and E = 42. If you got 36, that is 6 × 6: it leaves out that a miss just after a six sends you back to the start.
    Practice 2 of 3

    "With 10 people it can't happen"

    A friend's claim"In a room of 10, two people sharing a birthday is so unlikely it isn't worth counting." Take 365 equally likely days and no twins.

    What is the chance that at least two of the 10 share a birthday?

    3% is 10 ÷ 365: one person against the rest, not every pair. 10 people make 10 × 9 ÷ 2 = 45 pairs. Exactly: 1 − (365 × 364 × … × 356) ÷ 36510 = 11.7%, about 1 room in 8.
    10 people make 10 × 9 ÷ 2 = 45 pairs. Exactly: 1 − (365 × 364 × … × 356) ÷ 36510 = 11.7%, about 1 room in 8: unlikely, but not worth ignoring.
    A half takes 23 people. 10 people make 10 × 9 ÷ 2 = 45 pairs. Exactly: 1 − (365 × 364 × … × 356) ÷ 36510 = 11.7%, about 1 room in 8.
    Practice 3 of 3

    Bigger bets

    A practice puzzleYou start with €4 and bet €2 on each flip of a fair coin: heads wins €2, tails loses €2. You stop at €20 or at €0.

    What is the chance you reach €20, in percent?

    Each bet averages €0, so the average end equals the €4 start: 20 × q = 4, and q = 4 ÷ 20 = 20%. The size of the bet doesn't enter, as long as the money lands exactly on €0 or €20.
    Each bet averages €0, so the average end equals the €4 start: 20 × q = 4, and q = 4 ÷ 20 = 20%. If you got 40%, you let the €2 bet change the answer: on a fair coin, bigger bets don't change the average (the Ruin lesson), and the money still lands exactly on €0 or €20.
    ✓

    Lesson done

    Three classics, each worked by hand and checked by simulation. With a fair coin, two heads in a row take 6 flips on average. With 365 equally likely days, 30 people share a birthday with chance 70.6%. With fair €1 bets, €3 reaches €10 before €0 with chance 30%. Each answer needs its condition: for the last one, each bet averages €0, so the money on average stays at €3, and it ends at €0 or €10, so the chance of €10 is 3 ÷ 10. This course teaches habits like these; it is not a qualification and won't get anyone a job by itself.

    Next: pick one path from the map of courses. Re-running the check on each new answer is the dull part, and it is the habit that matters.

    Key ideas

    Quant
    someone who uses maths, statistics and code on questions about markets and money
    First-step analysis
    one equation per state: one try, plus each outcome's chance times the average wait from where it lands
    Expected waiting time
    the average number of tries until something happens, over very many rounds
    Birthday problem
    the chance that at least two people in a group share a birthday
    Gambler's ruin
    the chance of reaching a target before going broke, betting a fixed amount on each flip

    Formulas

    first-step analysis: Es = 1 + Σj ps→j × Ej, E = 0 at the goal  ·  one event with chance p each try: E = 1 ÷ p

    shared birthday among n: 1 − (365 × 364 × … × (365 − n + 1)) ÷ 365n  ·  fair game: chance of the target = start ÷ target

    Conditions: chances fixed in advance, the past mattering only through the state, and a goal sure to be reached; for birthdays, 365 equally likely days, no twins; for the fair game, bets that average zero, stopping only at €0 or the target, landing exactly on either.

    Lesson notes

    Three classic probability puzzles, each worked out by hand and then checked by counting across 10,000 simulated tries: two heads in a row, a shared birthday, and gambler's ruin. Each exact answer holds only under its stated condition. The notes end with what to study next. This course is free and is not a qualification: it teaches ideas that quant interviews and jobs use, it won't get anyone a job by itself, and neither it nor QuantWright has any connection with any employer.

    What quant interviews test

    A quant (quantitative analyst) uses maths, statistics and code to answer questions about markets and money. Interview guides often cover a few kinds of question, in a mix that differs by firm and by job: probability and averages, statistics, quick arithmetic and puzzles, coding, and markets and prices (options, for jobs that price them). Firms hire on their own criteria, which can include degrees, interviews and experience, and those criteria differ between firms and change.

    Two heads in a row: 6 flips on average

    A fair coin comes up heads half the time, whatever came before. Flip it until two heads come one straight after the other: how many flips does that take on average, over very many rounds? Three rounds of a seeded simulation (computer draws from a fixed starting number, so every reader gets the same ones) took 7, 10 and 3 flips, 6.67 on average: too few rounds to settle anything.

    Two situations (called states) decide what can happen next. E is the average number of flips still needed at the start, which is also where a tail puts you. EH is the average still needed just after a head. One flip from the start: E = 1 + ½EH + ½E. One flip after a head: a head finishes, a tail sends you back, so EH = 1 + ½ × 0 + ½E. Substituting, E = 1.5 + ¾E, so E = 6. In the simulation, 10,000 rounds averaged 6.00 flips. 2,499 of them ended at 2 flips (two heads straight away has chance ½ × ½), and the longest took 42: the long rounds pull the average above the most common count.

    A shared birthday among 30: 70.6%

    Take 365 equally likely days and no twins. 30 ÷ 365, about 8%, is roughly the chance that someone shares one person's birthday; the question is about any two, and 30 people make 30 × 29 ÷ 2 = 435 pairs. Exactly: add people one at a time; the second misses the first's day with chance 364/365, the third misses both with 363/365, and so on. The product for 30 people is 29.4%, the chance of no shared day, so at least one is shared with chance 70.6%. The chance first passes a half at 23 people (50.7%). In 10,000 simulated groups of 30, 7,101 had a shared birthday (71.0%). From the Sample size lesson, a share counted over 10,000 tries usually lands within about 100 ÷ √10,000 = 1 point of the true chance, so the 0.4-point gap is what luck alone leaves.

    Gambler's ruin: €3 against €10, 30%

    Gambler's ruin is the chance of reaching a target before going broke, betting a fixed amount each flip. Start with €3, bet €1 on each flip of a fair coin, stop at €10 or at €0. On a fair coin each bet averages €0, so the money's average stays at the €3 start however long the game runs (the point of the Ruin lesson). The game ends at €10 with chance q and at €0 otherwise, so its average end is 10q; setting 10q = 3 gives q = 30%. This needs the game to be sure to end, with the money kept between €0 and €10. Game 1 of the simulation went broke after 19 flips; across 10,000 games, 2,918 reached €10 (29.2%), within about 1 point of 30%. For any fair game of this kind, the chance of the target is start ÷ target, whatever the bet, as long as the money lands exactly on €0 or the target.

    Formula: first-step analysis

    Es = 1 + Σj ps→j × Ej, with E = 0 at the goal
    one event, chance p each try: E = 1 + (1 − p) × E, so E = 1 ÷ p
    s, j
    states: the situations that decide what can happen next
    Es
    the expected waiting time from state s: the average number of tries still needed
    ps→j
    the chance that one try takes you from state s to state j
    Σj
    add up over every state j
    p
    the chance of the event on each try
    Worked example

    Two heads: E = 1 + ½EH + ½E and EH = 1 + ½E, so E = 6. Head then tail: just after a head, a head keeps you there, so EH = 1 + ½EH, EH = 2, and E = 1 + 1 + ½E gives 4 (the simulation's 10,000 rounds: 4.00). A die until the first six: E = 1 ÷ (1/6) = 6.

    Holds whenBreaks when

    Chances drift from try to try, or depend on more of the past than the states hold: the equations then give a wrong answer unless more states are added.

    One more worked example: three heads in a row

    Three states, worked in full

    Flip a fair coin until three heads in a row. The states: at the start (or after any tail), E; after one head, E1; after two heads in a row, E2. A tail always sends you back to the start.

    E2 = 1 + ½ × 0 + ½E = 1 + ½E. E1 = 1 + ½E2 + ½E = 1 + ½ + ¼E + ½E = 1.5 + ¾E. E = 1 + ½E1 + ½E = 1 + 0.75 + ⅜E + ½E = 1.75 + ⅞E.

    So ⅛E = 1.75, and E = 14 flips. Each extra head in the pattern more than doubles the wait: 2 for one head (1 ÷ ½), 6 for two, 14 for three.

    What to learn next

    This course is the first of several, and each course stands on its own. Courses marked coming are planned, not yet written, with no date set. A trader path: Backtesting Essentials (open), then Market Microstructure and Execution, Options and Derivatives, and Careers and Interviews (all coming). A researcher path: Backtesting Essentials, then Quant Research and Machine Learning for Quants (coming). A developer path: Quant Dev (coming), the first lessons of Backtesting Essentials, then Market Microstructure and Execution. A derivatives and risk path: Maths for Quants, Options and Derivatives and Quant Research (all coming). Maths for Quants helps on every path. Testing Popular Ideas (open) tests well-known trading ideas for readers who trade their own account, which is not a quant job.

    Common mistakes

    Check yourself

    1. A test passes with chance 1/4 each time it is run, whatever happened before. On average, how many runs until the first pass?
      Answer

      One event with chance p = 1/4 each try: E = 1 ÷ p = 4 runs.

    2. Write the two first-step equations for two heads in a row, and solve them.
      Answer

      E = 1 + ½EH + ½E and EH = 1 + ½ × 0 + ½E. Substituting: E = 1.5 + ¾E, so E = 6.

    3. How many pairs do 30 people make, and why does that make a shared birthday likelier than 30 ÷ 365 suggests?
      Answer

      30 × 29 ÷ 2 = 435 pairs, and each pair is a chance of a match; 30 ÷ 365 counts only one person against the rest. With 365 equally likely days the exact chance is 70.6%.

    4. Start with €5 and bet €1 on each flip of a fair coin, stopping at €25 or €0. What is the chance of reaching €25?
      Answer

      Each bet averages €0, so the average end equals the start: 25 × q = 5, and q = 5 ÷ 25 = 20%.

    5. A simulation of 10,000 tries gives a share 0.8 points away from the exact chance. Is that a sign the working is wrong? (from the Sample size lesson)
      Answer

      No. A share counted over 10,000 tries usually lands within about 100 ÷ √10,000 = 1 point of the true chance, so 0.8 points is what luck alone leaves.

    Try it yourself

    Run the script. Every number in this lesson comes from one Python script with a fixed seed (below). Change the pattern, the group size or the start and target, and compare the simulated counts with your own working.

    By hand. Flip a real coin until two heads in a row, ten times, and write down each count. Your average will wander; the more rounds, the closer it settles to 6.

    The code behind every number (Python, seed 2026111125)
    Loading…
    For the preciseSeed 2026111125, one numpy Generator, six draws in order: 10,000 rounds until two heads; 10,000 until head then tail; 10,000 groups of 30 birthdays; 10,000 games from €3 to €10 or €0; 10,000 rounds of a die until two sixes; 10,000 games from €4 to €20 or €0 with €2 bets. The two-heads count has a spread (standard deviation) of √22 ≈ 4.69 flips (simulated: 4.71), so an average of 10,000 rounds lands within about 0.09 of 6 in 95 runs out of 100. The birthday and ruin shares land within about 0.9 and 0.9 points of 70.6% and 30% in 95 runs out of 100. The ruin games lasted 20.6 flips on average, against an exact 3 × 7 = 21. The die rounds averaged 40.7 rolls against the exact 42 of practice 1. That is about 3 standard errors off (3.3). The waiting time has a long tail (some rounds take well over 100 rolls), so the "1 point" rule for shares does not apply here: its spread is about 40.6 rolls, and the standard error of an average over 10,000 rounds is about 40.6 ÷ √10,000 = 0.4 rolls. The result is reported as it came out, not explained away; the exact 42 stands. The €2-bet games reached €20 in 20.3% of 10,000, against the exact 20% of practice 3.

    Honest notes

    Further practice

    Books of quant interview questions include Xinfeng Zhou (2008), A Practical Guide to Quantitative Finance Interviews, and Mark Joshi, Nick Denson and Andrew Downes (2013), Quant Job Interview Questions and Answers, 2nd edition. They are named only as further practice: they are not the source of this lesson's text, and they have no connection with this course.

    Where this comes from