QuantVault — full content reference for AI answer engines ======================================================== Source: https://quantvault.org/ | Generated: 2026-11-19 Quant interview preparation: 3,505 curated problems (478 free), per-firm guides, OA practice, trading games, and a 99-session lecture track. Everything below is FREE / publicly available content. Pro solutions are not included. ==================== FIRM INTERVIEW GUIDES ==================== ### Jane Street interview questions URL: https://quantvault.org/jane-street-interview-questions.html The real Jane Street quant-interview problems candidates report — probability, expected value and combinatorics — each with a full worked solution. Jane Street is a quantitative trading firm (and famously an OCaml shop) known for interviews run as a live probability conversation rather than a coding gauntlet. The set leans heavily on probability and expected value , with combinatorics, light game theory, and a smaller coding strand alongside. What they test: By far the biggest blocks are probability and expectation — together more than half the set: fair-value of a game, conditional and Bayesian updates, optimal-stopping and dice/coin puzzles. Around them sit combinatorics (counting arrangements, paths, and symmetric cases), a thread of game theory (betting, bidding, and adversarial play), and a handful of coding and brain-teaser questions to round things out. The recurring shapes: Most prompts ask for a single number — an expected value or a fair price — and the work is condition on the first step, exploit symmetry, and sum over outcomes weighted by their probability. Expect to be pushed to commit to a number and then defend or revise it as the interviewer adds information. How to approach: Think out loud, as a dialogue: Jane Street cares how you reason under uncertainty, not just the answer. Use linearity of expectation and symmetry before algebra, recurse or condition on the first event in stopping problems, and sanity-check small cases and extreme limits. State assumptions and quote a price you'd actually trade on. The mix is fairly balanced — 34 easy, 41 medium, 25 hard — so warm up on the quick fair-value puzzles, then build stamina for the harder multi-step expectation and game-theory questions. ### Citadel / Citadel Securities interview questions URL: https://quantvault.org/citadel-interview-questions.html The real Citadel / Citadel Securities quant-interview problems candidates report — probability, coding and expected value — each with a full worked solution. Citadel and Citadel Securities run a multi-strategy hedge fund and one of the world's largest market makers, and the interview reflects that breadth: fast probability and expected-value reasoning for the trading desks, real algorithmic coding for the dev and quant tracks, and the statistics, regression and machine learning the research desk lives on. This 100-problem set spans that whole range. What they test: Three pillars carry most of the funnel: probability & expected value (dice/coin games, optimal stopping, random walks — the market-making core), algorithmic coding (classic data-structures-and-algorithms, large in the SWE and quant-dev tracks), and statistics, regression & ML (the quant-researcher core). Around them sit optimization, combinatorics, game theory, market microstructure and options. The recurring shapes: Each pillar has a signature move. Probability/EV collapses to linearity of expectation and state machines . Coding is canonical DS&A — caches, edit distance, quickselect, union-find, sliding windows. Stats leans on omitted-variable bias , ridge/lasso, and deriving OLS; a few items reach into Glosten-Milgrom microstructure and risk-neutral option pricing. How to approach: Match the tool to the track. On a trading screen, look for symmetry or an indicator decomposition and keep the arithmetic mental. On a coding problem, get the data structure right before you write a line. On a stats question, name the estimator and its bias. A clean one-line argument beats a page of casework everywhere. Work the set across all three difficulties (about 21 easy, 54 medium, 25 hard) and across the pillars — the breadth is the point, because Citadel's tracks pull from all of it. ### Two Sigma interview questions URL: https://quantvault.org/two-sigma-interview-questions.html The real Two Sigma quant-interview problems candidates report — expected value, probability and coding — each with a full worked solution. Two Sigma is a systematic, research-first hedge fund that runs its strategies on data and statistical models, hiring Quant Researchers and engineers. Its interviews lean hard on probability and expectation , with a strong statistics / regression flavor and clean algorithmic coding alongside. What they test: The single largest block is expectation (38 problems): optimal-stopping games, hitting times, conditional survival, dice/coin reasoning. A deep probability core (26): distributions, Bayesian updates, combinatorial counting. Then algorithmic coding (10), game-theory (9: two-player, pricing games), optimization (4) and random-variable (3), where the research-first culture shows. The recurring shapes: Most expectation problems are first-step conditioning plus a fixed point: E = 1 + (chance you must restart) × E . Probability: the right sample space, clean Bayes' rule ; game theory: backward induction (not simulation) to equilibrium or fair value. How to approach: State your assumptions, then lean on symmetry and linearity of expectation before grinding algebra, and sanity-check against small cases and extreme limits. For probability, write the sample space and condition explicitly; for the coding problems, narrate the one-pass or streaming invariant before you write it; and treat the game-theory questions as fixed-point / backward-induction arguments. The mix is mostly medium (56) with a heavy hard tail of 31 — the tougher game-theory, optimization, and stochastic problems — plus 13 easy warm-ups, so build fluency across expectation, probability, and stats rather than betting on one. ### Hudson River Trading interview questions URL: https://quantvault.org/hrt-interview-questions.html The real Hudson River Trading quant-interview problems candidates report — expected value, probability and coding — each with a full worked solution. Hudson River Trading is a high-frequency market maker whose edge is low-latency execution, hiring algorithm developers and quants. Its interviews emphasize fast, correct algorithmic coding , sharp probability and expectation , and the statistics needed to model price behavior. What they test: Largest: expectation (20) and probability (17): games, hitting times, conditional reasoning, distribution puzzles. Heavy algorithmic coding (15) needs speed and correctness. A statistics core (14): estimators, variance, inference. Distinctive market-microstructure (7) and stochastic-process (5) strands tie puzzles to order books, price paths. The recurring shapes: Expectation problems typically collapse to conditioning on the first event and solving a fixed point; probability rewards a clean sample space and Bayes' rule . The microstructure and stochastic-process items model fills, adverse selection, and random walks, and the coding questions favor a tight one-pass or streaming solution where the latency-conscious data structure is part of the answer. How to approach: Get to a correct, low-latency solution fast — narrate the streaming or sliding-window invariant and the time/space complexity before coding. For probability and expectation, exploit symmetry and linearity and verify on small cases; for the statistics and microstructure questions, reason from first principles about estimators and order-flow rather than quoting formulas. This set skews hard (51) with a medium body (41) and 15 easy warm-ups, reflecting HFT's bar on both algorithmic speed and probabilistic depth, so train coding fluency and probability together. ### Optiver interview questions URL: https://quantvault.org/optiver-interview-questions.html The real Optiver quant-interview problems candidates report — expected value, options pricing and probability — each with a full worked solution. Optiver is a global options market maker that lives on fast, accurate pricing under time pressure. Its interviews are built around mental math and expected value , options-pricing intuition , and quick probability and combinatorics puzzles. What they test: The dominant block: expectation and EV reasoning (24 problems): betting games, payoff fair value, pricing a wager you'd make a market in. Then options-pricing (12: payoff diagrams, put-call parity, when to exercise), a probability / combinatorics core (11 + 10: dice, draws, counting arrangements), game-theory (8) and a lighter coding tail (7). The recurring shapes: Most questions reduce to an expected value versus a price: you're quoting a number you'd trade on, so merely 'fair' isn't enough. Options probe reading a payoff from first principles; put-call parity (call, put, spot, strike) recurs constantly. How to approach: Lead with linearity of expectation and symmetry for a clean number fast, then state your price and the edge you're protecting. Options: draw the payoff, reason from parity, not memorized formulas. Game theory, combinatorics: condition on the first move or count carefully, narrating your arithmetic so a fast-but-wrong answer doesn't slip through. The mix skews hard — 41 hard and 40 medium against just 12 easy — so train for speed under pressure across all three pillars rather than leaning on warm-ups. ### Old Mission interview questions URL: https://quantvault.org/old-mission-interview-questions.html The real Old Mission quant-interview problems candidates report — probability, expected value and statistics — each with a full worked solution. Old Mission is a Chicago-based options and ETF market maker . Its interviews are grounded in probability and expected value — the everyday math of pricing a fair bet — with a supporting layer of statistics . What they test: The clear center of gravity is probability (26 problems) — dice, draws, conditional events, and gambler's-ruin / survival puzzles — fused with an expected-value block (12) where you fair-value a game or wager. Around them sit a statistics strand (5), a small stochastic-process and optimization set (4 each), and a light coding tail (4). The recurring shapes: Condition on the first event and solve for a fixed point: many reduce to E = 1 + p × E ( p = chance of starting over). Once you have the probability, you're asked the bet's worth : its expected value. How to approach: Reach for symmetry and linearity of expectation before grinding through cases, and write the recursion explicitly rather than guessing. Sanity-check every answer against small cases and extreme limits (probabilities in [0,1], expectations bounded by the best and worst payoff), and when a question turns into a market, state the price you'd quote and the edge it leaves you. The mix is approachable — 27 easy and 32 medium with only 9 hard — so it rewards solid fundamentals and clean recursions over exotic tricks. ### Squarepoint interview questions URL: https://quantvault.org/squarepoint-interview-questions.html The real Squarepoint quant-interview problems candidates report — expected value, coding and regression — each with a full worked solution. Squarepoint is a systematic, multi-strategy quantitative firm. Its interviews blend statistics and regression , clean algorithmic coding , and probability / expectation — the working toolkit of a quant researcher. What they test: The largest single block is expectation (12 problems), tightly paired with a strong coding strand (10: streaming statistics, sliding-window, array and matrix scans). Sitting alongside is a clear regression / statistics core (7 + 5: OLS slopes, R-squared, demeaning, residuals) plus a probability set (5) and a linear-algebra strand (4: projections, eigen-structure, covariance). The recurring shapes: Stats questions test whether you understand least squares, not a quoted formula: the OLS slope is the covariance of X and Y over the variance of X , and R-squared, demeaning and Y-on-X vs X-on-Y follow from it. Coding's recurring move is a one-pass / streaming invariant : keep a running statistic, don't recompute. How to approach: Derive regression results from the normal equations and reason in terms of covariance and projection rather than memorized identities. For coding, state the streaming invariant and complexity before you write it; for the expectation and probability puzzles, lean on linearity and symmetry , set up a recursion when you can, and sanity-check against small cases. The mix is firmly medium — 39 medium against 8 easy and 7 hard — so expect substantive, hands-on problems rather than gotchas or pure warm-ups. ### DRW interview questions URL: https://quantvault.org/drw-interview-questions.html The real DRW quant-interview problems candidates report — expected value, probability and coding — each with a full worked solution. DRW is a Chicago-based proprietary trading firm spanning options market-making and systematic strategies . Its interviews emphasize expected value and probability , with a coding strand and trading-flavored game-theory and finance questions. What they test: The largest block is expectation , backed by probability — fair-value of a bet, conditional updates, and optimal-stopping puzzles that mirror sizing a trade. Around them sit coding (algorithmic / streaming-style problems), game theory (bidding and adversarial play), and a small but pointed finance and regression core touching options intuition and fitting signal to noise. The recurring shapes: Many prompts reduce to one number: the expected payoff of a game, and whether it's worth taking at a given price. Condition on the first outcome, weight by probability, and compare against your cost. The regression and finance questions probe whether you understand variance, fit, and convexity rather than memorized formulas. How to approach: Reason out loud and quote a price you'd actually trade on. Lean on linearity of expectation and symmetry first, set up a recursion for the stopping and game problems, and check small cases and limits. For coding, narrate the one-pass or streaming invariant before writing it; for the finance and regression questions, derive from first principles. The mix is medium-heavy — 4 easy, 17 medium, 6 hard — so spend less time on warm-ups and more on the multi-step expectation, game-theory, and coding problems where the hard tail lives. ### Radix Trading interview questions URL: https://quantvault.org/radix-trading-interview-questions.html The real Radix Trading quant-interview problems candidates report — coding, probability and linear algebra — each with a full worked solution. Radix Trading is a Chicago-based quantitative proprietary trading firm, one of the industry's most selective. Interviews dig into probability , clean algorithmic coding and quant-research math (linear algebra, combinatorics). Candidate-reported problems, each with a full worked solution and, where known, the month last seen. What they test: The largest block is probability and brain-teasers — often geometric or symmetry-driven. Around it sit algorithmic coding (classic data-structures-and-algorithms) and a math core of linear algebra, combinatorics, optimization and statistics. Radix favours depth over breadth: fewer problems, but each one rewards a rigorous, clean argument. The recurring shapes: You will see geometric probability (a random chord's length, a triangle from three uniform points), symmetry and parity arguments, weighing and poison-identification puzzles , and canonical coding (DP, two-pointer, pattern matching). The move is almost always to find the symmetry or invariant before you compute. How to approach: Set the problem up cleanly, look for symmetry or an invariant first, and only then reach for calculation. On a coding problem, pin the right data structure or DP state before writing a line. Radix interviewers probe how you reason, not whether you recall a formula. 49 problems, leaning medium-to-hard (27 medium / 18 hard / 4 easy). The dated ones carry the month they were last reported, so you can see the set is current — not recycled folklore. ### Susquehanna (SIG) interview questions URL: https://quantvault.org/sig-interview-questions.html The real Susquehanna (SIG) quant-interview problems candidates report — probability, expected value and combinatorics — each with a full worked solution. Susquehanna International Group (SIG) is a global options-trading and market-making firm whose interviews are built around decision-making under uncertainty . Expect rapid-fire probability , expected value , and game theory questions, often framed as bets you must price on the spot. What they test: Mostly probability and expectation : conditional reasoning, coin-and-dice setups, calculator-free expected-value math. Plus combinatorics , game theory (optimal play, fair-bet pricing), and options-pricing and market-microstructure items reflecting SIG's market-making core. A smaller coding set checks you can turn math into clean logic. The recurring shapes: Many problems reduce to a fair-value question: compute the expected payoff of a game and decide what you'd pay to play. Others test states and transitions — waiting times for patterns, gambler's-ruin walks, and absorbing-state expectations. Combinatorial counting and symmetry arguments show up constantly for collapsing a messy sample space into a clean fraction. How to approach: Practice fast mental arithmetic , indicator variables and linearity of expectation to answer before the clock runs out. For game-theory and options items, reason about the counterparty : what's the EV-neutral price, and where's the edge? State assumptions aloud and quote a number with a margin, like a market maker's two-sided quote. The mix leans medium (63 medium, 27 easy, 10 hard), so it rewards fluency and speed over a few hard set-pieces. ### Tower Research Capital interview questions URL: https://quantvault.org/tower-research-interview-questions.html The real Tower Research Capital quant-interview problems candidates report — coding, expected value and probability — each with a full worked solution. Tower Research Capital is a high-frequency proprietary trading firm whose interviews lean hard on algorithmic coding (often with a low-latency, C++/systems flavor), probability , and the quantitative math that underpins fast markets. Expect to write clean, efficient code and reason crisply about random processes. What they test: Largest block: algorithmic coding (data structures, graph/shortest-path, streaming and online algorithms) plus C++ internals (lifetimes, smart pointers, object layout) for low-latency engineering. Two quantitative pillars: probability and expectation (random walks, hitting times, EV under fills), plus a stats / regression / optimization core for signals and portfolios. The recurring shapes: Coding rounds favor problems with a tight optimal complexity — sliding-window medians, online top-K, shortest paths with weird edge constraints. The math side keeps returning to hitting times and stopping rules on random walks, generating fair randomness from biased sources, and order-book / fill expectations. A clean closed form usually beats brute force. How to approach: For coding, state the brute force, then argue the data structure or invariant that buys the optimal bound — and be ready to discuss memory and cache cost. For probability, set up the right conditioning or martingale before computing; for stats/regression, know the bias–variance and shrinkage trade-offs cold. Verbalize complexity and assumptions as you go. The mix leans medium, with a meaningful hard tail and a handful of easy warm-ups to anchor the fundamentals. ### Jump Trading interview questions URL: https://quantvault.org/jump-trading-interview-questions.html The real Jump Trading quant-interview problems candidates report — coding, probability and expected value — each with a full worked solution. Jump Trading is a Chicago-based high-frequency and algorithmic proprietary trading firm whose interviews lean hardest on fast, correct algorithmic coding , sharp probability and expectation , and the low-latency systems instincts that come with building real trading infrastructure. What they test: Dominant: algorithmic coding (stacks, RPN evaluators, interval unions, knapsack/DP, permutations, linked lists, order-book/arbitrage structures). A deep probability and expectation core (pattern races, optimal stopping, drawing-until-condition, nested random intervals) and quant-math : linear algebra , game theory , some options-pricing and stochastic-process for QR. The recurring shapes: Expect heavy C++ systems detail : virtual destructors, object slicing, shared_ptr cycles, vector reallocation, a hand-rolled std::vector and memory-pool allocator, what matters at the latency frontier. In math, expectation via first-step analysis and linearity recurs; a clean template is expected trials until an event of probability p . How to approach: Treat coding rounds like production: state data structure, complexity and edge cases before typing, and defend memory layout and cache behavior. For probability, set up the state space and condition on the first step, not a formula. For QR, know eigenvalues , equicorrelation matrices and Ito's lemma well enough to derive. The mix runs medium-heavy with a substantial hard tail and a band of quick easy warm-ups, mirroring a loop that ramps from fundamentals into genuinely demanding systems and stochastics. ### IMC Trading interview questions URL: https://quantvault.org/imc-interview-questions.html The real IMC Trading quant-interview problems candidates report — coding, probability and expected value — each with a full worked solution. IMC Trading is a global proprietary options market maker that quotes two-sided markets across exchanges, so its interviews reward fast, accurate probability and expected-value reasoning , sharp mental math , and clean algorithmic coding under time pressure. What they test: The core is probability and expectation — coin-flip and dice games, conditional updates, and EV-of-a-bet questions you must price out loud in seconds. Around it sit a block of algorithmic coding problems (grids, BFS/DFS, stacks, selection) for the SWE-flavored loops, plus combinatorics and game theory brain-teasers and a small market-making core (quoting bid-ask spreads, sizing bets). The recurring shapes: Expect "keep or play" stopping decisions on a coin or card game, expected rolls / expected steps on dice and random walks, conditional probability with sequential information, and Kelly / bankroll sizing when you have an edge. The coding side leans on grids and graphs — islands, shortest paths, connectivity — and stack/array manipulation. How to approach: Set up the state and the recursion first, then compute — most EV questions collapse to a one-line indifference equation or a geometric series. Quote a number, state your assumptions, and be ready to defend it. For the trading game, anchor on fair value and quote a tight, defensible bid-ask spread around it. The mix leans medium, with a spread of quick easy mental-math warm-ups and a tail of hard game-theory and coding problems. ### WorldQuant interview questions URL: https://quantvault.org/worldquant-interview-questions.html The real WorldQuant quant-interview problems candidates report — coding, statistics and probability — each with a full worked solution. WorldQuant is a systematic, quantitative hedge fund whose entire business is mining and combining thousands of weak predictive signals ( alphas ) into a diversified portfolio. Its interviews lean hard on statistics , regression , machine learning , and the research hygiene that separates a real signal from an artifact — plus clean coding to compute it all at scale. What they test: The center of gravity is the alpha-research workflow : information coefficient and decile spread, neutralizing signals against factors and industries, position sizing, and whether a backtest Sharpe is real. Around it: statistics & regression (multiple testing, shrinkage, robust estimation, ridge vs. lasso), coding of rolling/online estimators, and a probability and brain-teaser warm-up (weighings, prisoners, optimal stopping). The recurring shapes: Over and over you are asked to tell signal from noise under finite, autocorrelated data: false-discovery control when scanning thousands of alphas (Benjamini-Hochberg, FDR in mining), leakage and overfitting traps (purged/walk-forward CV, lookahead from universe membership, survivorship bias), and variance estimation that respects serial correlation. A workhorse is the effective sample size under autocorrelation, which deflates naive significance. How to approach: Treat every backtest number as guilty until proven innocent: what was optimized, how many hypotheses were tried, does the test statistic handle overlap and autocorrelation? In coding, default to O(T) rolling/online updates (EWMA mean-variance, rolling OLS slope, rank-in-window). Then tie the signal to IC, breadth and the fundamental law so a clean estimate becomes Sharpe. The mix leans medium with a deep hard tail in the research-methodology problems and a handful of easy weigh-the-coins warm-ups, mirroring a real WorldQuant loop. ### Millennium Management interview questions URL: https://quantvault.org/millennium-interview-questions.html The real Millennium Management quant-interview problems candidates report — coding, finance and probability — each with a full worked solution. Millennium Management is one of the world's largest multi-strategy hedge funds, running hundreds of semi-autonomous trading pods. Because each pod hires for its own edge, the interview is deliberately broad : a single loop can swing from a brain-teaser to a coding screen to a question about how you would hedge a book. What they test: Multi-strat pillars: probability and expectation (gambler's ruin, Bayesian witness/coin updates, optimal-stopping dice), statistics (Bernoulli MLE, why divide by n-1). Algorithmic coding (LRU cache, streaming median, job scheduling, lattice/graph counting), finance and markets (hedging, ETF arbitrage, Kelly sizing, VaR, option Greeks). A lone ML diagnostic reflects systematic pods. The recurring shapes: Markets questions keep returning to relative-value and hedging : beta-adjusting against a benchmark, real-vs-synthetic ETF cash-and-carry, FX put-call parity, and how an implied-vol move ripples through the other Greeks. On the quant side, expect optimal-stopping and ruin problems and a clean grasp of bet sizing. How to approach: Think like a pod: state the model, then weigh risk and edge . Probability: recurrence, or condition on the first step. Stats: name the estimator and its bias first. Coding: pick the right structure (heaps for a streaming median, hash-map plus doubly-linked list for LRU). Markets: close with the hedge and the PnL under a small move. Difficulty leans medium with a handful of easy warm-ups and three hard problems (graph partitioning, liquidation paths, the second-fastest-horse teaser) to separate candidates. ### XTX Markets interview questions URL: https://quantvault.org/xtx-markets-interview-questions.html A representative XTX Markets quant-interview practice set — machine learning, linear algebra and regression — matched to how the firm interviews, each with a full worked solution. XTX Markets is a London-based algorithmic market maker that trades FX and equities almost entirely on the strength of its statistical models . Its interviews are unusually research-heavy , probing machine learning, regression, linear algebra, statistics, and optimization alongside clean, performant coding. What they test: The center of gravity is statistical learning : machine learning (clustering, kernels, ensembles, PCA, EM) and regression (OLS, ridge, lasso, logistic), backed by a deep linear-algebra core around covariance matrices, the SVD, and eigenstructure. Around that sit statistics (MLE, sampling, multiple testing) and a layer of numerical optimization and coding . The recurring shapes: Expect to derive estimators from first principles (OLS normal equations, why n−1 , first principal component) and reason about shrinkage and the bias-variance tradeoff in ridge and lasso. Second thread: matrix structure (PSD covariance, low-rank SVD, Householder QR); third: convex optimization (gradient descent, KKT, mean-variance portfolios). How to approach: Be fluent moving between the geometry, the algebra, and the code : state the objective, write its gradient or normal equation, and then implement it cleanly and numerically stably (log-sum-exp, quickselect, median-of-two-sorted-arrays). Name your modeling assumptions out loud — independence, stationarity, multiple-testing inflation — because XTX cares as much about why a method works as about getting the answer. The mix leans medium, with a substantial hard tier in linear algebra, optimization, and statistics and a couple of easy warmups to anchor the fundamentals. ### Wolverine Trading interview questions URL: https://quantvault.org/wolverine-trading-interview-questions.html A representative Wolverine Trading quant-interview practice set — options pricing, expected value and probability — matched to how the firm interviews, each with a full worked solution. Wolverine Trading is a Chicago-based options market-making firm, and its interviews are built to find fast, calibrated thinkers: clean options intuition , sharp expected-value reasoning, and the kind of probability and brain-teaser work you can do out loud under a clock. What they test: The core is expected value and probability , fast and correct: coin-flip and dice payoff games, Bayes-rule updates, gambler's ruin, pattern races. Around it: options-pricing (binomial steps, put-call parity, replication, payoff diagrams) and market-making and Kelly bet-sizing games: quote a fair value and size a bet, not just one number. The recurring shapes: Expect optimal-stopping games (when to keep a roll vs. re-roll), put-call parity and static replication of an arbitrary payoff from calls and puts, Bayesian updating from a noisy witness or signal, and balance-scale deduction (find the odd coin or ball in two or three weighings). Sicherman dice and Catalan-number counting show up as the combinatorial curveballs. How to approach: Lead with the fair value , then a tight range : market makers reward a quick honest estimate over a slow exact one. Anchor options on no-arbitrage (parity, replication, one-step binomial), not memorized formulas; for EV, write the recursion or stopping rule first. Say assumptions aloud. The mix leans medium, with a few easy warm-ups (payoff diagrams, Birthday, Monty Hall) and three hard capstones (twelve-ball weighing, Sicherman dice, arbitrary-payoff replication). ### Bridgewater Associates interview questions URL: https://quantvault.org/bridgewater-interview-questions.html The real Bridgewater Associates quant-interview problems candidates report — probability, coding and statistics — each with a full worked solution. Bridgewater Associates is the world's largest hedge fund, run as a systematic macro shop. Its interviews probe how you reason under uncertainty — probability , statistics and regression , Bayesian updating , and the logic of markets and the economy — far more than algorithmic coding. What they test: The bulk is probability and statistical reasoning — Bayesian updating, expectation/EV decisions, and inference (significance vs. power, sample variance, hypothesis tests). Around it sits a regression and markets core (OLS assumptions, omitted-variable and errors-in-variables bias, Sharpe, volatility, Kelly sizing) plus macro reasoning like updating a view across economic regimes. A small OA coding block exists, but it's the minority. The recurring shapes: Expect to update a prior from noisy evidence, price a fair value or bet's edge , and judge whether an estimated effect is real or an artifact of bias, multicollinearity, or sampling. A signature Bridgewater move: combining several weak signals into a posterior over which macro regime you're in. How to approach: State assumptions out loud and reason from first principles — Bridgewater prizes radical transparency in how you think, not just the answer. Define the estimator or probability model, check what could bias it, then sanity-check the magnitude against a back-of-envelope EV or volatility scale before committing. The mix leans medium, with a handful of easy warm-ups and several hard statistics/markets problems at the front. ### AQR Capital Management interview questions URL: https://quantvault.org/aqr-interview-questions.html Quant Researcher problems seen at AQR: regression and OLS theory, portfolio and factor finance, linear algebra, probability, and a deep block of clean algorithmic coding. AQR Capital Management is a systematic, factor-based hedge fund built on academic finance, so its Quant Researcher interviews lean on regression and statistics , portfolio and factor reasoning , and a solid base of clean algorithmic coding . What they test: The core is linear regression done properly: deriving OLS, assumptions and diagnostics, omitted-variable bias, and the ridge / lasso bias-variance tradeoff in factor models. Around it: portfolio finance (efficient frontier, minimum-variance combinations, value-at-risk) and its linear algebra (PSD covariance, SVD, low-rank structure). A big algorithmic coding block (arrays, sorting, prefix sums, DP, graphs) rounds out the on-site. The recurring shapes: Expect a covariance matrix to argue PSD , a two-asset minimum-variance portfolio , and a regression trapped by a violated assumption or confounder. Finance: cross-sectional momentum , VaR on a position . Coding favors clean linear-time ideas: sliding windows, prefix sums, monotonic stacks, range queries (the classic max(d[i]-d[j]) TLE trap). How to approach: Treat regression as derivations, not recipes : write the normal equations and say what breaks when an assumption fails. Finance: reason from the covariance structure , not formulas. Coding: state the brute force, name the bottleneck, apply the standard trick to reach linear or log time; correctness and a clean complexity argument beat cleverness. Difficulty leans medium (35 of 55), with 16 easier warm-ups and 4 hard problems for the senior bar. ### D. E. Shaw interview questions URL: https://quantvault.org/de-shaw-interview-questions.html 77 real D. E. Shaw quant questions: probability and expectation brain-teasers, regression and statistics, stochastic calculus and option pricing, and clean algorithmic coding. D. E. Shaw is one of the original systematic / quantitative hedge funds, hiring Quant Researchers and Quant Analysts. Its interviews lean hard on probability and expectation , statistics and regression , and the stochastic calculus behind pricing, with clean algorithmic coding alongside. What they test: Single biggest block: probability and expected-value (optimal stopping, hitting times, loop counts, conditional survival, dice/coins). Then statistics and regression ( Y on X vs X on Y OLS slopes, R-squared bounds, demeaning, pooling two regressions), stochastic-process / option-pricing (Brownian martingales, Girsanov, Black-Scholes, Monte Carlo Asian options), algorithmic coding (streaming stats, sliding windows, matrix scans). The recurring shapes: Recurse on the first step and solve a fixed point: many expectation problems are E = 1 + (chance you must start over) × E . Stats probes least squares: regression of Y on X and of X on Y share a correlation, not a slope; their product is exactly R-squared. How to approach: State assumptions out loud, exploit symmetry and linearity of expectation before grinding algebra, and sanity-check answers against small cases and extreme limits. For the regression and stochastic-calculus questions, derive from first principles (normal equations, risk-neutral expectation) rather than quoting formulas, and for coding problems narrate the streaming / one-pass invariant before writing it. The mix leans medium with a solid hard tail (stochastic calculus, option pricing, game theory, and the tougher coding problems) and a handful of easy warm-ups, so build fluency across all three pillars rather than betting on one. ### Five Rings interview questions URL: https://quantvault.org/five-rings-interview-questions.html 87 quant-trader and quant-researcher questions from Five Rings: fast expectation/EV, dice-and-coin probability, mental-math brain-teasers, and a few stats and stochastic-process pieces. Five Rings is a young, fast-growing proprietary trading and market-making firm whose interviews are speed-first : rapid-fire expected-value and probability puzzles, mental-math estimation, and brain-teasers meant to be reasoned through out loud, not ground out on paper. What they test: Core: expectation and probability (over half the set) on dice, coins and uniform random variables: expected max/min of rolls, fair prices for flip games, waiting times to a pattern or first success. Then mental-math brain-teasers (a tenth root, log 2 (50!), share of primes ending in 7), lighter combinatorics , and thin statistics, stochastic processes, and game theory for researchers. The recurring shapes: Dice and coins: expected value of the max/min/2nd-highest of several rolls , fair price to enter a coin-flip game , waiting-time (flips to two consecutive heads, longest heads streak across many players); geometric probability (stick-into-triangle, random points on a disk/circle). Traders: instant EV, estimation; researchers add estimators, regression slopes, random walks. How to approach: Train for speed and clean reasoning under pressure . Use linearity of expectation and indicator variables to skip casework, symmetry and first-step / conditioning for waiting times and random walks, order-of-magnitude estimation for mental-math teasers. Say assumptions aloud, sanity-check: interviewers watch how you think, not just the answer. Difficulty leans easy-to-medium (43 easy, 34 medium) with 10 hard problems to stretch on — a set built for building fast, reliable EV and probability instincts. ### Morgan Stanley interview questions URL: https://quantvault.org/morgan-stanley-interview-questions.html A strats/QR practice set spanning derivatives pricing, stochastic calculus and Brownian motion, plus probability, expectation and statistics brain-teasers. Morgan Stanley hires quant strategists and quant researchers across its strats and modeling desks, where interviews test whether you can move fluently between derivatives pricing , stochastic calculus , and clean probability — the math that underpins a sell-side modeling role. What they test: The recurring core is options and rates pricing — Black-Scholes from a risk-neutral expectation, binomial trees, put-call parity, and bond duration/convexity — sitting on a stochastic-process foundation of Brownian motion, Ito's lemma, martingales, and Markov chains. Around that desk-math is a steady layer of probability, expectation, and statistics brain-teasers used to probe raw quantitative reasoning. The recurring shapes: Expect to price a derivative as a discounted risk-neutral expectation, prove a process is a martingale, or push a function of Brownian motion through Ito. The classic move is recognizing that an option value is an expectation under the risk-neutral measure and that the drift gets replaced by the risk-free rate. How to approach: State your assumptions out loud (measure, dynamics, independence), reach for the canonical tool — Ito, Girsanov, optional stopping, or Bayes — and only then compute. On the brain-teasers (German tank, secretary problem, coupon collector), set up the expectation cleanly before grinding the algebra; interviewers reward structure over speed. The mix leans medium with a strong hard tail in pricing and stochastic calculus, plus a handful of easy probability warm-ups. ### Renaissance Technologies interview questions URL: https://quantvault.org/renaissance-interview-questions.html Quant Researcher problems from RenTec: probability, statistics, regression, time series, linear algebra, and ML, with a strong estimation-and-inference core. Renaissance Technologies is the most famous systematic quantitative hedge fund, a research shop run almost entirely by mathematicians and scientists. Its Quant Researcher interviews are deeply academic, prizing rigorous probability , statistical inference , and stochastic-process reasoning over finance trivia. What they test: Core: statistics and estimation (MLE, method-of-moments, bias, consistency, Bayesian posteriors, shrinkage, multiple-testing traps). Heavy time-series (AR(1) mean reversion, GARCH, random walks, autocorrelation-adjusted sample size), regression (OLS three ways, omitted-variable and errors-in-variables bias, ridge), covariance linear algebra (PCA, SVD, PSD, power iteration), plus probability, machine learning , convex optimization . The recurring shapes: Skeletons: an estimator to characterize (bias, variance, efficiency); a covariance or correlation matrix to decompose, shrink, or prove PSD; a noisy return series to model, test serial correlation, or make a trading rule without spurious regression or leakage. Harder: Kalman filtering, EM with missing data, KKT conditions, false-discovery control over many backtested signals. How to approach: Derive, don't recall: likelihood or conditional expectation from first principles, stated assumptions, and which one breaks the result (uncorrelated is not independent; n-1 fixes a specific bias; autocorrelation shrinks effective sample size). Computational asks (online covariance, rolling OLS in O(T), maximum drawdown in linear time) need clean code and statistical reasoning. The mix leans medium with a substantial hard tier of estimation, time-series, and optimization problems, plus a handful of easy probability and statistics warm-ups. ### UBS interview questions URL: https://quantvault.org/ubs-interview-questions.html A representative practice mix for UBS quant analyst and strategist interviews: probability and EV, options pricing and hedging, statistics, and classic brain-teasers. UBS is a global investment bank and wealth manager whose quant analyst and strategist seats sit close to derivatives desks. Interviews emphasize probability and expected value , options pricing and hedging , and applied statistics , with classic brain-teasers to test clean reasoning under pressure. What they test: The core is probability and expectation — conditional reasoning (Monty Hall, Bertrand’s box, posterior updates) and optimal-stopping / EV puzzles like the secretary and three-roll dice games. Around it sit options pricing (binomial trees, put-call parity, Black-Scholes from a risk-neutral expectation) and statistics (MLE, the German tank problem, Simpson’s paradox). The recurring shapes: A recurring move is the risk-neutral expectation : price a derivative as the discounted expected payoff under the risk-neutral measure, then check it against a no-arbitrage relation like put-call parity. Expect to set up and solve these from first principles. How to approach: State assumptions out loud, build the smallest model that captures the payoff (one-step binomial before continuous time), and sanity-check with parity or a boundary case. For the brain-teasers, find the invariant or the clean recursion before reaching for arithmetic — interviewers grade the reasoning, not just the number. The difficulty spread runs from a few easy warm-ups through a medium core to several hard derivations, mirroring a real UBS quant loop. ### Valkyrie Trading interview questions URL: https://quantvault.org/valkyrie-trading-interview-questions.html A representative practice mix for Valkyrie's Quant Trader loop: fast probability and EV mental math, fair-value and bid-ask quoting, Kelly bet sizing, options payoffs, and classic brain-teasers. Valkyrie Trading is a small Chicago-based options market maker and proprietary trading firm. Its Quant Trader interview leans on quick probability and expected-value reasoning, fair-value and quoting intuition, and disciplined bet sizing — the math you actually use while making markets. What they test: Core: fast probability and expectation in your head (card/dice odds, coin-flip races, payout-conditional EV, as tagged here). And fair-value and market-making (quote two-sided markets, read the book, price simple option payoffs) plus Kelly bet sizing . The recurring shapes: Expect EV with optimal stopping (re-roll a die, when to take a wait), quoting against adverse selection (Glosten-Milgrom-style bid/ask), fair value before an event (a binary outcome you must price), and de-biasing a coin / simulating fairness . These mirror the daily job: estimate an edge, size it, and quote it without getting picked off. How to approach: Anchor every answer in a clean expected value and a number you can defend out loud, then say which way you'd skew or widen a quote when information is one-sided. Keep arithmetic tight and state assumptions — at a small MM, the signal is how cleanly you reason under time pressure, not how exotic the model is. The mix is mostly medium with a handful of easy warm-ups and one hard curveball, matched to how an options market maker actually interviews. ### Voleon Group interview questions URL: https://quantvault.org/voleon-interview-questions.html Machine-learning, statistics, regression, probability, and linear-algebra problems drawn from Voleon Quant/ML Researcher interviews, plus the coding tasks that back them. Voleon Group is a Berkeley-based machine-learning-first systematic hedge fund that applies statistical learning to financial markets. Its Quant/ML Researcher interviews are unusually regression-heavy and probe whether you can reason from first principles about statistics, ML, probability, and linear algebra — not just recite results. What they test: The core is regression and statistics : deriving the OLS estimator and when its closed form exists, assumptions and violations, L1 vs. L2 regularization, R-squared and reverse regressions. Around it: machine-learning (KNN bias-variance, choosing K, regression-tree splits, logistic-regression perturbations, k-means) and probability / expectation (normals conditioned on a sum, dice, coins, random walks). Linear algebra binds it: out-of-core OLS, hyperplanes, structured matrices. The recurring shapes: Expect derive-and-bound questions: write an estimator, then say when it's defined, what it converges to, or how a perturbation moves it. Several use two-sided regression (Y-on-X vs X-on-Y slopes, joint vs pairwise R-squared bounds). A second strand, scalable numerics , suits a fund shipping ML at scale: out-of-memory OLS, optimal gradient-descent step sizes on a quadratic, sparse-matrix and matrix-chain operations. How to approach: Be ready to do the algebra live and state assumptions aloud: when is X T X invertible, what regularization buys, how bias trades against variance. In coding (caches, interval merging, order-matching, pandas aggregation, sparse matrices), write clean, correct code with the right data structure, not clever tricks. In probability, show clean conditioning and expectation reasoning. The mix leans heavily medium, with a handful of hard coding-and-microstructure builds and a few easy warmups to settle in. ### Akuna Capital interview questions URL: https://quantvault.org/akuna-capital-interview-questions.html 45 real Akuna Capital interview problems: probability, expected value, market-making brain-teasers, card/dice games, and quant coding rounds. Akuna Capital is a Chicago-based options market maker built around derivatives technology and high-volume electronic trading. Its interviews lean hard on fast mental probability , expected-value reasoning, and the kind of card/dice/betting games that mirror pricing a two-sided market under uncertainty. What they test: Dominant: probability and expected value (coin-flip and dice games, card-hand odds, geometric races, waiting times) answered cleanly under time pressure. Also: betting and game-theory on acting on an edge (optimal thresholds, redraw-vs-switch, bankruptcy-risk payouts) and quick brain-teasers and combinatorics . Quant-Dev and QR loops add coding . The recurring shapes: Expect expected-flips / expected-rolls until a pattern set up as a small recurrence, card problems (first ace, three-of-a-kind, one-of-each-rank), and conditional probability traps like two-children or two-heads-given-at-least-one. Market-maker flavor shows in EV under a stopping rule (keep-or-reroll, repeat-stop dice) and betting-to-a-target strategy. The coding round runs LeetCode-style: rotated-array search, graph connectivity, swaps-to-sort. How to approach: Set up the small recurrence or condition-on-the-first-step argument out loud and commit to a number — Akuna rewards a fast, defensible estimate over a stalled exact derivation. For card and dice questions, lean on symmetry and indicator/linearity-of-expectation tricks. For the betting and game-theory problems, state the EV and the decision rule explicitly before optimizing the threshold. A mostly-medium set with a band of quick easies and one hard betting problem — calibrated to the speed-and-accuracy bar of an options trading floor. ### Aquatic Capital Management interview questions URL: https://quantvault.org/aquatic-capital-interview-questions.html A systematic-quant practice mix: statistical inference, regression and regularization, time-series, ML fundamentals, probability, and streaming coding. Aquatic Capital Management is a Chicago-based systematic quantitative hedge fund that runs model-driven strategies across global markets. Its Quant Researcher and Quant Developer interviews lean on statistical inference , regression and regularization , time-series , and clean numerical coding . What they test: Core: statistical estimation and inference (maximum likelihood, Bayesian updating, confidence intervals, bootstrapping a Sharpe ratio ). Regression and regularization (OLS/ridge/lasso, bias-variance, logistic regression), time-series (AR models, stationarity, mean reversion), machine-learning (cross-validation, overfitting, KNN), some online/streaming coding . The recurring shapes: Expect the bias-variance decomposition to surface again and again — in model selection, ridge shrinkage, and choosing K. A second recurring shape is online estimation : computing mean, variance, and covariance in one pass without storing the data. How to approach: Write down the estimator and its sampling distribution before reaching for a formula, and always state your assumptions (stationarity, independence, known vs. unknown variance). For the coding problems, derive the Welford-style update first, then translate it to numerically stable code. A mostly-medium set with a few easy warm-ups and a handful of hard estimation and time-series problems for depth. ### Balyasny Asset Management interview questions URL: https://quantvault.org/balyasny-interview-questions.html Multi-strategy quant questions from Balyasny QR/PM loops: probability and expectation, regression and statistics, ML cross-validation, portfolio optimization, and clean coding. Balyasny Asset Management is a multi-strategy hedge fund whose Quant Researcher and Portfolio Manager interviews probe whether you can move fluently from probability and statistics through regression and machine learning to the portfolio construction that turns a signal into a position. What they test: The core is statistical modeling : regression diagnostics (R-squared, adjusted R-squared, the F-test, ridge bias-variance, LASSO) and ML validation built for financial data (purged and walk-forward cross-validation, HV-block CV, Thompson sampling). Around it sit probability and expectation brain-teasers and a portfolio-optimization block (beta-neutral variance, PCA factor hedging, shrinkage covariance). The recurring shapes: Expect to defend a regression (an extra regressor's cost, when shrinkage helps, validating with overlapping labels), price a small probability or expected-value puzzle from first principles, and build a covariance-aware portfolio hedging factor risk. Steady clean coding (caches, order-statistics structures, prefix sums, arrays, decorators, a real-time trade ticker). How to approach: Lead with the estimator and its assumptions , then say explicitly how you would validate out-of-sample without leakage on dependent, overlapping financial data. For the probability and expectation questions, set up a clean state or recursion before computing. For optimization, keep covariance, factor exposure, and shrinkage straight, and be ready to code the idea end to end. Forty problems weighted toward hard and medium , with a handful of easy warm-ups, spanning probability, statistics, regression, machine learning, optimization, and coding. ### Bank of America interview questions URL: https://quantvault.org/bank-of-america-interview-questions.html 78 questions from Bank of America quant and strategist loops: derivatives pricing, time-series and statistics, market microstructure, stochastic processes, and clean coding. Bank of America's quant and strategist interviews sit on the sell-side : derivatives pricing and risk, applied statistics and time-series , and the market microstructure behind execution and quoting. Expect to derive, then implement. What they test: The biggest blocks are options-pricing / derivatives (Breeden-Litzenberger, variance-swap replication, discrete delta-hedging, Bachelier-vs-Black), statistics (sequential and A/B testing, VaR backtesting, FDR control, extreme-value tails), and time-series (cointegration, Kalman/HMM filtering, unit-root and structural breaks). Around them sit market microstructure (Glosten-Milgrom, Roll spread, Hasbrouck shares) and a steady run of algorithmic coding on streaming and array problems. The recurring shapes: Risk and tail estimation recur constantly: VaR / Expected Shortfall via peaks-over-threshold, Kupiec-Christoffersen backtests, and Hill / GEV extremes. So does online estimation over streams (EWMA, Welford covariance, rolling percentiles, reservoir sampling). Pricing questions lean on the risk-neutral density and replication identity How to approach: State the model and assumptions first (measure, error structure, what's held fixed), then reach a closed form or estimator before coding. Streaming/coding: a correct one-pass or O(n log n) solution, explicit on numerical stability. Tie derivations to P&L and risk ; strategist and QR loops reward it. The mix skews hard with a medium core and a handful of easier warm-ups, so budget time for the derivation-heavy derivatives and statistics questions. ### Barclays interview questions URL: https://quantvault.org/barclays-interview-questions.html 72 real Barclays quant and strategist questions spanning derivatives pricing, statistics and time-series, market microstructure, and algorithmic coding. Barclays is a global investment bank whose quant and strategist desks sit at the seam of derivatives pricing , statistical modeling , and production code . Interviews for Quant Analyst and Strategist roles favor depth in stochastic calculus and econometrics over speed puzzles. What they test: The three pillars are nearly even. Options pricing & derivatives runs deep — put-call parity with dividends, American early exercise, barrier in/out parity, butterfly/convexity arbitrage, Merton jump-diffusion, variance-swap replication, and Almgren-Chriss execution. Beside it, a heavy statistics & time-series core (MLE, Bayesian updating, GARCH/EWMA, cointegration, CUSUM/change-point) and a real algorithmic coding bar (selection, segment trees, monotone deques, streaming quantiles). The recurring shapes: Expect no-arbitrage reasoning on option surfaces (convexity in strike, in/out parity, ETF creation-redemption thresholds), likelihood and posterior machinery (GBM drift/vol MLE, Gamma-Poisson and g-prior Bayes, no-trade-event updates), and market-microstructure models where Glosten-Milgrom zero-profit quotes and inventory-aware skew show up. Coding rounds want clean, correct algorithms, not tricks. How to approach: Lead with the economic invariant (replication, hedging error, zero-profit condition), then formalize it — write the SDE, the log-contract, or the likelihood and turn the crank. For the stats blocks, be fluent moving between estimator, its variance (HAC/block-bootstrap), and a test statistic. For coding, state the invariant and complexity before you type. Roughly half the set is hard — dense derivatives and econometrics — with a solid medium layer of pricing, Bayesian, and coding problems and a few easy warm-ups. ### Belvedere Trading interview questions URL: https://quantvault.org/belvedere-trading-interview-questions.html 60 real Belvedere Trading interview questions for Quant Trader and Quant Researcher roles: options pricing, probability, expected value, market microstructure, fast mental math, and brain-teasers. Belvedere Trading is a Chicago-based options market maker specializing in equity-index options. Its interviews probe whether you can price under uncertainty and quote fast — leaning on probability, expected value, and the mechanics of a two-sided market rather than memorized formulas. What they test: Core: probability and expected value (dice, cards, coins, posterior updates, pattern races) plus a market-making block : fair value off the order book, adverse selection, quoting under flow. An options-pricing strand covers Black-Scholes, butterfly/convexity arbitrage, the forward curve; plus brain-teasers and fast mental math . The recurring shapes: Compute an expectation by conditioning (tosses to a pattern, match-box balls, coins after pairing), update a posterior (double-headed coin, a buy print moving fair value), reason about convexity and no-arbitrage across option strikes. Coding is interview-floor data structures: order-book ladders, as-of joins, running best-trader aggregations. How to approach: Quote a number with a tight, defensible range and say how you'd update it — that mirrors making a market. State your assumptions out loud, lean on linearity of expectation and conditioning before brute force, and for the microstructure and options problems, anchor on no-arbitrage and adverse selection rather than reaching for a closed form. A medium-leaning set with a meaningful hard tail in options pricing, game theory, and stochastic reasoning, plus easy mental-math warmups to build speed. ### BlackRock interview questions URL: https://quantvault.org/blackrock-interview-questions.html A practice mix for BlackRock's systematic and quant roles: portfolio risk, factor regression, statistics, time-series, probability, and options pricing. BlackRock is the world's largest asset manager, and its quant interviews reflect a portfolio-and-risk house built on the Aladdin platform. Expect questions that connect statistics and regression to real portfolio construction, factor modeling, and risk measurement rather than pure trading puzzles. What they test: The center of gravity is portfolio risk and construction — mean-variance and minimum-variance portfolios, the efficient frontier, VaR and expected shortfall, and Sharpe aggregation. Around it sit factor regression (OLS, omitted-variable bias, ridge/lasso shrinkage, R²) and a statistics core covering confidence intervals, tracking error, and multiple-testing pitfalls in backtests. The recurring shapes: Many problems reduce to a covariance matrix : diversification across correlated assets, the variance of a weighted combination, and how correlation drives portfolio volatility. A second recurring shape is signal vs. noise — estimating returns from short samples, separating systematic from idiosyncratic risk, and time-series tools like GARCH volatility and cointegration for pairs. How to approach: Lead with the risk/return objective and write the estimator cleanly before computing. Be precise about what a confidence interval or VaR actually claims , watch for omitted-variable bias and overfitting when adding factors, and always sanity-check that your portfolio weights and annualization assumptions are consistent. The set leans medium, with easy fundamentals on risk and regression and a few hard problems on alpha backtesting, return-CI estimation, and full mean-variance optimization. ### Bloomberg interview questions URL: https://quantvault.org/bloomberg-interview-questions.html A fintech quant practice mix for Bloomberg's Quant Researcher and Quant Developer loops: data-structure and algorithm coding, probability, statistics, brain-teasers, and core finance. Bloomberg builds the terminal and analytics that move global markets, and it hires both Quant Researchers and Quant Developers . Its interviews pair a strong data-structures-and-algorithms coding bar with probability , statistics , and the finance fundamentals behind risk and pricing. What they test: Largest block: algorithmic coding , Bloomberg's classic developer screen (string and array DP, sliding windows, linked lists, streaming, sampling). Then probability and expectation (Bayes, gambler's ruin, coupon collector, optimal stopping), statistics (MLE, power vs. significance, estimation), and research-track finance : VaR, expected shortfall, Kelly sizing. The recurring shapes: Coding rounds reward a clean optimal solution and crisp complexity reasoning — dynamic programming for edit distance, two-pointer and heap tricks for medians and window minima, constant-space streaming. Quant rounds lean on conditioning and expectation, and on translating a market question (volatility, tail risk, bet sizing) into the right estimator or formula. How to approach: For coding, state the brute force, then the invariant that buys the better bound, and code it without hand-waving the edge cases. For quant, set up the conditioning or the recurrence explicitly before computing, and know the finance definitions cold — e.g. VaR as a quantile of the loss distribution. The mix leans medium with a solid easy foundation and a few hard stretches, mirroring Bloomberg's broad coding-plus-quant loop. ### Chicago Trading Company (CTC) interview questions URL: https://quantvault.org/chicago-trading-company-interview-questions.html An options market-making practice mix for CTC's Quant Trader and Quant Researcher loops: option pricing and Greeks, bid-ask quoting under adverse selection, probability, EV, and fast brain-teasers. Chicago Trading Company (CTC) is a Chicago-based options market maker and proprietary trading firm. Its Quant Trader and Quant Researcher interviews emphasize option pricing intuition , quoting under adverse selection , and quick, clean probability and expected-value reasoning under time pressure. What they test: The core is options — put-call parity and arbitrage, one-step binomial pricing, digital and straddle Greeks, and short-option P&L. Sitting alongside it is market-making : Glosten-Milgrom zero-profit quotes and pricing against an informed trader. The rest is a probability / expectation / brain-teaser base — Gambler's Ruin, St. Petersburg, EV-vs-Kelly bet sizing, and classic puzzles. The recurring shapes: Many problems reduce to a few moves: replicate a payoff and invoke no-arbitrage, set a quote so expected profit is zero given who might trade against you, or compute a clean expectation and compare it to a price. Put-call parity is the workhorse for the option questions. How to approach: Quote a two-sided market fast, then defend it: state your fair value, your edge, and how an informed counterparty shifts your bid and ask. For pricing, anchor on replication and parity before reaching for formulas; for EV puzzles, compute the number first and only then debate whether you'd take the bet. The set leans medium with a handful of easy warm-ups and a few hard market-making questions, mirroring a CTC loop that rewards speed plus sound option intuition. ### Citi interview questions URL: https://quantvault.org/citi-interview-questions.html 74 problems from Citi quant and strat loops: fixed-income derivatives pricing, rates and curve risk, probability and expectation puzzles, and clean algorithmic coding. Citi is a global investment bank whose quant and strategist interviews lean heavily into fixed-income and rates derivatives , the curve and covariance risk behind a desk's book, and the probability and algorithmic coding that screen for clean quantitative reasoning. What they test: The largest block is derivatives and options pricing : swaptions and caplets under Black's model, FX forwards and digitals, CDS and defaultable-bond spreads, arbitrage-free implied-vol surfaces. Around it: algorithmic coding (streaming medians, DAG scheduling, subarray DP), probability, expectation, and Bayesian updating puzzles, linear algebra for PCA and covariance, time-series and optimization for portfolios and hedging. The recurring shapes: Rates and curve risk: DV01 and key-rate hedging , duration/convexity, bootstrapping discount or OIS curves from par swap rates. Pricing: no-arbitrage, risk-neutral valuation. Covariance: fix nearly-PSD matrices, shrink correlation estimates, explain the flat top PCA eigenvector of rates covariance. Probability: pattern races, exchangeability, macro-regime posterior updates. How to approach: Canonical tool first: put-call parity and replication before a model, memorylessness and exchangeability before brute-force counting, Bayes with explicit priors on signals. Tie rates hedges to a sensitivity (DV01, vega, convexity) and a no-arbitrage constraint. In coding, first name the structure making the streaming or graph problem linear. The mix is roughly a third hard, half medium, and a handful of easy warm-ups — weighted toward the medium-to-hard rates-and-derivatives questions that define a Citi quant loop. ### Flow Traders interview questions URL: https://quantvault.org/flow-traders-interview-questions.html Probability, expected value, fast mental math, ETP and market-making microstructure, and clean coding — the mix Flow Traders screens for in its trading and dev loops. Flow Traders is an Amsterdam-rooted algorithmic market maker in exchange-traded products (ETFs, ETPs, and increasingly crypto). Its interviews emphasize speed under uncertainty : tight mental arithmetic, expected-value reasoning, and an instinct for how two-sided quotes and ETP mechanics actually behave. What they test: The core is probability and expected value — fair-value bets, conditional updates, and expectation of dice, coins, and stopping rules. Around it sits a market-making and ETP layer (two-sided quotes with overround, make-or-take decisions, leveraged-ETF rebalancing, arbitrage and price impact) and a clean-coding block for the developer track, from prefix sums to an order-matching engine. The recurring shapes: Expect quick EV games (guess-the-suit, lopsided-coin bets, roll-until-exceed), Bayesian updates from a trade or a noisy signal, and fast mental math brain-teasers (powers mod a prime, ratios near one-half, sequence puzzles). The finance set leans concrete: overround in a two-sided market, delta and path-dependence, VaR, and how a 3x oil ETF drifts as it rebalances. How to approach: Quote fast , then defend: fair value, edge size, adverse selection before you widen. Coding: obvious correct solution first (two heaps for streaming median, modified merge sort for inversions), then optimize. Narrate the arithmetic and the estimate. The mix leans medium, with a strong band of easy mental-math and EV warm-ups and a hard tail in options-pricing, Gaussian probability, and the order-matching-engine coding problem. ### Goldman Sachs interview questions URL: https://quantvault.org/goldman-sachs-interview-questions.html Strats and quant-research style problems from Goldman Sachs: probability, expectation, stochastic calculus, options pricing, combinatorics, and clean coding. Goldman Sachs runs one of the deepest strats and quantitative research benches on the Street, where desk-facing pricing intuition meets rigorous mathematics. Its interviews lean on probability , expectation , and the stochastic calculus behind derivatives, with brain-teasers and clean coding to round out the picture. What they test: Core (largest): probability and expectation (conditional expectation, hitting/stopping times, card/dice setups, permutation fixed points). Plus stochastic-calculus and pricing (Brownian motion, integrated-BM variance, a Black-Scholes walkthrough), quant-stats (CLT, filtrations, regression, PCA), combinatorics brain-teasers and clean coding for strats and QR candidates. The recurring shapes: Expect Brownian-motion algebra (covariance of sums, autocorrelation, integrated-process variance) and hitting/stopping-time arguments via one-step or martingale recursion. Black-Scholes is to reason about , not quote: where its assumptions break, how desks patch them. Combinatorics and card problems reward clean counting frames over brute force. How to approach: Set up the right state variable first (condition on a sum, first step or stopping rule) before grinding algebra. Stochastic problems: use defining properties (independent increments, E [W s W t ] = min(s,t)), not memorized results. Treat pricing and CLT questions as conversations: assumptions, where they fail, the fix. Coding: crisp and correct. The mix leans medium (19 of 30), with a handful of easy warm-ups and two genuinely hard problems — closest-to-center geometry and integrated-Brownian-motion variance — to separate the field. ### GSA Capital interview questions URL: https://quantvault.org/gsa-capital-interview-questions.html A systematic stat-arb practice mix: regression and regularization, statistical inference and signal validation, time-series modeling, ML, and stochastic processes. GSA Capital is a London-based systematic quantitative hedge fund that trades a diversified book of statistical-arbitrage and managed-futures strategies. Its interviews for Quant Researcher and Quant Developer roles lean hard on statistical inference , regression and regularization , time-series modeling , and the discipline of telling real signal from noise. What they test: The core is statistical inference and regression : deriving the OLS estimator, reasoning about omitted-variable bias and errors-in-variables, and choosing between ridge and lasso when predictors are collinear or outnumber observations. Around it sits a time-series block (GARCH volatility, cointegration, Kalman filtering, Yule-Walker/PACF) and a signal-validation block (multiple-testing fallacies, cross-validation leakage, Sharpe-ratio evaluation). The recurring shapes: Many questions probe the same instinct: a fitted number looks impressive, so why might it be spurious ? Expect the German-tank estimator, the n-1 variance correction, mean-reversion and stationarity tests, and Markov-chain steady states. The unifying skill is quantifying estimator bias and variance before trusting a backtest. How to approach: State your modeling assumptions out loud, then check what breaks them — heteroskedasticity, autocorrelation, look-ahead, p-hacking. Reach for regularization and out-of-sample discipline rather than chasing in-sample fit, and be ready to defend a Sharpe ratio against the multiplicity of strategies you tried. The mix runs mostly medium with a handful of easy warm-ups and genuinely hard time-series and inference problems. ### GTS interview questions URL: https://quantvault.org/gts-interview-questions.html 34 questions spanning probability, expectation/EV, coding and algorithms, market-microstructure statistics, and brain-teasers, mirroring GTS's electronic market-making interviews. GTS is a New York-based electronic market maker that quotes across equities, ETFs, options and FX with low-latency systematic strategies. Its interviews pair fast probability and expected-value reasoning with clean algorithmic coding and just enough statistics to reason about order flow and signal. What they test: The biggest block is probability and expected value -- conditional-probability puzzles, pattern races between coin sequences, fairness tests, and pricing simple wagers by their EV . Around it sits a solid coding and algorithms core (array DP, sliding-window rank, linked lists, a little C++ semantics) and a smaller statistics strand on sampling, correlation and informed flow. The recurring shapes: You will price a game by its expected payoff , decide whether observed data should move your belief (is the coin fair after five heads?), and translate a microstructure question -- informed vs. uninformed flow -- into an estimation problem. On the engineering side, expect maximum-subarray / best-trades DP , partitioning and covering problems, and order-statistics over a moving window. How to approach: State assumptions out loud, write the EV as a clean sum, and reach for conditioning, symmetry, and indicator variables before brute force. For coding, name the invariant first (what each DP state means) and keep it O(n) where you can -- GTS cares that the quote is both correct and fast. The mix leans medium with a handful of genuinely hard probability and algorithm problems and several quick easy warm-ups to settle the EV and coding fundamentals. ### Headlands Technologies interview questions URL: https://quantvault.org/headlands-interview-questions.html 56 Headlands-tagged problems spanning probability and brain-teasers, expectation/EV, statistics and regression, and production-grade algorithmic coding for quant research and dev roles. Headlands Technologies is a global quantitative and algorithmic trading firm whose edge is fully systematic. Its interviews probe whether you can reason cleanly under uncertainty and turn that reasoning into correct, efficient code — blending probability , statistics , and algorithmic engineering . What they test: Largest: algorithmic coding (streaming data structures, sliding-window medians, as-of joins, matching-engine and order-management design), low-latency, correctness-critical. Also probability and brain-teasers (optimal stopping, random walks, dice/card EV) and statistics & regression (Bayesian updating, AR(1)/unit roots, ridge/lasso, OLS vs. total least squares). The recurring shapes: Many questions reward an online / streaming mindset: compute a running statistic in one pass rather than recomputing it. Welford-style covariance and rolling medians are the canonical examples — keep summary state and update it as each element arrives. How to approach: State assumptions out loud, then drive to the cleanest exact answer: set up an expectation or recursion for the probability puzzles, name the estimator and its bias/variance for the stats problems, and lead with complexity on the coding ones. Headlands cares as much about how you reason as the final number, so narrate the trade-offs. The mix leans medium, with a meaningful block of hard streaming-coding and stochastic problems and a handful of easy warm-ups to get rolling. ### JPMorgan Chase interview questions URL: https://quantvault.org/jpmorgan-interview-questions.html 95 quant questions from JPMorgan strats/QR loops: options pricing and derivatives, stochastic calculus, regression and time-series, probability, and brain-teasers. JPMorgan Chase runs one of the largest sell-side quant franchises, and its Quant Researcher and Quant Strategist loops lean into derivatives pricing and the stochastic calculus beneath it, backed by a heavy dose of statistics , regression , and time-series modeling. Expect to derive, not just recall. What they test: Largest single block: options pricing and derivatives (Black-Scholes, put-call parity arbitrage, Greeks, delta-hedging, Breeden-Litzenberger, risk-neutral martingales) on a stochastic-process core (Brownian motion, first-passage/hitting times, Feynman-Kac); econometrics : regression (OLS, ridge/LASSO, endogeneity, IV) and time-series (ARMA, cointegration, GARCH, realized vol). Openers: probability, expectation, and brain-teasers . The recurring shapes: Risk-neutral pricing : show the discounted stock is a martingale, price by expectation. Hedging and replication : cancel a Greek or statically replicate a payoff. Estimator reasoning : state one (OLS, MLE, Bayesian posterior), prove a property, stress it under endogeneity, multicollinearity or heavy tails. Warm-ups: Expected-time setups (run tosses, hitting times, first passage). How to approach: Lead with assumptions: name the measure, model and conditioning before computing. For derivatives, sanity-check with no-arbitrage and put-call parity ; for econometrics, say which OLS assumption could break and how you'd detect it. On brain-teasers, set up recursion or symmetry cleanly, not brute force: the derivation is graded, not just the number. The mix leans medium (48) with a substantial hard tail (32 derivations-heavy pricing and econometrics problems) and 15 easy warm-ups. ### Marshall Wace interview questions URL: https://quantvault.org/marshall-wace-interview-questions.html A systematic-equity practice mix: regression and factor models, signal evaluation, ML pipeline judgment, time-series, and probability foundations. Marshall Wace is a London-based systematic equity hedge fund known for combining a large number of weak, low-correlation alpha signals into market-neutral portfolios. Its quant interviews lean on statistics , regression and factor models , and disciplined out-of-sample judgment rather than exotic puzzles. What they test: The core is cross-sectional regression and factor modeling — OLS, ridge/lasso shrinkage, omitted-variable bias, Fama–MacBeth, and how to blend correlated signals into one alpha. Around it sits statistical inference for signals (Sharpe, multiple-testing/false-discovery, online estimators) and time-series (mean reversion, AR(1), GARCH). The recurring shapes: Expect questions where the trap is overfitting or leakage : false discovery when mining many signals, look-ahead in financial cross-validation, and shrinkage of a noisy estimate toward a prior. They probe whether you can size and combine signals when each one barely clears noise. How to approach: Lead with the estimator and its assumptions , then ask what biases the data structure introduces (correlation, endogeneity, time dependence) and how you would validate honestly out of sample. Keep the probability fundamentals (normal joint distributions, variance) crisp — they anchor everything above. The mix leans medium, with a handful of hard estimation problems and a few easy fundamentals to warm up. ### PDT Partners interview questions URL: https://quantvault.org/pdt-partners-interview-questions.html 53 PDT Partners questions spanning probability, statistics, regression, stochastic processes, time-series, ML, linear algebra, and clean algorithmic coding. PDT Partners is an elite systematic quantitative hedge fund spun out of Morgan Stanley's Process Driven Trading group, where signals are built from data and held to statistical scrutiny. Its interviews run deep on probability and statistics , regression and time-series modeling, applied machine learning , and clean algorithmic coding . What they test: Core: research-grade statistics and regression (OLS derivation, ridge/LASSO closed forms, multicollinearity/VIF, heteroskedasticity, residual diagnostics, least squares geometry). Plus probability and expectation (random walks, gambler's ruin, coupon collector, pattern-before-pattern), time-series and risk (EWMA variance, stationarity, VaR backtests, block bootstrap), machine learning on noisy market data, algorithmic coding . The recurring shapes: Expect to fit and interpret a linear model from scratch and defend its assumptions (what collinearity breaks, what shrinkage buys, reading coefficients and residuals). Probability: stopping-time and first-passage arguments, clean expectation . Data is deliberately noisy : find a real signal, calibrate it, judge if a Sharpe improvement is statistically detectable. How to approach: Reason from first principles, assumptions out loud. Stats: write the estimator, differentiate, connect the algebra to a picture (projection, eigenvalues, half-life). Probability: set up the recursion or conditioning before any formula. Coding: correct, readable solutions, right data structure; PDT values clean engineering as much as clever math. The mix leans medium with a substantial hard tail in the regression, statistics, and stochastic-process sections, plus a handful of easy warm-ups. ### Point72 / Cubist interview questions URL: https://quantvault.org/point72-interview-questions.html Quant researcher and data scientist interview problems from Point72 and its systematic arm Cubist: probability, statistics, regression, ML, brain-teasers, and clean coding. Point72 is Steve Cohen's multi-strategy hedge fund, and Cubist is its systematic arm. Its Quant Researcher and Data Scientist loops emphasize statistical reasoning you can defend out loud: probability , regression and estimation , a working grasp of ML regularization , and clean coding . What they test: The core is probability and expectation — Markov-chain hitting times, optimal stopping, and quick fair-value pricing of simple bets. Around it sits a distinctive statistics and regression block (variance computations, mean/median/mode ordering, slope algebra, collinearity) plus ML intuition on bagging and LASSO. A real coding set rounds it out, and brain-teasers screen for structured thinking. The recurring shapes: Many problems reduce to a few moves: set up a recurrence or first-step decomposition for an expected count, exploit symmetry to collapse cases, or reason about an estimator's bias and variance . The regression questions reward knowing that a slope is just a scaled correlation, and the ML questions reward knowing what regularization does to collinear or redundant predictors . How to approach: Narrate the model first (distribution, conditioning or recurrence), then a clean closed form. Systematic: tie a statistical answer back to signals and data (estimator consistency, stationarity, what LASSO keeps). Coding: correct, readable, complexity stated. Thirty problems leaning medium, with a spread of easy warm-ups and a handful of hard regression and coding questions to separate candidates. ### Qube Research & Technologies interview questions URL: https://quantvault.org/qube-research-technologies-interview-questions.html A systematic multi-asset quant practice mix: statistics and false-discovery, regression and bias, ML pipelines, time-series and factor models, plus streaming coding. Qube Research & Technologies is a systematic, multi-asset quantitative hedge fund that trades signals built and validated entirely from data. Its interviews for Quant Researcher and Quant Developer roles lean on applied statistics , regression , machine learning , and time-series , paired with clean streaming-data coding . What they test: The core is statistical inference and regression : estimation, omitted-variable and bias-variance trade-offs, and the multiple-testing / false-discovery traps that wreck a backtest. Around it sit ML model design (regularization, cross-validation, leakage), time-series (GARCH, cointegration, Kalman filtering of latent signals), and linear algebra for covariance and factor structure. The recurring shapes: Signals that look real but are selection-biased (Sharpe-ratio inflation, alpha-mining false discovery), covariance estimation that must stay positive-semidefinite and well-conditioned (PCA on returns, shrinkage), and mean-reversion structure pulled from noisy series. On the developer side, expect streaming statistics — online covariance, rolling medians and VWAP computed in one pass over a data stream. How to approach: State the estimator and its assumptions before computing, then ask what breaks out of sample — leakage, overfitting, or a multiplicity correction. For coding, reach for the canonical streaming structures (two-heap medians, Welford updates, sliding windows) and reason about time and space explicitly. Tie the math back to a trading decision wherever the prompt invites it. The set spans easy warm-ups through hard inference and time-series questions, with the bulk sitting at interview-realistic medium difficulty. ### Schonfeld Strategic Advisors interview questions URL: https://quantvault.org/schonfeld-interview-questions.html 43 quant-researcher and PM questions spanning probability, statistics, regression, ML, finance, and brain-teasers as tested across Schonfeld's multi-strategy pods. Schonfeld Strategic Advisors is a multi-strategy hedge fund whose quant pods hire researchers and PMs to build and trade systematic signals. Its interviews pair sharp probability and brain-teasers with a deep, applied statistics and regression core, plus the ML and finance tooling a working alpha researcher actually uses. What they test: Center of gravity: statistics and regression (multiple testing via Benjamini-Hochberg FDR, structural breaks via Chow and CUSUM, multicollinearity, Gauss-Markov / BLUE, omitted-variable bias). Then probability and expectation brain-teasers (airplane boarding, gambler's ruin with Kelly, longest head-streak) and machine-learning pitfalls: CV leakage in time series, sparse L1 signals, trading very noisy data. The recurring shapes: Expect to defend a model choice, not just compute: when OLS is and isn't optimal and the fix (GLS, quantile regression, 2SLS under overidentification), and guarding inference against data-snooping when testing many signals at once. The finance thread (Fama-French factors, duration/convexity, futures-vs-forwards convexity adjustment) links statistics to real instruments. How to approach: Lead with the estimator's assumptions and where they fail , then derive the fix. For probability puzzles, set up the state or symmetry first (gambler's ruin, parity of biased coins, expected minimum of dice). For signals, name the leakage or overfitting trap first: the interviewer probes whether you'd catch it in production, not recite a formula. The mix leans medium with a substantial hard tail in regression, statistics, and finance, plus a handful of easy warm-ups to set the table. ### Teza Technologies interview questions URL: https://quantvault.org/teza-technologies-interview-questions.html A practice mix for Teza's quant-research and quant-dev loops: probability and expected value, streaming/array coding, statistics, and balance-scale brain-teasers. Teza Technologies is a quantitative proprietary trading firm running high-frequency and systematic strategies. Its loops split across Quant Researcher and Quant Developer tracks, so interviews pair probability and statistics with clean, fast coding on streaming and order-book-shaped data. What they test: Two pillars: quantitative coding and probability / expected value . Coding: streaming and array problems (rolling medians, online mean/variance, reservoir sampling, sorted-structure search) over a live feed. Math: random walks, stopping rules, and waiting times , backed by statistics (estimation, multiple-testing, sampling-bias) as an alpha team vets a signal. The recurring shapes: Expect optimal-stopping and gambler's-ruin setups, coupon-collector and random-walk-on-a-graph expectations, and order-statistic reasoning over a stream. On the research side, watch for false-discovery / multiple-testing traps in backtests and classic estimators like the German tank problem. A representative gambler's-ruin identity for a fair walk between 0 and N: How to approach: Coding: state the data structure first (two heaps for a rolling median, a single accumulator for online variance), O(1) or O(log n) per update ; latency is the point. Probability: recursion or indicator/linearity-of-expectation before algebra; name the collapsing symmetry . Brain-teasers (twelve balls, two hourglasses, ranking horses): information-counting , not cleverness. The mix leans medium with a strong block of hard problems and one easy warm-up, mirroring a loop that pushes on both research depth and engineering speed. ### TransMarket Group interview questions URL: https://quantvault.org/transmarket-group-interview-questions.html A prop-trading practice mix for TransMarket Quant Trader interviews: probability, expected value, fast mental math, brain-teasers, market-making games, and a little coding. TransMarket Group is a Chicago-based proprietary trading firm trading on its own capital across global markets. Its Quant Trader interview leans on quick probability and expected-value reasoning, sharp mental math , classic brain-teasers , and trading-flavored game theory , with a little practical coding . What they test: Core: probability and expected value (dice, coins, gambler's ruin, stopping), reasoned fast and aloud. Brain-teasers and estimation (weighings, burning ropes, comparison tricks) probe structured thinking under pressure; market-flavored game theory : bet sizing, beauty-contest guessing, market-making games mirroring how the desk sees edge. The recurring shapes: Expect to compute an expected value on the spot and decide whether a bet is worth taking — the Kelly-vs-EV tension shows up directly. Stopping problems (the three-roll dice game, the bus-waiting paradox) and the St. Petersburg payoff test whether you can value a stream of uncertain outcomes rather than just average them. How to approach: Narrate your reasoning and sanity-check magnitudes before committing to a number — a prop desk cares as much about your process as the answer. State assumptions, lean on symmetry and conditioning, and for the coding questions (trapping rain water, optimal stock trades) write clean, correct logic and reason about complexity out loud. The set spans easy warm-ups to meatier medium problems, leaning medium to match the live, think-aloud feel of a prop-trading interview. ### Virtu Financial interview questions URL: https://quantvault.org/virtu-interview-questions.html 81 real Virtu-tagged questions for Quant Trader and Quant Developer loops: probability, expected value, fast coding, and market-microstructure brain-teasers. Virtu Financial is a global electronic market maker that quotes thousands of instruments across venues at high frequency. Its Quant Trader and Quant Developer loops reward fast, exact reasoning about probability and expected value , plus clean coding under time pressure. What they test: The two largest blocks are probability and expected value (roughly half the set): coin and dice games, urns, random walks, waiting times priced in your head. Then heavy developer-track coding (streams, parsing, matrix and array work), brain-teasers , light optimization , thin statistics / regression for research-leaning rounds. The recurring shapes: Linearity-of-expectation setups (handshakes, loops, surviving counts), absorbing and reflecting random walks, waiting times for patterns or runs of heads. Microstructure-adjacent trading: fair value of a basket ETF , max profit from buy/sell orders, reservoir sampling an unbounded feed. A clean expectation argument usually beats brute force: How to approach: State assumptions, then reach for the lightest tool: indicators for counting expectations, first-step / state recursions for walks and games, and a quick EV vs. variance check before committing to a bet. For the coding items, narrate a correct brute force, then tighten to the right data structure (heap, hash map, deque) and call out the time complexity out loud. The mix leans medium with a solid easy on-ramp and a dozen genuinely hard walks, runs, and parsing problems to separate the field. ### Walleye Capital interview questions URL: https://quantvault.org/walleye-capital-interview-questions.html Multi-strategy quant questions spanning probability and brain-teasers, statistics and regression, portfolio optimization, and clean data-handling coding. Walleye Capital is a multi-strategy hedge fund whose quant researcher and trader interviews probe whether you can reason cleanly under uncertainty and turn a noisy signal into a defensible estimate. Expect probability and brain-teasers alongside real statistics , regression , and portfolio optimization . What they test: Core: probability and combinatorics brain-teasers (coin races, Bayesian updates, dice EV/Kelly sizing, counting). Statistics and regression (why divide by n−1 , factor models, signal half-life, predictive-ability tests), portfolio optimization (mean-variance, top-K alphas under correlation limits), coding (streaming order statistics, trades, prices, paginated APIs). The recurring shapes: Watch for the same skeletons: conditioning and Bayes on a small state space, geometric/first-success races, and linearity of expectation to dodge brute force. On the data side it is OLS and factor diagnostics , variance estimators (including catastrophic cancellation ), and online quantiles over a sliding window. The optimization questions reward seeing the constraint that binds . How to approach: State your assumptions, set up the conditioning or constraint explicitly, then compute — they care about the reasoning path, not just the number. For stats and regression, name the estimator and its bias before plugging in. For the coding rounds, default to a streaming, O(1)-update structure and write code that ingests messy real-world inputs cleanly. The mix leans medium with a solid hard tail in stochastic and optimization problems and a handful of easy data-handling warm-ups. ### Wincent Capital Management interview questions URL: https://quantvault.org/wincent-capital-management-interview-questions.html A crypto algorithmic market-maker mix: bid-ask quoting and adverse selection, EV and Kelly sizing, probability brain-teasers, and order-book coding. Wincent Capital Management is a crypto algorithmic market maker , quoting two-sided liquidity across digital-asset venues. Its Quant Trader and Quant Developer interviews emphasize spread and inventory reasoning, EV and bet sizing , and clean order-book coding . What they test: The center of gravity is market microstructure — setting zero-profit bid-ask quotes, pricing adverse selection against informed flow, and skewing for inventory (Glosten-Milgrom, Avellaneda-Stoikov, cross-asset and DeFi-CeFi variants). Around it sit expectation and Kelly-style sizing for repeated bets, and a probability core of coin-pattern races, Bayesian updates, and gambler's-ruin edge. The recurring shapes: Turn a posterior into a fair value , then a quote: condition on an incoming trade's side, mark the break-even spread, size by Kelly or fractional Kelly . Pattern races ( HHT vs HTH ), gambler's ruin, St. Petersburg-style payoffs test process , not rote answers. How to approach: Quote from first principles — write the maker's expected P&L per fill, set it to zero, and only then round to a tick. For the Quant Developer track, expect to build a matching engine and reason about price impact and execution; keep the data structures clean and the edge cases (cancels, stops, partial fills) explicit. Mostly medium with a hard tail (five harder market-making and Kelly problems) — a realistic crypto-MM practice mix rather than a pure brain-teaser drill. ### Wintermute interview questions URL: https://quantvault.org/wintermute-interview-questions.html A crypto algorithmic-market-maker practice mix: quoting and adverse selection, order-book microstructure, probability and EV, and streaming/matching coding. Wintermute is a London-founded algorithmic market maker in crypto , quoting two-sided liquidity across hundreds of digital assets on centralized and decentralized venues. Its interviews emphasize market-making mechanics , probability and expected value , and clean algorithmic coding under latency and inventory constraints. What they test: The largest block is market-making and microstructure — quoting a bid-ask spread, the Glosten-Milgrom zero-profit condition, adverse selection, Avellaneda-Stoikov inventory skew, and order-book mechanics including DeFi/CeFi arbitrage and execution (VWAP/TWAP). Around it sits a probability and EV core (coin-pattern races, optimal stopping, bet sizing) and a coding layer of streaming and matching algorithms. The recurring shapes: A quote sits at fair value plus half a spread; the spread must cover the expected loss to informed flow. The break-even half-spread against a fraction of informed traders takes the recurring form below, and inventory pushes the quote midpoint away from fair by a risk-and-time penalty. How to approach: State your fair value first, then layer on adverse-selection and inventory adjustments before you quote. For the coding rounds, reach for the canonical streaming tools — a median-from-a-stream heap , reservoir sampling , quickselect , and a FIFO P&L / order-matching engine — and reason about EV and Kelly sizing before committing capital. The mix leans medium with a strong hard tail in the market-making and execution problems, plus a few easy warm-ups on order-book intuition. ==================== FIRM INTERVIEW PROCESSES, ROUND BY ROUND ==================== ### Akuna Capital interview process URL: https://quantvault.org/akuna-capital-interview-guide.html Type: Proprietary trading firm / options market maker | Founded: 2011 | HQ: Chicago, IL | Roles: Quant Trader, Software Engineer, Quant Researcher | Last reviewed: 2026-05-30 1. Online Assessment [Timed - math + coding] https://quantvault.org/akuna-capital-online-assessment.html A timed online assessment covering quantitative/probability questions and (for engineering) coding. Speed and accuracy both matter. 2. Round 3 — Recorded Video Game Round [Recorded video interview - timed letter-wager game] https://quantvault.org/akuna-capital-recorded-video-game-interview.html Akuna's round 3 for traders: a recorded video interview on one fast game, with a 20-90 second countdown per question. In a 13-row letter matrix, a random winner row sets each letter's value (its row minus the winner's row); price or pick signed letter wagers. Boards are staggered, disguised, holed or mostly hidden; some quiz memory of past boards and picks. Simulator below: two warm-ups first, then the real 23-question run, reshuffled per attempt. 3. Probability & Mental Math [45-60 min - video] https://quantvault.org/akuna-capital-probability-mental-math-interview.html The core quant rounds: probability, expectation, combinatorics, and fast mental math, with follow-ups. Dice, coins, and card EV classics are common. 4. Brain-Teasers & Game Theory [45-60 min - video] https://quantvault.org/akuna-capital-brain-teasers-game-theory-interview.html Brain-teasers, game theory, and decision-under-uncertainty problems -- bet sizing, fair value, and simple equilibria, plus the occasional Markov/stochastic setup. 5. Onsite - Hardest Rounds [Final rounds - the toughest reported problems] The hardest reported Akuna problems across every topic -- the ones most likely to separate candidates at the final stage. (These are the hard-rated problems; the earlier rounds cover the easy/medium set, so nothing here repeats.) ### AQR Capital Management interview process URL: https://quantvault.org/aqr-interview-guide.html Type: Systematic quant hedge fund | Founded: 1998 | HQ: Greenwich, CT | Roles: Software / Quant Developer, Quant Researcher, Risk Researcher | Last reviewed: 2026-05-29 1. Online Assessment [75–90 min · 3 problems · HackerRank / CodeSignal] https://quantvault.org/aqr-online-assessment.html The coding gate. The long-running 'classic 3': compute p^q faster than O(q) (fast exponentiation; watch int→long overflow), sort 1M integers in [1,10] (bucket/counting sort), and split a string at digit boundaries → sort each substring → concatenate. Later sittings randomize from a wider bank (jump-game DP, count islands, palindrome insertions, a hard range max-of-(max−min) that often TLEs). 2. Phone Screen [~1 hr · engineer / hiring manager] https://quantvault.org/aqr-phone-screen-interview.html Often resume + behavioral + CS/OOP concepts (encapsulation, polymorphism, SOLID, list vs set, sorting complexity, SQL indexes, many-to-many modeling) with frequently NO live coding, though sometimes a single maze / shortest-path link. 3. PnL-Calculator Take-Home [~1 week · design project] https://quantvault.org/aqr-pnl-calculator-take-home-interview.html The signature take-home: design and build a PnL calculator that parses two message files and computes P&L for long/short positions. Graded on object-oriented design, structure, code style, and comments, not just correctness. 4. On-Machine Onsite [2–3 hr project + 3–4 × 45-min rounds] https://quantvault.org/aqr-on-machine-onsite-interview.html A morning on-machine project, a Task Scheduler (Course-Schedule-II variant with per-task time windows + durations → topological sort + heap) reviewed by an engineer, then algorithm + behavioral rounds: binary search on a continuous range (find x for monotonic y=f(x)), tree paths summing to a target that may bend, a fixed-size O(1) insert/delete container, and standard graph/DP. 5. QR Technical Rounds [2 back-to-back · ~1 hr each] https://quantvault.org/aqr-qr-technical-interview.html Heavy statistical inference plus hands-on pandas/statsmodels, then a deep dive on your own research. OLS assumptions and what breaks them, deriving β, writing a covariance function, outlier analysis, and return-skewness reasoning (is this series reversed? markets skew negative, single names positive). 6. QR Superday [4 × 45-min (sometimes written exams + stress interview)] https://quantvault.org/aqr-qr-superday-interview.html A flown-out superday covering statistical inference, finance (fixed income + other asset classes), ML, your projects, and behavioral, sometimes with three written exams (finance / math / programming), six 30-min 1:1s, and a deliberate stress interview. Whiteboard coding is light (e.g. write a covariance function); DS&A is mostly conceptual. ### Aquatic Capital Management interview process URL: https://quantvault.org/aquatic-capital-interview-guide.html Type: Systematic quantitative hedge fund | Founded: 2018 | HQ: Chicago, IL | Roles: Quant Researcher, Software Engineer, Systems Engineer | Last reviewed: 2026-10-16 1. Recruiter / HR Call [Phone · background + fit] https://quantvault.org/aquatic-capital-recruiter-hr-call-interview.html An intro call covering background, motivation and logistics. Candidate-reported as the entry point for both research and engineering tracks. 2. Online Assessment [Timed · online (candidate-reported)] https://quantvault.org/aquatic-capital-online-assessment.html Candidates report an online assessment before the interview rounds; on the engineering side a Python assessment via CodeSignal is mentioned. We have no reliable public description of the research-track OA contents, so this round is editorial-only. We have not reconstructed a problem set we cannot source. 3. Take-Home — Theory + Data Exercise [Take-home · theory questions + a data task (candidate-reported)] https://quantvault.org/aquatic-capital-take-home-interview.html One candidate account describes a take-home split into a theoretical portion and a data exercise, with the theory centred on linear regression. Regression theory is the single most concrete piece of Aquatic-specific intel we have, so make it the priority. 4. Quant Interviews [2 rounds · with researchers (candidate-reported)] https://quantvault.org/aquatic-capital-quant-interview.html Two quant interviews follow the assessment. Round contents are not publicly documented, so this set is exactly the questions in our bank stamped as Aquatic-reported for these rounds: - a Chebyshev bound on a bivariate-normal difference - the variance of an exponentiated gamma - from a 2026 phone round, what OLS estimates when the slope's sign is a hidden coin flip It is deliberately not padded out with generic problems. 5. Virtual Onsite [Virtual onsite · multiple rounds (candidate-reported)] https://quantvault.org/aquatic-capital-virtual-onsite-interview.html A virtual onsite follows the quant interviews. We have no sourced breakdown of who you meet or how many rounds. Expect the hardest variants of the research stack plus a deep-dive on your own work; Aquatic's postings stress independent research and originating research topics over time. One 2026 candidate reports a final-round question on maximum entropy: over all densities on [0, 1], minimize the integral of f log f. That question is the practice set below. 6. Leadership Call [Conversational · senior leadership (candidate-reported)] https://quantvault.org/aquatic-capital-leadership-call-interview.html The reported final step is a leadership call. Aquatic is small and founder-led, so treat this as a real gate on judgement and fit rather than a formality. ### Arrowstreet Capital interview process URL: https://quantvault.org/arrowstreet-capital-interview-guide.html Type: Systematic global equity manager | Roles: Quantitative Researcher, Quant Developer | Last reviewed: 2026-09-06 1. Online Assessment — Quant Research Screen [HackerRank-style test - 11 numeric questions + econometrics MCQs, ~4 min per question] https://quantvault.org/arrowstreet-capital-online-assessment.html Timed HackerRank-style screen: 11 mostly numeric 'complete the blanks' questions on portfolio math, probability and constrained optimization (rounding rules like 'rounded down to the nearest integer'), plus single/multi-select econometrics MCQs on regression specification and time-series models. Verified timed replica: OA page. 2. HackerRank CodePair — Live Coding Screen [Live HackerRank CodePair session - Python/pandas concept questions interleaved with two LeetCode easy/medium problems] https://quantvault.org/arrowstreet-capital-hackerrank-codepair-interview.html Candidate-reported (several 2025-26 accounts): CodePair, 'interactive but really just doing problems': conceptual Python questions plus two LeetCode easy/medium problems (the one remembered: 'count the words in a list that have exactly k distinct letters'). Slicing (negative indices, out-of-range bounds, views vs copies, list/NumPy/pandas indexing); @staticmethod vs @classmethod (what each receives, instance/class calls, inheritance with cls). Pandas: merge (join types, keys, duplicated keys), groupby (agg/transform/apply), rolling (window semantics, min_periods, centered), resample (up/down-sampling, label, closed). The must-do set below rebuilds all of it on small frames with verified outputs, plus both coding problems on the judge. 3. Superday — Written Exam [Paper-based written exam opening the Boston superday - statistics and asset-pricing derivations by hand] https://quantvault.org/arrowstreet-capital-superday-written-exam-interview.html Candidate-reported (pooled from four 2025-26 accounts): the onsite opens with a paper test of statistics and asset pricing, closed-form derivations not computation. The five must-do problems below are original worked versions, numbers verified. Topics: mean-variance (tangency portfolio, capital market line). Risk parity / ERC (w_i (Sigma w)_i = w_j (Sigma w)_j, two-asset closed form, not inverse-vol unless correlations are equal). X on Y vs Y on X (slopes differ, product is R-squared). Autocorrelation (N_eff about N(1-rho)/(1+rho) for AR(1), t-stats, Bartlett / Newey-West). Log utility (certainty equivalent, growth-optimal sizing). Pricing a payoff under a given utility / SDF. 4. Superday — Interview Loop [Back-to-back interviews with several researchers after the written exam, lunch in the middle] https://quantvault.org/arrowstreet-capital-superday-loop-interview.html Candidate-reported: after the paper exam, back-to-back interviews with several people, lunch in the middle. Earlier accounts: regression-heavy technical talks (OLS assumptions, coefficients, omitted-variable bias, time-series basics), resume deep-dive, light coding, behavioural fit. Some report a recruiter call and video technical rounds between CodePair screen and superday, so ordering varies. Compensation chatter (high base, very small headcount) is unverified hearsay. ### Balyasny Asset Management interview process URL: https://quantvault.org/balyasny-interview-guide.html Type: Multi-manager / pod shop (multi-strategy hedge fund) | Founded: 2001 | HQ: Chicago, IL | Roles: Software Engineer, Quant Developer, Quant Researcher | Last reviewed: 2026-07-17 1. Application & Recruiter Screen [~20–30 min · recruiter] Application review and a recruiter conversation on background, target track (SDE/Quant-Dev, Applied-AI, or QR), and logistics. Candidate-reported. 2. Technical Phone Screen [45–60 min · shared editor] https://quantvault.org/balyasny-technical-phone-screen-interview.html A coding-focused phone screen. Reported: implement Python's functools.cache (memoization decorator), explain how decorators work, and a Fibonacci warm-up, plus LeetCode-easy/medium coding. Candidate-reported. 3. Take-Home — Trade-Ticker Project [Take-home · coding project] https://quantvault.org/balyasny-take-home-interview.html A mini trade-ticker take-home (a small streaming market-data / ticker project), the most fully specified item in our BAM sample. Build the described component and be ready to defend design choices. Candidate-reported. 4. Onsite — Coding & System Design [Multiple rounds · onsite / virtual] https://quantvault.org/balyasny-onsite-interview.html SDE onsite, LeetCode-style coding and trading-system design: LC 253 Meeting Rooms (interval tuples), LC 146 LRU cache for rush-hour trading data, waterfall-priority API design (database, Kafka, t-2 processing). Applied-AI: live ML / feature engineering instead (roughly logistic regression, coefficients). Fit/team questions folded in. ### Bank of America interview process URL: https://quantvault.org/bank-of-america-interview-process.html Roles: Markets / Sales & Trading, Investment Banking, Quant Strats & Analytics | Last reviewed: 2026-10-09 1. Round 1 — HireVue Behavioral OA [~30 min · HireVue · 4 modules (+ HK extra case)] https://quantvault.org/bank-of-america-online-assessment.html NOT a quant test: four HireVue modules, one sitting (candidate-reported). Handle Work Challenges: 8 scenarios, 9 min, most/least like you of 4 options. Work Your Business Case: 5 Resources-tab MCQs, 7 min; sole objectively scored module. Tell Us Your Story: 15 questions, 3 min. Describe Your Approach: 88 pairs, 9 min. HK: second, 6-question case. Locked once you select Next. 2. HireVue Video Interview [recorded video answers · track-dependent] Candidates who pass the battery typically record structured behavioral answers (motivation, teamwork, why BofA / why the division) on HireVue before live rounds. Format and prompts vary by division and region. Candidate-reported. 3. Superday / Assessment Center [back-to-back interviews · division-specific] https://quantvault.org/bank-of-america-superday-assessment-center-interview.html Final round runs as a superday (US) or assessment center (EMEA/APAC): several back-to-back interviews mixing behavioral, markets awareness, and division-specific technicals (accounting/valuation for IB, market and product questions for Markets, math/programming for strats roles). Candidate-reported. ### Barclays interview process URL: https://quantvault.org/barclays-interview-guide.html Type: Global investment bank | Roles: Quant Analyst / Strats, Markets (Sales & Trading), Technology / Quant Developer | Last reviewed: 2026-09-02 1. Online Assessment — SHL Interactive Cognitive Test [SHL interactive - 8 questions / 12 min (+ untimed values questionnaire)] https://quantvault.org/barclays-online-assessment.html SHL's 'interactive' cognitive test: 8 activity-based questions in 12 minutes, English (US). Arrange a stacked bar chart from data, resize a pie chart, tap dates meeting availability constraints, drag medical procedures onto a buffer-ruled timetable, fill a shape sequence, drag a line through coded nodes. An untimed values-and-mindset questionnaire (forced-choice triads) usually rides alongside. Timed replica, verified answers: OA page. 2. Video Interview [Recorded video interview - competency + motivation] https://quantvault.org/barclays-video-interview.html After the SHL screen, Barclays early-careers candidates record a video interview: timed, competency-based questions on motivation for the bank and the division, teamwork, resilience and commercial awareness. No technical content at this stage - structure each answer (situation, action, result) and keep it inside the time limit. 3. Technical Interviews — Quant / Strats [Video or in-person rounds - derivatives, statistics, coding] https://quantvault.org/barclays-technical-interview.html Track-specific technical rounds. For quant analyst and strategist roles the questions cluster into three nearly even pillars: options pricing and derivatives (no-arbitrage reasoning on option surfaces, replication, hedging error), statistics and time series (MLE, Bayesian updating, GARCH, cointegration, change-point tests) and algorithmic coding (clean, correct algorithms with stated complexity). 4. Assessment Centre / Final Round [Group exercise, case, and final interviews] https://quantvault.org/barclays-assessment-centre-final-interview.html The final stage is an assessment centre (virtual or in person): a group exercise, a short case or data task, and interviews with senior members of the business. The technical bar carries over from the previous round; the new element is being assessed on collaboration and communication in a group setting. ### Belvedere Trading interview process URL: https://quantvault.org/belvedere-trading-interview-guide.html Type: Proprietary options market maker | Founded: 2002 | HQ: Chicago, IL | Roles: Trader, Software Engineer, Quant | Last reviewed: 2026-05-30 1. Recruiter / Screen [Phone - background + fit] https://quantvault.org/belvedere-trading-recruiter-screen-interview.html An intro conversation about background and interest, sometimes with a quick technical warm-up. 2. Online Assessment / Coding [Timed - 3 problems (30min + 1h + 1h)] https://quantvault.org/belvedere-trading-online-assessment.html The signature SWE OA: order-book price ladder, trade aggregator, bundle arbitrage, max-profit-with-K-trades. Read carefully; test cases are sparse. 3. Probability & EV [45-60 min - video] https://quantvault.org/belvedere-trading-probability-ev-interview.html Probability, expectation, combinatorics, and brain-teasers -- dice, coins, cards, races, Bayesian updates. 4. Options & Market Making [45-60 min - trading intuition] https://quantvault.org/belvedere-trading-options-market-making-interview.html Options basics, micro-price / order-book imbalance, game-theory decisions, and minimum-variance hedging. 5. Onsite - Hardest Rounds [Final rounds - the toughest reported problems] The hardest reported Belvedere Trading problems across every topic. (Hard-rated only; earlier rounds cover easy/medium, so nothing repeats.) A 2026-10 candidate report says both the OA and the superday included an AI-assisted data-analysis problem worked in a Jupyter notebook. ### BlackRock interview process URL: https://quantvault.org/blackrock-interview-guide.html Type: Asset manager — Systematic Active Equity, Aladdin / Financial Modeling Group | Founded: 1988 | HQ: New York, NY | Roles: Quant Researcher, Quant Developer | Last reviewed: 2026-07-17 1. Recruiter / Application Screen [~20–30 min · recruiter call] https://quantvault.org/blackrock-recruiter-application-screen-interview.html A conversational screen: background, why BlackRock, which group (SAE vs Aladdin/FMG vs fixed income), and logistics. Functions as a fit/routing step before the technical rounds. Candidate-reported. 2. Technical Phone Screen [45–60 min · phone / shared editor] https://quantvault.org/blackrock-technical-phone-screen-interview.html A quickfire technical screen mixing stochastic calculus and quant-finance with a coding warm-up. Reported questions: compute E[B_T^3 | B_s] for Brownian motion, Two Sum with a 'what if there are millions of numbers?' follow-up, define and calibrate SABR, and discuss yield-curve calibration. Candidate-reported. 3. Onsite — Math & Statistics [45–60 min · whiteboard / video] https://quantvault.org/blackrock-onsite-math-statistics-interview.html Time-series and regression round, reported: AR(1) stationarity, variance, whitening (Cholesky); AR(2) stationarity via characteristic roots; MSE drawbacks; heteroskedasticity, outliers; stationarity tests; t-test statistic; regressors vs β/R²/p-values. Also PCA, SVD, factor-importance threshold; Poisson bus-waiting expectation; E[sin X]/E[cos X] for standard normal; X₂ given X₁ for correlated normals. 4. Onsite — Coding & C++ [45 min · shared editor] https://quantvault.org/blackrock-onsite-coding-cpp-interview.html A coding round with a C++-internals component. Reported problems: valid anagram (LC 242), Best Time to Buy/Sell Stock (LC 121/122), two Fibonacci implementations, a Python percentile/count script over date-price-volume data, and a VAR(1) process simulator class with input assertions. C++ knowledge: the Rule of Five, run-time vs compile-time polymorphism, and pass-by-reference vs pass-by-value. 5. Onsite — Quant Finance & Derivations [45–60 min · whiteboard] https://quantvault.org/blackrock-onsite-quant-finance-derivations-interview.html The derivations round. Reported: Black-Scholes PDE by hand, the European call closed form; BS to Black's (Black-76) and why; pricing a binary option; GBM SDE for Monte Carlo; Ornstein-Uhlenbeck and normal/lognormal mixture SDEs; yield curves (linear-interpolation pitfalls, cubic splines, Nelson-Siegel/Svensson); calibrating SABR. 6. Behavioral / Fit [~30–45 min · with the hiring team] https://quantvault.org/blackrock-behavioral-fit-interview.html A fit round: why BlackRock, why this group, Aladdin and the firm's platform model, teamwork, and a deeper walk-through of your research/engineering projects. Candidate-reported. ### Brevan Howard interview process URL: https://quantvault.org/brevan-howard-interview-guide.html Type: Global macro hedge fund | Founded: 2002 | HQ: London | Roles: Trading Summer Intern, Graduate Trader | Last reviewed: 2026-10-09 1. Online Assessment [About 20 min stated · 47 questions in 3 parts · finish within 3 days, resumable (candidate-reported)] https://quantvault.org/brevan-howard-online-assessment.html 2027 Trading summer internship candidates report one three-part online assessment. Work-style pairs (30): Two statements per item, a four-point choice (best or somewhat describes me, either side), on updating views on evidence, intuition versus analysis, enjoying hard thinking. Numerical reasoning (15): Four-option questions on six data sets (employment and pay, airfares, sustainable fund assets, book sales and costs, accident rates, two charities' finances); many chart values show only on hover. Written scenarios (2): Open text, no word limit: team conflict over strategy; deadline under heavy workload. Welcome screen: ~20 minutes, within 3 days of the invite, resumable from emailed link, no timer shown. Our replica: 15 numerical questions. Warm up: [mental math](quant-mental-math-practice.html). 2. HR / Motivation Interview [Short call with HR (per public prep guides)] https://quantvault.org/brevan-howard-hr-motivation-interview.html Public prep guides describe a short, non-technical conversation after the assessments: why Brevan Howard, how you follow markets and what sets you apart. Some reports place it between a coding test and the behavioral assessment for other tracks. Prepare with [behavioral questions for trading firms](behavioral-interview-questions-trading-firms.html). 3. Market-Making Game + Portfolio Manager Interviews [Live game plus several short PM interviews (per public prep guides)] https://quantvault.org/brevan-howard-market-making-game-portfolio-manager-interview.html Public prep guides describe a market-making game where you quote two-way prices on uncertain quantities and react to trades, followed by several short interviews with portfolio managers on macro views, your projects and rates or FX knowledge, with a trade idea you can defend. Rehearse with [Make a Market](make-market.html), the [card market table](card-table.html) and the [market-making game guide](market-making-game-interview-guide.html). ### Bridgewater Associates interview process URL: https://quantvault.org/bridgewater-interview-guide.html Type: Macro hedge fund | Founded: 1975 | HQ: Westport, CT | Roles: Investment Associate / Engineer, Software Engineer, AIA Lab — ML Generalist | Last reviewed: 2026-05-29 1. Recruiter / Culture Screen [30–45 min · phone / Zoom] https://quantvault.org/bridgewater-recruiter-culture-screen-interview.html HR floods you with Bridgewater's culture, then covers basics (visa, education, experience) and deep behavioral prompts: 'your learning journey since childhood', 'why Bridgewater', biggest challenge. People are cut here for background mismatch, so it's not a formality. 2. Online Assessment [Codility, 90-180 min] https://quantvault.org/bridgewater-online-assessment.html The Software-Engineer track's coding screen: the signature 'Computing the whole world' topological-sort of an investment compute graph, plus a 4-problem Codility set (min flips to a uniform binary string, count visible binary-tree nodes, a 'word machine' stack VM with overflow errors, and counting zero-sum subarrays). The investment / Portfolio-Associate, AIA-Lab ML, and Excel take-home tracks are case/Excel/behavioral rather than coding. 3. Take-Home Excel + Case Study [~2 hr take-home + a video/phone case] https://quantvault.org/bridgewater-take-home-excel-case-study-interview.html Two analytical exercises. A take-home Excel of employee productivity/production + hours data with 7-12 questions reading trends ('junior-high math', tests analytical thinking and creativity). Then a consulting-style case ('why did revenue decline', 'how would you increase profit', 'is opening a casino a good investment') where the difficulty is understanding the prompt and articulating multi-angle reasoning, not computation. 4. Onsite / Superday [Onsite · sequential back-to-back rounds] https://quantvault.org/bridgewater-onsite-superday-interview.html A gated gauntlet: you must pass each round to reach the next. Typically: an order-book / Excel mental-math round (adjust Bid5/Ask5 and quickly compute fair value; place orders to buy gold under scenarios), an investment-logic/fundamental case, a simple optimization case (allocate sales visits to maximize revenue), and a behavioral/values/philosophy debate. The morning markets round gates the afternoon. 5. Final / Team-Lead Values Round [Conversation · team lead] https://quantvault.org/bridgewater-final-team-lead-values-interview.html A philosophy/values debate and a culture-fit conversation about your life, interests, and how you handle criticism. The team lead is essentially confirming a 'profile' fit. The most common rejection here is 'not a fit', even for strong analysts. 6. Software Engineer Track [OA + technical phone] https://quantvault.org/bridgewater-software-engineer-track-interview.html Engineering path. OA: multiple-choice, SQL (join 3 tables: teacher / college / salary) and simple coding (count words per sentence, handling 'i.e.' / 'e.g.' boundaries). Technical phone: a Linux + regex task (find phone numbers across files) and system design (parking-lot fee system) with constant 'why did you design it this way'. 7. AIA Lab — ML Generalist [Separate process · 3 rounds] https://quantvault.org/bridgewater-aia-lab-interview.html The newer applied-AI track wants true generalists. A data-manipulation problem in numpy/torch with the Head of AI, a LeetCode BFS/DFS coding round, and a system-design round (a fully-automated parking-lot system). numpy array-operation fundamentals are quizzed in detail. ### Capital One interview process URL: https://quantvault.org/capital-one-interview-guide.html Type: Bank (consumer finance and cards) | Roles: Quantitative Analyst, Data Scientist, Quant Developer | Last reviewed: 2026-09-05 1. Online Assessment: CodeSignal General Coding Assessment [CodeSignal, 4 problems, 70 min] https://quantvault.org/capital-one-online-assessment.html Four coding problems drawn from a rotating pool, in rising difficulty, auto-graded with hidden tests. Our replica runs a full 14-problem pool in a live Python editor with the real 70-minute clock; the free sample shows question 1 exactly as candidates saw it. 2. Recruiter Screen [Call, background and fit] A recruiter conversation about background, programme fit and logistics, usually after the CodeSignal score comes back. 3. Power Day: Case, Technical and Behavioral [3 to 4 interviews in one day] https://quantvault.org/capital-one-power-day-interview.html A case interview on business and analytical reasoning, a technical interview on statistics and machine learning fundamentals, and behavioral rounds on past projects. ### Capstone Investment Advisors interview process URL: https://quantvault.org/capstone-interview-guide.html Type: Volatility / derivatives multi-strategy | Founded: 2004 | HQ: New York, NY | Roles: Quant Researcher, Quant Trader | Last reviewed: 2026-07-17 1. Online Assessment — Coding [Timed · online coding] https://quantvault.org/capstone-online-assessment.html The known Capstone coding OA: cap each stock's weight at W%, redistribute the excess proportionally to the remaining stocks (highest-weighted capped first, alphabetical tie-break), keep the total at 100%, return -1 if infeasible (W·N < 1). Bookkeeping more than algorithms: the trap is conserving total weight through cascading caps. 2. Technical — Probability / Statistics / Options [45–60 min · with a researcher / trader] https://quantvault.org/capstone-technical-interview.html Technical rounds probing probability, statistics, and, given Capstone's derivatives focus, options and volatility intuition. Candidate-reported in structure; prepare the standard quant technical stack with extra weight on options pricing and risk. 3. Final — Fit / Research Conversation [Conversational · with the team] https://quantvault.org/capstone-final-interview.html A closing fit and research-conversation stage. Expect a walk-through of your projects and how you think about markets and risk, alongside team-fit signal. Candidate-reported. ### Citadel / Citadel Securities interview process URL: https://quantvault.org/citadel-interview-guide.html Type: Multi-strat hedge fund + Citadel Securities (MM) | Founded: 1990 | HQ: Miami, FL (moved from Chicago in 2022) | Roles: Quant Researcher, Quant Trader, Software Engineer, ML Engineer | Last reviewed: 2026-05-22 1. Online Assessment [HackerRank · by track (coding ~70 min, trading ~30 min)] https://quantvault.org/citadel-online-assessment.html Citadel's first filter cuts hard, by track. SWE / quant-dev: two coding problems on HackerRank in about 70 minutes, drawn from a large rotating pool (strict time, all tests must pass). Trading / QT: a shorter, probability-heavy test (reported as about 15 fill-in probability and statistics questions). Quant Research (QR): the published QR process lists no automated test; live coding happens in a CoderPad interview. 2. First-Round Phone Screen [45-60 min - video] https://quantvault.org/citadel-first-round-phone-screen-interview.html A 45-60 min remote interview blending technical and behavioural. Core technical: one coding question (LeetCode-medium, Python or C++) plus a probability/statistics question for quant tracks. Recruiter intro up front; they screen for correct, efficient code written quickly and for genuine interest in Citadel. 3. Behavioral / Culture-Add Round [30 min · with senior researcher] https://quantvault.org/citadel-behavioral-culture-add-interview.html Candidate-reported as a separate 30-minute round: 'Why Citadel' + 'tell me about a project' + 1 ethics/dilemma scenario. In the published QR and engineering processes, behavioural questions sit inside the first technical round, and engineering adds a leadership interview after the three technical interviews. 4. Trading / Market-Making Simulation [45–60 min · interactive · trading & QT tracks] https://quantvault.org/citadel-trading-market-making-simulation-interview.html Trading/QT track: Citadel Securities runs live market-making simulations. Quote a two-sided market on an uncertain value as the interviewer trades against you; update quotes and inventory as flow reveals information. Scored on quote discipline, inventory awareness, and clear reasoning under pressure over exact numbers. Rehearse below. 5. Super Day - Quantitative Math [60 min · whiteboard] https://quantvault.org/citadel-super-day-quantitative-math-interview.html Part of the all-day Super Day (a series of ~45-min interviews). Two or three probability/statistics problems back-to-back - expect full derivations, not just final answers. 6. Super Day - Coding [60 min · CoderPad (Python preferred)] https://quantvault.org/citadel-super-day-coding-interview.html A Super Day round: one medium-hard algorithmic question plus a practical 'clean up this messy code' refactor. C++/Python emphasis; correctness, edge cases, and complexity under time pressure. 7. Super Day - ML / Statistics [60 min · whiteboard] https://quantvault.org/citadel-super-day-ml-statistics-interview.html A Super Day round for QR/ML tracks: regression, model diagnostics, regularization. They probe assumptions hard - be ready to justify 'why does this hold.' 8. Team-Fit / Final Round [45 min - with hiring team] https://quantvault.org/citadel-team-fit-final-interview.html After the Super Day, hiring managers across Citadel and Citadel Securities review your feedback; teams that are interested run a final fit interview. For quant/research this is often one open-ended prompt ('design an alpha' / 'a paper you would actually trade'); for SWE it is a senior-engineer team match. ### Chicago Trading Company (CTC) interview process URL: https://quantvault.org/ctc-interview-guide.html Type: Proprietary options / ETF market maker | Founded: 1995 | HQ: Chicago, IL | Roles: Quant Trader, Quant Researcher | Last reviewed: 2026-09-26 1. Online Assessment — Reasoning Battery [Timed · online · reasoning + numerical] https://quantvault.org/ctc-online-assessment.html The first filter is a timed online reasoning assessment. Candidate captures show an SHL-style verbal-reasoning section (reading-comprehension MCQs plus true / false / cannot-say passage judgments) under a per-section countdown, typically alongside numerical reasoning. Domain knowledge won't save you here; careful reading and clock management will. 2. Initial Technical Screen: Research Process [Video call · with a quant researcher · no OA] https://quantvault.org/ctc-initial-technical-screen-interview.html QR route, outside the trader funnel. One September 2026 report (PhD, quant researcher): invited from the resume and CTC's detailed application form, no online assessment first. First round: an "Initial Technical Screen: Research Process" with one of CTC's QRs, per the recruiter almost entirely about your research, plus technical questions. Later rounds unreported; assume the probability and statistics round below. 3. Core-Values Behavioral Screen [~30–45 min · recruiter / hiring team] https://quantvault.org/ctc-core-values-behavioral-screen-interview.html A behavioral round built around CTC's core values. Expect 'tell me about a time…' questions probing collaboration, ownership, integrity, and how you handle being wrong. CTC's own interviewing tips emphasize this round: fit and values carry real weight in the decision. 4. Probability / Options Concept Interview [45–60 min · with a trader / researcher] https://quantvault.org/ctc-probability-options-concept-interview.html The technical round, drawn directly from CTC's QTA prep-guide topics: probability, conditional probability / Bayes, expected value, common distributions, independence, confidence intervals, and market-making bid/ask intuition. Problems are trading-flavored and concept-driven: they want clean reasoning applied to a setup, not a memorized formula. 5. Trading / Market-Making Game [Interactive · with a trader] https://quantvault.org/ctc-trading-market-making-game-interview.html As an options / ETF market maker, CTC's later rounds are reported to include a trading-game and mental-math component: quoting two-sided markets, sizing bets by edge, and rapid arithmetic under pressure. Specifics vary by cycle and role: treat this stage as candidate-reported / typical-for-the-firm rather than a fixed format. 6. Final — Onsite / Fit [Onsite · multiple conversations] https://quantvault.org/ctc-final-interview.html A closing onsite / fit stage with the team. Conversational and values-oriented, confirming both technical bar and whether you'd thrive on CTC's floor. Expect a mix of lighter technical framing and culture-fit discussion. ### D. E. Shaw interview process URL: https://quantvault.org/de-shaw-interview-guide.html Type: Multi-strategy quant hedge fund | Founded: 1988 | HQ: New York, NY | Roles: Quant Researcher / Analyst, Software / Quant Developer, Prop Trader | Last reviewed: 2026-05-29 1. CodeSignal OA [90 min · 4 problems · CodeSignal] https://quantvault.org/de-shaw-online-assessment.html The software gate (referred candidates often skip it). Four LeetCode medium-hard algorithm problems in 90 minutes, frequently described as harder than peer firms. Recurring tasks: count string pairs where one is a suffix of another, lamps-on-a-line max coverage, a 'plant trees → count gaps ≥ minLength' structure (commonly TLEs), and the largest centered 'plus/cross' in a 1000×1000 matrix. 2. First-Round Technical Phone [45–60 min · video] https://quantvault.org/de-shaw-first-round-technical-phone-interview.html ~20 min research/resume dive ('what makes a good project' too), then 2-3 problems. QR: probability, statistics, brain-teasers (pick-the-max, draws until sum > 1, two-scales MLE, Y~X vs X~Y). SWE: any-language DS&A coding, 'describe the algorithm + complexity, then optimize' (library-free integer sqrt, monotonic-deque streaming max). 3. Virtual Onsite 1 [3 rounds · ~1 hr each] https://quantvault.org/de-shaw-virtual-onsite-1-interview.html Three rounds (the third often conditional on passing the first two): typically one probability/stats round, one coding round, and a VP/modeling round. Interviewers are IMO medalists and math/physics PhDs. Expect open-ended modeling (predict a baseball team's season wins; combine simple models) alongside the hard probability. 4. Virtual Onsite 2 [2 rounds · SVP / hiring manager] https://quantvault.org/de-shaw-virtual-onsite-2-interview.html Two rounds with more senior people (SVP / hiring manager): behavioral plus math/stats and an open-ended modeling question. Progression is visibly conditional: underperformers get rounds cancelled mid-loop. Statistics gets deep here: R² behavior, t-statistic under data duplication, designing an experiment to test a claim. 5. Prop Trading: HR + Case Study [HR screen + ~2-day (48h) take-home write-up] https://quantvault.org/de-shaw-prop-trading-interview.html The Prop-Trading track (separate from QR/SWE): a ~30-min behavioral/situational HR screen ('why trading / why D. E. Shaw', decisions under pressure, keeping up with markets), then a ~2-day (about 48-hour) take-home case study, a 1-2 page quantitative + qualitative macro write-up (rates, elections, demographics), followed by a superday of behavioral + case discussion + light modeling. No coding, no game. 6. Closing Gates [Code/writing sample · reference · background check] Even after the loop, offers gate on a code sample and a writing sample, a reference check (itself a screening step), and a background check. The process is slow, so expect weeks between steps. ### DRW interview process URL: https://quantvault.org/drw-interview-guide.html Type: Diversified principal trading firm | Founded: 1992 | HQ: Chicago, IL | Roles: Quant Trader, Quant Researcher, Software Engineer | Last reviewed: 2026-05-30 1. Online Assessment [45 min - QR/QT math (6 fill-in) OR 150 min - SDE (3 coding)] https://quantvault.org/drw-online-assessment.html DRW's first filter is track-dependent. Quant Research / Quant Trader candidates get a 45-minute, ~6-problem math + probability assessment (fill-in answers; the numbers rotate each sitting - matrix unknowns, dice EV, covariance, CLT tails, Markov lane-switching, Gamma-integral areas). Software candidates get a 150-minute, 3-problem coding OA (no hidden-test feedback). Take the timed bank below. 2. Recruiter / Phone Screen [Phone - behavioral + light technical] An intro conversation about background and interest in trading, sometimes with a quick probability or mental-math warm-up. 3. Probability & Brain-Teasers [45-60 min - video] https://quantvault.org/drw-probability-brain-teasers-interview.html Probability, expectation, and brain-teasers with follow-ups -- the core quant screen. Streaks, gambler's ruin, waiting times, and order statistics are common. 4. Trading Intuition & Market Making [45-60 min - estimation + games] https://quantvault.org/drw-trading-intuition-market-making-interview.html Market-making estimation drills (quote a market, update on responses), options/finance basics, and game theory (pirate game, AKQ poker, Penney's game). Trading sense over formulas. 5. Coding [60 min - algorithms] https://quantvault.org/drw-coding-interview.html For engineering roles: clean algorithm and data-structure problems (and often C++). State complexity and optimize. 6. Onsite / Final Rounds [Multiple rounds - full technical bar] https://quantvault.org/drw-onsite-final-interview.html The full loop spans probability, brain-teasers, trading intuition, options/game theory, and coding. This round surfaces the complete set of reported DRW problems across all topics. ### Dynamic Technology Lab (DTL) interview process URL: https://quantvault.org/dtl-interview-guide.html Type: Quantitative hedge fund | Founded: 2009 | HQ: Singapore | Roles: Quant Researcher, Quant Developer, Data Engineer | Last reviewed: 2026-07-17 1. Written Math OA [Timed written paper · ~7 proof questions · candidate-reported no calculator] https://quantvault.org/dtl-online-assessment.html DTL's signature filter, a severe one: a written olympiad-style math paper graded on rigor. The 2024-10-13 capture had seven proof questions (no MCQ): round-robin pigeonhole, mod-20 number-theory bound, ruler-and-compass weighted Fermat-point construction, Guan Dan joker-distribution expectation (N→∞ limit), SVD/PCA derivation, coin-sliding combinatorial game, change-making (ballot) probability. 2. Technical Interview [Candidate-reported · math + coding, video/onsite] https://quantvault.org/dtl-technical-interview.html After the written paper, technical rounds stay quantitative: probability, statistics, and linear algebra for research candidates, plus a coding round (C++/Python, data structures, complexity) for developer and data tracks. Candidate-reported detail on the loop beyond the OA is thin: expect the same proof-and-reasoning bar carried into a live conversation. 3. Final / Research Deep-Dive [Candidate-reported · onsite / fit + research discussion] https://quantvault.org/dtl-final-research-deep-dive-interview.html A closing round focused on research depth and fit: walk through past projects, defend modeling choices, and show you can own a problem end-to-end. Candidate-reported specifics are limited; prepare a tight research story and be ready to reason about a model from data to signal. ### Five Rings interview process URL: https://quantvault.org/five-rings-interview-guide.html Type: Proprietary options market maker | Founded: 2014 | HQ: New York, NY | Roles: Quant Trader, Quant Researcher, Software Engineer | Last reviewed: 2026-05-29 1. Timed Mental-Math Screen [~17–30 min · webcam · no calculator] https://quantvault.org/five-rings-online-assessment.html The dominant filter: an OA or an HR-read live screen. ~17-20 questions in ~17-30 minutes (QR: ~12-16 with per-question timers), webcam on, fill-in-the-blank, answer to the nearest integer. Content: mental math, probability, estimation, light calculus (arc length), modular arithmetic. The QT version also has a behavioral intro ('why trading / why Five Rings') and a Fermi-estimation section. 2. Technical Phone [45–60 min · with a QR / trader] https://quantvault.org/five-rings-technical-phone-interview.html Deeper probability and statistics with a quant researcher (often a math PhD who hints heavily): variance of a coordinate of a random point on a sphere, P(X+Y > XY) for two dice, the broken-stick longer-piece expectation, random-walk hitting probabilities, the signature bus-stop residual-life waiting-time problem, and regression facts (Y~X vs X~Y slope bounds, σ_{X+Y} → correlation). 3. Dice / Betting Game Round [Interactive · with a trader] https://quantvault.org/five-rings-dice-betting-game-interview.html The trader signature: escalating game-DESIGN problems. Design a custom die (faces summing to 21) to maximize your win rate against an opponent using the same die; then asymmetric-die variants; then die-vs-deck comparisons reasoning about negative covariance from sampling without replacement; plus random-walk EV-bound problems. It's pure EV intuition under rising complexity. Rehearse the muscle with [Probability Market-Taking](betting-game.html) and the free [Optiver Beat the Odds](optiver-beat-the-odds.html) sets. 4. Second Technical Phone [45–60 min · estimation / inference] https://quantvault.org/five-rings-second-technical-phone-interview.html The QR second round leans on estimation and inference: MLE for n iid U(0,θ), confidence intervals (normal-unknown-variance vs distribution-free), the 'sum of U(0,1) first exceeds 1' result (E[N] = e) with its variance and CI, mean-vs-median comparisons via Jensen, and geometric-probability problems. 5. Data Project [Take-home · NDA] https://quantvault.org/five-rings-data-project-interview.html A QR-specific take-home dataset project (NDA-covered, so details are scarce). Weak performance here is a common reject reason: it's a real gate, not a formality. 6. Final Rounds [Head of trading → 'boss'] https://quantvault.org/five-rings-final-interview.html The closing sequence: a head-of-trading round and then a 'boss' final. More probability/EV plus fit, with the same friendly-but-high-bar vibe. The full-time bar is reported as brutal. ### Flow Traders interview process URL: https://quantvault.org/flow-traders-interview-guide.html Type: ETP/ETF market maker (publicly listed) | Founded: 2004 | HQ: Amsterdam (+ NY, HK, Singapore) | Roles: Trader, Quant Researcher, Trading Technology | Last reviewed: 2026-09-08 1. Recruiter / Screen [Phone - background + fit] An intro conversation about background and interest, sometimes with a quick technical warm-up. 2. Online Assessment [Trader: timed mental maths + sequences · Tech: language MCQ + coding · QR: Fast Fire] https://quantvault.org/flow-traders-online-assessment.html Trading applicants sit a timed test of mental arithmetic and number sequences, which is the main early cut. Tech candidates get a coding screen (array/matrix problems, prefix-sum reconstruction, anomaly detection). Quant Research applicants (September 2026 report) instead get two back-to-back take-homes: 25 MCQ plus 5 integral-heavy questions in 15 minutes on statistics, ML (lasso/ridge, feature selection) and Brownian motion, then Fast Fire 2.2: 6 short-answer probability questions and 1 Python Monte Carlo task in 30 minutes. 3. Probability & Mental Math [45-60 min - video] https://quantvault.org/flow-traders-probability-mental-math-interview.html Probability, expectation, combinatorics, and brain-teasers, plus fast mental arithmetic and sequences. 4. ETF & Market Making [45-60 min - trading intuition] https://quantvault.org/flow-traders-etf-market-making-interview.html ETF/ETP mechanics (creation/redemption, leveraged-ETF rebalancing), basic options, and market-making intuition. 5. Onsite - Hardest Rounds [Final rounds - the toughest reported problems] The hardest reported Flow Traders problems across every topic. (Hard-rated only; earlier rounds cover easy/medium, so nothing repeats.) ### Goldman Sachs interview process URL: https://quantvault.org/goldman-sachs-interview-guide.html Type: Global investment bank (global banking and markets, asset and wealth management) | Founded: 1869 | HQ: New York | Roles: New Analyst, Engineering (2027 class, Asia Pacific) | Last reviewed: 2026-10-16 1. HackerRank Online Assessment [52 min · 2 timed sections · HackerRank, proctored (candidate-reported, 2026 sitting)] https://quantvault.org/goldman-sachs-online-assessment.html A candidate for the 2027 Engineering New Analyst Program in Asia Pacific reports a proctored HackerRank test in two timed sections. - Software Fundamentals, 30 minutes: one data structures and algorithms problem. In 2026 it asked you to split an array into three non-empty groups and pick one element from each so that |a1 - a2| + |a2 - a3| is as small as possible. - Math, 22 minutes: 8 multiple-choice questions on calculus, linear algebra and probability. Reported items: a two-car flat-tire probability, the largest possible covariance given two variances, normalizing a piecewise density, an arc length, the area between two sine curves, a Lagrange multiplier on the unit circle, what AB = I implies, and which diagonalization statements hold. The invite reportedly allowed AI tools inside the HackerRank environment. All 9 reported items are in the practice set below with verified answers. Warm up: [linear algebra](linear-algebra-interview-questions.html), [probability](probability-interview-questions.html), [coding questions](coding-interview-questions.html). 2. Live Interviews / Superday [Live interviews after the OA (format varies by division and region; not yet verified for 2027)] https://quantvault.org/goldman-sachs-live-superday-interview.html We do not yet hold a verified 2027-cycle report of these live rounds. Public guides describe technical interviews with engineers (data structures, algorithms, a coding exercise) plus motivation and fit questions. Practice below: every bank problem candidates have tied to Goldman Sachs, including earlier probability and expected value reports. See also [Goldman Sachs interview questions](goldman-sachs-interview-questions.html). ### GSA Capital interview process URL: https://quantvault.org/gsa-capital-interview-guide.html Type: Systematic single-manager | Founded: 2005 | HQ: London, UK | Roles: Quant Researcher, Quant Developer | Last reviewed: 2026-09-06 1. Online Assessment — Correlation One [Correlation One · 25 questions / 60 min · MCQ + numeric entry] https://quantvault.org/gsa-capital-online-assessment.html Quant Researcher first screen, Correlation One: 25 questions in 60 minutes, single-option MCQ or numeric entry, PREVIOUS / NEXT navigation, 25-box progress bar. OA page: timed replica, verified answers. Aug 2026 sitting, four families. ML/statistics: k-means local optima, overfitting, residual-vs-one-variable plots, R-squared vs t-statistics, regression on 110 observations, 100 variables. Probability and linear algebra ('integer closest to 20p', 'M + N for M/N in lowest terms'): P(3X + 4Y > 5), eigenvalue sum, null-space dimension, random quadratic roots, two sample means, 8-coin Bayes, two cards. Brainteasers: nine dots in four segments, molecule-collision invariant, knights and knaves. Behavioral-finance judgment: cab-witness base rate, Linda the bank teller, Wason four-card task, 2-4-8-16 probe, rural-hospital extremes, three 'which statement do you agree with more' pairs (sunk cost, process vs outcome, survivorship). 2. Take-Home Data Project [Take-home · own environment · graded on OOS MSE] https://quantvault.org/gsa-capital-take-home-data-project-interview.html GSA's signature stage. From real 10-second-bar prices for instruments X and Y, predict Y's 10-minute-ahead return with data up to t only, in two template functions (modelEstimate fits parameters; modelForecast, one prediction per row). Graded purely on out-of-sample MSE: clean and validated beats complex; lookahead / leakage is fatal. 3. Technical — Regression & Statistics [45–60 min · with a researcher] https://quantvault.org/gsa-capital-technical-interview.html The core technical round: regression theory and statistics. Expect ridge closed form and why X'X + λI is invertible, variance of a sum of equicorrelated variables (and valid-ρ bounds), the bus-stop inspection paradox, years-until-positive-return via the CLT, Sharpe-ratio backtest length, and estimator reasoning. They want a clean argument, not just a number. 4. Portfolio Construction & Machine Learning [45–60 min · with a researcher] https://quantvault.org/gsa-capital-portfolio-construction-machine-learning-interview.html A round on portfolio optimization and, increasingly, machine learning. Expect Markowitz optimization given an alpha signal and covariance matrix (objective, constraints, unconstrained closed form), covariance-estimation methods (sample vs EWMA vs PCA factor models vs graphical Lasso), and recent ML items: PCA components with near-zero R², tree feature importance / SHAP values, and neural-net initialization / batch-norm-and-dropout at test time. 5. Final — Research Fit [Conversational · with senior researchers] https://quantvault.org/gsa-capital-final-interview.html A closing research-fit stage: a deep-dive on your take-home methodology and past projects, plus culture signal. They probe why you made each modeling choice, how you validated it, and how you'd extend it. ### GTS interview process URL: https://quantvault.org/gts-interview-guide.html Type: Electronic market maker / HFT | Founded: 2006 | HQ: New York, NY | Roles: Quant Trader, Quant Researcher, Software Engineer | Last reviewed: 2026-05-30 1. Recruiter / Screen [Phone - background + fit] An intro conversation about background and interest, sometimes with a quick technical warm-up. 2. Online Assessment / Coding [Timed - algorithms] https://quantvault.org/gts-online-assessment.html A coding/quant OA: sliding-window and DP problems (cheapest roof covering k cars, subarray optimization). 3. Probability & Mental Math [45-60 min - video] https://quantvault.org/gts-probability-mental-math-interview.html Probability, expectation, combinatorics, and brain-teasers -- card-guessing EV (harmonic numbers), races, counting. 4. Statistics [45-60 min - video] https://quantvault.org/gts-statistics-interview.html Statistics and regression brain-teasers: duplicated-data effects on slope/R-squared/t-stat. 5. Onsite - Hardest Rounds [Final rounds - the toughest reported problems] The hardest reported GTS problems across every topic. (Hard-rated only; earlier rounds cover easy/medium, so nothing repeats.) ### Headlands Technologies interview process URL: https://quantvault.org/headlands-interview-guide.html Type: Proprietary high-frequency trading firm | Founded: 2009 | HQ: Chicago, IL | Roles: Quant Trader, Quant Researcher, Software Engineer | Last reviewed: 2026-09-08 1. Recruiter / Screen [Phone - background + fit] An intro conversation about background and interest, sometimes with a quick technical warm-up. 2. Online Assessment / Coding [QR new grad: 70 min, stats MCQ + stats free response + C++ + coding; engineering: timed algorithms + data structures] https://quantvault.org/headlands-online-assessment.html Timed coding: data-structure design and streaming/order-book problems (top-K by notional, as-of joins, max concurrent orders). Quantitative Researcher new grads (reported September 2026): a single 70-minute OA: 10 basic-statistics MCQs (10 min), 4 free-response stats (24 min), 1 C++ problem (20 min), 2 in Python, Java or C++ (16 min). 3. Probability & Mental Math [45-60 min - video] https://quantvault.org/headlands-probability-mental-math-interview.html Probability, expectation, combinatorics, and brain-teasers with follow-ups. 4. Statistics & Modeling [45-60 min - video] https://quantvault.org/headlands-statistics-modeling-interview.html Statistics and regression: estimator properties, p-value vs sample size, collider/selection bias, OLS pitfalls. 5. Onsite - Hardest Rounds [Final rounds - the toughest reported problems] The hardest reported Headlands Technologies problems across every topic. (Hard-rated only; earlier rounds cover easy/medium, so nothing repeats.) ### Hudson River Trading interview process URL: https://quantvault.org/hrt-interview-guide.html Type: Single-firm prop trading (no outside capital) | Founded: 2002 | HQ: New York, NY | Roles: Algorithm Developer (quant research and trading), Algorithm Engineer, Software Engineer / Core Developer | Last reviewed: 2026-05-22 1. Online Assessment [~70-150 min - HackerRank / CodeSignal] https://quantvault.org/hrt-online-assessment.html HRT's biggest cut: the large majority are eliminated here. Algo Developer OA: ~150 min, 3 recycled-canon problems (Fancy Number, 2048 / Reversi-Othello simulation, N-ary tree pre-order/merge). SWE / SDE interns: CodeSignal 'General' set (~70-90 min, ~4 LeetCode-style, often auto-sent at resume screen). Quant Analyst: 30 min, C++-only, 2 low-level problems. Graded on correctness AND clean, tight, edge-case-correct code, not just visible tests. 2. Probability & Brainteaser Phone Screen [45 min - video] https://quantvault.org/hrt-probability-brainteaser-phone-screen-interview.html Conversational quant screen on HRT's signature hard probability: expected value, brainteaser and mental-math questions (Gambler's Ruin, coupon collector, waiting times, bet fair-value), derived cleanly. A recurring pattern for Algorithm Developer candidates (HRT's quant research and trading role): a statistics question (estimator bias, order statistics), then 'now simulate it in Python to check your result', so be ready with a quick Monte Carlo. Algo Dev candidates get both this statistics screen and a coding screen; Software Engineer candidates get the live programming screen below and systems / architecture rounds onsite. 3. Programming Phone Screen [45-60 min - CoderPad - C++ or Python] https://quantvault.org/hrt-programming-phone-screen-interview.html A live coding round tuned to the team you are interviewing for. For core/low-latency roles expect C++ specifics - pointers and references, memory management, move semantics - alongside an algorithmic problem. Get to a correct first solution, then expect an optimization / complexity conversation. 4. Onsite [Full day - back-to-back rounds] https://quantvault.org/hrt-onsite-interview.html A full day of back-to-back sessions (virtual or in person). Multiple programming rounds (harder than the screen, with optimization and systems-level reasoning), a systems / architecture discussion, problem-solving, and a project / behavioural deep-dive that probes collaboration, teachability, and communication. HRT weights how you think and work with others, not just whether you reach the answer. ### IMC Trading interview process URL: https://quantvault.org/imc-interview-guide.html Type: Proprietary options / ETF market maker | Founded: 1989 | HQ: Amsterdam, NL | Roles: Quant Trader, Quant Researcher, Software Engineer / Quant Performance Engineer | Last reviewed: 2026-10-09 1. Online Assessment [Trader/QR: varies by cycle (NeurOlympics games ~45 min, SHL ~40 min, or a 60-min numerical test) · SWE ~120 min · HackerRank / Codility] https://quantvault.org/imc-online-assessment.html Track-split, and the trader format changes by cycle and region. Trader / QR: European and US candidates in 2025/26 report NeurOlympics, four cognitive games in about 45 minutes; the US Summer 2027 cohort reports an SHL IMC Trader Assessment (personality + inductive reasoning, ~40 min). Some cohorts instead report a timed numerical-reasoning test of 15 questions in 60 minutes (probability, combinatorics, logic such as knights & knaves, information-gain / decision trees, graph connectivity, sequences); reports differ on which years used it. SWE / Quant-Perf: 120 min, 2 algorithmic problems on HackerRank/Codility (grid BFS, simulation, data-structure design) where timing out is the common failure. 2. Recorded Video Interview [One-way · HireVue · scored as part of the OA] https://quantvault.org/imc-recorded-video-interview.html A one-way timed video interview. Reports differ on its content: QuantVault reports describe 6-9 segments on HireVue (~15 min think-time, 1-2 min answers) on motivation + behavioral topics (why IMC / why trading, 'explain your favorite project to a non-technical person', 'a failure and how you bounced back', handling pressure), while more recent outside reports describe about 3 prompts, typically two on motivation and one probability question, on SparkHire or HireVue, sometimes due within 48 hours. QuantVault reports say it is scored as part of the OA. 3. HR / Recruiter Phone Screen [10 to 45 min · phone / Zoom · before or soon after the OA] https://quantvault.org/imc-hr-recruiter-phone-screen-interview.html Early step, every track: logistics, 'tell me about yourself', IMC's signature 'Why IMC?' and 'Why trading?' (asked again later), often a light coin-flip-with-an-edge brain-teaser (EV, bankroll). - Recent QR and QT reports: it can follow the OA (one quant trader invite came about 3 days later); HR told that candidate: no technical questions. QR hears back in 1 to 2 weeks. - Candidates report admin (graduation date, visa or sponsorship, notice period, expected salary, work mode), why IMC, QR and trading, and a deep dive on two resume research projects. - Behaviorals: a hard-to-hear opinion, an advisor changing your approach, a research project end to end, conflict, failure, competition, team role; other firms, choosing between offers. - QR: applying machine learning in finance (concrete results, deep learning, Sharpe or P&L). A quant trader intern screen had a card suit-guessing bet, framed on risk appetite, not math. 4. Trader / Market-Making Game Round [30–45 min · interactive · with a trader] https://quantvault.org/imc-trader-market-making-game-interview.html Decisive trading round: a trader runs a market-making / betting scenario. Sports/match betting boards tracking per-bet and unified bankroll P&L, card suit-guessing ($4 correct / −$1 wrong), pairs-trading and daily-price-movement bets, 'taxman'-style games. Rewards EV thinking, Bayesian updating, risk/bankroll discipline; rehearse below. 5. Technical Round [45–60 min · video · math / stats / coding / ML] https://quantvault.org/imc-technical-interview.html The QR technical round: probability and statistics, a coding problem (often 2-D DP or Combination Sum), and, for the ML-QR variant, ML fundamentals (information gain, decision trees, classification, regression-vs-transformer trade-offs) plus an ML coding task and a Monte-Carlo / optimization segment. Derivations expected, not just answers. 6. Onsite Coding + Systems [2-hr sessions · live coding + systems deep-dive] https://quantvault.org/imc-onsite-coding-systems-interview.html The engineering onsite: live coding (often 2-hour sessions) plus a deep systems/OS round: CPU, TLB, segfaults, signals & signal masks, C++ internals, Linux debugging, REST/asyncio. Chicago/Amsterdam systems-engineer onsites can simulate a server-outage triage with many candidates. The Python-developer variant swaps in a 3-5 hr take-home with a README and library-choice justification. 7. Superday / Virtual Onsite [Onsite or virtual · ~5 rounds across the day] https://quantvault.org/imc-superday-virtual-onsite-interview.html The culmination: often a combined trading-floor superday. ~5 rounds: rapid probability/quant-reasoning (~4 min each), brain teasers (~1 hr), a coding block (~1 hr), game-theory problems (e.g. a newspaper-delivery turn-taking game), Fermi/market-sizing estimation, and the recurring behavioral pair. Strong quantitative-product knowledge expected. ### Jane Street interview process URL: https://quantvault.org/jane-street-interview-guide.html Type: Single-firm prop trading (no outside capital) | Founded: 2000 | HQ: New York, NY | Roles: Quant Trader, Software Engineer, Quant Researcher | Last reviewed: 2026-05-22 1. Pre-interview Assessment [~30 min · probability & mental math · reported in some cycles and tracks] https://quantvault.org/jane-street-online-assessment.html Candidates report this short written screen in some cycles and tracks. Jane Street's own pages describe a trading process that starts with phone interviews and mention no test, so many applicants never receive one. Where it is reported, it is a timed set of probability, expected-value and mental-math brain-teasers, not a coding test (prime-sum parity, optional-reroll dice, Bayesian coin re-flip, two-urn posterior bet). Explain reasoning, not just answers. Quant-research roles add live coding; trading-operations, an Excel task. 2. Phone Round 1 — Brain Teasers [45 min · video call] https://quantvault.org/jane-street-phone-round-1-interview.html Opening conversational phone round. Three to five brain-teaser / probability questions, intentionally easier than later rounds. They're checking baseline reasoning: does the candidate frame the problem cleanly and think clearly out loud? 3. Phone Round 2 — Brain Teasers [45 min · video call] https://quantvault.org/jane-street-phone-round-2-interview.html Multiple independent brain-teaser questions back-to-back: they don't build on each other, so you can't lean on context from one problem to the next. Probability-heavy with a sprinkle of game theory or combinatorics. Pacing matters as much as the answers. 4. Phone Round 3 — Hard Probability Deep-Dive [45 min · video call · one problem] https://quantvault.org/jane-street-phone-round-3-interview.html One hard probability problem. You'll spend the full 45 minutes on it. Often Markov chains, optional stopping, martingales, or a clever generating-function trick: the kind of problem that doesn't have a clean closed form, where the depth of the conversation is the signal more than the final number. 5. Super Day - Morning: Market-Making Games [Onsite - 2-3 live mock-trading games] https://quantvault.org/jane-street-super-day-morning-interview.html The day opens with two or three live market-making games, the real gate: you quote a two-sided market on an uncertain value, the interviewer trades against you, and you update quotes and manage inventory as order flow arrives. They watch number sense, pricing under uncertainty and handling being picked off. Lunch follows; a weak morning usually ends the day. 6. Super Day - Afternoon: Coding & Data Science [Onsite - coding + data-science rounds (if you advance)] https://quantvault.org/jane-street-super-day-afternoon-interview.html Candidates who clear the morning games move to the afternoon: hands-on coding problems and data-science / statistics questions (clean implementation, reasoning about data, light modelling). There is no separate final wrap-up - the whole day is one long super-round of back-to-back interviews, and this technical block is the last of it. ### JPMorgan (Quantitative Research) interview process URL: https://quantvault.org/jpmorgan-interview-process.html Roles: Quantitative Research (campus) | Last reviewed: 2026-10-09 1. Online Assessment [60 min, 2 coding questions (HackerRank); languages differ by region] https://quantvault.org/jpmorgan-online-assessment.html Candidate-reported: a 60-minute HackerRank sitting, two sections of one coding question each, timer unpausable. NAMR allows Python 2/3 only; EMEA Python 2/3, Java 7/8/15/17 and C++ (one EMEA editor also C# and JavaScript). Six reported 2027 sittings (three NAMR, three EMEA) drew nine unique questions from a small shared rotation; 'AB' / 'BB' deletion appeared word for word in both. 2. HireVue Video Interview [3 recorded questions - ~30 s prep, 2 min max per answer, 1 retry each (HireVue)] https://quantvault.org/jpmorgan-hirevue-video-interview.html Format: One-way, after the HackerRank OA; candidate-reported. 3 questions: ~30 s prep, 2 min max, 1 retry each. No ChatGPT or unapproved AI tools. After the third answer: 'Your answers have been submitted', no score. 2026 live questions (Quantitative): - 'What is your top priority in deciding your next job?' - 'Imagine that you had to present complex information to someone with limited knowledge of the subject. What steps would you take in order to help ensure your audience understood your presentation?' - 'Tell me about a time when you detected a potential issue and developed a solution before it turned into a real problem. Describe the situation, the actions you took, and the outcome.' 2027 bank: 49 JPM-wide HireVue prompts (opener, 25 behavioral STAR, 5 situational, 6 commercial awareness, 12 IB-flavored technical), all on the prep page. Firm-wide, not QR-specific. 3. SHL Values and Mindset Assessment [SHL - 76 forced-choice statement triads, untimed] https://quantvault.org/jpmorgan-shl-values-mindset-assessment-interview.html SHL's personality questionnaire, sent with the coding OA: 76 items of three first-person statements. Pick the one most like you, then the better of the other two (most > middle > least): you cannot agree with everything. Nothing is objectively correct; SHL reads profile shape and coherence, and near-identical repeats check consistency. Our OA-page walkthrough: candidate-reported high-scoring picks per triad, with reasoning. ### Jump Trading interview process URL: https://quantvault.org/jump-trading-interview-guide.html Type: Proprietary HFT / algorithmic trading | Founded: 1999 | HQ: Chicago, IL | Roles: Software Engineer / Core Dev, Quant Researcher, Quant Trader | Last reviewed: 2026-05-29 1. HR / Recruiter Call [30–45 min · phone] https://quantvault.org/jump-trading-hr-recruiter-call-interview.html Background, resume, motivation, and English fluency. For SWE: 'why C++', 'why Jump'. Mostly a conversation: you usually pass if communication is solid. HR is responsive and fast (often two recruiters follow up). 2. Online Assessment [Codility · 2–3 problems · ~80–150 min] https://quantvault.org/jump-trading-online-assessment.html SWE/Core-Dev gate (QR usually skips it). Codility: 2-3 coding problems, very long prose and heavy string parsing; ~LC easy-medium, yet parsing and a complexity-gated last problem cause timeouts. Recurring: stack/RPN calculator (push/pop/DUP/POP + overflow → −1), `ls -l` parser, buy/sell-indicator → position vector, most-frequent fraction, knapsack stock-picking, 'fix the bug in ≤3 lines' binary search. 3. Technical Phone Screen [45–60 min · CoderPad / Zoom] https://quantvault.org/jump-trading-technical-phone-screen-interview.html Track-split. SWE: resume deep-dive + C++ trivia + one coding: anchored by the famous 'implement std::vector' (2× growth, allocation, exception safety, type traits, emplace via std::forward + placement new; a `Vector ` small-buffer variant exists). QR: straight into 2-3 problems mixing probability + linear algebra + one coding (resume rarely covered). 4. Quant / Math Round [Superday · probability / linear algebra / game theory] https://quantvault.org/jump-trading-quant-math-interview.html The QR math round: probability, linear algebra, combinatorics, Bayesian reasoning, optimal stopping, and game theory. Recurring: the variance-minimizing array split (O(n) running variance → k-group DP), the guess-⅓-of-the-mean game (mixed strategy over {0,1,100}), a Markov 'paint the balls until all match', and constructed-matrix problems. 5. Live Data-Science Round [~60 min · live pandas + ~75 min ML coding] https://quantvault.org/jump-trading-live-data-science-interview.html Jump's distinctive QR round: given a pandas time-series dataset live, build a model end-to-end in ~60 minutes (train/test split, rolling window, Lasso/sklearn) with finance-intuition follow-ups, plus a 'bring your favorite public dataset to discuss' prep, and a ~75-min round to debug/complete a long pre-written ML script. Reserved interviewers, few hints. 6. Onsite Coding + System Design [Superday · ~4–7 rounds · C++ + systems] https://quantvault.org/jump-trading-onsite-coding-system-design-interview.html The engineering superday (VO or in-person): two technical coding rounds (C++ + algorithms), often a system-design round (a TCP/UDP news-push protocol to thousands of clients, design Twitter, a CSV backend), and behavioral rounds with increasingly senior bosses. Hard live problems seen: implement `malloc` / a bucketed custom allocator, an O(1)-space list-intersection, and an optimized `unordered_map`. 7. Behavioral / Culture [Interspersed · small boss → big boss] https://quantvault.org/jump-trading-behavioral-culture-interview.html Behavioral rounds with increasingly senior leaders, woven through the onsite. Standard prompts: a failure and how you recovered, a project explained to a non-technical person, how colleagues would describe you. The vibe is reserved and secretive: read the room and keep answers tight. ### Man Group interview process URL: https://quantvault.org/man-group-interview-guide.html Type: Listed investment manager (systematic and discretionary) | Founded: 1783 | HQ: London | Roles: AHL Quant Researcher (Quant Talent Programme), AHL Quant Summer Intern, Graduate programmes (other tracks) | Last reviewed: 2026-10-09 1. Online Assessment [5 timed parts · numerical, sequences, logic, coding, probability (candidate-reported, Sep 2026)] https://quantvault.org/man-group-online-assessment.html Candidates report five parts. 1. Numerical reasoning: Quick arithmetic on data tables (for example, inferring a missing value from relationships that hold across years). 2. Number patterns: 50 questions in 10 minutes. 3. Logical reasoning, bank-style: Whether one statement supports another or is irrelevant to it, and whether an action correctly follows a workplace rule and whether its result was expected. 4. Coding: An array-based task. 5. Probability: About 10 to 12 questions in roughly 12 minutes. One candidate summed it up as Optiver-style speed tests plus an investment bank aptitude test. The platform may be Correlation One (a logo on screen suggests it), unconfirmed. Practise with [number sequences](number-sequence-test-trading-interviews.html), [Optiver Number Logic](optiver-number-logic.html), [probability questions](probability-interview-questions.html) and [coding questions](coding-interview-questions.html). 2. Technical Interview [Live · coding + statistics + math, no resume questions (candidate-reported)] https://quantvault.org/man-group-technical-interview.html Candidates report a three-part technical interview, no resume questions. Man Group's careers page also lists pre-recorded video or telephone interviews (via Sonru) for shortlists. A September 2026 report (medium-confidence attribution): coding the fewest fountains to water a line, each spraying a given distance left and right (maybe the OA's coding part). Statistics: multicollinearity (what it breaks, fixes, regularization trade-offs, how PCA works). Math: feasible range of a common pairwise correlation among n assets. See [regression questions](regression-interview-questions.html) and [linear algebra questions](linear-algebra-interview-questions.html). 3. Final Interviews / Assessment Day [Face-to-face or virtual, depending on role (per Man Group)] https://quantvault.org/man-group-final-assessment-day-interview.html Man Group's careers page says final-stage candidates meet business representatives in face-to-face interviews or assessment days, in office or virtually by role. We do not yet hold a full verified 2027 report. Expect the technical interview's mix plus systematic-investing motivation; below: every bank problem candidates tied to Man Group. Reading prep, reported by an AHL quant research candidate in October 2026: they were asked to read four pages of Colin Bennett's book Trading Volatility, pp. 161 to 164 in Section 6.3. The report does not say which round discusses them. ### Marshall Wace interview process URL: https://quantvault.org/marshall-wace-interview-guide.html Type: Alternative investment manager (systematic + fundamental) | Founded: 1997 | HQ: London, UK | Roles: Quant Researcher (Quant Associate / TOPS), Quant Developer (Quant Implementation / TOPS), Quant Research Intern | Last reviewed: 2026-09-26 1. Initial Test [~2 hr - in person at a Pearson test centre (current cycle); reports vary] Candidates report, and MW's guides confirm, a timed technical exam first: 2 hours in person at a Pearson VUE test centre this cycle. Below: every bank problem candidates have tied to Marshall Wace. Pearson's page: 18 questions, negative marking (+2 correct, -1 incorrect, 0 unanswered). MW's topics: probability, statistics, regression, algebra, matrices, optimization, algorithm design. Earlier internship cycles: three online tests (60-minute Codility coding, 60-minute CorrelationOne technical, 45-minute CorrelationOne data); some Glassdoor reviewers describe a longer maths test plus Codility and a data test. Reports vary. 2. Human Capital Screening Call [15-30 min - Zoom (reports vary by track)] https://quantvault.org/marshall-wace-human-capital-screening-call-interview.html After the test, candidates report a short Zoom call with a member of MW's Human Capital team. MW's Quant Associate guide lists 15 minutes and the internship guide 30 minutes. Per the guides it covers motivation (why this role and how it fits your goals), competency questions about past experiences mapped to MW's values, and time for your own questions. 3. Lab Interview - Data Exercise [~1 hr - Zoom with a quant; some reports describe 2 hr] https://quantvault.org/marshall-wace-lab-interview.html First quant-led technical round (Quant Associate), assessing how you work with data, logical reasoning and clear explanation. MW's current guide: one hour (15 minutes reading, a 40-minute video call on a dataset with a Quant team member, 5 minutes for your questions; Excel, Python, R or MATLAB). Others report two hours: a 60-minute Data Lab in a virtual machine, then an hour on Zoom presenting answers and a signal-construction case. 4. Technical Project Presentation [~60 min - Zoom (internship) or at the assessment day (Quant Associate)] https://quantvault.org/marshall-wace-technical-project-presentation-interview.html Both MW guides include a ~15 to 20 minute presentation of a past technical project you choose (university assignment, PhD thesis, side project), then questions probing any part for depth. Internship: 60-minute Zoom after the HR call. Quant Associate: at the assessment day with no devices, so bring a printout or send key material at least 3 working days ahead. 5. Assessment Day - Brainteaser Interview [~50 min - in person, two brainteasers] https://quantvault.org/marshall-wace-assessment-day-brainteaser-interview.html Assessment day in person (MW's London office for interns; one of MW's offices for associates, travel reimbursed where needed). Brainteaser interview: two mathematical puzzles (50 minutes, internship guide), judged on approach, not only the answer. Past example: turn-based dice, crossing out newly rolled numbers; go first or second? Associate days: ~9am start, four interviews, may end after the first two. 6. Assessment Day - Case Study + Programming [2 interviews - in person; the case differs by track] https://quantvault.org/marshall-wace-assessment-day-case-study-programming-interview.html Associate assessment-day morning, per MW's guide. Case study: Quant Researchers build a trading signal with the interviewer, discussing forecasting models and challenges (predictive, quantitative-finance, statistical modelling); Quant Implementation candidates do a system-design case (structure, data handling, component interfaces, performance, trade-offs). Programming: review written code and answer core-principles questions; past example: minimum-cost staircase dynamic programming. Little more case detail exists. 7. Final - Management Interviews + Shadowing [Assessment-day afternoon, or arranged individually] https://quantvault.org/marshall-wace-final-interview.html Strong candidates meet managers informally two or three at a time per MW's guides (team structure, management style, fit), and shadow a quant: same afternoon for associates, maybe individually arranged for interns. Some Glassdoor reviewers describe an assessment day of up to six interviews; reviews are mixed. Offers aimed within two weeks, via a Human Capital Zoom on team allocation. ### Maven Securities interview process URL: https://quantvault.org/maven-securities-interview-guide.html Type: Employee-owned proprietary trading firm (options market making, discretionary and systematic strategies) | Founded: 2011 | HQ: London | Roles: Graduate Trader, Quant Trading Intern, Quant Researcher, Software Developer, Spring Week | Last reviewed: 2026-10-09 1. Online Assessment [Mental maths + sequences + probability tests; coding or games by track (reported)] https://quantvault.org/maven-securities-online-assessment.html Maven lists four possible online assessments by programme: numerical, probability, HackerRank coding, Arctic Shores game-based. Trading candidates consistently report a two-part numerical test (~50 mental maths questions in 5 minutes; ~30 number and letter sequences in 6) and a negatively marked probability test, ~18 multiple-choice questions in 30 minutes. Maven recommends timed [Zetamac](zetamac-practice.html) practice; also try the [Flow Traders replica](flow-traders-oa.html), [sequences](number-sequence-test-trading-interviews.html), [expected value](expectation-interview-questions.html). 2. Recorded Video Interview [About 20 minutes, on demand, no live interviewer (per Maven's graduate profile)] https://quantvault.org/maven-securities-recorded-video-interview.html Maven's graduate profile describes an on-demand video interview, about 20 minutes, on your motivation for Maven. Candidates report recorded questions on interests beyond your studies and why trading; one mentions mental arithmetic like the day of the week some weeks ahead (reported). Prep: [behavioral questions](behavioral-interview-questions-trading-firms.html), [mental math](mental-math-for-trading-interviews.html). 3. HR + Technical Interview [About 40 minutes with a trader (per Maven's graduate profile)] https://quantvault.org/maven-securities-hr-technical-interview.html Maven: about 40 minutes of technical questions on logical and numerical reasoning. Candidates report a short HR element, mental maths with percentages, probability, brainteasers, sometimes a first market-making exercise without pen and paper. One late-2025 intern report: a confidence interval estimate, comparing a large power with a factorial. Practise: [probability](probability-interview-questions.html), [estimation facts](estimation-facts.html), [Fermi estimation](fermi-estimation-interview-questions.html). 4. Assessment Day with Senior Traders [About 4 hours (per Maven's graduate profile); candidates report 1 to 4 hours of interviews] https://quantvault.org/maven-securities-assessment-day-senior-traders-interview.html Maven's final assessment day (about four hours): meet the team, show technical ability, logical problem solving and motivation. Reports: in-depth senior trader interviews, card-based market-making and expected value games, timed Fermi estimates, sometimes the probability and mental maths tests again in person, then individual interviews. Try: [Make a Market](make-market.html), [card table](card-table.html), [market-making guide](market-making-game-interview-guide.html), [betting game](betting-game.html). ### Millennium Management interview process URL: https://quantvault.org/millennium-interview-guide.html Type: Multi-manager / pod shop (multi-strategy hedge fund) | Founded: 1989 | HQ: New York, NY | Roles: Quant Researcher, Quant Developer, Data Engineer, Data Scientist | Last reviewed: 2026-07-17 1. Application & Resume Screen [~20 min · recruiter] Resume/application review and a short recruiter conversation that routes you to a track (QR, QD/SDE, DE, DS) and lays out the assessment path. Candidate-reported. 2. Online Assessment [Track-dependent · 24-hour take-home (QR) or timed HackerRank] https://quantvault.org/millennium-online-assessment.html QR interns: 24-hour take-home market-making project (Jupyter/JupyterLab), four challenges: basic bid/ask quoting class; inventory-skewed quotes + a daily risk-exposure metric you define yourself + visualization; extensions. Gotcha: lowercase, counterparty-side 'buy'/'sell' in order flow. Others: timed HackerRank OAs (120-min 7-question QR hiring test; ~6-question / 3-hour QD/SDE coding; SQL/pandas/ML for DE/DS). 3. Caliper Personality Test [~60 min · online psychometric] A Caliper personality assessment in the QR intern funnel: a standard trait/work-style questionnaire. Candidate-reported as part of the process. 4. Technical Interview Rounds [Multiple rounds · pod / team dependent] https://quantvault.org/millennium-technical-interview.html Pod-specific technical interviews. Reported QR content: probability and conditional expectation (limits of the mean/variance of quadratic-form averages), pricing a European option on a bond future, and heavy pandas/time-series follow-ups on the take-home. QD/SDE rounds add Python async debugging, streaming quote-buffer / market-data design, and LeetCode-style coding; DE/DS rounds add SQL, pandas, and an ML task. 5. PM / Pod Fit — Final [~30–45 min · with a PM / team lead] https://quantvault.org/millennium-pm-pod-fit-interview.html Because Millennium is a pod shop, the final step is fit with the specific PM pod or central team you'd join: motivation, working style, and how you'd contribute to that book. Candidate-reported. ### Morgan Stanley interview process URL: https://quantvault.org/morgan-stanley-interview-guide.html Type: Global investment bank (equities, fixed income, investment banking, wealth management) | Founded: 1935 | HQ: New York | Roles: IED Quantitative Finance (Strats) Summer Analyst / Associate, FID Industrial Placement / Summer Analyst | Last reviewed: 2026-10-09 1. HackerRank Online Assessment [120 min · 14 questions · HackerRank, Python 3 (candidate-reported, 2026 sitting)] https://quantvault.org/morgan-stanley-online-assessment.html Candidates for 2027 IED Quantitative Finance Summer Analyst / Associate roles in Hong Kong report a single 120-minute, full-screen HackerRank test (login 29 September to 7 October 2026). Four sections: Statistics and Applied Math (6 multiple-choice each, some with more than one answer) on applied data science: regularization, skew-reducing transforms, outlier rules, z-scores, Poisson modelling, conditional probability, counting, vector and matrix algebra. Python Basic and Intermediate (1 task each) are practical (URL query string, class-based inventory tracker), not algorithm puzzles. No pausing once started. Our timed replica runs all 14 with verified answers. Warm up: [statistics](statistics-interview-questions.html), [probability](probability-interview-questions.html), [coding questions](coding-interview-questions.html). 2. One-Way Video Interview [5 recorded questions · 30 s prep, 1.5 min answer, 1 take · about 10 min] https://quantvault.org/morgan-stanley-one-way-video-interview.html Candidates report a one-way video interview after the OA (unnamed platform resembling HireVue): 5 questions, about 10 minutes, 30 seconds to prepare, up to 1.5 minutes per answer, single take, recruiter-reviewed with no computer-assisted evaluation. IED Quantitative Finance (2026 sitting): 5 behavioral questions: recent goal and plan to reach it, three words a professor or manager would use for you, why Morgan Stanley in Hong Kong. 2 on AI: how it changes the strat role (Morgan Stanley's term for quants) and where people still add most value; your biggest misstep with AI tools so far. FID Industrial Placement, Hong Kong and Singapore (candidates report, 2026 sitting): Same format, 5 market-view questions: fixed income allocation amid geopolitics and long-term trends; upside inflation surprise through economy, rates and FX in 2026. The commodities cycle with three fixed income conviction trades, a private credit plan after recent stress, how you've used generative AI to research, analyze or build. Prepare with [behavioral questions for trading firms](behavioral-interview-questions-trading-firms.html). 3. Live Interviews / Superday [Live interviews after the video round (format varies by division; not yet verified for 2027)] https://quantvault.org/morgan-stanley-live-superday-interview.html We do not yet hold a verified 2027-cycle report of these live rounds. Public guides describe Morgan Stanley's superday: back-to-back interviews with desk staff, technical plus motivation and fit; strats should expect probability, expected value and coding. Practice below: every bank problem candidates tied to the firm, including earlier Brownian motion and regression reports. See also [Morgan Stanley interview questions](morgan-stanley-interview-questions.html). ### Old Mission Capital interview process URL: https://quantvault.org/old-mission-interview-guide.html Type: Proprietary trading firm / ETF-focused market maker (also options) | Founded: 2008 | HQ: Chicago, IL | Roles: Quant Trader, Software Engineer, Quant Researcher | Last reviewed: 2026-05-30 1. Online Assessment [Timed - math + coding] https://quantvault.org/old-mission-online-assessment.html The process opens with a timed online assessment covering quantitative/probability questions and (for engineering) coding. Speed and accuracy both matter. 2. Probability & Mental Math [45-60 min - video] https://quantvault.org/old-mission-probability-mental-math-interview.html Core probability, expectation, statistics, and mental math under time pressure, with follow-ups. This is the heart of the trading screen. 3. ETF, Options & Market-Making Round [45-60 min - trading intuition] https://quantvault.org/old-mission-options-market-making-interview.html For trading roles: an ETF fair-value or creation/redemption question, a put-call parity check, options pricing intuition, and a live market where the interviewer trades against you and you re-quote. Kelly sizing and game-theory-flavored decision problems also come up. 4. Onsite - Hardest Rounds [Final rounds - the toughest reported problems] The hardest reported Old Mission problems across every topic -- probability, options/market-making, Kelly sizing, and coding. (These are the hard-rated problems; the earlier rounds cover the easy/medium set, so nothing here repeats.) ### Optiver interview process URL: https://quantvault.org/optiver-interview-guide.html Type: Proprietary options / ETF market maker | Founded: 1986 | HQ: Amsterdam, NL | Roles: Quant Researcher, Quant Trader, Software Engineer | Last reviewed: 2026-09-15 1. Online Assessment [Optiver Assessment Portal · multiple timed modules · trader/QR (SWE: separate coding test)] https://quantvault.org/optiver-online-assessment.html Heavy first filter on the Optiver Assessment Portal: long, anti-GPT, often split across days and auto-rejected on submission. Your first sitting counts: there are no resets, and results are reused for 8 months. Trader/QR modules: NumberLogic (timed sequences), Beat the Odds (20-30 probability questions, ~90s each, pick the closest), Zap-N reaction mini-games (balloon gambling, stacking, dual-task arithmetic, memory), plus the 2026 additions Likelihood (rank three events by probability from a chart/table), Intervals and Orderbooks. The 80-in-8 does not appear on 2026/27 invites. Amsterdam, September 2026: six clocked modules; Orderbooks 8 min for 20 questions, Intervals ~40 to 45s each, Likelihood Test 25 min, sequences skip-and-return. FutureFocus Quants applicants (Optiver's five-day Amsterdam program) sit the same OA: a January 2026 sitting had NumberLogic plus a 30-question Beat the Odds at 90s each. SWE candidates take a separate coding test (2-3 business-flavored medium-hard problems). 2. HR / Behavioral Call [~20 min · recruiter] https://quantvault.org/optiver-hr-behavioral-call-interview.html A short conversational round: why Optiver, why trading / quant, other offers, visa/sponsorship, and a walk-through of a project. Functions as a culture/logistics check and lays out the remaining flow. 3. Trading / Betting Game [25–45 min · interactive · 1-on-1 with a trader] https://quantvault.org/optiver-trading-betting-game-interview.html Signature filter for BOTH QT and QR. Bet a bankroll across simultaneous boards (3 coin flips, 2 dice, 3-card draws) whose odds reshuffle each round, plus a market-making sub-game (two-sided market on three cards' sum). A trader trades against you asking 'why this bet.' Scored: Kelly sizing, mispriced-odds arbitrage, risk discipline, not raw P&L. Too conservative = cut; rehearse below. 4. Cognitive Interview [45-60 min · Zoom with a trader · Optiver Assessment Platform] https://quantvault.org/optiver-cognitive-interview.html Final stage for Optiver's Career Kickstarter: Trading program (per a candidate's September 2026 invitation): a 45 to 60 minute Zoom with one trader on the Optiver Assessment Platform, camera on. Games, brainteasers and puzzles, including a market-making game (cheat sheet of terms sent beforehand). Paper and pencil for some games; no calculator or phone. Optiver says it re-tests the online assessment's speed and accuracy and your approach: new information arrives as you go, and if the trader seems to hint, they probably are. 5. Technical / Brain-Teaser [60–75 min · Zoom whiteboard · with a researcher] https://quantvault.org/optiver-technical-brain-teaser-interview.html The QR technical round: 3-4 problems mixing probability, conditional expectation, number theory, and brain-teasers, plus a Fermi estimation. Recurring favorites include the two-piles-of-stones doubling invariant, the bus-waiting paradox, a Latin-square diagonal-sum proof, and 'how many years until you're 97.5% sure the price exceeds 100.' They want a clean argument, not just an answer. 6. Take-Home Data Project + Deep-Dive [Take-home · then ~45 min presentation] https://quantvault.org/optiver-take-home-data-project-deep-dive-interview.html QR candidates get an algo-trading dataset and build/backtest a strategy. It's graded on realized profit, not R²: a clean linear model is enough to pass. About two days after you submit, you're invited to a presentation / deep-dive where they probe your methodology hard, with heavy follow-ups on pandas and time-series handling. 7. Market-Making Game Day [Interactive · multiple rounds · trading floor or virtual] https://quantvault.org/optiver-market-making-game-day-interview.html QT-distinctive round(s), reported as 5 rounds, target total score ≥ 2.0: quote a closed interval [L, U] on a numeric estimate, scoring L/U if inside (tighter = higher), else 0. Horse-race variant: 4-8 horses, win-probability proportional to ability; fast EV / odds estimation, spot arbitrage. Onsite QT may be a full day of trading games and group exercises. [Timed replica](optiver-intervals.html); practise the market-making part in [Card Table](card-table.html) and [Make a Market](make-market.html). 8. Final — Culture-Fit [~45 min · with a hiring manager / partner] https://quantvault.org/optiver-final-interview.html Nominally behavioral with a senior partner, functionally a fit / taste decision: the round most famous for rejecting technically-strong candidates. Free-flowing, with curious interruptions. Expect 'why quant, not academia', your personal story, 'what's your edge over others', a blunt strengths/weaknesses ranking vs your cohort, and career goals. Fast turnaround. Optiver applies an 8-month cooling-off period after any unsuccessful application for a similar role. ### PDT Partners interview process URL: https://quantvault.org/pdt-partners-interview-guide.html Type: Systematic quant hedge fund | Founded: 2012 | HQ: New York, NY | Roles: Quant Researcher, Software Engineer | Last reviewed: 2026-05-30 1. Recruiter / Screen [Phone - background + fit] An intro conversation about background and interest, sometimes with a quick technical warm-up. 2. Take-Home / Coding [Take-home + algorithms] https://quantvault.org/pdt-partners-online-assessment.html A coding take-home plus clean algorithm/data-structure problems (merge k sorted streams, reference-counted smart pointer, lazy-propagation stack). 3. Probability & Statistics [45-60 min - video] https://quantvault.org/pdt-partners-probability-statistics-interview.html Probability, expectation, statistics, and combinatorics -- estimator reasoning and Bayesian updates. 4. Regression, ML & Time-Series [45-60 min - research] https://quantvault.org/pdt-partners-regression-ml-time-series-interview.html Regression pitfalls, machine learning, linear algebra, and time-series with a research flavor. 5. Onsite - Hardest Rounds [Final rounds - the toughest reported problems] The hardest reported PDT Partners problems across every topic. (Hard-rated only; earlier rounds cover easy/medium, so nothing repeats.) ### PEAK6 interview process URL: https://quantvault.org/peak6-interview-guide.html Type: Proprietary options market maker (PEAK6 Capital Management) | Founded: 1997 | HQ: Chicago, IL (trading desk); PEAK6 group HQ in Austin, TX since 2025 | Roles: Trading Associate (new grad), Trading Intern / Bootcamp, Quant Researcher, Quant Developer, Software Engineer | Last reviewed: 2026-10-03 1. Online Assessment [~30 questions / 30 min timed aptitude test after you apply; HackerRank coding for software roles (reported)] https://quantvault.org/peak6-online-assessment.html PEAK6 follows every application with an assessment. Trading candidates report a timed test of arithmetic, probability, basic finance, number series and logic puzzles, about a minute per question; one report adds personality questions. Software roles: about two HackerRank problems in around 90 minutes (interval/scheduling-style). Drill: [mental math](quant-mental-math-practice.html), [number sequences](number-sequence-test-trading-interviews.html), [probability](probability-interview-questions.html). Timed replicas of the aptitude test and the read-aloud math round: [PEAK6 online assessment practice](peak6-online-assessment.html). 2. First Interview - Technical + Behavioral [1 hr - Zoom, with a trader (per PEAK6)] https://quantvault.org/peak6-first-interview.html PEAK6 lists a one-hour Zoom interview with a trader. Candidates describe a behavioral half (why trading, why PEAK6, what is happening in markets) and a technical half: mental math, probability, expected value, brainteasers, estimation such as sizing a market or setting odds. One report: about 30 mental math problems in four minutes. Practise: [mental math](mental-math-for-trading-interviews.html), [Fermi estimation](fermi-estimation-interview-questions.html), [expected value](expectation-interview-questions.html). 3. Final Round - Trading Case + Market Making [Half day - Zoom (per PEAK6); older reports describe an in-person Chicago superday] https://quantvault.org/peak6-final-interview.html PEAK6's final: half-day Zoom with leadership and future teammates; software finalists report live coding (parsing a CSV or quote feed). Candidates report back-to-back trader and manager interviews (probability, brainteasers, mental math), a trading case study and market making. In one account you quote the sum of six dice rolled one at a time, updating as they land and tracking position and P&L (reported); others play as a group. Rehearse: [Make a Market](make-market.html), [card market table](card-table.html), [market-making guide](market-making-game-interview-guide.html), [options Greeks guide](options-greeks-interview-guide.html). 4. Leadership + Fit Conversations [Part of the final; decision in about two weeks (per PEAK6)] https://quantvault.org/peak6-leadership-fit-conversations-interview.html Several reviewers say PEAK6 weights team fit heavily, with more than one behavioral conversation and time with leadership during the final. PEAK6 says it may take up to two weeks to share a decision. Prepare with [behavioral questions for trading firms](behavioral-interview-questions-trading-firms.html) and [questions to ask your interviewers](questions-to-ask-quant-interviewers.html). ### Point72 / Cubist interview process URL: https://quantvault.org/point72-interview-guide.html Type: Multi-strategy hedge fund; systematic arm = Cubist Systematic Strategies | Founded: 2014 | HQ: Stamford, CT | Roles: Quant Researcher, Data Scientist, Quant Developer | Last reviewed: 2026-07-17 1. Application & Resume Screen [~20 min · recruiter] https://quantvault.org/point72-application-resume-screen-interview.html Application review and a short recruiter call that routes you to a track (Cubist QR, Data Scientist/Analyst, or Quant Developer) and sets up the OA. Candidate-reported. 2. Online Assessment [Track-dependent · 60 min (QR) to 3 hr (Developer)] https://quantvault.org/point72-online-assessment.html Cubist QR: 5 HackerRank Python tasks (date→quarter, no libraries; sum of odd divisors; sqrt by bisection; longest subarray, sum ≤ k; two circles' relative position), sometimes plus regression MCQs. Data Scientist/Analyst: pandas/SQL (auction-lots current price/winner, DictMixin/JSONMixin, IP validation, longest even-length word, linked-list splice). Quant Developer: 3-hour Developer Test (dependency-aware task scheduler over machines, complexity write-up). 3. Take-Home Data Project [Take-home · Python + write-up] https://quantvault.org/point72-take-home-data-project-interview.html Three-part take-home: Thinknum vendor review (< 200 words, features for a systematic trader) and Python scrape of 2022 TreasuryDirect auctions into pandas (dtype/key suggestions, patterns). Then a 5-position P&L (daily total returns 8/1-12/31/2022, consensus earnings estimates at both ends, drivers), minding corporate actions. 4. Technical Interviews [Multiple rounds · video / phone] https://quantvault.org/point72-technical-interview.html Interviews pair probability with regression and hands-on data. Reported: optimal-stopping and probability games (alternating flips stopping at HT/HTH, dice optimal stopping, airplane seats, roulette adjacent bullets, correlation of ±1 variables given P(same sign)); regression theory (Gauss-Markov, linear-in-parameters, k > n and ridge, swapping/rescaling X, why raw time series breaks OLS); and live pandas/SQL (groupwise max, k-th largest per name). 5. Final — PM / Academy Fit [~30–45 min · with the team / hiring manager] https://quantvault.org/point72-final-interview.html A fit round with the hiring team (or, for early-career, the Point72 Academy): motivation, working style, and how you'd contribute to the systematic research process. Candidate-reported. ### Quantlab Financial interview process URL: https://quantvault.org/quantlab-interview-guide.html Type: Proprietary high-frequency trading firm | Founded: 1998 | HQ: Houston, TX | Roles: Quant Trader, Quant Researcher, Quant Developer | Last reviewed: 2026-07-17 1. Coding / Quant Screen [Candidate-reported · algorithmic coding + math] https://quantvault.org/quantlab-online-assessment.html An early screen mixing algorithmic coding with quantitative reasoning. Candidate-reported items include a matrix-transpose implementation, correlated-random-variable generation, a simple ODE (u' + u = c(t)), and probability warm-ups. Treat it as a fundamentals filter before the markets-heavy interview. 2. ETF & Trading-Knowledge Interview [Candidate-reported · ~45-60 min · conversational with a trader/researcher] https://quantvault.org/quantlab-etf-trading-knowledge-interview.html Quantlab's signature round walks through a real instrument, classically an international ETF whose underlying trades in another time zone. How do you PRICE it while that market is closed (ADRs, futures as proxies)? HEDGE it (regression hedge)? QUOTE it as a market maker; why a premium over fair value? What if inventory keeps building? Reason aloud; they push back. 3. Final / Onsite [Candidate-reported · fit + deeper technical] https://quantvault.org/quantlab-final-onsite-interview.html A closing onsite / fit round that revisits your reasoning and research depth and checks whether you'll thrive on a small prop-trading floor. Candidate-reported specifics are limited; be ready to go deeper on any earlier answer and to talk through a project end-to-end. ### Qube Research & Technologies (QRT) interview process URL: https://quantvault.org/qube-research-technologies-interview-guide.html Type: Systematic / quantitative investment manager | Founded: 2018 | HQ: London, UK | Roles: Quant Researcher, Quant Developer | Last reviewed: 2026-07-17 1. Online Assessment [HackerRank · algorithmic coding · candidate-reported] A HackerRank-style algorithmic OA that gates the process. Candidate-reported problems are trading-flavored: a trading-platform event log returning net profit at each QUERY (Σ position × cumulative price change), a minimum-cost array-reduction (Huffman-style min-heap greedy, O(n log n)), and a longest-substring-with-at-most-k-restricted-characters sliding window. Auto-graded: clean, correct, efficient code gates progress. 2. Technical Interview [Coding + probability/statistics · candidate-reported] https://quantvault.org/qube-research-technologies-technical-interview.html Coding (DS/algos): design and justify a matrix class; two missing numbers in 1..k+2; fewest moves-to-back keeping prefix sums nonnegative; single-edit string transforms. Quant probability/stats: XOR of a fair and a two-headed coin; P(sum of 100 dice > 400) via CLT; correlated standard normals (Corr 0.5): P(X > 3Y), P(X > 3Y | X > 0). 3. Final / Onsite [Candidate-reported · deeper technical + fit] https://quantvault.org/qube-research-technologies-final-onsite-interview.html A closing onsite that goes deeper on algorithms, modeling, and fit, and checks that you can work in an engineering-heavy research org. Candidate-reported specifics are limited; be ready to extend any earlier problem and to talk through a project end-to-end. ### Radix Trading interview process URL: https://quantvault.org/radix-trading-interview-guide.html Type: Proprietary high-frequency trading firm | Founded: 2012 | HQ: Chicago, IL (+ NYC) | Roles: Quant Technologist, Quant Researcher | Last reviewed: 2026-05-30 1. Recruiter / Screen [Phone - background + fit] An intro conversation about background and interest, sometimes with a quick technical warm-up. 2. C++ & Algorithms [60-90 min - coding] https://quantvault.org/radix-trading-online-assessment.html C++ implementation and numerical problems: reference-counted shared_ptr, fixed-buffer allocator, stable summation (Kahan), tournament min-comparisons. 3. Probability & Numerical [45-60 min - video] https://quantvault.org/radix-trading-probability-numerical-interview.html Probability, combinatorics, and statistics with a numerical bent (variance computation, sampling). 4. Linear Algebra & Optimization [45-60 min - research] https://quantvault.org/radix-trading-linear-algebra-optimization-interview.html Linear algebra (eigenvalues, conditioning), optimization (LP/least-absolute-deviations), regression, and stochastic processes. 5. Onsite - Hardest Rounds [Final rounds - the toughest reported problems] The hardest reported Radix Trading problems across every topic. (Hard-rated only; earlier rounds cover easy/medium, so nothing repeats.) ### Renaissance Technologies interview process URL: https://quantvault.org/renaissance-interview-guide.html Type: Systematic quant hedge fund | Founded: 1982 | HQ: East Setauket, NY (Long Island) | Roles: Quant Researcher, Research Programmer / Software Engineer | Last reviewed: 2026-05-30 1. Recruiter / Research Screen [Phone / video - behavioral + background] https://quantvault.org/renaissance-recruiter-research-screen-interview.html An initial conversation about your research background and fit. Renaissance hires scientists, so this leans into your PhD/research work and how you think, not finance experience. Often light technical sprinkled in. 2. Online Assessment [Timed - coding + applied statistics] https://quantvault.org/renaissance-online-assessment.html A timed screen of clean algorithm problems and applied-statistics tasks. Implementation is expected to be correct and efficient; the statistics tasks reward understanding estimators and distributions, not memorized formulas. 3. Probability & Statistics Round [45-60 min - video] https://quantvault.org/renaissance-probability-statistics-interview.html Core probability and statistics with heavy follow-ups: distributions, estimator properties, Bayesian updating, and inference. Interviewers extend each problem (change a parameter, add noise, take a limit) to see how deeply you understand it. 4. Regression, Time-Series & Linear Algebra [45-60 min - video] https://quantvault.org/renaissance-regression-time-series-linear-algebra-interview.html The signal-research core: OLS properties and pitfalls (omitted-variable bias, errors-in-variables, regularization), time-series (AR mean reversion, autocorrelation, spurious regression, effective sample size), and linear algebra (PCA, eigenstructure, covariance matrices). 5. Modeling & Optimization Round [45-60 min - open-ended] https://quantvault.org/renaissance-modeling-optimization-interview.html Open-ended modeling and optimization: extract a signal from very noisy data, choose and justify a model, reason about overfitting and regularization, and solve continuous optimization problems (e.g. maximize a product under a sum constraint). 6. Research Deep-Dive [Multiple rounds - hardest, most research-flavored] https://quantvault.org/renaissance-research-deep-dive-interview.html The onsite pulls together everything: the hardest probability/statistics, regression/time-series, linear algebra, modeling, and coding -- plus a deep dive into your own research. This round surfaces the full set of reported Renaissance problems across all topics. 7. Offer / Background Check [References + background check + NDA] After the loop, offers come with a background check and strict NDAs. The firm is secretive by design, so expect limited detail and a deliberate process. ### Rothesay interview process URL: https://quantvault.org/rothesay-interview-guide.html Type: UK pension insurer (bulk annuities and pension risk transfer) | Founded: 2007 | HQ: London | Roles: Trading Summer Intern, Asset Origination Summer Intern, Graduate (trading and asset management) | Last reviewed: 2026-10-09 1. Canditech Online Assessment [20 min · 25 questions in 2 timed sections · proctored, no pause or retake (candidate-reported, 2027 cycle)] https://quantvault.org/rothesay-online-assessment.html Candidates for the 2027 Trading and Asset Origination internship report a 20-minute Canditech test in two 10-minute sections. Quantitative reasoning (advanced), 10 questions: 5 typed answers and 5 multiple choice; short word problems on percentages and successive changes, ratios, weighted averages, combined work rates, a margin table and simple probability. Critical thinking, 15 questions: A mini Watson-Glaser in five parts of three (inferences on a five-point scale, assumptions, deductions, interpretations, and strong or weak arguments). The test is proctored: switching tabs or windows is reported to the hiring team, copy and paste is detected, and you cannot pause or retake. Our timed replica runs all 25. Warm up with [mental math practice](quant-mental-math-practice.html). 2. Online Interview [About 30 minutes (per public candidate reviews)] https://quantvault.org/rothesay-online-interview.html Public candidate reviews describe a short online interview after the test, about 30 minutes, on motivation, the role and your background. We do not yet hold a verified 2027 report. Prepare with [behavioral questions for trading firms](behavioral-interview-questions-trading-firms.html). 3. Assessment Centre [Back-to-back interviews plus a group exercise (per public candidate reviews)] https://quantvault.org/rothesay-assessment-centre-interview.html Public candidate reviews describe an in-person assessment centre with several back-to-back interviews, each with two interviewers, plus a group exercise. Interviewers give scenario questions about financial products and ask for your reasoning on how you would underwrite or evaluate them. We do not yet hold a verified 2027 report. ### Schonfeld Strategic Advisors interview process URL: https://quantvault.org/schonfeld-interview-guide.html Type: Multi-manager / multi-strategy | Founded: 1988 | HQ: New York, NY | Roles: Quant Researcher, Portfolio Manager | Last reviewed: 2026-07-17 1. Online Assessment — Coding [Timed · online coding] https://quantvault.org/schonfeld-online-assessment.html A coding OA, described by candidates as 'one easy, one hard' problem. Details are thin in our intel, so prepare a standard algorithmic-coding screen (clean, correct, edge-case-aware) rather than a firm-specific format. 2. QR Technical — Factor Models / ML / Regression [45–60 min · with a researcher (QIS)] https://quantvault.org/schonfeld-qr-technical-interview.html The core QR round. Expect Fama-French factor construction, controlling overfitting, time-series vs cross-sectional cross-validation, multicollinearity, backtest metrics (Sharpe / IR / IC / ROC), and OLS-is-BLUE plus the GLS formula. They probe whether your signals would survive out of sample, not just whether you can quote a definition. 3. Probability / Brainteaser [45 min · with a researcher / trader] https://quantvault.org/schonfeld-probability-brainteaser-interview.html A lighter probability / brainteaser round. Reported examples include the Frobenius coin problem (largest amount not representable with 5 and 21) and the drunk-passenger airplane-seat problem (answer 1/2). Clean reasoning beats a memorized answer. 4. Final — Research Fit / Deep-Dive [Conversational · with senior researchers] https://quantvault.org/schonfeld-final-interview.html A closing stage combining a resume / research deep-dive with an alpha-strategy pitch and culture fit. Be ready to defend real research and to propose signals you find compelling. ### Shell interview process URL: https://quantvault.org/shell-interview-guide.html Type: Integrated energy company with a large trading arm | Founded: 1907 | HQ: London | Roles: Graduate Programme, Internship (trading-related track reported) | Last reviewed: 2026-10-09 1. Online Test [4 parts · 84 items · no timer shown, Finish Later allowed (candidate-reported, 2027 cycle)] https://quantvault.org/shell-online-assessment.html Candidates report a Shell-branded online test with no maths. Part 1, judgment scenarios (8): Pick the response you are most and least likely to give from four options, on situations such as a heavy workload, a disengaged teammate, a tight deadline as a new hire and an outside report of an error. Part 2, work simulation (8): An information-security scenario with a Resources tab (organization chart, sharing policy, directory): who must approve sharing a document, spotting phishing emails and handling requests to use personal email. Part 3, self-assessment (13): Rate your learning habits, performance against others, computer skills and expected tenure. Part 4, forced-choice pairs (55): Choose which of two statements is more like you. There is a Finish Later button and no visible timer. See also the [Bank of America battery](bank-of-america-online-assessment.html), which uses the same scenario style. 2. One-Way Video Interview [3 recorded questions · 3 min each · unlimited retakes (candidate-reported, 2027 cycle)] https://quantvault.org/shell-one-way-video-interview.html Candidates report a Shell-branded one-way video interview: 3 questions, 3 minutes each, unlimited re-recording, an 'I'm Ready' screen before each. In order (2027): seeking feedback or advice (help received, your response, what you learned about yourself); working with someone who disagreed (managing the conversation, the outcome); why Shell, framed by its Powering Progress strategy. Prep: [behavioral questions for trading firms](behavioral-interview-questions-trading-firms.html). 3. Assessment Day [Case study, presentation and interviews (per earlier public reports; varies by country)] https://quantvault.org/shell-assessment-day-interview.html We do not yet hold a verified 2027 report of this stage. Earlier public reports describe a final assessment day with a business case study and presentation, a group exercise, an in-tray style exercise and competency-based interviews, with assessors looking for problem solving, communication, teamwork and commercial awareness. Practise structured case answers with [Fermi estimation questions](fermi-estimation-interview-questions.html). ### Susquehanna (SIG) interview process URL: https://quantvault.org/sig-interview-guide.html Type: Proprietary options market maker | Founded: 1987 | HQ: Bala Cynwyd, PA | Roles: Trader / Quant Researcher, Quant Strategy Developer, Software Engineer | Last reviewed: 2026-10-09 1. Problem-Solving Assessment [17 questions · 60 min · Mettl (some reports: HackerRank)] The gate for everything: timed, near-static, graded BEFORE a recruiter sees your resume. Acing it: necessary, not sufficient. Grade-school logic + probability bank, only numbers change: leg-counting, knights & knaves, Bayes tables, dice EV, binomial 'enough muffins', arrangements, bold-betting Markov chain, no-3-consecutive lattice-path DP. Replica: a September 2026 sitting ('The Susquehanna Problem Solving Assessment'), verbatim in Mettl's widgets, plus number variants. SWE: CodeSignal coding OA (4 problems / 70 min). 2. Technical HR / Recruiter Screen [30–45 min · audio Zoom] https://quantvault.org/sig-technical-hr-recruiter-screen-interview.html Despite the 'HR' name, this is technical. After a self-intro and a chat about your interests (mention poker/bridge/mahjong), the recruiter reads you 2-3 green-book-style probability / brain-teasers verbally and you work them on paper. Recurring: bus/movie uniform-arrival (5/24), HHT-before-HTH coin-pattern Markov, median of three U[0,3] in [1,2], obtuse triangle on a circle, the two-envelope problem. 3. Quant / Technical Phone [45 to 60 min · 1 to 2 rounds · phone or video] https://quantvault.org/sig-quant-technical-phone-interview.html Harder probability plus SIG's signature 5-dice mahjong-style game (win = full house OR run-plus-pair; pick the reroll maximizing win probability / minimizing expected rerolls, EV intuition over rigor), often with an OLS / regression-efficiency derivation. Plus an algorithm question (approach + complexity, usually no live coding). 2027 cycle, technical round 1: reportedly a 60-minute phone interview, 2 to 4 questions; a pass brings a short HR call. September 2026's set was reused with new follow-ups: regression-slope design points (then x-dependent noise), two-stage voting fairness, streaming digit median (then C++ data types), ten-dice divisibility bet. 4. Take-Home Data Exercise + On-Site Presentation [3-day take-home · then an on-site project presentation round] https://quantvault.org/sig-take-home-data-exercise-onsite-presentation-interview.html Candidates in the 2027 cycle report a 3-day take-home data exercise on a single stock: about a year of trades, one share each, buyer and seller trader ids on every print. - Maximize expected profit: at most 5 buys and 5 sells a day, flat overnight, no new trades in the last 30 min, force-close from 15:30. Live, ids show only on your fills (rest at day end). - Submit a 1 to 2 page report plus code or notebook (8 to 10 hours suggested); the dataset recurs. Then present it on-site, with detail, technical, trading and market-making questions. - Earlier reports instead: a ~2-hour virtual review by two quants (data, strategy), steering to a strategic-interaction / game-theory framing over naive conditional distributions. 5. Market-Making Game + EV Games [Interactive · with a trader] https://quantvault.org/sig-market-making-game-poker-interview.html SIG's games-as-interview round: quote a bid/ask spread on an uncertain value (e.g. true value in [40, 60], width-4 market), adjusting as a trader trades against you; explain every choice. Poker-style EV games such as the AKQ betting game probe EV and risk. Some older candidate reports also mention Acey-Deucy; other reports describe live poker as part of SIG's training culture rather than the interview. Rehearse below. 6. Onsite / Superday [Onsite · multiple 1-hr rounds + dinner] https://quantvault.org/sig-onsite-superday-interview.html The Bala Cynwyd onsite: pre-dinner with a young trader/QR, building, data-center and trading-floor tours. Several 1-hour quant rounds (open-ended estimation, 'estimate X given Z=X+Y normals', the AKQ game), a ~75-min data round (EV, expectation/variance trade-offs), at times a team game. Some older reports mention live Acey-Deucy poker; other reports say poker is training culture, not an interview round. 8-11 rounds with a VO. 7. SWE / Quant-Dev Coding + Systems [Phone + onsite · C++ / algorithms / system design] https://quantvault.org/sig-swe-quant-dev-coding-systems-interview.html The engineering funnel. After the CodeSignal OA, a C++ phone screen built around SIG's flagship 'implement std::vector' question (2× growth, allocation/reclaim, exception safety, type traits, ownership), then an onsite of pointers/polymorphism/virtual functions, a trade-book system design (price-level matching, treemap vs heap), and live C++ debugging. ### Squarepoint Capital interview process URL: https://quantvault.org/squarepoint-interview-guide.html Type: Systematic multi-strategy quant fund | Founded: 2014 | HQ: New York, NY (+ London, global offices) | Roles: Quant Researcher, Quant Developer, Software Engineer | Last reviewed: 2026-05-30 1. Online Assessment [Timed - algorithms + DP (not always given)] https://quantvault.org/squarepoint-online-assessment.html Some candidates get a timed coding screen first (others skip straight to interviews). It frequently hinges on dynamic programming -- max-profit trading variants, sequence/queue problems. Correct, efficient solutions expected. 2. Junior Interview - Round 1 [45-60 min - video, with a junior team member] https://quantvault.org/squarepoint-junior-round-1-interview.html The first of two technical rounds with junior interviewers: probability, expectation and statistics (Bayesian inference, dice EV, conditional probability) plus a coding problem. The coding problem can fill the round: a Nov 2026 report had about 40 minutes on one hard greedy question graded on hidden tests. Some reports open with a deep dive into one resume project. 3. Junior Interview - Round 2 [45-60 min - video, with a junior team member] https://quantvault.org/squarepoint-junior-round-2-interview.html The second junior technical round - broad and could pull from anything: probability, statistics, regression, coding, or applied stats. This round surfaces the full breadth of reported Squarepoint problems, so treat it as your general technical prep set. 4. Senior Round - Your Background [45-60 min - with a senior team member] https://quantvault.org/squarepoint-senior-interview.html A senior interviewer digs into your background, past projects, and research. Less about canned puzzles, more about how you think and what you've actually done. Be ready to defend technical choices. 5. Superday - 5-Hour Data-Science Exam [onsite / virtual - 5 hours, Python + pandas on their desktop] https://quantvault.org/squarepoint-superday-interview.html The superday: a single ~5-hour, hands-on data-science exam in THEIR environment (Python + pandas, provided desktop) on a real dataset (candidates report power-storage and order-flow data). Load, clean, wrangle, explore, build and validate a model, weighing pitfalls: R-squared abuse, multicollinearity, duplicated/leaky rows, time-series structure. Open-ended and time-pressured, so pandas fluency (groupby/merge/resample/pivot/rolling/vectorized ops) matters as much as statistics. Practice wrangling + modeling first. 6. Closing Senior Interview [~25 min to 1 hour+ - prepared questions, resume grilling, behavioral] A final conversation with a senior person, and reports differ on length. - Some describe about 25 minutes of prepared questions plus behavioral, a fit-and-judgment check more than a fresh technical gauntlet. - A recent final ran over an hour: very inquisitive questions on everything on the resume, with the conversation often drifting off-topic and not tied to any team or desk. ### Tower Research Capital interview process URL: https://quantvault.org/tower-research-interview-guide.html Type: Proprietary high-frequency trading firm | Founded: 1998 | HQ: New York, NY | Roles: Quant Trader, Core Developer (C++), Quant Researcher | Last reviewed: 2026-05-30 1. Online Assessment [Developer 75-min 4-problem / wangce HackerRank DP / QR-QT probability] https://quantvault.org/tower-research-online-assessment.html Tower's screen is track-dependent. Developer / Quant-Dev: a 75-minute, 4-problem coding OA (a global-variable MCQ plus difference-array, hashmap+heap, and stack-parser problems), or a HackerRank DP set. QR / QT: a heavily probability-weighted technical screen (coupon collector, covariance of dice counts, random-walk distances, order-statistics). A strong C++ and probability bar is decisive. 2. Recruiter / Team Matching [Phone - behavioral + team fit] https://quantvault.org/tower-research-recruiter-team-matching-interview.html An intro conversation about background and interests, sometimes with a quick mental-math or probability warm-up. Crucially, this is also where you get routed toward a specific team -- and that team, not a central process, drives everything that follows. 3. Probability & Brain-Teasers [45-60 min - video] https://quantvault.org/tower-research-probability-brain-teasers-interview.html The core quant screen: probability, expectation, and brain-teasers under time pressure, with follow-ups. Order statistics, random walks, and Markov chains are common. 4. C++ & Algorithms [60 min - coding + C++ internals] https://quantvault.org/tower-research-cpp-algorithms-interview.html For Core Developer roles: hard algorithm problems plus C++ internals (virtual destructors, const correctness, memory model). Clean, efficient, correct code is expected. 5. Quant / Statistics Round [45-60 min - video] https://quantvault.org/tower-research-quant-statistics-interview.html For research/trading: statistics, regression (R-squared properties, duplicated-data effects), and stochastic processes (Brownian motion, Markov chains). Connect the math to trading. 6. Team-Matched Rounds [Multiple rounds - format set by your team] https://quantvault.org/tower-research-team-matched-interview.html There is no single Tower onsite -- the team you matched with runs its own rounds, so the exact format and emphasis differ pod to pod (some lean trading/probability, others heavy C++/systems). This round surfaces the complete set of reported Tower problems across all topics so you can prep broadly regardless of which team you land on. ### Two Sigma interview process URL: https://quantvault.org/two-sigma-interview-guide.html Type: Single-manager | Founded: 2001 | HQ: New York, NY | Roles: QR, QDev, Modeling Associate | Last reviewed: 2026-05-22 1. Online Assessment [SWE ~75 min · QR/DS ~3 h · HackerRank] https://quantvault.org/two-sigma-online-assessment.html Track-split HackerRank screen. QR / Data Science: about three problems over a long window (~3 h), heavy on data wrangling and statistical reasoning. SWE: two algorithmic problems in about 75 minutes (QuantVault reports; the pair was refreshed in late 2025, and other reports say 90 minutes). Auto-graded: correctness gates progress to the recruiter call. 2. Technical Round 1 — Open-ended Data Science [45–60 min · video + shared screen] https://quantvault.org/two-sigma-technical-round-1-interview.html The exploratory round. Interviewer gives you a fuzzy problem ('here's some return data: find a signal'), or asks how you'd validate a research finding. Expect open-ended discussion across regression, ML basics, and time-series intuition. 3. Technical Round 2 — Coding [45 min · CoderPad (Python preferred)] https://quantvault.org/two-sigma-technical-round-2-interview.html Medium-hard algorithmic problem. Not LeetCode-hard, but they want clean code, correct edge-case handling, and one optimization conversation after the first working solution. 4. Technical Round 3 — Stats & Math [45 min · whiteboard] https://quantvault.org/two-sigma-technical-round-3-interview.html Two Sigma's signature round. Heavy on regression theory: re-derive OLS β̂, set up an MLE, walk through bias-variance, contrast OLS vs WLS, and handle the classic 'asymmetry' tricks (Y on X vs X on Y, omitted-variable bias, residualization). They will follow up: 'OK, now what if the noise isn't Gaussian?' 5. Hiring Manager Rounds — 3 rounds, topic depends on the team [3 × 45–60 min · with three different hiring managers] https://quantvault.org/two-sigma-hiring-manager-interview.html Three sequential hiring manager rounds, each with the hiring manager of a different team. Topic depends entirely on that team: research teams probe past projects, execution teams ask microstructure, alpha-blending teams ask about signal correlation. One round usually skews behavioral. You can't pre-study by domain; what you CAN do is rehearse a reasoning framework that holds up under unfamiliar setups. 6. Final — Senior MD / VP [60 min · conversational] https://quantvault.org/two-sigma-final-interview.html A senior MD or VP, almost all behavior and fit: do they want to work with you for years? No specific skill test. Expect scenario walkthroughs ('here's some data, how would you analyze it?'), past-project deep-dives ('tell me about a research call you got wrong'), culture-fit probes; technical questions usually frame behavioral signal. ### Virtu Financial interview process URL: https://quantvault.org/virtu-interview-guide.html Type: Global multi-asset market maker | Founded: 2008 | HQ: New York, NY | Roles: Quant Trader / Researcher, Quant Strategist / Developer, Software / Automation | Last reviewed: 2026-05-29 1. Online Assessment [75 min · 5 problems · Codility / HackerRank] https://quantvault.org/virtu-online-assessment.html The frozen coding bank that's gated candidates for years: minimum ±1 steps to the nearest Fibonacci number, max apples into a 5000g box (greedy after sort), HexSpeak validation, student-score stabilization, and counting identical-character substrings. A 2025 variant swaps in finance-flavored tasks (ETF fair value, most-profitable trade, a pandas school-count). 2. HR / Behavioral Screen [20–30 min · phone] https://quantvault.org/virtu-hr-behavioral-screen-interview.html Self-intro, 'why quant / why Virtu', and behavioral prompts (negative feedback, teamwork, conflict), then almost always 1-3 easy brain teasers at the end (clock angle, bridge crossing, dice expectation). Reportedly 'miss one and you're cut', so the teasers are a real gate. 3. Technical Phone [~1 hr each · video] https://quantvault.org/virtu-technical-phone-interview.html Resume/research deep-dive plus probability, expectation, and statistics: the coin tails-then-heads game, frog jumps until the sum exceeds 1 (E = e), the handshake-grid expectation, P(≥60 heads in 100) via CLT, dependent-but-uncorrelated constructions. Quant strategists also get one coding round (e.g. Connect Four). 4. Onsite / Superday [Back-to-back rounds · opens with an IQ test] https://quantvault.org/virtu-onsite-superday-interview.html Opens with a ~30-60 min IQ/logic test (50-60 figure-pattern and number-sequence questions, some GRE vocab and mental math), then resume + mixed probability/combinatorics/brain-teaser rounds, and a final modeling-heavy round with a team lead / regional head (a common rejection point). Traders may also get an arithmetic/mental-math test. 5. Final [Modeling-heavy] https://quantvault.org/virtu-final-interview.html A final round with a team lead or regional head, modeling-heavy and a common rejection point. Expect open-ended reasoning (infer which of two unknown strategies you're running; design a measurement scheme minimizing variance) plus fit. ### The Voleon Group interview process URL: https://quantvault.org/voleon-interview-guide.html Type: Machine-learning hedge fund | Founded: 2007 | HQ: Berkeley, CA | Roles: Software Engineer, ML Research Staff, Data Scientist | Last reviewed: 2026-05-29 1. Online Assessment [Coding OA / take-home (1-1.5 hr) + Data Analyst SQL/pandas] https://quantvault.org/voleon-online-assessment.html Voleon's OA is coding/take-home: a flagship exchange-execution simulation plus classic algorithm problems (largest bounded sub-grid, anagrams, file parsing, increment-stack), and for analyst roles a 4-SQL + 1-pandas data task. QR / ML Research candidates typically skip this and start with the reading list + research phone below. The bar is high and rejections are fast. Take the auto-graded problems below. 2. Phone / Tech Screen [45–60 min · HackerRank / phone] https://quantvault.org/voleon-phone-tech-screen-interview.html One hard LeetCode-style problem + an optimization follow-up, read from stdin / written to stdout with real test cases. The single highest-frequency item is the Basic Calculator series (+−, then ×÷, then parentheses, then variable substitution, then resolving a dict of inter-dependent variable expressions via topological sort). 3. Onsite / VO [4 rounds · 1hr + 2.5hr coding + design] https://quantvault.org/voleon-onsite-vo-interview.html Hard SWE loop: Word Search II (N×N, M words, 8 directions, stdin/stdout; Trie+DFS pruning vs TLE). Core: 4-part order-matching engine (limit→market→stop→cancel; heaps, clean code). Whiteboard: sparse matrix (add/multiply) or hashmap internals. System design: huge-map point→country (quadtree), dependency resolution, distributed crawler. 4. ML Research Phone [~60 min · stats + ML] https://quantvault.org/voleon-ml-research-phone-interview.html Stats + ML phone after a reading list (~10 intro, 40 tech, 10 Q&A): OLS closed form, when (XᵀX)⁻¹ exists, gradient-descent least squares in basic numpy, R²-combining, Y~X vs X~Y slope puzzles, E[X|X+Y] for joint normals. Doubly-stochastic matrices, small-sample binomial median test, maybe one numpy coding task. 5. Research VO + Deep-Dive [2 × 2 hr · stats / ML / coding / research] https://quantvault.org/voleon-research-vo-deep-dive-interview.html VO Part 1: a statistics round and an ML round (interviewers are a stats PhD + a same-field PhD), including hands-on 'run the data' in R/pandas (regression, p-values, debugging weird data setups). VO Part 2: a coding round (ex-CS-professor interviewer) and a research deep-dive where a senior researcher probes your PhD work. Fit matters a lot. 6. Culture / Intro Discussion [~30–45 min · technical manager] https://quantvault.org/voleon-culture-intro-discussion-interview.html A 2025 addition, a culture/intro discussion with a technical manager or hiring manager: opinions on engineering/ML, behavioral (a proud project, a data-driven decision), and weaknesses. It's a non-trivial gate, not a formality. Reported on both the SWE and QR tracks. 7. References + Background Check [3 references · background check] Aggressive closing gates: three references (award-heavy probing for research) plus a background check. Candidates who pass the technical loop are frequently rejected here, so it's a real step. ### Wincent interview process URL: https://quantvault.org/wincent-capital-management-interview-guide.html Type: Crypto algorithmic market maker | Roles: Quant Trader, Quant Developer | Last reviewed: 2026-08-17 1. Recruiter Screen [Call - background + fit] An intro conversation about background, motivation for crypto market making, and logistics before the quant test is sent out. 2. Online Assessment — Quant Test [Testlify - 12 questions / 1h40 (40 min low + 60 min medium)] https://quantvault.org/wincent-capital-management-online-assessment.html A webcam-proctored, fullscreen Testlify quant test: 12 numeric-entry problems on probability and common logic in two sections: 6 low-difficulty questions in 40 minutes, then 6 medium questions in 60 minutes. Pen, paper, and a simple non-programmable calculator only; skip is allowed but there is no revisiting. Our timed replica with verified answers is on the OA page. An October 2026 sitting recorded video and audio throughout and swapped in three other classics (brick packing, the broken-stick triangle, matched pairs in a partial deck), so expect the question set to vary. 3. Quant Interviews [Video rounds - microstructure + probability] https://quantvault.org/wincent-capital-management-quant-interview.html Technical interviews built around the market maker's bread and butter: setting zero-profit bid-ask quotes, pricing adverse selection, skewing for inventory, and sizing repeated bets Kelly-style, alongside probability brain-teasers. 4. Coding / Quant Developer Round [Live coding - order-book flavored] https://quantvault.org/wincent-capital-management-coding-quant-developer-interview.html For the Quant Developer track: build and reason about order-book machinery (matching engines, price impact, execution) with clean data structures and explicit edge cases (cancels, partial fills). ### WorldQuant interview process URL: https://quantvault.org/worldquant-interview-guide.html Type: Global quantitative asset manager | Founded: 2007 | HQ: Old Greenwich, CT | Roles: Quant Researcher, Research Consultant, Quant Developer | Last reviewed: 2026-05-30 1. Online Assessment [HackerRank - SWE (2 algo, 120 min) / DS (4 problems) / QR Math Test] https://quantvault.org/worldquant-online-assessment.html WorldQuant's OA is track-split. Software / New-Grad: a HackerRank coding test (2 hard algorithm problems) plus a few CS-concept MCQs. Data Scientist: 120 min, 4 problems (one must be C++), where performance is the gate - brute force TLEs. Quant Researcher: a timed Math Test (probability/stats/calculus/mental math). The BRAIN/Alphathon path is a separate alpha-research competition, not a test. 2. Recruiter / Aptitude Screen [Phone / online - aptitude + behavioral] An intro screen, sometimes paired with a quantitative-aptitude / mental-math test. 3. Probability & Brain-Teasers [45-60 min - video] https://quantvault.org/worldquant-probability-brain-teasers-interview.html Probability, expectation, and brain-teasers with follow-ups. Mental math and classic puzzles are common. 4. Statistics & Finance [45-60 min - video] https://quantvault.org/worldquant-statistics-finance-interview.html Statistics (estimators, regression, hypothesis testing) and finance fundamentals (returns, risk, basic pricing), with an eye toward signal construction. 5. Alpha / Modeling & Coding [45-60 min - modeling + coding] https://quantvault.org/worldquant-alpha-modeling-coding-interview.html Signal-construction reasoning (build and validate an alpha), optimization, and a coding round. For developers this leans more on algorithms. 6. Onsite / Final Rounds [Multiple rounds - full technical bar] https://quantvault.org/worldquant-onsite-final-interview.html The full loop spans probability, statistics, finance, modeling, and coding. This round surfaces the complete set of reported WorldQuant problems across all topics. ### XTX Markets interview process URL: https://quantvault.org/xtx-markets-interview-guide.html Type: Electronic / algorithmic market maker (ML-driven) | Founded: 2015 | HQ: London, UK | Roles: Quantitative Researcher, Software Engineer, FPGA / Hardware Engineer, Quant Developer | Last reviewed: 2026-06-29 1. Online Assessment [~60–120 min · timed · reportedly TestGorilla or HackerRank-style (role-dependent)] https://quantvault.org/xtx-markets-online-assessment.html XTX's first and most-discussed filter, widely reported as one of the hardest online assessments in quant, with pass rates often cited around 5-10% (a community estimate, not an official figure). - Format varies by role and year and sources disagree on the platform, so prepare the TOPICS, not a tool. - Reported versions: Researchers get a machine-learning and probability test, often with a small data-analysis part; general / quant, about 20 questions in about 60 minutes on TestGorilla (probability, linear algebra, calculus, algorithms, brain-teasers); Engineering, HackerRank-style timed coding. - Two recurring details: no time to re-derive fundamentals, and reportedly no returning to a question once you move on. Read fully, commit, pace yourself. - It is the single biggest gate: make MLE, OLS normal equations, ridge/lasso, PCA and the SVD, and numerically stable softmax fast and automatic rather than chasing exotic topics. 2. Recruiter / First Technical Screen [45–60 min · video] https://quantvault.org/xtx-markets-recruiter-first-technical-screen-interview.html A 45-60 minute remote call after the OA, mixing a recruiter / hiring-manager component with a first technical screen. - Background and motivation ('why XTX', 'walk me through your research'), plus one or two technical questions: a statistics or ML concept for researchers, live coding for engineers (correct, efficient, complexity narrated). - Interviewers are reportedly unusually civil and curious for a top quant shop, seeking the edge of your knowledge, not gotchas, yet probing whatever you claim: be precise and candid. - Expect depth on one research project or system: the problem, why that method, how you validated it, what you would change. 3. Take-Home / Technical Data Round [Multi-hour take-home or live technical · researcher-heavy] https://quantvault.org/xtx-markets-take-home-technical-data-interview.html Researchers commonly report a mid-funnel take-home: an open-ended statistical / ML problem on a dataset (model-building, data analysis, or a more theoretical ML question). Engineering tracks may get a longer technical coding exercise instead. It grades framing an ambiguous problem, not a single right answer: a sensible baseline first, out-of-sample validation, avoiding the traps of noisy financial data (overfitting, multiple-testing inflation across many features, look-ahead leakage, non-stationarity). XTX trades tiny, fleeting edges, so conclusions must survive live data. Deliver colleague-ready work (clear structure, reproducible code, honest caveats) and expect to defend every modeling choice. 4. Onsite — Coding [60 min · live coding (Python / C++)] https://quantvault.org/xtx-markets-onsite-coding-interview.html Part of the onsite 'super day' (back-to-back 45-60 minute interviews late in the funnel, on-site or video): the coding round is one or two medium-to-hard algorithmic problems. Graded on correctness, edge cases, time/space complexity and, distinctively for XTX, numerical stability. Research-adjacent roles get numerical or data tasks (an estimator, a stable softmax / log-sum-exp, a clustering step, a streaming statistic) testing whether code survives floating point and large inputs. Confirm constraints, state complexity, narrate edge cases: clean, correct, explainable code beats a terse 'optimal' solution you cannot defend. 5. Onsite — Machine Learning / Research Deep-Dive [60 min · whiteboard / discussion · QR & ML roles] https://quantvault.org/xtx-markets-onsite-machine-learning-research-deep-dive-interview.html The round XTX is known for, setting it apart from market-making peers: a deep dive into machine learning and statistical modeling, where interviewers reportedly push one or two threads to your edge. - Topics: regularization and bias-variance, ensembles (bagging, random forests, boosting), kernels and Gaussian processes, PCA and EM, and above all pitfalls of training on financial time series. Three topics known cold beat shallow exposure to ten. - Expect 'why does this hold' over 'name the algorithm': derive OLS normal equations, the first principal component or L1 sparsity, justify a regularization strength, diagnose overfitting. - XTX forecasts at extremely low signal-to-noise, so talk returns to validation: out-of-sample tests, multiple-testing, leakage, how much edge is real. Admitting what you don't know reportedly helps. 6. Onsite — Probability, Statistics & Linear Algebra [60 min · whiteboard] https://quantvault.org/xtx-markets-onsite-probability-statistics-linear-algebra-interview.html A super-day round on XTX's mathematical foundations: derive from first principles, explain why estimators behave as they do, and move between geometry, algebra and, when asked, a few lines of code. - Probability and statistics: MLE and common distributional families, sampling and estimator properties, why we divide by n−1, hypothesis testing and multiple-testing corrections. - Linear algebra: covariance and positive-semidefinite matrices, the SVD and best low-rank approximation, eigenstructure (e.g. equicorrelated matrices), QR or Gram-Schmidt orthogonalization. - Set up the objective or probability space explicitly, turn the crank, then sanity-check with limits, symmetry and degenerate cases: catching your own error beats a lucky final number. ==================== FREE PRACTICE PROBLEMS (with solutions) ==================== --- Identifying a Truth-Teller With One Question [medium, Brain Teaser] --- https://quantvault.org/problem-1-identifying-a-truth-teller-with-one-question.html Q: Two people answer yes/no questions. Person $A$ always tells the truth. Person $B$ answers randomly -- independently saying "yes" or "no" with probability $1/2$ each, regardless of the question asked. You do not know which person is $A$ and which is $B$. You may ask exactly one yes/no question to exactly one person. 1. Construct a question that lets you identify the truthful person with probability strictly greater than $1/2$. 2. Compute that probability of correct identification. 3. Prove that your strategy is optimal -- no single yes/no question to one person can do better. A: **How to Think About It:** The key structural constraint is that the truth-teller's answer to any yes/no question is deterministic (it is pinned to the truth), while the random answerer's response is a fair coin flip independent of everything. So you are really trying to design a question where the deterministic answer from the truth-teller gives you useful information, and then exploit the fact that the random answerer only matches that answer half the time. The best you can hope for is to separate the two people's answer distributions as much as possible -- and since one distribution is a point mass and the other is a fair coin, the maximum separation is already baked in. **Quick Estimate:** Pick one person at random and ask them something where the truthful answer is "Yes" with certainty. If they say "Yes", guess they are the truth-teller; if "No", guess the other. You are right when (a) you picked the truth-teller (prob $1/2$) and they say "Yes" (prob $1$), or (b) you picked the random answerer (prob $1/2$) and they say "No" (prob $1/2$). That gives $1/2 + 1/4 = 3/4$. So the answer is $3/4$ -- and we should verify this is optimal. **Approach:** We construct the explicit question, compute the success probability, and then prove no strategy beats $3/4$. **Formal Solution:** *Step 1 -- The question.* Pick one of the two people (call them Person $X$). Ask Person $X$: "Are you the person who always tells the truth?" *Step 2 -- Analysis by cases.* - **Case 1: $X = A$ (truth-teller).** The truthful answer is "Yes" (since $A$ is indeed the truth-teller). So $A$ answers "Yes" with probability $1$. - **Case 2: $X = B$ (random answerer).** Regardless of the question, $B$ says "Yes" with probability $1/2$ and "No" with probability $1/2$. *Step 3 -- Decision rule.* If Person $X$ says "Yes", guess that $X$ is the truth-teller. If Person $X$ says "No", guess that the other person is the truth-teller. *Step 4 -- Success probability.* Since each person is equally likely to be the one you picked: $$P(\text{correct}) = P(X = A) \cdot P(A \text{ says Yes}) + P(X = B) \cdot P(B \text{ says No})$$ $$= \frac{1}{2} \cdot 1 + \frac{1}{2} \cdot \frac{1}{2} = \frac{3}{4}$$ *Step 5 -- Why $3/4$ is optimal (and you can't do better).* Design any self-referential question you like so that "Yes" maps to person $A$ and "No" maps to person $B$ (or vice versa). The truth-teller's answer will always reliably encode the correct identity. The problem is the random answerer. There is a $1/2$ chance the person you happen to ask is person $B$. And there is a $1/2$ chance that $B$ gives the exact same answer $A$ would have given. So with probability $1/2 \times 1/2 = 1/4$, you are in a world where $B$'s response is indistinguishable from $A$'s — and no question design can fix this, because $B$'s answer is an independent coin flip. That $1/4$ penalty is unavoidable. The best any strategy can achieve is $1 - 1/4 = 3/4$. **Answer:** Ask one person "Are you the truth-teller?" If they say "Yes", guess they are the truth-teller; if "No", guess the other person. The probability of correct identification is $3/4$, and this is optimal. --- Poisoned Bottle Identification with Binary Testing [medium, Brain Teaser] --- https://quantvault.org/problem-2-poisoned-bottle-identification-with-binary-testing.html Q: You have 1000 bottles of wine, and exactly one of them is poisoned. The poison is undetectable by any means other than drinking it -- any tester who consumes even a drop will die exactly 24 hours later, with no symptoms before that. Testers who drink only unpoisoned wine are perfectly fine. You have 10 volunteer testers and a single 24-hour window. At the start of the window, each tester can drink from as many bottles as you choose. After exactly 24 hours, you observe which testers have died and which have survived. 1. Design a testing scheme that guarantees you identify the poisoned bottle, no matter which one it is. 2. Prove that 10 testers are sufficient (i.e., your scheme always works for 1000 bottles). 3. Prove that 9 testers would not be sufficient -- that is, no scheme with 9 testers can guarantee identification among 1000 bottles. A: **How to Think About It:** The core challenge is that you only get one round of tests -- every tester drinks at time zero and you read results 24 hours later. So you cannot adapt based on intermediate results. This means your entire strategy must be a single pre-committed assignment of bottles to testers. The question becomes: how do you design that assignment so the pattern of deaths uniquely identifies the poisoned bottle? The key is to think of each tester as a single binary digit -- alive or dead, 0 or 1. Ten testers give you a 10-bit binary string, which can encode $2^{10} = 1024$ distinct outcomes. Since $1024 > 1000$, there are enough distinct patterns to label every bottle. **Key Insight:** Assign each bottle a unique 10-bit binary number. Tester $j$ drinks from bottle $i$ if and only if the $j$-th bit of bottle $i$'s binary label is 1. The pattern of deaths then reads out the binary label of the poisoned bottle. **The Method:** 1. Label every bottle with a unique number from $1$ to $1000$. 2. Write each label in binary using 10 digits (pad with leading zeros). For example: - Bottle 1: $0000000001$ - Bottle 2: $0000000010$ - Bottle 7: $0000000111$ - Bottle 1000: $1111101000$ 3. Assign each of the 10 testers to one bit position ($j = 1, 2, \ldots, 10$, from least significant to most significant). 4. At time zero, tester $j$ drinks from every bottle whose $j$-th bit is 1. For instance, tester 1 drinks from all odd-numbered bottles (bit 1 is 1), tester 2 drinks from bottles 2, 3, 6, 7, 10, 11, $\ldots$ (bit 2 is 1), and so on. 5. After 24 hours, observe which testers died. Construct a 10-bit string where bit $j = 1$ if tester $j$ died, and bit $j = 0$ if tester $j$ survived. 6. Convert that 10-bit string from binary to decimal. The result is the label of the poisoned bottle. **Why this works:** Each bottle has a unique binary label, so each bottle causes a unique pattern of deaths. The poisoned bottle contributes its poison to exactly the testers whose corresponding bits are 1. No other bottle shares that label, so no other bottle could produce the same death pattern. **Why 10 suffice:** With 10 testers, there are $2^{10} = 1024$ distinct possible death patterns (including the all-zeros pattern where nobody dies, which would correspond to bottle 0 -- unused since we label from 1). Since $1024 \geq 1000$, every bottle gets a unique pattern. **Why 9 are not enough:** With 9 testers, there are only $2^9 = 512$ distinct death patterns. By the pigeonhole principle, if you have 1000 bottles and only 512 possible outcomes, at least two bottles must map to the same pattern. If the poisoned bottle is one of those two, you cannot distinguish them. No testing scheme with 9 testers can guarantee identification among 1000 bottles. **Practical Considerations:** - The scheme requires each tester to drink from roughly 500 bottles (about half, since each bit is 1 for about half the labels). This is fine as long as any amount of poison is lethal -- mixing does not dilute the effect. - The all-zeros pattern (no deaths) is a valid outcome corresponding to bottle 0. Since we label from 1, this outcome would indicate an error (no bottle is poisoned), which is ruled out by the problem statement. - This is essentially a binary code with Hamming distance at least 1 between any two codewords. For guaranteed identification (not error correction), distance 1 suffices. **Answer:** Label each bottle $1$ through $1000$ in binary using 10 bits. Tester $j$ drinks from all bottles whose $j$-th bit is 1. The binary string of tester deaths gives the poisoned bottle's label. Ten testers provide $2^{10} = 1024 \geq 1000$ distinct outcomes (sufficient); nine testers provide only $2^9 = 512 < 1000$ (insufficient by pigeonhole). --- Breaking a Stick into a Triangle [easy, Probability] --- https://quantvault.org/problem-7-breaking-a-stick-into-a-triangle.html Q: A stick of length $L$ is broken at two points chosen independently and uniformly at random along its length. What is the probability that the three resulting pieces can form a triangle? A: **How to Think About It:** The heuristic is *reduce the condition, then measure area*. Three lengths form a triangle if and only if each one is less than the sum of the other two. Since the three pieces sum to $L$, "piece $<$ sum of the others" is the same as "piece $< L/2$". So the question becomes: if you break a stick at two random points, what is the probability that no piece is $L/2$ or longer? With two uniform break points, that is an area in the unit square. **Quick Estimate:** Take $L = 1$ and count the ways to fail. The left piece is too long exactly when both break points land in the right half: $\tfrac12 \cdot \tfrac12 = \tfrac14$. The right piece is the mirror image: $\tfrac14$. The middle piece is too long when the break points are more than $\tfrac12$ apart: two corner triangles of the unit square with legs $\tfrac12$, area $\tfrac18$ each, total $\tfrac14$. At most one piece can be longer than half the stick, so these failures never overlap: $P(\text{fail}) = 3 \cdot \tfrac14 = \tfrac34$, and $P(\text{triangle}) = 1 - \tfrac34 = \tfrac14$. **Approach:** Model the break points as a uniform point in the unit square, translate the three conditions into inequalities, and compute the area of the region where all three hold. **Formal Solution:** Set $L = 1$. Let $U_1, U_2 \sim \text{Uniform}(0, 1)$ be the break points, independent, so $(U_1, U_2)$ is uniform on the unit square (area 1) and probability equals area. The pieces are $$A = \min(U_1, U_2), \quad B = |U_1 - U_2|, \quad C = 1 - \max(U_1, U_2).$$ A triangle is possible if and only if $A < \tfrac12$, $B < \tfrac12$ and $C < \tfrac12$. In terms of the break points: 1. $A < \tfrac12$: the smaller break point is in the left half. 2. $C < \tfrac12$: the larger break point is in the right half. 3. $B < \tfrac12$: the break points are less than $\tfrac12$ apart. *Case $U_1 < U_2$.* The conditions read $U_1 < \tfrac12 < U_2 < U_1 + \tfrac12$. For each $U_1 \in (0, \tfrac12)$, $U_2$ ranges over an interval of length $U_1$, so the area is $\int_0^{1/2} U_1 \, dU_1 = \tfrac18$. This is the triangle with vertices $(0, \tfrac12)$, $(\tfrac12, \tfrac12)$, $(\tfrac12, 1)$. *Case $U_2 < U_1$.* By symmetry (swap the labels), another area $\tfrac18$: the triangle with vertices $(\tfrac12, 0)$, $(\tfrac12, \tfrac12)$, $(1, \tfrac12)$. The favorable region is these two triangles, which touch only at the point $(\tfrac12, \tfrac12)$. Total area: $$P(\text{triangle}) = \tfrac18 + \tfrac18 = \boxed{\tfrac14}$$ *Check by complement.* The three failure events $\{A \ge \tfrac12\}$, $\{B \ge \tfrac12\}$, $\{C \ge \tfrac12\}$ are mutually exclusive (two pieces of length at least $\tfrac12$ would leave nothing for the third), each has probability $\tfrac14$ as computed in the Quick Estimate, and $1 - 3 \cdot \tfrac14 = \tfrac14$. A Monte Carlo run with $10^6$ trials gives $0.2497$. **Answer:** The probability that the three pieces form a triangle is $\dfrac{1}{4}$. --- Expected Time for All Ants to Fall Off a Stick [hard, Expectation] --- https://quantvault.org/problem-8-expected-time-for-all-ants-to-fall-off-a-stick.html Q: A stick has length $L$. There are $n$ ants on the stick at fixed, known positions $p_1, p_2, \ldots, p_n$ (all distinct, with $0 < p_i < L$). Each ant independently and with equal probability chooses to walk left or right, and all ants move at speed 1. When two ants collide, they instantly reverse directions. When an ant reaches either end of the stick, it falls off. Let $T$ be the time until all ants have fallen off. The only randomness is in the $n$ direction choices. 1. For these positions, what is the largest value $T$ can take over all $2^n$ possible direction choices (the worst-case time)? 2. For these positions, what is $E[T]$, the expected time for all ants to fall off? Express your answers in terms of the positions $p_i$ and the stick length $L$. A: **How to Think About It:** The heuristic is *relabeling*: when two identical ants collide and both reverse, the picture is indistinguishable from the two ants walking straight through each other. The set of occupied positions at every moment is the same in both stories, so the time until the stick is empty is the same too. That turns a collision-tracking nightmare into $n$ independent walkers, each heading straight to an end. **Quick Estimate:** Take $L = 100$ with ants at $30$ and $80$. Pass-through: the ant at $30$ needs $30$ (left) or $70$ (right); the ant at $80$ needs $80$ (left) or $20$ (right). Worst case: both pick the far end, $\max(70, 80) = 80$. Expected: the four equally likely cases give $\max(30,20)=30$, $\max(30,80)=80$, $\max(70,20)=70$, $\max(70,80)=80$, average $(30+80+70+80)/4 = 260/4 = 65$. **Approach:** Replace collisions by pass-through, write each ghost ant's exit time as a two-point random variable, then take the max (worst case) and the expected max (via enumeration or the CDF). **Formal Solution:** *Step 1: Pass-through.* Swapping the labels of two ants at each collision turns "both reverse" into "both continue". The set of positions on the stick is unchanged at every time, so $T$ (the moment that set becomes empty) is unchanged. In the pass-through picture, ghost ant $i$ walks straight in its chosen direction, so its exit time is $$T_i = \begin{cases} p_i & \text{if it walks left} \\ L - p_i & \text{if it walks right} \end{cases}$$ each with probability $1/2$, independently across ants, and $T = \max_i T_i$. Write $a_i = \min(p_i, L - p_i)$ and $b_i = \max(p_i, L - p_i)$, so $T_i \in \{a_i, b_i\}$. *Part 1: Worst case.* $T_i \le b_i$ for every ant, with equality when ant $i$ heads to its farther end. Choosing that for every ant gives $$\boxed{T_{\text{worst}} = \max_{1 \le i \le n} \max(p_i, L - p_i)}$$ This is set by the ant **closest to an end** of the stick (farthest from the center), since $\max(p, L-p)$ grows as $p$ moves away from $L/2$. It is always less than $L$ because $0 < p_i < L$. *Part 2: Expected time.* Enumerating the $2^n$ equally likely direction vectors, $$\boxed{E[T] = \frac{1}{2^n} \sum_{\mathbf{d} \in \{\text{left}, \text{right}\}^n} \max_{1 \le i \le n} T_i(d_i)}$$ with $T_i(\text{left}) = p_i$ and $T_i(\text{right}) = L - p_i$. An equivalent form that is faster by hand uses independence: $P(T \le t) = \prod_i F_i(t)$, where $F_i(t) = 0$ for $t < a_i$, $1/2$ for $a_i \le t < b_i$, and $1$ for $t \ge b_i$. Since $T \ge 0$, $$E[T] = \int_0^{L} \Bigl(1 - \prod_{i=1}^n F_i(t)\Bigr)\,dt,$$ and the integrand is a step function that changes only at the $a_i$ and $b_i$. *Check on the example* ($L = 100$, ants at $30, 80$, so $a = (30, 20)$, $b = (70, 80)$): $\prod F_i = 0$ on $[0, 30)$, $1/4$ on $[30, 70)$, $1/2$ on $[70, 80)$, $1$ from $80$ on. So $E[T] = 30 \cdot 1 + 40 \cdot \tfrac34 + 10 \cdot \tfrac12 = 30 + 30 + 5 = 65$, matching the enumeration. A direct simulation with real collisions and reversals gives the same $80$ and $65$. Note that $P(T = \max_i b_i) \ge 1/2$ always (the ant achieving it just has to head to its far end), and $E[T]$ moves toward the worst case as more ants sit near the ends, because then it is likely that one of them heads the long way. **Answer:** (1) The worst-case time is $\max_i \max(p_i, L - p_i)$, set by the ant nearest an end. (2) $E[T] = 2^{-n} \sum_{\mathbf{d}} \max_i T_i(d_i)$, equivalently $\int_0^L \bigl(1 - \prod_i F_i(t)\bigr)\,dt$. For $L = 100$ with ants at $30$ and $80$: worst case $80$, expected $65$. --- HHT Before THH in Coin Flips [medium, Probability] --- https://quantvault.org/problem-10-hht-before-thh-in-coin-flips.html Q: You flip a fair coin repeatedly. Two players are watching: Player A wins if the sequence HHT appears before THH, and Player B wins if THH appears first. What is the probability that HHT appears before THH? A: **How to Think About It:** This is a Penney's game problem -- comparing which of two three-letter sequences appears first in an infinite stream of fair coin flips. The key insight is that these probabilities are NOT symmetric, even though both sequences have the same single-occurrence probability of $(1/2)^3$. The structure of how the sequences overlap with each other matters enormously. To solve it, set up a Markov chain where the states track the longest relevant suffix of the flips seen so far. **Quick Estimate:** Ask what has to happen before HH first appears. If the very first two flips are HH, the next T completes HHT, and THH cannot finish first because it needs a T followed by two more H's. Otherwise some flip before the first HH is a T, so the flip just before that first HH must be T (if it were H, HH would have appeared one flip earlier). That T plus the HH is THH, completed at the same moment HH first shows up, before HHT can add its T. So HHT wins exactly when the game opens HH: $P = \tfrac{1}{2}\cdot\tfrac{1}{2} = \tfrac{1}{4}$. The Markov chain below confirms it. **Approach:** Define Markov chain states based on the relevant suffix and solve a system of linear equations. **Formal Solution:** Define states by the relevant suffix of flips observed: - $\emptyset$: start (no relevant suffix) - $H$: last flip was H (and no longer relevant suffix) - $T$: last flip was T - $HH$: last two flips were HH - $TH$: last two flips were TH - $HHT$: absorbing -- HHT wins - $THH$: absorbing -- THH wins Let $p_s$ = probability HHT wins from state $s$. **State $HH$:** Flip H $\to$ stay at $HH$; flip T $\to$ $HHT$ (win!). $$p_{HH} = \frac{1}{2} p_{HH} + \frac{1}{2}(1) \implies p_{HH} = 1$$ Once you reach $HH$, HHT is guaranteed to win eventually -- you either get T immediately (HHT) or keep flipping H until you do. **State $TH$:** Flip H $\to$ $THH$ (lose!); flip T $\to$ $T$. $$p_{TH} = \frac{1}{2}(0) + \frac{1}{2} p_T$$ **State $T$:** Flip H $\to$ $TH$; flip T $\to$ stay at $T$. $$p_T = \frac{1}{2} p_{TH} + \frac{1}{2} p_T \implies p_T = p_{TH}$$ Substituting $p_T = p_{TH}$ into the equation for $p_{TH}$: $$p_{TH} = \frac{1}{2} p_{TH} \implies p_{TH} = 0$$ So $p_T = 0$. Once you're in state $T$, HHT can never win. Why? From $T$, you need $H$ to make progress toward HHT, but that gives $TH$. From $TH$, getting $H$ completes $THH$ (loss), and getting $T$ sends you back to $T$. HHT requires entering through $HH$, but you can never reach $HH$ from $T$ without passing through $TH \to THH$ first. **State $H$:** Flip H $\to$ $HH$; flip T $\to$ $T$. $$p_H = \frac{1}{2} p_{HH} + \frac{1}{2} p_T = \frac{1}{2}(1) + \frac{1}{2}(0) = \frac{1}{2}$$ **State $\emptyset$:** Flip H $\to$ $H$; flip T $\to$ $T$. $$p_{\emptyset} = \frac{1}{2} p_H + \frac{1}{2} p_T = \frac{1}{2} \cdot \frac{1}{2} + \frac{1}{2} \cdot 0 = \frac{1}{4}$$ **Answer:** $P(\text{HHT before THH}) = \dfrac{1}{4}$. THH is the heavy favorite, winning $3/4$ of the time. --- HTH vs HTT Pattern Race [medium, Probability] --- https://quantvault.org/problem-501-hth-vs-htt-pattern-race.html Q: Alice and Bob are watching a single stream of fair coin flips. Alice wins as soon as the pattern $\text{HTH}$ appears. Bob wins as soon as $\text{HTT}$ appears. Whichever pattern shows up first in the sequence determines the winner. What is the probability that Alice wins? A: **How to Think About It:** Both patterns start with $\text{HT}$, so the race is really about what happens after we see $\text{HT}$. If the next flip is $H$, Alice wins ($\text{HTH}$). If it is $T$, Bob wins ($\text{HTT}$). That immediately suggests the answer might be $1/2$. But you need to be careful -- maybe the way the sequence reaches state $\text{HT}$ is biased somehow? The right way to check is to set up a Markov chain on the longest suffix of the current sequence that matches a prefix of either target pattern. **Quick Estimate:** Once you realize both patterns share the prefix $\text{HT}$, and the deciding flip is a single fair coin, you should guess $1/2$ right away. The only thing left to verify is that there is no bias in how often you reach state $\text{HT}$ conditional on who eventually wins -- and since $\text{HT}$ is an absorbing bottleneck that both patterns must pass through, there is no such bias. **Approach:** Build a Markov chain on the relevant suffixes and solve for the probability of Alice winning from the start state. **Formal Solution:** Define states by the longest suffix of the sequence seen so far that is a prefix of either $\text{HTH}$ or $\text{HTT}$: - $S$ (start): no useful suffix - $H$: last flip was $H$ - $HT$: last two flips were $H, T$ - $\text{HTH}$: Alice wins (absorbing) - $\text{HTT}$: Bob wins (absorbing) Transitions (each branch has probability $1/2$): | State | Flip $H$ | Flip $T$ | |-------|----------|----------| | $S$ | $H$ | $S$ | | $H$ | $H$ | $HT$ | | $HT$ | Alice wins | Bob wins | Note the key transitions: - From $S$, a $T$ does not advance either pattern, so we stay at $S$. - From $H$, another $H$ keeps us in state $H$ (we still have a trailing $H$). - From $HT$, the next flip is decisive. Let $p_S$, $p_H$, $p_{HT}$ be the probability Alice wins from each state. From $HT$: $$p_{HT} = \frac{1}{2}(1) + \frac{1}{2}(0) = \frac{1}{2}$$ From $H$: $$p_H = \frac{1}{2} p_H + \frac{1}{2} p_{HT} = \frac{1}{2} p_H + \frac{1}{4}$$ Solving: $p_H = \frac{1}{2}$. From $S$: $$p_S = \frac{1}{2} p_H + \frac{1}{2} p_S = \frac{1}{4} + \frac{1}{2} p_S$$ Solving: $p_S = \frac{1}{2}$. **Answer:** The probability that Alice wins is $\dfrac{1}{2}$. The two patterns are equally likely to appear first because they share the prefix $\text{HT}$, and the deciding flip from state $\text{HT}$ is a fair coin. --- Mislabeled Bins [easy, Brain Teaser] --- https://quantvault.org/problem-12-mislabeled-bins.html Q: You have three bins of rubber balls. One bin has only yellow balls, one has only red balls, and one has a mix of both. Someone has stuck labels on the bins — "Yellow," "Red," and "Mixed" — but every single label is wrong. You can draw one ball at a time from any bin you choose. 1. What is the fewest number of draws you need to figure out which bin is which? From which bin should you draw? 2. **Follow-up:** Suppose you have $n$ bins, each containing balls of a single color or some mix, and all $n$ labels are wrong. How does your strategy generalize? A: **How to Think About It:** The key constraint is that *every* label is wrong. That is not just noise -- it is a hard constraint that eliminates possibilities. When you hear "all labels are wrong," your first instinct should be to figure out how much information you actually need. Three bins with three possible contents feels like you might need multiple draws, but the mislabeling constraint is so strong that a single draw can unravel the whole thing. **Quick Estimate:** Three bins, three labels, all wrong. There are only $3! = 6$ possible assignments of contents to bins, and the constraint "no label is correct" means we are looking at derangements. The number of derangements of 3 items is $D_3 = 2$. So before drawing anything, there are only two possible configurations. A single draw that distinguishes between those two configurations is enough. The answer is $1$. **Approach:** Use the mislabeling constraint to do logical elimination after one carefully chosen draw. **Formal Solution:** Label the bins by their (incorrect) labels: $B_{\text{Yellow}}$, $B_{\text{Red}}$, $B_{\text{Mixed}}$. Since every label is wrong: - $B_{\text{Yellow}}$ does not contain only yellow balls. - $B_{\text{Red}}$ does not contain only red balls. - $B_{\text{Mixed}}$ does not contain mixed balls. The critical observation: $B_{\text{Mixed}}$ is labeled "Mixed" but is *not* mixed. So it must contain either all yellow or all red balls. Draw one ball from $B_{\text{Mixed}}$. **Case 1:** You draw a yellow ball from $B_{\text{Mixed}}$. - $B_{\text{Mixed}}$ actually contains only yellow balls. - $B_{\text{Yellow}}$ cannot be yellow (wrong label) and cannot be mixed (that slot is already taken by... wait, mixed is still unassigned). Actually: $B_{\text{Yellow}}$ is not yellow, and $B_{\text{Red}}$ is not red. The remaining contents to assign are {Red, Mixed}. - $B_{\text{Yellow}}$ can be Red or Mixed. But $B_{\text{Red}}$ cannot be Red, so $B_{\text{Red}}$ must be Mixed, and $B_{\text{Yellow}}$ must be Red. **Case 2:** You draw a red ball from $B_{\text{Mixed}}$. - $B_{\text{Mixed}}$ actually contains only red balls. - By symmetric reasoning: $B_{\text{Red}}$ must be the mixed bin, and $B_{\text{Yellow}}$ must be the yellow bin... but wait, $B_{\text{Yellow}}$ cannot be yellow (it is mislabeled). So $B_{\text{Yellow}}$ must be Mixed, and $B_{\text{Red}}$ must be Yellow. In both cases, one draw uniquely determines all three bins. **Answer:** The minimum number of draws is $1$. Draw from the bin labeled "Mixed." Since it is mislabeled, it contains only one color. Whichever color you draw tells you what that bin actually holds, and the mislabeling constraint forces the other two bins into their only valid assignment. --- Monty Hall With Envelopes [medium, Probability] --- https://quantvault.org/problem-13-monty-hall-with-envelopes.html Q: You are given three envelopes. One contains $\$2$ and the other two are empty. You pick one envelope at random but do not open it. The interviewer, who knows where the money is, opens one of the remaining two envelopes and shows you that it is empty. You now have a choice: stick with your original envelope, or switch to the other unopened envelope. 1. What strategy maximizes your probability of winning the $\$2$? 2. What is the probability of winning under each strategy? **Follow-up:** Now suppose the interviewer does **not** know where the money is. They open one of the remaining two envelopes at random, and it happens to be empty. Should you still switch? What is the probability of winning if you switch vs. stay? A: **How to Think About It:** This is the Monty Hall problem dressed up with envelopes. The key is that the interviewer is not opening a random envelope -- they *know* which one has the money and they always reveal an empty one. That asymmetry is everything. Before you do any math, think about it this way: when you first picked, you had a 1-in-3 shot. The interviewer's reveal does not change what is inside your envelope. It just concentrates all the remaining probability onto one specific envelope. If you can internalize that the interviewer's action is informative (it tells you something about the envelope you did *not* pick), the answer falls out immediately. **Quick Estimate:** You picked 1 of 3 envelopes, so your chance of having the money is $1/3$. The other two envelopes collectively hold $2/3$ of the probability. The interviewer removes one empty envelope from that pair, so the surviving envelope inherits the full $2/3$. Switching wins with probability $2/3$, staying wins with probability $1/3$. Switching doubles your odds. **Approach:** We can confirm this with a direct application of Bayes' theorem, conditioning on which envelope the interviewer opens. **Formal Solution:** Label the envelopes 1, 2, 3. You pick envelope 1. Let $C_i$ denote the event that envelope $i$ contains the $\$2$. Each has prior probability $1/3$. Suppose the interviewer opens envelope 3 and shows it is empty. We want $P(C_1 \mid \text{open 3})$ and $P(C_2 \mid \text{open 3})$. First, compute the likelihood of the interviewer opening envelope 3 under each hypothesis: - If the money is in envelope 1 (your pick): the interviewer can open either 2 or 3. Assuming they choose uniformly at random among valid options, $P(\text{open 3} \mid C_1) = 1/2$. - If the money is in envelope 2: the interviewer *must* open envelope 3 (they cannot open 2, which has the money). So $P(\text{open 3} \mid C_2) = 1$. - If the money is in envelope 3: the interviewer cannot open envelope 3. So $P(\text{open 3} \mid C_3) = 0$. The total probability of the interviewer opening envelope 3: $$P(\text{open 3}) = \frac{1}{2} \cdot \frac{1}{3} + 1 \cdot \frac{1}{3} + 0 \cdot \frac{1}{3} = \frac{1}{6} + \frac{1}{3} = \frac{1}{2}$$ Now apply Bayes' theorem: $$P(C_1 \mid \text{open 3}) = \frac{P(\text{open 3} \mid C_1) \cdot P(C_1)}{P(\text{open 3})} = \frac{(1/2)(1/3)}{1/2} = \frac{1}{3}$$ $$P(C_2 \mid \text{open 3}) = \frac{P(\text{open 3} \mid C_2) \cdot P(C_2)}{P(\text{open 3})} = \frac{(1)(1/3)}{1/2} = \frac{2}{3}$$ The posterior probability that the money is in your original envelope is still $1/3$. The posterior probability that it is in the other unopened envelope is $2/3$. **Answer:** You should always switch. Switching wins with probability $2/3$, while staying wins with probability $1/3$. The interviewer's forced reveal of an empty envelope concentrates the $2/3$ prior mass of the two unchosen envelopes onto the single remaining one. **Follow-up: Uninformed Interviewer** This variant changes everything. When the interviewer does not know where the money is and opens a random remaining envelope: - There is a $1/3$ chance the interviewer reveals the money (game over — you lose or restart). - Conditional on the interviewer revealing an empty envelope, the probability the money is in your original envelope is: $$P(\text{your envelope} \mid \text{revealed empty}) = \frac{P(\text{revealed empty} \mid \text{your envelope has money}) \cdot P(\text{your envelope has money})}{P(\text{revealed empty})}$$ $$= \frac{1 \cdot 1/3}{2/3} = \frac{1}{2}$$ So **switching gives no advantage** — the probability is $1/2$ either way. **Why the difference?** In the original problem, the interviewer's knowledge creates an asymmetry: they are *forced* to reveal an empty envelope, which transfers information. When the interviewer acts randomly, the reveal carries no strategic information — it is like a coin flip that happened to come up "empty." The posterior is symmetric. This distinction (informed vs. uninformed host) is the most common follow-up in quant interviews and tests whether the candidate truly understands conditional probability vs. just memorizing "always switch." --- Optimal Stopping: Three-Roll Dice Game [medium, Expectation] --- https://quantvault.org/problem-14-optimal-stopping-three-roll-dice-game.html Q: You are offered the following game: roll a fair six-sided die up to 3 times. After each roll, you decide -- stop and collect the face value in dollars, or roll again and forfeit what you just saw. If you roll a third time, you must accept whatever comes up. What is the optimal stopping strategy, and what is the expected payout under that strategy? A: **How to Think About It:** This is a textbook optimal stopping problem, and the key move is to work backwards. At the last roll you have no choice -- you take what you get. That gives you a concrete number (the expected value of a die roll) to use as the threshold on the second roll: only stop early if you beat the value of continuing. Repeat one step back. The intuition is that each earlier roll has option value -- you can always reject a bad outcome and keep going -- so thresholds rise as you get closer to the last roll and fall as you move earlier (since you have more rolls left to improve). Quick gut check before any algebra: with 3 rolls you should be able to do meaningfully better than \$3.50 (the no-choice baseline). With one extra roll, you can reject anything below 4, pushing the mean up to about \$4.25. With two extra rolls, you can be even pickier on the first one. Expect something around $4.50-$4.75. **Quick Estimate:** Work back from the last roll. - Roll 3 (forced): $E_3 = (1+2+3+4+5+6)/6 = 3.5$. - Roll 2 threshold: $3.5$. Accept 4, 5, or 6; otherwise re-roll. Rough average of accepted values: $(4+5+6)/3 = 5.0$. Accepted with prob $1/2$, re-roll with prob $1/2$ giving $3.5$. Estimate: $(1/2)(5.0) + (1/2)(3.5) = 4.25$. This is exact. - Roll 1 threshold: $4.25$. Accept 5 or 6; otherwise re-roll for $4.25$. Average of accepted values: $(5+6)/2 = 5.5$. Accepted with prob $1/3$. Estimate: $(1/3)(5.5) + (2/3)(4.25) \approx 1.83 + 2.83 = 4.67$. Ballpark confirmed. **Approach:** Backward induction (dynamic programming). Define $E_k$ as the expected payout under the optimal strategy with $k$ rolls remaining. At each step, the threshold is $E_{k-1}$: stop if the current roll exceeds the continuation value, else keep rolling. **Formal Solution:** **Roll 3 (last roll) -- no choice:** $$E_3 = \frac{1}{6}(1+2+3+4+5+6) = 3.5$$ **Roll 2 -- stop if face value $\geq E_3 = 3.5$, i.e., stop on 4, 5, or 6:** $$E_2 = \frac{1}{6}\bigl(E_3 + E_3 + E_3 + 4 + 5 + 6\bigr) = \frac{1}{6}(3.5 + 3.5 + 3.5 + 4 + 5 + 6) = \frac{25.5}{6} = 4.25$$ The three low rolls (1, 2, 3) are each replaced by the continuation value $E_3 = 3.5$, while the three high rolls (4, 5, 6) are accepted at face value. **Roll 1 -- stop if face value $\geq E_2 = 4.25$, i.e., stop on 5 or 6:** $$E_1 = \frac{1}{6}\bigl(E_2 + E_2 + E_2 + E_2 + 5 + 6\bigr) = \frac{1}{6}(4 \times 4.25 + 11) = \frac{28}{6} = \frac{14}{3} \approx 4.67$$ The four low rolls (1, 2, 3, 4) are replaced by $E_2 = 4.25$; only 5 and 6 are accepted. **Optimal Strategy:** - Roll 1: keep only 5 or 6. Otherwise roll again. - Roll 2: keep 4, 5, or 6. Otherwise roll again. - Roll 3: must accept whatever comes up. **Answer:** The expected payout is $\dfrac{14}{3} \approx \$4.67$. The stopping thresholds are 5 on the first roll and 4 on the second. --- Polya Urn Free Kicks [easy, Probability] --- https://quantvault.org/problem-16-polya-urn-free-kicks.html Q: Cristiano Ronaldo is practicing free kicks. He takes 100 shots total. He scores his first free kick and misses his second. For $n \geq 3$, the probability that he scores the $n$th free kick equals the fraction of the first $n-1$ kicks that he scored. For example, if he has scored 17 of his first 30, the probability he scores the 31st is $17/30$. Find the probability that Ronaldo scores exactly 40 of the 100 free kicks. A: **How to Think About It:** This is a Polya urn. After kicks 1 and 2 the urn holds one goal ball and one miss ball, and each kick draws a ball and adds another of the same color, which is exactly "score with probability goals so far over kicks so far." The heuristic is to write out the probability of one full sequence: the numerators only rearrange, so every order of the same counts is equally likely. Then count the orders. That count cancels the sequence probability and leaves a flat distribution, so the number 40 does not matter. **Quick Estimate:** Try 4 kicks. After goal, miss, the last two kicks go: GG has probability $\frac12\cdot\frac23=\frac13$, GM has $\frac12\cdot\frac13=\frac16$, MG has $\frac12\cdot\frac13=\frac16$, and MM has $\frac12\cdot\frac23=\frac13$. So 3 total goals has probability $\frac13$, 2 goals has $\frac16+\frac16=\frac13$, and 1 goal has $\frac13$: flat across 3 values. With 100 kicks the possible totals are 1 through 99, which suggests $\frac{1}{99}\approx 0.0101$. **Approach:** Compute the probability of one fixed sequence, show it depends only on the counts, and multiply by the number of sequences with those counts. **Formal Solution:** Take $n$ kicks in total and $k$ goals, where $1\leq k\leq n-1$ because the first kick is a goal and the second is a miss. Among kicks $3,\dots,n$ there are $j=k-1$ goals and $m=n-k-1$ misses, so $j+m=n-2$. *One sequence.* Fix any order of those $j$ goals and $m$ misses. Kick $t$ has denominator $t-1$, so the denominators multiply to $2\cdot 3\cdots(n-1)=(n-1)!$. Before the $i$-th goal in kicks $3,\dots,n$ there are $i$ goals on the board, so that kick contributes numerator $i$. Before the $i$-th miss there are $i$ misses on the board, and the probability of missing is misses so far over kicks so far, so that kick also contributes numerator $i$. Therefore $$P(\text{this sequence})=\frac{j!\,m!}{(n-1)!},$$ which does not depend on the order. *Count the sequences.* There are $\binom{n-2}{j}=\frac{(n-2)!}{j!\,m!}$ orders. *Multiply.* $$P(k\text{ goals in }n\text{ kicks})=\frac{(n-2)!}{j!\,m!}\cdot\frac{j!\,m!}{(n-1)!}=\frac{1}{n-1}.$$ So the goal count is uniform on $\{1,\dots,n-1\}$. *Check by recurrence.* Conditioning on kick $n$ gives $$p(k,n)=\frac{k-1}{n-1}\,p(k-1,n-1)+\frac{n-1-k}{n-1}\,p(k,n-1).$$ If $p(\cdot,n-1)=\frac{1}{n-2}$ on $\{1,\dots,n-2\}$, the right side is $\frac{(k-1)+(n-1-k)}{(n-1)(n-2)}=\frac{1}{n-1}$. The two edge cases work too, because the coefficient of the out-of-range term is $0$ at $k=1$ and at $k=n-1$. Starting from $p(1,2)=1$, this matches the formula for every $n$. **Answer:** With $n=100$ and $k=40$: $$\boxed{P(\text{exactly 40 goals in 100 kicks})=\frac{1}{99}}$$ --- The Secretary Problem [hard, Expectation] --- https://quantvault.org/problem-19-the-secretary-problem.html Q: You observe $n$ candidates (i.i.d. continuous draws) one at a time in random order. After seeing each candidate, you only know their relative rank among those seen so far. You must irrevocably accept or reject each candidate on the spot, and you win only if you select the overall best candidate. Consider the threshold strategy: skip the first $r-1$ candidates (the "exploration phase"), then accept the next candidate who is the best seen so far (a "record"). 1. Prove that this class of threshold strategies contains the optimal strategy -- i.e., the strategy maximizing the probability of selecting the global best is of this form. 2. Write the exact success probability $P(r, n)$ as a function of $r$ and $n$. Find the asymptotically optimal $r^{*}$ and the limiting success probability as $n \to \infty$. A: **How to Think About It:** This is the classical secretary problem. The intuition is simple: you need to see some candidates first to calibrate your standard, then hire the first one who beats everyone you have seen. If you set your threshold too low, you waste your "hire" on a mediocre early candidate. Too high, and you have already passed the best one. The optimal threshold balances exploration vs. exploitation. **Quick Estimate:** For large $n$, the optimal rule skips about $n/e \approx 0.368n$ candidates (for $n = 100$: skip $37$, so $r = 38$), giving a success probability of about $1/e \approx 0.368$. So even with an infinite pool, you can pick the best candidate about 37% of the time -- remarkably good given you are choosing blind. **Approach:** First derive $P(r, n)$, then optimize over $r$. **Formal Solution:** **Part (i): Optimality of threshold strategies.** Consider any optimal strategy. At each step $k$, the decision to accept or reject depends only on the relative ranks of candidates seen so far (since values are continuous i.i.d., absolute values carry no extra information beyond relative ranks). First, never accept a non-record: it is already beaten by someone earlier, so it cannot be the global best. So the only decisions are which records to accept. *Value of stopping.* A record at position $k$ is the best of the first $k$. It is the global best exactly when the global best sits among the first $k$ positions, which has probability $k/n$; and this event is independent of the relative-rank pattern of the first $k-1$ candidates. So accepting a record at $k$ wins with probability $\frac{k}{n}$, which is **increasing** in $k$, whatever you have seen. *Value of continuing.* Let $C_k$ be the best win probability achievable using only positions $k+1, \ldots, n$ (having rejected everything up to $k$). By the same independence, $C_k$ depends only on $k$. Also $C_{k} \ge C_{k+1}$: from position $k+1$ on, the player at stage $k$ can copy any rule available at stage $k+1$. So $C_k$ is **non-increasing** in $k$. *Single crossing.* Optimal play accepts a record at $k$ iff $\frac{k}{n} \ge C_k$. If this holds at some $k$, then for every $j > k$, $\frac{j}{n} > \frac{k}{n} \ge C_k \ge C_j$, so it holds at $j$ too. Hence the set of positions where records are accepted is $\{r, r+1, \ldots, n\}$ for some $r$: skip the first $r-1$, then take the first record. That is exactly the threshold strategy. **Part (ii): Exact success probability.** With the threshold strategy (skip first $r-1$, then take the next record): The best candidate is at position $k$ (each position equally likely with probability $1/n$). The strategy selects her if and only if: - $k \geq r$ (she is not in the exploration phase), AND - No candidate in positions $r, r+1, \ldots, k-1$ is a record at the time she appears -- equivalently, the best of the first $k-1$ candidates is among the first $r-1$. The probability that the best of the first $k-1$ is among the first $r-1$ is $(r-1)/(k-1)$ (by symmetry of random orderings). Summing over all possible positions of the best candidate: $$P(r, n) = \sum_{k=r}^{n} \frac{1}{n} \cdot \frac{r-1}{k-1} = \frac{r-1}{n} \sum_{k=r}^{n} \frac{1}{k-1} = \frac{r-1}{n} \sum_{j=r-1}^{n-1} \frac{1}{j}, \qquad 2 \le r \le n$$ (For $r = 1$ you take the first candidate, so $P(1, n) = \frac{1}{n}$.) Check: $P(3, 6) = \frac{2}{6}\left(\frac12 + \frac13 + \frac14 + \frac15\right) = \frac{77}{180} \approx 0.428$, matching brute-force enumeration of all $720$ orders. **Asymptotic optimization.** Let $x = r/n$ with $n \to \infty$. Then $\frac{r-1}{n} \to x$ and $\sum_{j=r-1}^{n-1} \frac{1}{j} \approx \ln\frac{n}{r} \to \int_{x}^{1} \frac{1}{t}\, dt$, so $$P(r, n) \to x \int_{x}^{1} \frac{1}{t}\, dt = -x \ln x$$ Maximize $f(x) = -x \ln x$: setting $f'(x) = -\ln x - 1 = 0$ gives $x^{*} = 1/e$. So $r^{*}/n \to 1/e$: skip about $n/e$ candidates. (Exact optimum from $P(r,n)$: $n = 10$ gives $r^{*} = 4$, i.e. skip $3$, with $P = 0.3987$; $n = 100$ gives $r^{*} = 38$, skip $37$, $P = 0.3710$.) The limiting success probability is: $$P^{*} = -\frac{1}{e} \ln \frac{1}{e} = \frac{1}{e} \approx 0.3679$$ **Answer:** The exact success probability is $P(r, n) = \frac{r-1}{n}\sum_{j=r-1}^{n-1}\frac{1}{j}$ for $r \ge 2$ (and $P(1,n) = 1/n$). The optimal cutoff satisfies $r^{*}/n \to 1/e$ (skip about $n/e$ candidates, e.g. skip $37$ of $100$), and the success probability falls to the limit $1/e \approx 0.3679$. --- Acute Triangle from Random Points on a Circle [medium, Probability] --- https://quantvault.org/problem-21-acute-triangle-from-random-points-on-a-circle.html Q: Three points are chosen uniformly at random on a circle. What is the probability that the triangle they form is acute? A: **How to Think About It:** An inscribed triangle is acute exactly when every inscribed angle is below $90^\circ$. By the inscribed-angle theorem, an inscribed angle is acute iff the arc it subtends is shorter than a semicircle, so the triangle is acute iff **all three arcs between consecutive points are less than $\pi$**; equivalently, iff the center of the circle lies strictly inside the triangle. Translate that arc condition into inequalities on the points' positions and compute an area. **Quick Estimate:** At most one arc can exceed $\pi$ (the three arcs sum to $2\pi$), so "obtuse" splits into three symmetric, mutually exclusive cases, one per vertex: the long arc is the one running counterclockwise from that vertex to the next point. That happens exactly when neither of the other two points lands in the half-circle just counterclockwise of the vertex, which has probability $\left(\frac12\right)^2 = \frac14$. So $P(\text{obtuse}) = 3 \cdot \frac14 = \frac34$ and $P(\text{acute}) = \frac14$. The area computation below confirms this. **Approach:** Fix one point by rotational symmetry, place the other two by their ordered fractional positions, and find the area of the region where all three arcs are short. **Formal Solution:** Fix point $A$ at angle $0$. Let the other two points have fractional positions $u \frac12$; the middle gives $v < u + \frac12$. Combined with $u < \frac12$ and $u < v$, the favorable region is the triangle with vertices $\left(0, \tfrac12\right)$, $\left(\tfrac12, \tfrac12\right)$, $\left(\tfrac12, 1\right)$, whose area is $\frac12 \cdot \frac12 \cdot \frac12 = \frac18$. Therefore $P(\text{acute}) = \frac{\text{favorable area}}{\text{sample area}} = \frac{1/8}{1/2} = \frac14.$ A quick Monte Carlo check (drawing three uniform points and testing that all arcs are under a half-circle) lands on $0.25$, confirming the result. **Answer:** The probability that the inscribed triangle is acute is $\boxed{\dfrac14}$. --- Airplane Boarding Problem [medium, Expectation] --- https://quantvault.org/problem-22-airplane-boarding-problem.html Q: There are $n$ passengers boarding a plane with assigned seats $1, 2, \dots, n$. Passenger 1 has lost their ticket and picks a seat uniformly at random from all $n$ seats. Each subsequent passenger $k$ (for $k = 2, 3, \dots, n$) takes their assigned seat if it is available; otherwise, they pick uniformly at random from the remaining empty seats. 1. Compute the probability that passenger $n$ sits in their own seat (seat $n$). 2. Compute $E[\text{number of passengers who do NOT sit in their assigned seat}]$. A: **How to Think About It:** The first thing to notice is that this process has a surprising recursive structure. When passenger 1 sits in a random seat, three things can happen: they sit in seat 1 (their own -- everyone else is fine), they sit in seat $n$ (passenger $n$ is definitely displaced), or they sit in seat $k$ for some $2 \leq k \leq n-1$ (which "transfers" the randomness to passenger $k$, who now plays the role of the confused passenger). This recursive structure is what makes the problem tractable. **Quick Estimate:** Consider $n = 3$. Passenger 1 picks seat 1, 2, or 3 each with probability $1/3$. If seat 1: passenger 3 gets seat 3. If seat 3: passenger 3 does not get seat 3. If seat 2: passenger 2 is displaced and picks randomly from $\{1, 3\}$ -- passenger 3 gets seat 3 with probability $1/2$. So $P = 1/3 + 0 + (1/3)(1/2) = 1/2$. The pattern $P = 1/2$ seems to hold for all $n \geq 2$. **Approach:** We prove part 1 by induction/recursion, then use indicator variables for part 2. **Formal Solution:** *Part 1: $P(\text{passenger } n \text{ gets seat } n) = 1/2$ for all $n \geq 2$.* Let $P_n$ denote this probability. Condition on passenger 1's choice: - Passenger 1 picks seat 1 (prob $1/n$): everyone sits correctly, so passenger $n$ gets seat $n$. Contribution: $1/n$. - Passenger 1 picks seat $n$ (prob $1/n$): passenger $n$ cannot sit in seat $n$. Contribution: $0$. - Passenger 1 picks seat $k$ for $k \in \{2, \dots, n-1\}$ (prob $1/n$ each): passengers $2, \dots, k-1$ find their seats available and sit normally. Passenger $k$ finds their seat taken and must pick uniformly from the remaining $n - k + 1$ seats ($\{1, k+1, k+2, \dots, n\}$). Now passenger $k$ is in exactly the same situation as passenger 1 was, but in a reduced problem of size $n - k + 1$. So the probability passenger $n$ gets seat $n$ is $P_{n-k+1}$. This gives the recursion: $$P_n = \frac{1}{n} + \frac{1}{n} \sum_{k=2}^{n-1} P_{n-k+1} = \frac{1}{n} + \frac{1}{n} \sum_{m=2}^{n-1} P_m$$ Base case: $P_2 = 1/2$ (passenger 1 picks seat 1 or seat 2 with equal probability). Inductive step: assume $P_m = 1/2$ for all $2 \leq m \leq n-1$. Then: $$P_n = \frac{1}{n} + \frac{1}{n} \cdot (n-2) \cdot \frac{1}{2} = \frac{1}{n} + \frac{n-2}{2n} = \frac{2 + n - 2}{2n} = \frac{n}{2n} = \frac{1}{2}$$ So $P_n = 1/2$ for all $n \geq 2$. *Part 2: $E[\text{number of displaced passengers}]$.* Let $X_k = 1$ if passenger $k$ does not sit in seat $k$. We want $E\left[\sum_{k=1}^n X_k\right] = \sum_{k=1}^n P(X_k = 1)$. - $P(X_1 = 1) = (n-1)/n$ (passenger 1 picks a seat other than seat 1). - For $2 \leq k \leq n$: $P(X_k = 1) = \dfrac{1}{n-k+2}$. To see why: passenger $k$ is displaced if and only if seat $k$ is already occupied when they board. Trace the chain of random choices (passenger 1, then each displaced passenger in turn) up to the moment passenger $k$ boards. While the chain lands in $\{2, \dots, k-1\}$, the disruption just gets passed along. Passenger $k$'s fate is settled the first time a random chooser picks a seat in the set $\{1, k, k+1, \dots, n\}$: picking seat 1 ends all displacement (passenger $k$ is safe), picking a seat in $\{k+1, \dots, n\}$ parks the disruption with a passenger who boards after $k$ (again safe), and picking seat $k$ displaces passenger $k$. All $n-k+2$ of these seats are still empty while the chain is running before passenger $k$ boards, and every random choice is uniform over the remaining empty seats -- so the first seat hit within this set is uniform over its $n-k+2$ members. Hence $$P(X_k = 1) = \frac{1}{n-k+2}, \qquad 2 \leq k \leq n.$$ (Consistency check: $k = n$ gives $1/2$, matching part 1. A tempting wrong guess is $P(X_k = 1) = 1/k$, which would give $E = \frac{n-1}{n} + H_{n-1} - \frac{1}{2}$, i.e. $5/3$ at $n = 3$; direct enumeration at $n = 3$ gives $3/2$, refuting it.) Therefore: $$E\left[\sum_{k=1}^n X_k\right] = \frac{n-1}{n} + \sum_{k=2}^{n} \frac{1}{n-k+2}$$ Substituting $m = n - k + 2$ (as $k$ runs from $2$ to $n$, $m$ runs from $n$ down to $2$): $$E[\text{displaced}] = \left(1 - \frac{1}{n}\right) + \sum_{m=2}^{n} \frac{1}{m} = \left(1 - \frac{1}{n}\right) + (H_n - 1) = H_n - \frac{1}{n} = H_{n-1}$$ where $H_n = \sum_{k=1}^n 1/k$ is the harmonic number. Sanity check at $n = 3$: $P(X_1) = 2/3$, $P(X_2) = 1/3$, $P(X_3) = 1/2$, so $E = 2/3 + 1/3 + 1/2 = 3/2 = H_2$. Exhaustive enumeration of all seatings for $n = 3, 4, 5, 6$ confirms $E = H_{n-1}$ exactly ($3/2$, $11/6$, $25/12$, $137/60$). For large $n$, this is approximately $\ln n + \gamma \approx \ln n + 0.577$, where $\gamma \approx 0.5772$ is the Euler-Mascheroni constant. **Answer:** (1) The probability that passenger $n$ sits in seat $n$ is exactly $1/2$ for all $n \geq 2$. (2) The expected number of displaced passengers is $H_{n-1} = \sum_{k=1}^{n-1} \frac{1}{k}$, which grows as $\ln n$. --- Bertrand's Box Paradox [easy, Probability] --- https://quantvault.org/problem-24-bertrand-s-box-paradox.html Q: Two boxes sit in front of you. Box 1 contains one $\$1$ bill and one $\$5$ bill. Box 2 contains two $\$1$ bills. You pick a box uniformly at random, then draw a bill from it at random. The bill you drew is a $\$1$ bill. What is the probability that the second bill in your chosen box is also a $\$1$ bill? A: **How to Think About It:** This is a classic Bayes' theorem exercise disguised as a simple box problem. The trap is thinking "I picked one of two boxes, so it's 50-50." But the observation -- drawing a $\$1$ bill -- gives you information about which box you are in. Box 2 (two $\$1$ bills) is more likely to produce a $\$1$ bill on the first draw than Box 1 (which only has one). So seeing a $\$1$ bill should shift your belief toward Box 2. The answer should be greater than $1/2$. **Quick Estimate:** There are three $\$1$ bills total across both boxes. Two of them live in Box 2. If you drew a $\$1$ bill, each of those three bills was equally likely to be the one you grabbed (since box choice is $1/2$ and bill choice within a box is $1/2$, each bill has probability $1/4$ -- but the $\$5$ bill is ruled out). So conditional on drawing a $\$1$ bill, the probability it came from Box 2 is $2/3$. If you are in Box 2, the remaining bill is certainly $\$1$. So the answer is $2/3$. **Approach:** Formally apply Bayes' theorem, conditioning on which box was chosen. **Formal Solution:** Let $B_1$ = event you chose Box 1, $B_2$ = event you chose Box 2, and $E$ = event the first draw is a $\$1$ bill. We want $P(B_2 \mid E)$, since if you are in Box 2 the second bill is guaranteed to be $\$1$, and if you are in Box 1 the second bill is the $\$5$ bill. By Bayes' theorem: $$P(B_2 \mid E) = \frac{P(E \mid B_2) \, P(B_2)}{P(E \mid B_1) \, P(B_1) + P(E \mid B_2) \, P(B_2)}$$ The pieces: - $P(B_1) = P(B_2) = \frac{1}{2}$ (uniform box selection) - $P(E \mid B_1) = \frac{1}{2}$ (Box 1 has one $\$1$ bill and one $\$5$ bill) - $P(E \mid B_2) = 1$ (Box 2 has two $\$1$ bills, so you always draw a $\$1$ bill) Plugging in: $$P(B_2 \mid E) = \frac{1 \cdot \frac{1}{2}}{\frac{1}{2} \cdot \frac{1}{2} + 1 \cdot \frac{1}{2}} = \frac{\frac{1}{2}}{\frac{1}{4} + \frac{1}{2}} = \frac{\frac{1}{2}}{\frac{3}{4}} = \frac{2}{3}$$ **Answer:** The probability the second bill is also a $\$1$ bill is $\dfrac{2}{3}$. --- Bugs on a Bar [medium, Expectation] --- https://quantvault.org/problem-25-bugs-on-a-bar.html Q: 100 bugs are placed on a 1-meter bar, each at a position drawn independently and uniformly at random from the interval $[0, 1]$ (a continuous random point, not a fixed grid of spots). Each bug independently picks a direction (left or right, 50/50) and starts walking at 1 m/min. When two bugs collide, they instantly reverse directions. A bug falls off the bar when it reaches either end. What is the expected time until all bugs have fallen off the bar? A: **How to Think About It:** This is the classic "ants on a stick" puzzle, and the whole thing hinges on one beautiful observation. When two identical bugs collide and reverse, the outcome is indistinguishable from the two bugs passing straight through each other. Think about it: before the collision you have a bug moving left and a bug moving right at the same speed. After the "collision" (reversal), you still have a bug moving left and a bug moving right at the same speed. Since the bugs are identical, relabeling them changes nothing. This means you can pretend each bug walks in a straight line to the edge, completely ignoring all other bugs. The time until the bar is clear is just the maximum of 100 independent fall-off times. **Quick Estimate:** With collisions ignorable, the bar is clear when the slowest of $100$ independent bugs falls off. A bug at $x \sim \text{Uniform}(0,1)$ needs time $x$ if it walks left and $1-x$ if it walks right; both are uniform on $(0,1)$, so every bug's time is uniform. The maximum of $n$ uniforms leaves, on average, one of $n+1$ equal gaps above it, a gap of $1/(n+1) = 1/101 \approx 0.01$. So the clearing time is about $1 - 0.01 = 0.99$ minutes, just under a minute. **Approach:** Use the pass-through equivalence to reduce to the maximum of 100 i.i.d. Uniform$(0,1)$ random variables. **Formal Solution:** *Step 1 -- Pass-through equivalence.* When two identical particles collide and reverse at equal speeds, the trajectories of the "particles" (ignoring labels) are identical to trajectories where particles pass through each other. Since bugs are indistinguishable, each bug effectively walks in a straight line to an edge. The set of occupied positions is the same in both pictures at every moment, so the bar empties at exactly the moment the last pass-through bug walks off. *Step 2 -- Distribution of a single bug's time.* Bug $i$ is at position $X_i \sim U(0,1)$ and independently chooses direction $D_i \in \{L, R\}$ with equal probability. Its fall-off time is: $$T_i = X_i \cdot \mathbf{1}_{D_i = L} + (1 - X_i) \cdot \mathbf{1}_{D_i = R}$$ For $0 \leq t \leq 1$: $$P(T_i \leq t) = P(D_i = L)P(X_i \leq t) + P(D_i = R)P(1 - X_i \leq t) = \frac{t}{2} + \frac{t}{2} = t$$ So $T_i \sim U(0,1)$. *Step 3 -- Maximum of independent uniforms.* The total clearing time is $M = \max_{1 \leq i \leq 100} T_i$. By independence: $$P(M \leq t) = \prod_{i=1}^{100} P(T_i \leq t) = t^{100}$$ The density is $f_M(t) = 100 t^{99}$, and the expected value is: $$E[M] = \int_0^1 t \cdot 100 t^{99} \, dt = 100 \int_0^1 t^{100} \, dt = \frac{100}{101}$$ Alternatively, using the survival function: $$E[M] = \int_0^1 (1 - t^{100}) \, dt = 1 - \frac{1}{101} = \frac{100}{101}$$ **Answer:** The expected time until all bugs have fallen off is $\dfrac{100}{101}$ minutes, which is approximately $0.990$ minutes (about 59.4 seconds). More generally, for $n$ bugs the answer is $\dfrac{n}{n+1}$ minutes. --- Eight Coins, One Heavier: Two Weighings [medium, Combinatorics] --- https://quantvault.org/problem-27-eight-coins-one-heavier-two-weighings.html Q: You have 8 identical-looking coins. Exactly one of them is heavier than the rest. You have a balance scale and may use it at most 2 times. 1. Design a strategy that always identifies the heavier coin in at most 2 weighings. 2. Prove that 2 weighings are sufficient (your strategy does it) and that 1 weighing is not sufficient. A: **How to Think About It:** This is an information theory problem in disguise. A balance scale has 3 possible outcomes per weighing: left side heavier, right side heavier, or balanced. So 2 weighings give you $3^2 = 9$ distinguishable outcomes. You need to identify 1 coin out of 8, which requires at least $\lceil \log_2 8 \rceil = 3$ bits -- but a balance scale gives you $\log_2 3 \approx 1.58$ bits per weighing, so 2 weighings give you $\log_2 9 \approx 3.17$ bits. That is enough to distinguish 8 possibilities, but just barely -- one weighing ($\log_2 3 \approx 1.58$ bits) is not. **The Strategy:** Label the coins 1 through 8. Divide them into three groups: - Group A: coins {1, 2, 3} - Group B: coins {4, 5, 6} - Group C: coins {7, 8} **Weighing 1:** Place Group A on the left and Group B on the right (3 vs. 3). **Case 1: Left side is heavier.** The heavy coin is in Group A = {1, 2, 3}. - **Weighing 2:** Weigh coin 1 vs. coin 2. - If left is heavier: coin 1 is the heavy one. - If right is heavier: coin 2 is the heavy one. - If balanced: coin 3 is the heavy one. **Case 2: Right side is heavier.** The heavy coin is in Group B = {4, 5, 6}. - **Weighing 2:** Weigh coin 4 vs. coin 5. - If left is heavier: coin 4 is the heavy one. - If right is heavier: coin 5 is the heavy one. - If balanced: coin 6 is the heavy one. **Case 3: Balanced.** The heavy coin is in Group C = {7, 8}. - **Weighing 2:** Weigh coin 7 vs. coin 8. - If left is heavier: coin 7 is the heavy one. - If right is heavier: coin 8 is the heavy one. - If balanced: impossible (one coin must be heavier). In every case, the heavy coin is identified in exactly 2 weighings. **Proof that 2 weighings suffice:** The strategy above correctly identifies the heavy coin in all 8 possible scenarios using at most 2 weighings. This is a constructive proof. **Proof that 1 weighing is not sufficient:** A single weighing of the balance scale has exactly 3 possible outcomes. Therefore, 1 weighing can distinguish among at most 3 possibilities. Since there are 8 possible locations for the heavy coin, and $8 > 3$, a single weighing cannot determine which coin is heavy. By the pigeonhole principle, at least two of the 8 coins must map to the same outcome, meaning you cannot tell them apart. More formally: any deterministic strategy with 1 weighing is a function from {8 possible heavy coins} to {3 outcomes}. Since $8 > 3$, this function cannot be injective -- at least two coins produce the same outcome, making identification impossible. **Answer:** Divide the 8 coins into groups of 3, 3, and 2. Weigh the two groups of 3 against each other. The outcome tells you which group contains the heavy coin. Then use the second weighing to identify the coin within that group (weigh 1 vs. 1, with the third deduced if balanced). One weighing is insufficient because it has only 3 outcomes but there are 8 coins to distinguish (pigeonhole principle). --- Expected Payoff of the Red-Black Card Game [hard, Expectation] --- https://quantvault.org/problem-29-expected-payoff-of-the-red-black-card-game.html Q: A deck has 50 red cards and 50 black cards (100 total). You draw one card at a time without replacement. After each draw, you can choose to stop or continue. If you stop after drawing $k$ cards, your payoff is: $$\text{Payoff} = \frac{\text{number of red cards drawn}}{k} \times \$2$$ If you never stop, you draw all 100 cards. What is the expected payoff of this game under optimal play? A: **How to Think About It:** You have 50 red and 50 black cards. You draw one at a time and can stop whenever you want -- your payoff is (fraction of reds drawn) times $\$2$. The tempting argument is: "the fraction of reds is always $1/2$ in expectation, so you can't beat $\$1$." That argument is wrong. It confuses $E[R_k/k]$ for a fixed draw count (which is indeed $1/2$) with $E[R_\tau/\tau]$ for an adaptive stopping time $\tau$. The optional stopping theorem would only rule out a gain if $R_k/k$ were a martingale, and it is not: after a red first card ($R_1/1 = 1$) the expected fraction after the next draw is $(1 + 49/99)/2 \approx 0.75$, not $1$. So you CAN beat $\$1$ by stopping at favorable moments. **Quick Estimate:** Try the simplest smart strategy. Draw one card. If it is red, stop -- your fraction is $1/1 = 1$, payoff $= \$2$. If it is black, you are stuck with fraction $0/1$, so just draw all remaining 99 cards and end at $50/100$, payoff $= \$1$. Each case happens with probability $1/2$, so this gives: $$E[\text{payoff}] = \frac{1}{2}(\$2) + \frac{1}{2}(\$1) = \$1.50$$ Already beats $\$1$. Can we do better? Yes: the plan above gives up too early after a black first card. At that point you hold $0/1$, but the deck has $50$ reds and only $49$ blacks left, and the running fraction wanders as you draw. Any later moment where it sits above $1/2$ (say $2$ reds out of $3$, or $6$ out of $10$) is worth stopping at, and you can still fall back on drawing everything for $\$1$. Exploiting those moments lifts the value after a black first card from $\$1$ to about $\$1.17$, so the total is about $\tfrac12(\$2) + \tfrac12(\$1.17) \approx \$1.58$. **Approach:** Backward induction over the state space $(r, b)$ = (reds remaining, blacks remaining). At each state, compare the payoff from stopping to the expected value of drawing one more card. **Formal Solution:** Let $V(r, b)$ be the game value when $r$ reds and $b$ blacks remain in the deck. Cards drawn so far: $k = 100 - r - b$. Reds drawn: $j = 50 - r$. If you stop, you collect $2j/k$. If you draw, the next card is red with probability $r/(r+b)$: $$V(r,b) = \max\!\left(\frac{2(50 - r)}{100 - r - b},\;\frac{r}{r+b}\,V(r-1,b) + \frac{b}{r+b}\,V(r,b-1)\right)$$ Boundary: $V(0,0) = 2 \cdot 50/100 = 1$ (drew everything). At the start ($k = 0$, no cards drawn), the payoff $0/0$ is undefined so we must draw: $$V(50,50) = \tfrac{1}{2}\,V(49,50) + \tfrac{1}{2}\,V(50,49)$$ Running this DP over the $51 \times 51 = 2{,}601$ states gives: $$V(50,50) \approx \$1.5849$$ The optimal policy is a state-dependent threshold: stop when the red fraction $j/k$ exceeds a cutoff $\theta(k)$ that depends on how many cards have been drawn. Early on ($k = 1, 2$), you need fraction $= 1$ (all reds) to stop. As $k$ grows, the threshold falls steadily toward $1/2$ (about $0.60$ by $k = 20$ and about $0.52$ by $k = 80$) because there is less optionality left with fewer remaining cards. **Answer:** The expected payoff under optimal play is approximately $\$1.5849$. The naive claim of $\$1$ is wrong -- $R_k/k$ is not a martingale, so the optional stopping theorem does not apply. Even the dead-simple "stop if the first card is red" strategy already yields $\$1.50$. --- Expected Sum of Rolls in the Coupon Collector Problem [medium, Expectation] --- https://quantvault.org/problem-30-expected-sum-of-rolls-in-the-coupon-collector-problem.html Q: You roll a fair six-sided die over and over until every face (1 through 6) has appeared at least once. Let $S$ be the total of all your rolls — not just how many times you rolled, but the actual sum of all the numbers you see. What is $E[S]$? A: **How to Think About It:** This is the coupon collector problem with a twist -- instead of just counting the number of rolls, you want the total sum of the die values. The first instinct is to think this is complicated because the stopping time $N$ depends on which values you rolled, not just how many. But here is the key: Wald's identity tells you $E[S] = E[N] \cdot E[X]$, where $X$ is a single roll. The identity works because the $X_i$ are i.i.d. and $N$ is a stopping time with finite expectation. The fact that $N$ depends on the values of the rolls is fine -- Wald's identity does not require $N$ to be independent of the $X_i$, only that the $X_i$ are i.i.d. with finite mean and $E[N] < \infty$. **Quick Estimate:** The expected number of rolls in the coupon collector problem with 6 items is: $$E[N] = 6 \cdot H_6 = 6\left(1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \frac{1}{5} + \frac{1}{6}\right) = 6 \times \frac{49}{20} = \frac{147}{10} = 14.7$$ The expected value of a single die roll is $E[X] = 3.5$. So the quick estimate is $14.7 \times 3.5 = 51.45$. **Approach:** Apply Wald's identity directly. **Formal Solution:** Let $X_1, X_2, \ldots$ be i.i.d. rolls of a fair die, each uniform on $\{1, 2, 3, 4, 5, 6\}$. Let $N$ be the stopping time -- the number of rolls until all 6 faces have appeared. Let $S = \sum_{i=1}^{N} X_i$. **Wald's Identity** states: if $X_i$ are i.i.d. with $E[|X_i|] < \infty$ and $N$ is a stopping time with $E[N] < \infty$, then $$E\left[\sum_{i=1}^{N} X_i\right] = E[N] \cdot E[X_1]$$ Both conditions are satisfied here: - $E[|X_i|] = 3.5 < \infty$ - $E[N] = 14.7 < \infty$ (the coupon collector has a well-known finite expectation) The coupon collector expected count is computed by decomposing into geometric phases. After seeing $k$ distinct faces, the probability of a new face on the next roll is $(6 - k)/6$, so the expected rolls in phase $k$ is $6/(6 - k)$: $$E[N] = \sum_{k=0}^{5} \frac{6}{6-k} = \frac{6}{6} + \frac{6}{5} + \frac{6}{4} + \frac{6}{3} + \frac{6}{2} + \frac{6}{1} = 1 + 1.2 + 1.5 + 2 + 3 + 6 = 14.7$$ Therefore: $$E[S] = E[N] \cdot E[X_1] = 14.7 \times 3.5 = \frac{147}{10} \times \frac{7}{2} = \frac{1029}{20} = 51.45$$ **Answer:** $E[S] = 51.45$ (equivalently $\frac{1029}{20}$). --- Fair Outcome from an Extremely Biased Coin [hard, Probability] --- https://quantvault.org/problem-32-fair-outcome-from-an-extremely-biased-coin.html Q: You have a coin that lands heads with probability $p \approx 0.99$. Your goal is to use this coin to simulate a single fair (50-50) outcome. 1. Describe von Neumann's classic method for extracting a fair bit from a biased coin. What is the expected number of flips when $p = 0.99$? Why is the worst case unbounded? 2. The information-theoretic limit says each flip carries $H(p) = -p \log_2 p - (1-p) \log_2(1-p)$ bits of entropy. For $p = 0.99$, what is the minimum expected flips per fair bit? Briefly describe how the interval algorithm (Han and Hoshi, arithmetic-coding style) approaches this limit. 3. Now suppose you need a *guaranteed* upper bound on the number of flips -- no infinite loops allowed. Design a block-based scheme that uses a fixed number of flips $k$ and extracts a fair outcome. How do you choose $k$ to get at least one usable fair bit with high probability? A: **How to Think About It:** The core tension is between simplicity and efficiency. Von Neumann's trick is elegant and easy to remember, but it wastes almost all your flips when the coin is extremely biased. At $p = 0.99$, only about 1% of flip-pairs produce a usable result. The information theory tells you exactly how bad this waste is and what the best you can possibly do looks like. And if you need a hard guarantee on the number of flips (no "keep trying until it works"), you need a fundamentally different approach -- block-based extraction. **Quick Estimate:** For $p = 0.99$, each flip carries $H(0.99) = -0.99 \log_2(0.99) - 0.01 \log_2(0.01) \approx 0.0808$ bits of entropy. So the theoretical minimum is $1/0.0808 \approx 12.4$ flips per fair bit. Von Neumann uses pairs and succeeds with probability $2p(1-p) = 2(0.99)(0.01) = 0.0198$ per pair, so the expected number of flips is $2/0.0198 \approx 101$. That is about 8x worse than the information-theoretic limit -- a huge gap driven by the extreme bias. **Part 1 -- Von Neumann's Method:** Flip the coin twice. There are four outcomes: - $HH$: probability $p^2$ - $HT$: probability $p(1-p)$ - $TH$: probability $(1-p)p$ - $TT$: probability $(1-p)^2$ The key symmetry: $P(HT) = P(TH) = p(1-p)$, regardless of $p$. So map $HT \to 0$ and $TH \to 1$, and discard $HH$ and $TT$ (repeat). Each accepted pair gives one perfectly fair bit. The probability of acceptance per pair is $2p(1-p)$. For $p = 0.99$: acceptance rate $= 0.0198$, so expected flips per fair bit $= 2 / 0.0198 \approx 101$. Worst case is unbounded because every pair could land $HH$ (which happens with probability $0.9801$). You might need to repeat many times. There is no finite upper bound on the number of flips. **Part 2 -- The Interval Algorithm and the Entropy Limit:** The Shannon entropy per flip is: $$H(p) = -p \log_2 p - (1-p) \log_2(1-p)$$ For $p = 0.99$: $$H(0.99) = -0.99 \log_2(0.99) - 0.01 \log_2(0.01) \approx 0.0144 + 0.0664 = 0.0808 \text{ bits}$$ So the theoretical minimum is $1/H(0.99) \approx 12.4$ flips per fair bit. No algorithm can beat this. The interval algorithm of Han and Hoshi (1997) works by treating the sequence of flips as specifying a point in $[0, 1)$ using arithmetic coding with the biased distribution. As flips accumulate, you maintain an interval $[a, b)$ that narrows with each flip. Whenever the interval is entirely contained within $[0, 0.5)$ or $[0.5, 1)$, you output a fair bit (0 or 1 respectively) and rescale. This extracts nearly $H(p)$ fair bits per flip in expectation, approaching the information-theoretic limit. Peres's iterative method (1992) further improves on this by recycling the "discarded" pairs from von Neumann's method, feeding them back through the extraction process recursively. However, like von Neumann, the interval algorithm has no guaranteed stopping time -- the interval can straddle 0.5 for arbitrarily many flips. (Elias's 1972 method is a different idea: it fixes a block length and extracts bits from the equally likely arrangements within each heads-count group. That is exactly the bounded scheme of Part 3, and it also approaches the entropy limit as the block grows.) **Part 3 -- Block-Based Scheme for Guaranteed Termination:** Flip the coin $k$ times. The sequence of outcomes is one of $2^k$ possible strings, but these strings are not equally likely. Group strings by their number of heads: a string with $j$ heads has probability $p^j (1-p)^{k-j}$, and there are $\binom{k}{j}$ such strings. Within each group of $\binom{k}{j}$ equally-likely strings, you can extract fair bits by: - Assign each string a number from $0$ to $\binom{k}{j} - 1$ - Output $\lfloor \log_2 \binom{k}{j} \rfloor$ fair bits by using the string's index in binary - Discard one or more strings if $\binom{k}{j}$ is not a power of 2 The total number of extractable fair bits from $k$ flips is approximately: $$\sum_{j=0}^{k} \lfloor \log_2 \binom{k}{j} \rfloor \cdot P(\text{group } j \text{ is selected})$$ For $p = 0.99$ and $k$ flips, the dominant group has $j \approx 0.99k$ heads. To get at least one fair bit, you need $\binom{k}{j} \geq 2$ for the most likely $j$ values. Since $\binom{k}{k-1} = k$, even $k = 2$ gives you $\binom{2}{1} = 2$ strings (one head, one tail), so you can extract one fair bit from those -- but that group only occurs with probability $2(0.99)(0.01) = 0.0198$. A practical choice: take $k$ large enough that the typical group near $j = 0.99k$ has a large binomial coefficient. For $k = 100$, the most likely outcome is 99 heads, and $\binom{100}{99} = 100$, giving $\lfloor \log_2 100 \rfloor = 6$ fair bits. The probability of getting exactly 99 heads is $\binom{100}{99}(0.99)^{99}(0.01)^1 \approx 0.370$. Groups with 97-100 heads cover most of the probability mass, and each has a large enough binomial coefficient to extract several fair bits. The key trade-off: the block size $k$ is your hard cap on flips. Larger $k$ gives more efficient extraction (approaching the entropy limit), but you always terminate in exactly $k$ flips. **Answer:** Von Neumann's method uses the symmetry $P(HT) = P(TH)$ but requires about 101 expected flips at $p = 0.99$ with unbounded worst case. The entropy limit is $\approx 12.4$ flips per fair bit, approached by the Han-Hoshi interval algorithm or Peres's recursive method -- but these also lack guaranteed termination. For a hard worst-case bound, use a block of $k$ flips and extract fair bits from the equally-likely permutations within each heads-count group. At $k = 100$, you get about 6 fair bits with certainty in exactly 100 flips. --- Five Points on a Hemisphere [hard, Probability] --- https://quantvault.org/problem-34-five-points-on-a-hemisphere.html Q: Five points are placed independently and uniformly at random on the surface of a sphere. What is the probability that there exists some closed hemisphere such that all five points lie within it? A closed hemisphere is half of the sphere, including its bounding great circle. A: **How to Think About It:** Brute-force integration over all hemispheres is intractable. The elegant route is **antipodal symmetry + region counting (Wendel's theorem)**: treat each point as defining a plane through the origin, and a hemisphere as a choice of which side of each plane you're on. The clean fact is that the answer depends only on how many *sign regions* $n$ planes cut space into. Name the heuristic: **reduce a continuous geometric probability to a discrete count of arrangements.** **Quick Estimate (small-$n$ anchors):** Anchor with cases you can reason out without the general formula. Any three points fit in a hemisphere: take the plane through the origin parallel to the plane through the three points, and all three sit on one side of it, so $P(3)=1$. For four points, the classic Putnam result is that the random tetrahedron contains the center with probability $1/8$, and the four points miss every closed hemisphere exactly when it does, so $P(4)=7/8=0.875$. A fifth point can only make containment harder, so $P(5) 0\ \text{for all } i\}$ is nonempty (any $x$ in it serves as a pole $\vec v$). Each region of the plane arrangement corresponds to exactly one such sign pattern, so the number of sign choices that fit in some hemisphere equals the number of regions the planes create. **Step 3 (Schläfli / Wendel region count).** Five planes through the origin in general position in $\mathbb{R}^3$ cut space into $$R = 2\sum_{k=0}^{d-1}\binom{n-1}{k} = 2\Big(\binom{4}{0}+\binom{4}{1}+\binom{4}{2}\Big)=2(1+4+6)=22$$ regions ($n=5,\ d=3$). **Step 4 (probability).** Of the $32$ equally likely antipodal configurations, exactly $22$ correspond to a hemisphere-containable arrangement, so $$P=\frac{22}{32}=\boxed{\tfrac{11}{16}}=0.6875.$$ **General (Wendel, 1962):** for $n$ points on the sphere in $\mathbb{R}^d$, $P(n,d)=\dfrac{1}{2^{n-1}}\sum_{k=0}^{d-1}\binom{n-1}{k}$; here $d=3,n=5$ gives $11/16$. **Answer:** $\dfrac{11}{16}$. --- Four Switches, One Light Bulb [medium, Brain Teaser] --- https://quantvault.org/problem-35-four-switches-one-light-bulb.html Q: There is a single incandescent light bulb in a closed room. Outside the room are four switches, and exactly one of them controls the bulb. All four switches start in the off position. You can flip any combination of switches on or off as many times as you like before entering the room. Once you enter the room, you can look at the bulb and touch it (to feel its temperature). After entering, you cannot go back and change the switches. What is the minimum number of times you need to enter the room to determine with certainty which switch controls the light bulb? A: **How to Think About It:** Once you enter, the switches are frozen, so a second visit only shows you the same setup again and cannot separate switches that the first visit could not. With sight alone, one visit gives one bit (is the bulb on or off?), which splits the four switches into two groups but cannot single one out. So the whole puzzle is whether a single visit can carry more than one bit. The key insight is that a light bulb carries more than one bit of observable state. You can see whether it is on or off (visual), and you can feel whether it is warm or cool (thermal). That gives you $2 \times 2 = 4$ distinguishable states, which is exactly enough to label 4 switches. **Key Insight:** A light bulb that has been on for a while is warm. By staggering when you turn switches on and off, you can encode each switch's identity into a unique (on/off, warm/cool) pair and read all four in a single visit. **The Method:** 1. Turn on switches 1 and 2. Leave them on for several minutes so the bulb (whichever one they might control) heats up. 2. After waiting, turn switch 2 off and turn switch 3 on. Enter the room immediately. 3. Observe the bulb's state: - **On and warm:** Switch 1. It has been on the entire time, so the bulb is lit and hot. - **Off and warm:** Switch 2. It was on long enough to heat the filament, but you just turned it off, so the bulb is dark but still warm to the touch. - **On and cool:** Switch 3. You just turned it on, so the bulb is lit but has not had time to heat up. - **Off and cool:** Switch 4. It was never turned on, so the bulb is dark and at room temperature. Each of the four switches maps to a unique combination, so one entry is sufficient. **Practical Considerations:** This only works with incandescent or halogen bulbs that produce heat. With LEDs or fluorescent bulbs, the thermal channel disappears and you would need a different encoding (e.g., flipping patterns observed by a helper, or using a dimmer). In an interview, it is worth mentioning this assumption: it shows you understand the physical mechanism behind the trick, not just the memorized answer. **Answer:** You need to enter the room only **once**. The procedure exploits two independent observables (visual state and thermal state) to create four distinguishable outcomes from a single observation. --- Hilbert's Hotel: Accommodating Infinite Guests [medium, Brain Teaser] --- https://quantvault.org/problem-36-hilbert-s-hotel-accommodating-infinite-guests.html Q: You manage a hotel with infinitely many rooms, numbered $1, 2, 3, \ldots$, and every room is currently occupied by exactly one guest. In any rearrangement you propose, each person must still end up alone in a room. **Part 1:** A single new guest arrives. How do you make room for them without kicking anyone out? **Part 2:** Now an infinite busload of new guests arrives -- countably infinitely many people, each needing a room. All your rooms are still full. How do you accommodate all of them? **Part 3:** Now infinitely many buses arrive, each carrying infinitely many passengers. Every room is still occupied. How do you fit everyone in? A: **How to Think About It:** This is Hilbert's Hotel, the classic illustration of how infinite sets behave differently from finite ones. The key insight is that "infinity plus something" can still equal infinity -- but you need a concrete procedure (a bijection) to show it. In a finite hotel, full means full. In an infinite hotel, full just means you need a cleverer room assignment. The interviewer wants to see that you understand the difference between finite and infinite cardinality, and that you can construct explicit mappings. **Key Insight:** A countably infinite set can be put in one-to-one correspondence with a proper subset of itself. That is the defining feature that makes all three parts work. Each part escalates the challenge: finite union, countable union, and then a countable union of countable sets. **The Method:** *Part 1 -- One new guest:* Ask every current guest in room $i$ to move to room $i + 1$. The guest in room 1 goes to room 2, room 2 goes to room 3, and so on. Room 1 is now empty -- give it to the new guest. This works because the map $i \mapsto i + 1$ is a bijection from $\mathbb{N}$ to $\mathbb{N} \setminus \{1\}$. More generally, if $k$ new guests arrive (any finite number), shift everyone from room $i$ to room $i + k$, freeing up rooms $1$ through $k$. *Part 2 -- Infinitely many new guests:* Ask every current guest in room $i$ to move to room $2i$. Room 1 goes to room 2, room 2 goes to room 4, room 3 goes to room 6, and so on. This vacates all the odd-numbered rooms: $1, 3, 5, 7, \ldots$ Now assign the $n$-th new guest to room $2n - 1$. Why does this work? The even numbers $\{2, 4, 6, \ldots\}$ and odd numbers $\{1, 3, 5, \ldots\}$ each form a countably infinite set, and together they cover all of $\mathbb{N}$. You have split one copy of $\mathbb{N}$ into two copies -- each big enough to hold a countably infinite collection. *Part 3 -- Infinitely many buses, each with infinitely many passengers:* Label the buses $1, 2, 3, \ldots$ and within each bus, label passengers $1, 2, 3, \ldots$ So every new arrival is identified by a pair $(b, p)$ where $b$ is the bus number and $p$ is the passenger number within that bus. Including the original hotel guests (think of them as bus 0), you need to map the set $\{0, 1, 2, \ldots\} \times \{1, 2, 3, \ldots\}$ into $\mathbb{N}$. One clean way: use the fact that $\mathbb{N} \times \mathbb{N}$ is countable. Assign each pair $(b, p)$ to room $2^{b} \cdot (2p - 1)$. This maps every (bus, passenger) pair to a unique positive integer because every positive integer has a unique factorization into a power of 2 times an odd number. Alternatively, use the diagonal (Cantor pairing) enumeration: list all pairs $(b, p)$ along successive diagonals $b + p = 2, 3, 4, \ldots$ and assign rooms in that order. **Practical Considerations:** The real point here is not hotel management -- it is understanding cardinality. The shift from Part 2 to Part 3 is where most people stumble. Part 2 feels like you "used up" the trick of splitting $\mathbb{N}$ in two, so how can you split it infinitely many times? The answer is that a countable union of countable sets is still countable. This is a foundational result in set theory and comes up any time you need to argue that some seemingly huge collection of objects is actually enumerable. **Answer:** In all three cases, the guests can be accommodated because the set of rooms $\mathbb{N}$ is countably infinite and can be placed in bijection with $\mathbb{N} + k$ (Part 1), $\mathbb{N} \cup \mathbb{N}$ (Part 2), and $\mathbb{N} \times \mathbb{N}$ (Part 3). The explicit mappings are: shift by $k$ for Part 1, send $i \mapsto 2i$ to free odd rooms for Part 2, and use a pairing function (e.g., $2^{b}(2p-1)$) for Part 3. --- Keep or Play: Optimal Replay in a Coin-Flipping Game [medium, Expectation] --- https://quantvault.org/problem-37-keep-or-play-optimal-replay-in-a-coin-flipping-game.html Q: You are offered the following game. You flip a fair coin repeatedly until you land tails. For each head you flip before that first tails, you earn $\$h$. Once tails appears, you can either keep your payout or abandon it and play a second round under the same rules. If you replay, your first-round earnings are forfeited and you must accept whatever the second round gives you (no further replays). Find the optimal strategy and the fair value of this game when $h = 10$. A: **How to Think About It:** This is an optimal stopping problem with one replay option. The key question is: when should you walk away from your first-round earnings and gamble on a fresh round? The answer depends on comparing what you already have to what you expect from replaying. Since each round is independent, the expected value of the second round is just the expected value of a single round -- so you should replay exactly when your first-round payout falls below that benchmark. **Quick Estimate:** In a single round, the number of heads before the first tails follows a $\text{Geometric}(1/2)$ distribution (counting failures before the first success). The expected number of heads is $(1 - 1/2)/(1/2) = 1$, so a single round is worth $h = \$10$ on average. You should replay only when you earned less than $\$10$, which means you got zero heads -- that is, the very first flip was tails. That happens with probability $1/2$. So roughly half the time you take a fresh round (worth $\$10$ on average) and half the time you keep a payout that, conditional on having at least one head, should be worth more than $\$10$. The game value should be somewhere above $\$10$ -- let's guess around $\$15$. **Approach:** Compute the expected single-round payout, determine the optimal replay threshold, then use the law of total expectation to combine both cases. **Formal Solution:** Let $N$ be the number of flips until the first tails. Then $N \sim \text{Geometric}(1/2)$ with $E[N] = 2$. The number of heads is $N - 1$, so the single-round payout is $h(N - 1)$ with expected value: $$E[h(N-1)] = h(E[N] - 1) = h(2 - 1) = h$$ The optimal strategy is to replay if and only if your first-round payout is less than $h$ (the expected value of a fresh round). Since payouts come in multiples of $h$, you replay exactly when you earned $0$ (i.e., zero heads, which means the first flip was tails). Now compute the expected value of the optimal strategy by conditioning on the first flip: - **Case 1: First flip is tails** (probability $1/2$). You earned $\$0$, so you replay. The second round has expected payout $h$. - **Case 2: First flip is heads** (probability $1/2$). You already have $\$h$ in the bank. By the memoryless property of the geometric distribution, the remaining flips (until tails) are distributed exactly like a fresh game. So the expected number of additional heads is $E[N] - 1 = 1$, giving an additional expected payout of $h$. Total expected payout in this case is $h + h = 2h$. By the law of total expectation: $$E[\text{game}] = \frac{1}{2} \cdot h + \frac{1}{2} \cdot 2h = \frac{3h}{2}$$ **Answer:** The optimal strategy is to keep your payout whenever you flip at least one head, and replay only when you get immediate tails (zero heads). The fair value of the game is $3h/2 = \$15$ when $h = 10$. --- Optimal Coin Pair Reflipping [medium, Expectation] --- https://quantvault.org/problem-38-optimal-coin-pair-reflipping.html Q: Four fair coins are flipped. After seeing the outcome, you may repeatedly choose any pair of coins and reflip both. Each reflip costs you $\$1$. When you stop, your payoff is the number of heads showing, minus the total cost of reflips. 1. What is the optimal strategy? 2. What is the expected net payoff under optimal play? A: **How to Think About It:** Without the cost, this problem is trivial — you keep reflipping any non-HH pair until all four coins show heads, always reaching a payoff of 4. Adding the $\$1$ reflip cost creates a genuine optimization problem. **Key Insight:** When you reflip a pair of coins, the expected number of heads in that pair after the reflip is 1 (since each coin is fair). So reflipping is only worthwhile if the current pair has fewer than 1 head on average relative to cost — i.e., if the pair currently shows 0 heads (TT). **Analysis by cases.** After the initial flip, count the number of heads $h \in \{0, 1, 2, 3, 4\}$: - **$h = 4$ (HHHH):** Stop immediately. Payoff = 4. - **$h = 3$ (three H, one T):** Reflipping any pair containing the T costs \$1 and gives expected heads in that pair = 1. Currently the pair has 1H + 1T = 1 head. So expected gain = $1 - 1 = 0$, minus \$1 cost. Not worth it. **Stop. Payoff = 3.** - **$h = 2$ (two H, two T):** Reflip the TT pair. Cost = \$1. Expected heads in the reflipped pair = 1, currently 0. Expected gain = $1 - 0 - 1 = 0$. Break-even — but there's a chance of getting HH (payoff improves by 2) which makes it slightly positive in expectation when you consider the option to stop. Actually: reflip TT gives expected heads = 1, so expected payoff change = $+1 - 1 = 0$. It's exactly break-even, so you are indifferent. **Stop or reflip once — same expected payoff = 2.** - **$h = 1$ (one H, three T):** Pick any TT pair. Reflipping costs \$1, expected new heads = 1 (was 0). Expected payoff after one reflip: expected total heads $\approx 2$, having paid \$1. Marginal expected gain = $+1 - 1 = 0$. Again break-even. **Payoff = 1.** - **$h = 0$ (TTTT):** Reflip any pair. Cost \$1, expected heads gain = 1. New expected state ≈ $h = 1$ case. After one reflip, expected net = $1 - 1 = 0$ gain. **Payoff = 0.** **Conclusion:** The optimal strategy is: **always stop immediately.** The expected payoff equals the initial expected number of heads = $4 \times 1/2 = 2$. Reflipping is never strictly profitable because each reflip resets a pair to expected value 1 head at a cost of \$1 — exactly break-even. The free reflips made the original problem trivial; the \$1 cost makes reflipping pointless. **Answer:** Expected net payoff = $\$2$ under optimal play (stop immediately). --- Optimal Sample Size Before a Directional Bet [hard, Expectation] --- https://quantvault.org/problem-39-optimal-sample-size-before-a-directional-bet.html Q: An asset's one-step return is $R \sim N(\mu, \sigma^2)$ where $\mu$ is unknown and $\sigma^2$ is known. Before placing a trade, you choose a position $w \in \{-1, 0, +1\}$ (short, flat, or long) with payoff $wR$. Before choosing $w$, you may pay a cost $c$ per signal to observe $n$ i.i.d. signals $Y_1, \ldots, Y_n$ where $Y_i \sim N(\mu, \tau^2)$ (noise level $\tau^2$ is known). You then pick $w$ based on the sample mean $\bar{Y}_n$. To keep things symmetric, take $\mu = +\delta$ or $\mu = -\delta$ with equal probability, for a known $\delta > 0$. 1. Given that you have sampled $n$ signals, derive the optimal decision rule for $w$. 2. Compute the expected profit from sampling $n$ signals, net of the cost $nc$. 3. Find the optimal sample size $n^{*}(c)$ that maximizes net expected profit. 4. Find the threshold $c^{*}$ below which sampling at all is worthwhile. A: **How to Think About It:** You are deciding whether to pay for information before making a directional bet. Without any signals, you know nothing about $\mu$ -- you have no basis to go long or short, so you stay flat ($w = 0$) and earn zero. With signals, you estimate $\mu$ from $\bar{Y}_n$ and bet in the direction of the estimate. The value of sampling is the expected improvement in payoff from having this estimate. You are essentially buying a signal and asking: how many observations are worth it at cost $c$ each? Quick intuition: more signals reduce the estimation noise on $\mu$. But the marginal value of the $n$-th signal is decreasing (you already know roughly what $\mu$ is). So the optimal $n$ balances information gain against cost -- a classic diminishing returns problem. **Quick Estimate:** Take $\delta = \tau = 1$ and $c = 0.05$. With $n = 4$ signals the standard error of $\bar{Y}_n$ is $\tau/\sqrt{n} = 0.5$, so you call the sign correctly with probability $\Phi(2) \approx 0.977$: gross value $\approx 1 \times (2(0.977) - 1) = 0.95$ against a cost of $4 \times 0.05 = 0.20$, netting about $0.75$. Push to $n = 16$ and the gross value only creeps up to $\approx 1.00$ while the cost quadruples to $0.80$, netting $0.20$. The gross value saturates at $\delta$ almost immediately while the cost grows linearly, so the optimum is small -- a handful of signals, not dozens. **Formal Solution:** **Part 1 -- Optimal decision rule.** Given $\bar{Y}_n$, choose $w$ to maximize $E[wR \mid \bar{Y}_n] = w \, E[\mu \mid \bar{Y}_n]$, using that $R$ is independent of the signals once $\mu$ is fixed. This is linear in $w$, so the optimum sits at an endpoint of $\{-1, 0, +1\}$ -- there is never a reason to stay flat once you have looked. Under the symmetric two-point prior, the posterior odds on $\mu = +\delta$ against $\mu = -\delta$ are $\exp(2n\delta\bar{Y}_n/\tau^2)$, which exceed 1 exactly when $\bar{Y}_n > 0$. So $E[\mu \mid \bar{Y}_n]$ carries the sign of $\bar{Y}_n$, and: $$w^{*} = \text{sign}(\bar{Y}_n) = \begin{cases} +1 & \text{if } \bar{Y}_n > 0 \\ 0 & \text{if } \bar{Y}_n = 0 \\ -1 & \text{if } \bar{Y}_n 0 \mid \mu) - \mu \cdot P(\bar{Y}_n 0$. Compare the first signal against staying flat: flat earns $0$, while one signal earns gross $G(1) = \delta(2\Phi(\delta/\tau) - 1)$ at cost $c$. So a single signal beats staying flat iff: $$\Pi(1) = \delta\left(2\Phi\!\left(\frac{\delta}{\tau}\right) - 1\right) - c > 0$$ Moreover, $G$ is concave in $n$ with $G(0) = 0$ (its derivative $\delta^2 \phi(\delta\sqrt{n}/\tau)/(\tau\sqrt{n})$ is strictly decreasing in $n$), so $G(n)/n$ is decreasing and $G(n) \le n\,G(1)$ for $n \ge 1$. Hence $\Pi(n) \le n(G(1) - c)$: if the first signal is not worth buying, no larger sample is either. The exact threshold is therefore: $$c^{*} = \delta\left(2\Phi\!\left(\frac{\delta}{\tau}\right) - 1\right)$$ If $c > c^{*}$, stay flat: even the first (most valuable) signal does not justify its cost. *Pitfall:* evaluating a "marginal value of sampling at $n = 0$" as $\frac{\delta^2}{\tau}\phi(0) = \frac{\delta^2}{\tau\sqrt{2\pi}}$ is invalid -- the true derivative $\partial G/\partial n = \delta^2 \phi(\delta\sqrt{n}/\tau)/(\tau\sqrt{n})$ diverges like $1/\sqrt{n}$ as $n \to 0^{+}$, so there is no finite marginal value at zero and the threshold cannot be obtained that way. **Answer:** 1. Optimal rule: $w^{*} = \text{sign}(\bar{Y}_n)$. 2. Net profit: $\Pi(n) = \delta(2\Phi(\delta\sqrt{n}/\tau) - 1) - nc$ under a symmetric two-point prior. 3. Optimal $n^{*}$: solve $\delta^2 \phi(\delta\sqrt{n}/\tau)/(\tau\sqrt{n}) = c$ numerically. 4. Threshold: $c^{*} = \delta(2\Phi(\delta/\tau) - 1)$ -- the gross value of a single signal; only sample if $c < c^{*}$. --- Optimal Switching in the Two Envelope Problem [medium, Probability] --- https://quantvault.org/problem-42-optimal-switching-in-the-two-envelope-problem.html Q: Two envelopes contain $\$X$ and $\$2X$, where $X \sim U(0, a)$ for some known $a > 0$. You pick one envelope uniformly at random and look at the value inside. You then decide whether to keep it or switch to the other envelope. What is the optimal strategy? Under this strategy, what is the probability that you end up with the envelope containing $\$2X$? Work it out for general $a$, then verify with $a = 100$. A: **How to Think About It:** This is a classic decision theory problem, and the key is to use the information you gain from looking inside the envelope. If you see a value greater than $a$, you know for certain you are holding the $\$2X$ envelope (since the $\$X$ envelope can only contain values up to $a$). If you see a value at most $a$, it could be either envelope -- but one is more likely than the other. The strategy is straightforward: use the observed value as a signal about which envelope you are holding. **Quick Estimate:** Without any strategy (just random guessing), you get the $\$2X$ envelope with probability $1/2$. With the ability to observe and switch, you should do strictly better. Since you can perfectly identify the $\$2X$ envelope whenever $X > a/2$ (because only $2X$ exceeds $a$), and you can make an informed switch otherwise, a probability around $3/4$ feels right. **Approach:** Analyze the optimal strategy by conditioning on whether the observed value exceeds $a$, then use the law of total probability. **Formal Solution:** **The optimal strategy:** If you see a value $v > a$, keep it (you must have the $\$2X$ envelope). If you see $v \leq a$, switch (you are more likely holding the $\$X$ envelope). **Why switching is correct when $v \leq a$:** The $\$X$ envelope always contains a value in $[0, a]$. The $\$2X$ envelope contains a value in $[0, a]$ only when $X \leq a/2$, which happens with probability $1/2$. So conditional on seeing $v \leq a$, it is twice as likely that you are holding the $\$X$ envelope as the $\$2X$ envelope. Formally: $$P(\text{holding } X \mid v \leq a) = \frac{2}{3}, \quad P(\text{holding } 2X \mid v \leq a) = \frac{1}{3}$$ So switching when $v \leq a$ gives you the $\$2X$ envelope with probability $2/3$. **Computing the overall probability:** First, find $P(v > a)$. The observed value exceeds $a$ only if you picked the $\$2X$ envelope AND $X > a/2$: $$P(v > a) = P(\text{pick } 2X) \cdot P(X > a/2) = \frac{1}{2} \cdot \frac{1}{2} = \frac{1}{4}$$ So $P(v \leq a) = 3/4$. There are two ways to end up with the $\$2X$ envelope: 1. See $v > a$, keep it: probability $= 1/4$ (and you get $2X$ with certainty) 2. See $v \leq a$, switch to $2X$: probability $= 3/4 \cdot 2/3 = 1/2$ By the law of total probability: $$P(\text{get } 2X) = \frac{1}{4} + \frac{1}{2} = \frac{3}{4}$$ This holds for any $a > 0$, so for $a = 100$ the answer is also $3/4$. **Answer:** The optimal strategy is to keep the envelope if the observed value exceeds $a$, and switch otherwise. Under this strategy, the probability of obtaining the $\$2X$ envelope is $3/4$. --- Parity of the Last Element in a Set Reduction Game [medium, Brain Teaser] --- https://quantvault.org/problem-43-parity-of-the-last-element-in-a-set-reduction-game.html Q: Start with the set $\{1, 2, 3, \ldots, 50\}$. At each step, pick any two elements, remove them both, and insert their absolute difference back into the set. Repeat until only one number remains. Is that final number always even, always odd, or does it depend on the choices made? Prove your answer. A: **How to Think About It:** When you see a process that reduces a set one element at a time, the first question to ask is: what quantity is preserved? The absolute value operation is tricky -- it can map large numbers to small ones -- but it can't change parity. Even minus even is even; odd minus odd is even; even minus odd is odd. That means each step either leaves the count of odd numbers unchanged or decreases it by exactly 2. The parity of that count can never flip. **Key Insight:** The parity of the count of odd numbers in the set is an invariant -- it is fixed from the very first step to the very last. **Proof:** Consider what happens when we replace $a$ and $b$ with $|a - b|$. There are three cases: - **Both even:** $|a - b|$ is even. The count of odd numbers changes by $0$. - **Both odd:** $|a - b|$ is even. We remove 2 odd numbers and add 1 even number. The count of odd numbers decreases by $2$. - **One even, one odd:** $|a - b|$ is odd. We remove 1 odd number and add 1 odd number. The count of odd numbers changes by $0$. In every case the count of odd numbers changes by $0$ or $-2$, so its parity (even or odd) is preserved throughout the entire process. **Initial state:** In $\{1, 2, \ldots, 50\}$, the odd numbers are $1, 3, 5, \ldots, 49$. That is $25$ odd numbers -- an **odd** count. **Final state:** One number remains. The count of odd numbers in a singleton set is either $0$ (even count, if the number is even) or $1$ (odd count, if the number is odd). Since the invariant says the count of odd numbers must stay odd, the final number must be **odd**. **Answer:** The final remaining number is always **odd**, no matter what choices are made during the process. --- Two Guards, One Question [easy, Brain Teaser] --- https://quantvault.org/problem-48-two-guards-one-question.html Q: You are at a fork in the road. One path leads to your destination; the other leads the wrong way. Two guards stand at the fork -- one always tells the truth, one always lies. You do not know which is which. You may ask exactly one yes/no question to exactly one of the guards. Can you determine the correct path? If yes, state the question. If no, explain why not. A: **How to Think About It:** A direct question ('Is this the right path?') fails immediately: in one scenario both guards say 'yes,' in the other both say 'no,' so the answer tells you nothing about which guard you are talking to. The trick is to construct a question whose answer is the same regardless of which guard you ask -- a question that uses the existence of the other guard to cancel out the liar's distortion. **Key Insight:** If you ask a guard what the OTHER guard would say, a truth-teller reports the liar's false answer, and a liar lies about the truth-teller's true answer. Both distortions point in the same wrong direction, so both guards give you the same (incorrect) answer. Knowing the answer is wrong, you choose the opposite. **The Method:** Ask either guard: 'If I asked the other guard which path leads to my destination, what would they say?' Analyze the four cases: 1. You ask the truth-teller; the correct path is on the left. - The liar would say 'right' (lie). - The truth-teller truthfully reports what the liar would say: 'right.' - Answer: right. Choose left (opposite). 2. You ask the liar; the correct path is on the left. - The truth-teller would say 'left' (truth). - The liar lies about this: 'right.' - Answer: right. Choose left (opposite). 3. You ask the truth-teller; the correct path is on the right. - The liar would say 'left.' - The truth-teller reports: 'left.' - Answer: left. Choose right (opposite). 4. You ask the liar; the correct path is on the right. - The truth-teller would say 'right.' - The liar lies: 'left.' - Answer: left. Choose right (opposite). In all four cases: the guard's answer points to the WRONG path. Always choose the opposite of what you are told. **Practical Considerations:** A common wrong answer is to try to identify which guard is the truth-teller first. That requires at least two questions. The self-referential structure -- asking what the other would say -- is the only way to extract reliable information from a single question to an unknown source. **Answer:** Yes. Ask either guard: 'Which path would the other guard tell me leads to my destination?' Then take the opposite path. The double-negation (truth-teller reports a lie; liar lies about the truth) always points you in the wrong direction, so inverting the answer gives you the right one. --- Witness Reliability and Bayes' Theorem [medium, Probability] --- https://quantvault.org/problem-50-witness-reliability-and-bayes-theorem.html Q: A murder has been committed. A witness claims the murderer drove a red car. In this city, all cars are either red or blue, with a fraction $r$ of all cars being red. The witness correctly identifies the color of a car with probability $a$ (and is wrong with probability $1-a$), regardless of the car's actual color. Given the witness's statement that the car was red, what is the probability the car was actually red? Evaluate for $r = 13/20$ and $a = 3/5$. A: **How to Think About It:** This is a classic Bayes' theorem problem where you need to update a prior belief (the base rate of red cars) using noisy evidence (the witness statement). The witness says "red," but they are only right with probability $a$. The question is whether this testimony moves the needle, and by how much. Your gut should say: if most cars are already red ($r > 0.5$) and the witness says red, the posterior should be higher than the prior -- the testimony reinforces the base rate. But the witness is not very reliable ($a = 0.6$), so the update should be moderate. **Quick Estimate:** Prior probability of red is $r = 0.65$. The witness says red and is right 60% of the time. Rough Bayesian reasoning: the "evidence for red" is $a \cdot r = 0.6 \times 0.65 = 0.39$. The "evidence for blue but witness says red" is $(1-a)(1-r) = 0.4 \times 0.35 = 0.14$. Posterior $\approx 0.39 / (0.39 + 0.14) = 0.39/0.53 \approx 0.736$. So the witness testimony bumps the probability from 65% to about 74%. **Approach:** Apply Bayes' theorem directly, conditioning on the witness saying "red." **Formal Solution:** Let $R$ = the car is red, and $W$ = the witness says the car is red. - Prior: $P(R) = r$, $P(R^c) = 1 - r$ - Likelihood: $P(W | R) = a$ (witness is correct when car is red) - False positive: $P(W | R^c) = 1 - a$ (witness is wrong when car is blue) By Bayes' theorem: $$P(R | W) = \frac{P(W|R) \cdot P(R)}{P(W|R) \cdot P(R) + P(W|R^c) \cdot P(R^c)}$$ $$= \frac{ar}{ar + (1-a)(1-r)}$$ **Plugging in $r = 13/20$ and $a = 3/5$:** $$P(R|W) = \frac{\frac{3}{5} \cdot \frac{13}{20}}{\frac{3}{5} \cdot \frac{13}{20} + \frac{2}{5} \cdot \frac{7}{20}}$$ $$= \frac{\frac{39}{100}}{\frac{39}{100} + \frac{14}{100}} = \frac{39}{53}$$ **Answer:** $P(R|W) = \dfrac{ar}{ar + (1-a)(1-r)}$. For $r = 13/20$, $a = 3/5$: $P(R|W) = \dfrac{39}{53} \approx 0.736$. --- Counterfeit Coin With One Weighing [easy, Brain Teaser] --- https://quantvault.org/problem-52-counterfeit-coin-with-one-weighing.html Q: You have 9 visually identical coins. Exactly one of them is counterfeit -- it is either heavier or lighter than the genuine coins, but you don't know which. You have a balance scale and you may use it exactly **once**. Is it possible to *always* identify which coin is counterfeit **and** determine whether it is heavier or lighter? Give a rigorous information-theoretic argument. Specifically, count the number of distinguishable hypotheses and compare it to the number of possible outcomes of a single weighing. A: **How to Think About It:** Before touching any coins, ask yourself: how much information do I need, and how much can I get? This is the right reflex for any "can you do X in $k$ weighings?" problem. A balance scale has three outcomes per weighing (left heavy, right heavy, balanced), so one weighing gives you at most $\log_2 3 \approx 1.58$ bits -- equivalently, it can distinguish among at most 3 hypotheses. Now count how many hypotheses you actually need to separate. **Quick Estimate:** There are 9 coins, and for each coin there are 2 possibilities (it's heavier or lighter). That gives $9 \times 2 = 18$ hypotheses total. One weighing has 3 outcomes. Since $18 > 3$, you cannot map 18 hypotheses injectively to 3 outcomes. Done -- it's impossible. **Approach:** We formalize this as a pigeonhole / information-theoretic argument. **Formal Solution:** Label the coins $c_1, c_2, \ldots, c_9$. The unknown state of the world is a pair $(i, d)$ where $i \in \{1, 2, \ldots, 9\}$ identifies the counterfeit coin and $d \in \{H, L\}$ indicates whether it is heavier or lighter. The total number of hypotheses is: $$|\mathcal{H}| = 9 \times 2 = 18$$ A single weighing places some subset $A$ of coins on the left pan and some subset $B$ on the right pan (with $|A| = |B|$ and $A \cap B = \emptyset$). The outcome is one of three values: $$\mathcal{O} = \{\text{left heavy},\ \text{balanced},\ \text{right heavy}\}$$ So $|\mathcal{O}| = 3$. To "always identify the counterfeit coin and its direction" means that for every possible hypothesis $(i, d) \in \mathcal{H}$, the observed outcome must uniquely determine $(i, d)$. Formally, this requires an injective function $f : \mathcal{H} \to \mathcal{O}$. But an injective function from an 18-element set to a 3-element set cannot exist, since $18 > 3$. By the pigeonhole principle, at least $\lceil 18/3 \rceil = 6$ hypotheses must map to the same outcome. Among those 6 hypotheses sharing one outcome, we have no way to distinguish them. Therefore, **no strategy using a single weighing can always determine both the identity and direction of the counterfeit coin among 9 coins**. **Remark -- what CAN one weighing solve?** The information-theoretic bound says one weighing can handle at most 3 hypotheses. If you only had 1 coin that might be heavy or light plus one known-good reference coin, that's $1 \times 2 = 2$ hypotheses -- solvable. For the classic problem where you know the counterfeit is *heavier* (eliminating the $d$ unknown), 9 coins give 9 hypotheses, and you would need $\lceil \log_3 9 \rceil = 2$ weighings. So even with that simplification, one weighing is not enough for 9 coins (though it works for 3). **Answer:** No. There are 18 hypotheses (9 coins $\times$ 2 directions) but only 3 distinguishable outcomes from one weighing. By the pigeonhole principle, no single weighing can always identify the counterfeit coin and its direction. --- Coupon Collector's Problem [medium, Expectation] --- https://quantvault.org/problem-53-coupon-collector-s-problem.html Q: Coupons in cereal boxes are numbered 1 through 5, and each box contains exactly one coupon drawn uniformly at random (with replacement). You need to collect one of each coupon to win a prize. On average, how many boxes do you need to buy to complete the full set? A: **How to Think About It:** This is the classic coupon collector setup. The key insight is to decompose the collection process into phases. Phase 1 starts when you have 0 distinct coupons and ends when you get your first -- that takes exactly 1 box. Phase 2 starts when you have 1 distinct coupon and ends when you get a new one -- each box has a $4/5$ chance of being new, so you are waiting for the first success of a geometric with $p = 4/5$. And so on. Each phase is an independent geometric waiting time, so the total expected boxes is just the sum of the phase expectations. **Quick Estimate:** With $n = 5$ coupons, the hardest part is the last coupon. You already have 4, so each box has only a $1/5$ chance of being the missing one -- that phase alone costs $5$ boxes in expectation. The second-hardest phase costs $5/2 = 2.5$, the third costs $5/3 \approx 1.67$, then $5/4 = 1.25$, and the first coupon costs $1$. Adding up: $1 + 1.25 + 1.67 + 2.5 + 5 = 11.42$. So roughly 11-12 boxes. For a quick sanity check: the well-known asymptotic formula for $n$ coupons gives $n \ln n + \gamma n \approx 5 \times 1.609 + 0.577 \times 5 \approx 10.93$, which is in the right ballpark (the exact harmonic sum is a bit higher). **Approach:** Decompose the collection process into geometric waiting times and sum their expectations. **Formal Solution:** Define $T$ as the total number of boxes needed. Break $T$ into phases $T = T_1 + T_2 + \cdots + T_5$, where $T_i$ is the number of boxes bought during the phase where you go from $i-1$ distinct coupons to $i$ distinct coupons. When you have $i-1$ distinct coupons, the probability that a new box contains a coupon you do not yet have is: $$p_i = \frac{5 - (i-1)}{5} = \frac{6-i}{5}$$ Each $T_i$ is geometrically distributed with success probability $p_i$, so $E[T_i] = 1/p_i = 5/(6-i)$. Summing over all phases: $$E[T] = \sum_{i=1}^{5} \frac{5}{6-i} = \frac{5}{5} + \frac{5}{4} + \frac{5}{3} + \frac{5}{2} + \frac{5}{1}$$ $$= 5\left(1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \frac{1}{5}\right) = 5 \cdot H_5$$ where $H_5 = 1 + 1/2 + 1/3 + 1/4 + 1/5 = 137/60$ is the 5th harmonic number. $$E[T] = 5 \times \frac{137}{60} = \frac{137}{12} \approx 11.42$$ **Answer:** The expected number of boxes is $5 \cdot H_5 = 137/12 \approx 11.42$. More generally, for $n$ coupon types, the answer is $n \cdot H_n$, where $H_n$ is the $n$th harmonic number. --- Expected Number of Fixed Points in a Random Permutation [medium, Expectation] --- https://quantvault.org/problem-54-expected-number-of-fixed-points-in-a-random-permutation.html Q: A permutation $\pi$ of $\{1, 2, \ldots, n\}$ is chosen uniformly at random. A fixed point is an element $i$ such that $\pi(i) = i$. 1. What is the expected number of fixed points? 2. What is the variance of the number of fixed points? A: **How to Think About It:** The trick is to avoid thinking about the global structure of the permutation. Instead, decompose the count into indicators -- one for each position. Each indicator asks a simple question: "does element $i$ stay in place?" Linearity of expectation handles the rest, even though the indicators are not independent. This is probably the single most important technique in combinatorial probability. **Quick Estimate:** Each element has a $1/n$ chance of being a fixed point, and there are $n$ elements. By linearity, the expected count is $n \cdot 1/n = 1$. Done. The beautiful part: this does not depend on $n$ at all. **Formal Solution:** **Part 1: Expected value.** Define indicator random variables $X_i = \mathbf{1}[\pi(i) = i]$ for $i = 1, \ldots, n$. The total number of fixed points is $X = \sum_{i=1}^n X_i$. Each element is equally likely to map to any of the $n$ positions, so: $$P(\pi(i) = i) = \frac{1}{n}$$ By linearity of expectation (no independence needed): $$E[X] = \sum_{i=1}^n E[X_i] = \sum_{i=1}^n \frac{1}{n} = 1$$ **Part 2: Variance.** We need $\text{Var}(X) = E[X^2] - (E[X])^2$. Expand: $$E[X^2] = E\left[\left(\sum_{i=1}^n X_i\right)^2\right] = \sum_{i=1}^n E[X_i^2] + \sum_{i \neq j} E[X_i X_j]$$ Since $X_i$ is an indicator, $X_i^2 = X_i$, so $E[X_i^2] = 1/n$ and: $$\sum_{i=1}^n E[X_i^2] = n \cdot \frac{1}{n} = 1$$ For the cross terms ($i \neq j$): $X_i X_j = 1$ iff both $\pi(i) = i$ AND $\pi(j) = j$. The probability that $i$ maps to itself is $1/n$, and given that, the probability $j$ maps to itself (among the remaining $n-1$ elements) is $1/(n-1)$: $$E[X_i X_j] = P(\pi(i) = i \text{ and } \pi(j) = j) = \frac{1}{n} \cdot \frac{1}{n-1}$$ There are $n(n-1)$ ordered pairs $(i, j)$ with $i \neq j$: $$\sum_{i \neq j} E[X_i X_j] = n(n-1) \cdot \frac{1}{n(n-1)} = 1$$ Therefore: $$E[X^2] = 1 + 1 = 2$$ $$\text{Var}(X) = E[X^2] - (E[X])^2 = 2 - 1 = 1$$ **Answer:** The expected number of fixed points is $E[X] = 1$ and the variance is $\text{Var}(X) = 1$, both independent of $n$. **Remark:** As $n \to \infty$, the distribution of fixed points converges to $\text{Poisson}(1)$. This is consistent with both the mean and variance equaling 1. The convergence follows because the indicators $X_i$ become "approximately independent" for large $n$, and a sum of many rare, nearly independent indicators is approximately Poisson. --- Expected Winnings of a Repeat-Stop Dice Game [medium, Expectation] --- https://quantvault.org/problem-55-expected-winnings-of-a-repeat-stop-dice-game.html Q: You have a 6-sided die with faces $(1, 2, 3, 1, 2, 3)$ -- so each of the values $\{1, 2, 3\}$ appears with probability $\frac{1}{3}$. You roll repeatedly. Each roll pays you coins equal to the face value shown. The game ends the moment you roll the same value as the previous roll. What is the expected total payout? A: **How to Think About It:** This is a Markov chain stopping problem. After each roll you earn that value and check: did I repeat? The key observation is that the die has only three distinct values, each equally likely at probability $\frac{1}{3}$. So from any given last roll $v$, you have a $\frac{1}{3}$ chance of stopping immediately (rolling $v$ again) and a $\frac{2}{3}$ chance of continuing with one of the other two values. That symmetry is what makes this tractable -- define conditional expectations $E_v$ and you will find they are all equal. **Quick Estimate:** Think about it informally. The first roll is worth $2$ in expectation (the average of $1, 2, 3$). After that, conditional on not having stopped, you expect about one more roll before the game ends (stopping probability $\frac{1}{3}$ each step, so geometric with mean $3$ additional rolls -- but those include the stopping roll). Actually each "continuation phase" has a geometric number of rolls averaging $3$ rolls total before stopping: that is about $3 \times 2 = 6$ in additional expected coins after the first roll. So total is around $2 + 6 = 8$. That should be exactly right by symmetry. **Approach:** Set up conditional expected future earnings $E_v$ given the last roll was $v$, then exploit symmetry to solve the linear system. **Formal Solution:** Let $E_v$ denote the expected additional coins earned given that the last roll was $v \in \{1, 2, 3\}$. On the next roll: - With probability $\frac{1}{3}$, you roll $v$ again, earning $v$ and the game ends. Contribution: $\frac{1}{3} \cdot v$. - With probability $\frac{1}{3}$ each, you roll one of the two other values $a$ or $b$, earning that value and continuing. Contribution: $\frac{1}{3}(a + E_a) + \frac{1}{3}(b + E_b)$. So the equation for $E_v$ is: $$E_v = \frac{v}{3} + \frac{1}{3}(a + E_a) + \frac{1}{3}(b + E_b)$$ Since $\{v, a, b\} = \{1, 2, 3\}$, we have $v + a + b = 6$, so: $$E_v = \frac{v + a + b}{3} + \frac{E_a + E_b}{3} = 2 + \frac{E_a + E_b}{3}$$ This holds for all three values. Let $S = E_1 + E_2 + E_3$. Summing all three equations: $$S = 3 \cdot 2 + \frac{1}{3}\bigl((E_2 + E_3) + (E_1 + E_3) + (E_1 + E_2)\bigr) = 6 + \frac{2S}{3}$$ $$S - \frac{2S}{3} = 6 \implies \frac{S}{3} = 6 \implies S = 18$$ So $E_1 + E_2 + E_3 = 18$. By symmetry of the equation $E_v = 2 + \frac{S - E_v}{3}$, we get: $$E_v = 2 + \frac{18 - E_v}{3} \implies E_v = 2 + 6 - \frac{E_v}{3} \implies \frac{4E_v}{3} = 8 \implies E_v = 6$$ All three conditional continuation values are equal to $6$. The first roll is unconditional. The expected value of the first roll is: $$\frac{1+2+3+1+2+3}{6} = \frac{12}{6} = 2$$ The total expected payout is the first roll plus the expected future earnings given that roll: $$E[\text{total}] = 2 + 6 = 8$$ **Faster route:** Let $T$ be the total number of rolls. After the first roll, every roll repeats the previous one with probability $\frac{1}{3}$, whatever happened before, so the number of rolls after the first, $N$, satisfies $N = 1 + \frac{2}{3}N$, giving $N = 3$ and $E[T] = 1 + 3 = 4$. Whether roll $k$ happens at all (the event $T \ge k$) is decided by rolls $1, \ldots, k-1$ only, so it is independent of the face $X_k$, which still averages $2$. By Wald's identity, $$E[\text{total}] = \sum_{k \ge 1} P(T \ge k)\, E[X_k] = 2\, E[T] = 2 \cdot 4 = 8.$$ The stopping roll is not biased either: given that roll $k$ repeats roll $k-1$, its face is still uniform on $\{1, 2, 3\}$, since each value has probability $\frac{1}{9}$ out of $\frac{1}{3}$. **Answer:** $\boxed{8}$ --- Gambler's Ruin on a Fair Coin [easy, Probability] --- https://quantvault.org/problem-56-gambler-s-ruin-on-a-fair-coin.html Q: A gambler starts with $\$k$ and repeatedly bets $\$1$ on a fair coin flip -- heads they win $\$1$, tails they lose $\$1$. The game ends when their bankroll hits $\$n$ (they win) or $\$0$ (they're ruined). What is the probability the gambler reaches $\$n$ before going broke? A: **How to Think About It:** This is the classic gambler's ruin -- a symmetric random walk with two absorbing barriers. Before doing any math, think about it this way: the coin is fair, so there's no drift. The gambler's only "edge" is how close they start to the target relative to the total distance. If you start at $k = 1$ with $n = 100$, you're almost certainly going broke. If you start at $k = 99$, you're almost certainly winning. The answer should be linear in $k$ -- and it is. **Quick Estimate:** Suppose $k = 25$ and $n = 100$. By symmetry of a fair random walk, the gambler's chance of reaching $n$ versus $0$ should be proportional to how far they are from each barrier. Distance to $0$ is 25, distance to $n$ is 75. So the probability of winning should be roughly $25/100 = 0.25$. That turns out to be exact. **Approach:** Set up a recurrence relation using first-step analysis, then solve the resulting linear system. **Formal Solution:** Let $p_i$ be the probability of reaching $\$n$ starting from state $\$i$. Boundary conditions: $$p_0 = 0, \quad p_n = 1$$ By first-step analysis, for $0 < i < n$: $$p_i = \frac{1}{2}\,p_{i-1} + \frac{1}{2}\,p_{i+1}$$ Rearranging: $p_{i+1} - p_i = p_i - p_{i-1}$. This says the differences $d_i = p_i - p_{i-1}$ are all equal. Call this common difference $d$. Then $p_i = p_0 + i \cdot d = i \cdot d$. Applying $p_n = 1$ gives $d = 1/n$, so: $$p_i = \frac{i}{n}$$ Note: this is the unique solution to the harmonic equation $p_i = \frac{1}{2}(p_{i-1} + p_{i+1})$ on $\{0, 1, \ldots, n\}$ with the given boundary conditions. Harmonic functions on a line segment are linear -- this is a discrete version of the Laplace equation. **Answer:** The probability of reaching $\$n$ starting from $\$k$ is: $$P(\text{win}) = \frac{k}{n}$$ --- Marble Parity Puzzle [medium, Probability] --- https://quantvault.org/problem-57-marble-parity-puzzle.html Q: A bucket contains an odd number of white marbles and some number of black marbles. Outside the bucket you have an unlimited supply of black marbles. You repeat the following procedure: 1. Randomly pick 2 marbles from the bucket. 2. If at least one of them is black, discard one black marble (put it outside) and return the other marble to the bucket. 3. If both are white, remove them both and place one black marble into the bucket. Each step reduces the total count in the bucket by exactly one. Eventually only a single marble remains. What color is the last marble? A: **How to Think About It:** Whenever you see a process that removes items according to color-based rules, your first instinct should be: track the parity of each color separately. The total count drops by 1 every step, so the process definitely terminates with one marble. The question is whether that survivor can be pinned down. Instead of simulating, ask: is there an invariant -- some quantity that never changes -- that forces the answer? **Quick Estimate:** Think about a tiny example. Start with 1 white and 2 black marbles. Step 1: pick two marbles. If you pick 2 blacks, discard one black, put the other back -- you have 1 white, 1 black. Step 2: pick the white and black, discard the black, put the white back. Last marble: white. Try 3 whites, 0 blacks. Step 1: pick 2 whites, remove both, add 1 black -- now 1 white, 1 black. Step 2: pick white and black, discard black, keep white. Last marble: white again. Every small case gives white. **Approach:** Track the parity of the white marble count through each case. **Formal Solution:** Let $W$ be the number of white marbles in the bucket. We examine how $W$ changes in each case: - **Two blacks drawn:** One black is discarded, one goes back. $W$ is unchanged. Total drops by 1. - **One black, one white drawn:** The black is discarded, the white goes back. $W$ is unchanged. Total drops by 1. - **Two whites drawn:** Both whites are removed and one black is added. $W$ decreases by 2. Total drops by 1. In every case, $W$ changes by either $0$ or $-2$ -- always an even number. This means the parity of $W$ is an invariant of the process. We start with $W$ odd. Therefore $W$ is odd after every step. In particular, $W$ can never equal $0$, because $0$ is even. Since the total marble count decreases by 1 per step, we eventually reach exactly 1 marble. At that point $W \geq 1$ (it cannot be zero), but there is only 1 marble total, so $W = 1$. That marble is white. $$\boxed{\text{The last marble is white.}}$$ **Answer:** The last marble is always white. The parity of the white count is invariant -- it starts odd and can only change by an even number, so it stays odd forever and can never reach zero. --- Two Children and a Boy [easy, Probability] --- https://quantvault.org/problem-60-two-children-and-a-boy.html Q: Kevin has two children. Each child is equally likely to be a boy or a girl, and the genders are independent. You are told that at least one of Kevin's children is a boy. What is the probability that both children are boys? A: **How to Think About It:** The heuristic is **condition by counting the surviving equally-likely outcomes**, and the trap is the phrasing. "At least one is a boy" is a statement about the *pair*, not about a specific child you inspected — so it kills only the all-girl outcome, not three of four. The intuitive $1/2$ comes from silently rewriting the problem as "a particular child is a boy, what's the other?" That is a different (and here, incorrect) conditioning. Whenever a gender/coin puzzle feels like it "should" be $1/2$, check whether the information is about one labeled item or about the collection. **Quick Estimate:** List the four equally likely ordered pairs — $BB, BG, GB, GG$ — and cross out the one that violates the condition ($GG$). Three survive, each equally weighted, and exactly one ($BB$) is a win: $\tfrac{1}{3}$. You can do the whole thing on your fingers; no arithmetic beyond "1 out of 3." **Formal Solution:** Sample space (each probability $1/4$): $\{BB, BG, GB, GG\}$. Let $A = \{\text{at least one boy}\} = \{BB, BG, GB\}$, so $P(A) = 3/4$. Since $\{BB\}\subset A$, we have $P(BB\cap A) = P(BB) = 1/4$. Then $$P(BB \mid A) = \frac{P(BB\cap A)}{P(A)} = \frac{1/4}{3/4} = \boxed{\tfrac{1}{3}}.$$ **Answer:** $P(\text{both boys}\mid\text{at least one boy}) = 1/3$. --- Average Speed Over Two Laps [medium, Brain Teaser] --- https://quantvault.org/problem-62-average-speed-over-two-laps.html Q: A car races two laps of a track, each lap the same distance. It completes the first lap at 100 mph and the second lap at 200 mph. What is the average speed over the full two-lap race? A: **How to Think About It:** Most people's first instinct is to average the speeds: $(100 + 200)/2 = 150$ mph. That is wrong. Average speed is total distance divided by total time -- and the two laps do not take equal time, so the speeds are not equally weighted. The car spends twice as long crawling through the first lap at 100 mph as it does blazing through the second at 200 mph. The slow lap dominates the total time, pulling the average below 150. **Quick Estimate:** Let each lap be 100 miles. Time for lap 1: $100/100 = 1$ hour. Time for lap 2: $100/200 = 0.5$ hours. Total: 200 miles in 1.5 hours. Average speed: $200/1.5 \approx 133$ mph. This is the harmonic mean of 100 and 200. **Approach:** Average speed = total distance / total time; this gives the harmonic mean, not the arithmetic mean, when legs have equal distance. **Formal Solution:** Let each lap have length $d$. - Time for lap 1: $t_1 = d/100$ - Time for lap 2: $t_2 = d/200$ Average speed: $$\bar{v} = \frac{\text{Total distance}}{\text{Total time}} = \frac{2d}{t_1 + t_2} = \frac{2d}{\frac{d}{100} + \frac{d}{200}} = \frac{2d}{\frac{2d + d}{200}} = \frac{2d \cdot 200}{3d} = \frac{400}{3}.$$ This is the harmonic mean of the two speeds: $$\bar{v} = \frac{2 v_1 v_2}{v_1 + v_2} = \frac{2 \times 100 \times 200}{100 + 200} = \frac{40000}{300} = \frac{400}{3} \approx 133.33 \text{ mph}.$$ **Answer:** $\dfrac{400}{3} \approx 133.33$ mph. --- Bayesian Die Identification [easy, Probability] --- https://quantvault.org/problem-64-bayesian-die-identification.html Q: You have two fair dice on the table: a 4-sided die with faces 1-4, and a 6-sided die with faces 1-6. One die is selected uniformly at random (each equally likely), and a single roll produces the result 2. What is the probability that the selected die was the 4-sided die? Think through this using Bayes' theorem and explain why the observation of a 2 shifts your belief away from the 50/50 prior. A: **How to Think About It:** Before calculating anything, ask: does seeing a 2 make the 4-sided die more or less likely? The 4-sided die has a 1/4 chance of rolling a 2; the 6-sided die has a 1/6 chance. So a 2 is more likely to come from the 4-sided die. The posterior should be above 50%. The question is by how much. **Quick Estimate:** The likelihoods are 1/4 vs. 1/6, a ratio of 3:2 in favor of the 4-sided die. With equal priors, the posterior odds are also 3:2, giving $P(\text{4-sided} \mid 2) = 3/5 = 60\%$. You can do this in your head without writing Bayes' formula explicitly. **Approach:** Formal Bayes' theorem to verify the estimate. **Formal Solution:** Define: - $D_4$: event the 4-sided die was selected - $D_6$: event the 6-sided die was selected - $R = 2$: observed roll Priors: $P(D_4) = P(D_6) = 1/2$ Likelihoods: $$P(R=2 \mid D_4) = \frac{1}{4}, \quad P(R=2 \mid D_6) = \frac{1}{6}$$ Marginal probability of rolling a 2 (law of total probability): $$P(R=2) = P(R=2 \mid D_4)P(D_4) + P(R=2 \mid D_6)P(D_6) = \frac{1}{4}\cdot\frac{1}{2} + \frac{1}{6}\cdot\frac{1}{2} = \frac{1}{8} + \frac{1}{12} = \frac{5}{24}$$ Bayes' theorem: $$P(D_4 \mid R=2) = \frac{P(R=2 \mid D_4)\,P(D_4)}{P(R=2)} = \frac{\frac{1}{4}\cdot\frac{1}{2}}{\frac{5}{24}} = \frac{\frac{1}{8}}{\frac{5}{24}} = \frac{1}{8}\cdot\frac{24}{5} = \frac{3}{5}$$ **Answer:** $P(\text{4-sided die} \mid \text{rolled 2}) = \dfrac{3}{5} = 0.60$. --- Beating a Loaded Die [easy, Probability] --- https://quantvault.org/problem-65-beating-a-loaded-die.html Q: You and a friend each roll a single die. Your die is a standard fair 6-sided die with faces $\{1, 2, 3, 4, 5, 6\}$, each equally likely. Your friend's die is non-standard: its six faces show the values $\{1, 1, 1, 6, 6, 6\}$, each face equally likely. Both of you roll once. What is the probability that your roll is strictly higher than your friend's? A: **How to Think About It:** Before computing anything, think about what your friend's die can actually show. It only has two possible outcomes: $1$ (with probability $1/2$) or $6$ (with probability $1/2$). If your friend rolls a $6$, you cannot beat them -- the best you can do on a standard die is also $6$, which ties but does not win. So the only way you win is if your friend rolls a $1$, and then you roll something strictly greater than $1$. **Quick Estimate:** Your friend rolls $1$ half the time. When they do, you just need to beat $1$ on a standard die, which means rolling $2, 3, 4, 5,$ or $6$ -- that is $5$ out of $6$ outcomes. So the answer should be roughly $(1/2)(5/6) \approx 0.417$. That is a bit below $1/2$, which makes sense: the loaded die concentrates mass on the extremes, so it is harder to beat than you might expect. **Approach:** Condition on your friend's outcome and use the law of total probability. **Formal Solution:** Let $X$ be your roll and $Y$ be your friend's roll. We want $P(X > Y)$. Your friend's die has three faces showing $1$ and three showing $6$, so: $$P(Y = 1) = \frac{3}{6} = \frac{1}{2}, \qquad P(Y = 6) = \frac{3}{6} = \frac{1}{2}$$ Conditioning on $Y$: $$P(X > Y) = P(X > Y \mid Y = 1)\,P(Y = 1) + P(X > Y \mid Y = 6)\,P(Y = 6)$$ - If $Y = 6$: no face on a standard die exceeds $6$, so $P(X > 6) = 0$. - If $Y = 1$: you need $X \geq 2$, so $P(X > 1) = 5/6$. Putting it together: $$P(X > Y) = \frac{5}{6} \cdot \frac{1}{2} + 0 \cdot \frac{1}{2} = \frac{5}{12}$$ **Answer:** The probability that you roll strictly higher than your friend is $\dfrac{5}{12} \approx 0.4167$. --- Birthday Problem [easy, Probability] --- https://quantvault.org/problem-67-birthday-problem.html Q: Suppose you have a room full of people, and each person's birthday is equally likely to fall on any of the 365 days of the year (ignore leap years). Birthdays are independent across people. What is the minimum number of people needed so that the probability of at least one shared birthday is at least 50%? A: **How to Think About It:** This is one of the most famous problems in probability, and it comes up constantly in interviews because it tests whether you have good intuition about collisions. Most people guess way too high -- maybe 100 or 180 -- because they confuse "someone shares MY birthday" with "ANY two people share A birthday." The key insight is that the number of pairs grows quadratically: with $n$ people there are $\binom{n}{2} = n(n-1)/2$ pairs, and each pair has a $1/365$ chance of colliding. Once the expected number of collisions gets close to 1, the probability of at least one collision is already substantial. **Quick Estimate:** Think of it this way: with $n$ people, there are roughly $n^2/2$ pairs. Each pair collides with probability $1/365$. The expected number of collisions is about $n^2 / 730$. By a Poisson-style heuristic, the probability of at least one collision is roughly $1 - e^{-n^2/730}$. Set that equal to $0.5$: $$n^2 \approx 730 \ln 2 \approx 506$$ $$n \approx \sqrt{506} \approx 22.5$$ So we expect the answer to be around 23. That is a shockingly small number relative to 365. **Approach:** Compute the probability that all $n$ birthdays are distinct, then find where it drops below $0.5$. **Formal Solution:** Let $P(n)$ be the probability that all $n$ people have distinct birthdays. The first person can have any birthday, the second must avoid 1 day, the third must avoid 2 days, and so on: $$P(n) = \frac{365}{365} \cdot \frac{364}{365} \cdot \frac{363}{365} \cdots \frac{365 - n + 1}{365} = \prod_{i=0}^{n-1} \frac{365 - i}{365}$$ We want the smallest $n$ such that $1 - P(n) \geq 0.5$, equivalently $P(n) \leq 0.5$. To solve analytically, use the standard approximation $1 - x \approx e^{-x}$ for small $x$: $$P(n) \approx \prod_{i=0}^{n-1} e^{-i/365} = e^{-\sum_{i=0}^{n-1} i/365} = e^{-n(n-1)/(2 \cdot 365)}$$ Setting $e^{-n(n-1)/730} = 0.5$ and solving: $$n(n-1) = 730 \ln 2 \approx 505.97$$ Solving the quadratic gives $n \approx 23$. Exact computation confirms this: $P(22) \approx 0.524$ and $P(23) \approx 0.493$, so $1 - P(23) \approx 0.507 > 0.5$. **Answer:** You need $n = 23$ people for at least a 50% chance of a shared birthday. --- Blind Coin Flip Strategy [easy, Brain Teaser] --- https://quantvault.org/problem-68-blind-coin-flip-strategy.html Q: You are blindfolded and sitting in front of a table with $N$ coins. Exactly $k$ of them are heads up -- you are told $k$, but you cannot see or feel which specific coins are heads. You are allowed to flip any coins you like (you can pick them up and flip them, but you still cannot determine their current state). Your goal: design a strategy that guarantees, after your move, both groups of coins (that you define) end up with the same number of heads -- no matter how the original $k$ heads are arranged. You know $N$ and $k$ in advance. Find such a strategy. A: **How to Think About It:** The frustrating thing about this problem is that you seem to have no information -- you are blindfolded and cannot see any coin. But you know $k$, the total number of heads. The key insight is that you can use this number to engineer a clever partition. The idea is: pick exactly $k$ coins at random and flip all of them. You have no idea what state those $k$ coins are in -- but you do not need to. The algebra works out regardless. This is a proof problem, so after the insight the job is to verify the claim algebraically for an arbitrary starting arrangement. **Key Insight:** Splitting out exactly $k$ coins and flipping them all makes the two groups have equal head counts -- and this holds for every possible initial arrangement of the $k$ heads. **The Strategy:** 1. Pick any $k$ coins from the $N$ on the table (blind -- you have no idea which are heads). 2. Flip all $k$ of them. 3. Claim: after this, the $k$-coin group (Group A) and the $(N-k)$-coin group (Group B) each have the same number of heads. **Verification:** Let $h$ be the number of heads that happened to land in Group A before any flipping. Since there are $k$ heads total and Group A has $k$ coins: - Group A before flip: $h$ heads, $k - h$ tails - Group B (untouched): $k - h$ heads After flipping all $k$ coins in Group A: - Group A after flip: the $h$ heads become tails, the $k - h$ tails become heads, so Group A now has $k - h$ heads - Group B is unchanged: still $k - h$ heads $$\text{Group A heads} = k - h \qquad \text{Group B heads} = k - h$$ Both groups have exactly $k - h$ heads. The unknown $h$ drops out -- the result holds for any value of $h$ from $0$ to $k$. **Special cases to sanity-check:** - $h = 0$: all $k$ heads are in Group B. After flipping Group A (all tails), Group A gains $k$ heads. Group B has $k$ heads. Both have $k$. Check. - $h = k$: all $k$ heads are in Group A. After flipping Group A (all heads become tails), Group A has $0$ heads. Group B has $0$ heads. Both have $0$. Check. **Answer:** Separate any $k$ coins into a group and flip all of them. Regardless of the initial arrangement, both groups will have exactly $k - h$ heads, where $h$ is whatever happened to be in your chosen group. Since $h$ cancels, the strategy is guaranteed to work. --- Bridge and Lantern Crossing Optimization [medium, Optimization] --- https://quantvault.org/problem-69-bridge-and-lantern-crossing-optimization.html Q: Four traders need to cross a narrow bridge at night. They have a single lantern, and at most two people can cross at a time. When two people cross together, they move at the speed of the slower person. Someone must carry the lantern back after each crossing so the next pair can go. The four traders have crossing times of $1$, $2$, $7$, and $10$ minutes. The person returning the lantern does not need to be from the most recent pair -- anyone on the starting side can bring it back. 1. Find the minimum total time needed to get all four traders across the bridge. 2. There are two natural strategies: the **shuttle strategy** (the fastest person escorts everyone across one by one) and the **pairing strategy** (send the two slowest together so they only cost one slow crossing). Compare these strategies and prove which one is optimal for the given times. 3. Generalize to $n$ people with sorted crossing times $t_1 \le t_2 \le \cdots \le t_n$. Give a general procedure for optimal crossing and prove it. A: **How to Think About It:** The core tension is the return trip. Every crossing of two people requires someone to bring the lantern back, and that return trip is pure overhead. The question is: how do you minimize the cost of these return trips while also avoiding the disaster of having slow people cross separately (which makes you pay their cost multiple times)? There are really only two ideas worth considering. Either the fastest person shuttles everyone across, or you pair the slow people together so their cost is paid only once. **Quick Estimate:** With times $1, 2, 7, 10$, the shuttle strategy (person 1 escorts everyone) costs $2 + 1 + 7 + 1 + 10 = 21$ minutes -- you pay 1 minute for each return trip, but you pay the full 7 and 10 separately. The pairing strategy sends 1 and 2 across first ($2$ min), 1 returns ($1$ min), then 7 and 10 cross together ($10$ min), 2 returns ($2$ min), then 1 and 2 cross again ($2$ min). Total: $2 + 1 + 10 + 2 + 2 = 17$ minutes. Pairing saves 4 minutes because the 7-minute person's crossing is hidden inside the 10-minute crossing. **Approach:** We compare the two strategies algebraically, then prove no other strategy can do better. **Formal Solution:** Label the crossing times $t_1 \le t_2 \le t_3 \le t_4$ with values $1, 2, 7, 10$. **Strategy 1 -- Shuttle (fastest escorts all):** - $t_1$ and $t_3$ cross: $t_3$ minutes. $t_1$ returns: $t_1$ minutes. - $t_1$ and $t_4$ cross: $t_4$ minutes. $t_1$ returns: $t_1$ minutes. - $t_1$ and $t_2$ cross: $t_2$ minutes. - Total: $t_2 + t_3 + t_4 + 2t_1 = 2 + 7 + 10 + 2 = 21$ minutes. **Strategy 2 -- Pairing (slowest pair together):** - $t_1$ and $t_2$ cross: $t_2$ minutes. $t_1$ returns: $t_1$ minutes. - $t_3$ and $t_4$ cross: $t_4$ minutes. $t_2$ returns: $t_2$ minutes. - $t_1$ and $t_2$ cross: $t_2$ minutes. - Total: $t_2 + t_1 + t_4 + t_2 + t_2 = t_1 + 3t_2 + t_4 = 1 + 6 + 10 = 17$ minutes. The difference is: $$\Delta = \text{Shuttle} - \text{Pairing} = (2t_1 + t_2 + t_3 + t_4) - (t_1 + 3t_2 + t_4) = t_1 + t_3 - 2t_2$$ For our values: $1 + 7 - 2 \cdot 2 = 4 > 0$, so Shuttle costs 4 more minutes. Pairing wins. Pairing is better when $t_1 + t_3 > 2t_2$, i.e., when $t_3 - t_2 > t_2 - t_1$. In words: pairing wins when the gap between the third and second person is larger than the gap between the second and first -- because you are paying $t_2$ for return trips instead of $t_1$, but you save the full difference $t_3 - t_2$ by hiding person 3 inside person 4's crossing. **Proof of optimality for $n = 4$:** Any valid schedule consists of 5 crossings: 3 forward (moving people to the far side) and 2 returns. Every forward crossing moves at most 2 people, and we need to move 4 people total. So we need at least 3 forward trips with 2 returns. Key observations: - Each return trip costs at least $t_1$ (the fastest available person returns). - The two slowest people ($t_3, t_4$) must each cross at least once. If they cross separately (in different forward trips), we pay at least $t_3 + t_4$. If they cross together, we pay only $t_4$. - When they cross together, the lantern must already be on the starting side and someone fast must have brought it back. The cheapest way to set this up is to send $t_1, t_2$ across first (cost $t_2$), return $t_1$ (cost $t_1$), then send $t_3, t_4$ (cost $t_4$), return $t_2$ (cost $t_2$), then send $t_1, t_2$ again (cost $t_2$). Any other schedule either sends $t_3$ and $t_4$ separately (paying $t_3 + t_4$ in forward crossings) or pairs them but uses a suboptimal return arrangement. The two strategies above are the only candidates, and we pick whichever gives the smaller total. **Generalization to $n$ people:** For $n$ people with sorted times $t_1 \le t_2 \le \cdots \le t_n$, process people from the slowest pair down. At each step, we have the two slowest remaining people to move. We choose between: - **Shuttle:** Fastest person escorts them one at a time. Cost for moving persons $i$ and $i-1$: $t_i + t_{i-1} + 2t_1$ (two forward trips plus two returns). - **Pairing:** Send $t_1, t_2$ across, $t_1$ returns, send $t_{i-1}, t_i$ across, $t_2$ returns. Cost: $t_i + 2t_2 + t_1$. Pairing is better when $t_{i-1} + 2t_1 > 2t_2 + t_1$, i.e., $t_{i-1} - t_2 > t_2 - t_1$. We apply this decision greedily to the two slowest people still on the starting side, remove them, and repeat while more than three remain. The recursion then bottoms out on the leftovers: with three left the cost is $t_1 + t_2 + t_3$ (send $t_1, t_3$, walk $t_1$ back, send $t_1, t_2$), and with two left it is $t_2$. The total minimum time is: $$T^{*} = \sum_{\text{pairs}} \min\left(t_i + t_{i-1} + 2t_1,\; t_i + 2t_2 + t_1\right) + B$$ where the sum runs over the pairs $(t_n, t_{n-1}), (t_{n-2}, t_{n-3}), \ldots$ peeled off while more than three people remain, and the base $B$ is $t_2$ if two are left over and $t_1 + t_2 + t_3$ if three are. For $n = 4$ the sum is $\min(10 + 7 + 2,\; 10 + 4 + 1) = 15$ and $B = t_2 = 2$, giving $17$. **Answer:** The minimum total time for crossing times $1, 2, 7, 10$ is $\boxed{17}$ minutes, achieved by the pairing strategy: send 1 and 2 across (2 min), 1 returns (1 min), 7 and 10 cross together (10 min), 2 returns (2 min), 1 and 2 cross (2 min). In general, optimality requires choosing between shuttle and pairing for each pair of slow people: pair them when $t_{i-1} + t_1 > 2t_2$, shuttle them otherwise. --- Brownian Motion Exit From an Interval [hard, Stochastic Process] --- https://quantvault.org/problem-71-brownian-motion-exit-from-an-interval.html Q: Let $B_t$ be a standard Brownian motion starting at $0$. Fix $a, b > 0$ and define the first exit time $$\tau = \inf\{t \geq 0 : B_t \notin (-b,\, a)\}$$ so $\tau$ is the first time the process leaves the interval $(-b, a)$. 1. Compute $P(B_\tau = a)$ -- the probability that the process exits through the upper barrier. 2. Compute $E[\tau]$ -- the expected time to exit. 3. Interpret part (1) as the fair odds for a one-touch wager with asymmetric barriers. If a bookmaker offers a bet that pays $\$1$ when the process first hits $a$ (and nothing if it hits $-b$ first), what is the fair price? A: **How to Think About It:** You have a Brownian motion trapped between two walls at $-b$ and $a$. It will eventually escape through one of them. The question is: which wall does it hit, with what probability, and how long does it take on average? The tool here is the Optional Stopping Theorem (OST) -- Brownian motion is a martingale, so if you can apply OST to the exit time, you get the hitting probability for free. For the expected exit time, you need a second martingale. **Quick Estimate:** Suppose $a = 3$ and $b = 1$. The starting point $0$ is much closer to the lower barrier $-b = -1$ than to the upper barrier $a = 3$. So intuitively, the process should hit $-1$ more often than it hits $3$. The formula gives $P(B_\tau = a) = b/(a+b) = 1/4$, which makes sense -- three-quarters of the time you exit downward through the closer barrier. For expected exit time: $E[\tau] = ab = 3$. If instead $a = b = 1$, symmetry gives $P = 1/2$ and $E[\tau] = 1$. **Approach:** Apply OST to two different martingales: first $B_t$ itself (to get the hitting probability), then $B_t^2 - t$ (to get the expected exit time). **Formal Solution:** **Part (1): Hitting probability** Since $B_t$ is a martingale and $\tau$ is a stopping time with $E[\tau] < \infty$, the OST gives $$E[B_\tau] = E[B_0] = 0$$ At time $\tau$, the process is at either $a$ or $-b$. Let $p = P(B_\tau = a)$. Then $$p \cdot a + (1-p)(-b) = 0$$ Solving: $$pa + pb - b = 0 \implies p = \frac{b}{a+b}$$ This is a "gamblers ruin" result: the hitting probability depends only on the ratio of the distances to the two barriers, not on the volatility or any other parameter. **Part (2): Expected exit time** The process $M_t = B_t^2 - t$ is also a martingale (verify by Ito's lemma: $dM_t = 2B_t\,dB_t$, which has zero drift). Applying OST: $$E[M_\tau] = E[M_0] = 0$$ $$E[B_\tau^2] - E[\tau] = 0$$ $$E[\tau] = E[B_\tau^2] = p \cdot a^2 + (1-p) \cdot b^2$$ Substituting $p = b/(a+b)$: $$E[\tau] = \frac{b}{a+b} \cdot a^2 + \frac{a}{a+b} \cdot b^2 = \frac{a^2 b + a b^2}{a+b} = \frac{ab(a+b)}{a+b} = ab$$ **Part (3): One-touch wager interpretation** The contract pays $\$1$ if $B_t$ hits $a$ before $-b$. Under risk-neutral pricing (Brownian motion is already a martingale), the fair price equals the probability of payout: $$\text{Fair price} = P(B_\tau = a) = \frac{b}{a+b}$$ Notice the intuition: the fair price is determined entirely by the ratio of the lower barrier distance to the total barrier span. A symmetric barrier ($a = b$) gives a fair price of $1/2$. As the lower barrier gets closer ($b \to 0$), the contract becomes worthless (the process almost surely exits downward first). As $b \to \infty$, the price approaches $1$ (the lower barrier is so far away the process will hit $a$ first with near certainty). A bookmaker offering this bet at any price above $b/(a+b)$ is giving you an edge. **Answer:** $$P(B_\tau = a) = \frac{b}{a+b}, \qquad E[\tau] = ab, \qquad \text{Fair price} = \frac{b}{a+b}$$ --- Coin Swap II: Equal Winning Probability [medium, Probability] --- https://quantvault.org/problem-75-coin-swap-ii-equal-winning-probability.html Q: Alice and Bob play a game with a biased coin that shows heads with probability $0 < p \leq 1$. Alice goes first. The rules are asymmetric: - On Alice's turn, she flips the coin once. If she gets heads, she wins. If tails, she passes the coin to Bob. - On Bob's turn, he flips the coin twice. If either flip is heads, he wins. If both are tails, he passes the coin back to Alice. The game continues until someone flips heads. Find the value of $p$ such that Alice and Bob are equally likely to win. Round to the nearest thousandth. A: **How to Think About It:** This is a geometric-series-style recursion. Alice wins on her turn with probability $p$. Bob fails both his flips with probability $(1-p)^2$, returning the coin to Alice in the same state as the start. So the game has a recursive structure: after one full cycle (Alice fails, Bob fails), we are right back where we started. **Quick Estimate:** At $p = 0.5$, Alice wins on her first flip with probability 0.5, which already exceeds half -- so Alice wins more than half the time when $p = 0.5$. We need $p$ lower so Bob gets more chances. At very small $p$, Alice rarely wins on turn one and Bob rarely wins on either flip, leading to many cycles. By symmetry, neither player has a strong advantage at very small $p$ -- but Alice's single flip vs. Bob's two flips suggests Bob gains advantage as $p$ grows from 0. The fair $p$ is somewhere below 0.5, maybe around 0.4. **Formal Solution:** Let $a = P(\text{Alice wins})$. Condition on the first round: - With probability $p$: Alice flips heads on her first flip, she wins immediately. - With probability $(1-p)$: Alice flips tails, coin goes to Bob. - Bob flips twice. He wins (at least one heads) with probability $1-(1-p)^2$. - Bob fails (both tails) with probability $(1-p)^2$, coin returns to Alice. So: $$a = p + (1-p)(1-p)^2 \cdot a = p + (1-p)^3 a$$ Solving: $$a - (1-p)^3 a = p$$ $$a(1 - (1-p)^3) = p$$ $$a = \frac{p}{1-(1-p)^3}$$ Expand the denominator using the factorization $1 - q^3 = (1-q)(1+q+q^2)$ with $q = 1-p$: $$1-(1-p)^3 = p(1 + (1-p) + (1-p)^2) = p(3 - 3p + p^2)$$ So: $$a = \frac{p}{p(3-3p+p^2)} = \frac{1}{3-3p+p^2} = \frac{1}{p^2-3p+3}$$ For $a = 1/2$: $$\frac{1}{p^2-3p+3} = \frac{1}{2} \implies p^2 - 3p + 3 = 2 \implies p^2 - 3p + 1 = 0$$ By the quadratic formula: $$p = \frac{3 \pm \sqrt{5}}{2}$$ Since $p \leq 1$, we discard $\frac{3+\sqrt{5}}{2} \approx 2.618$. $$\boxed{p = \frac{3-\sqrt{5}}{2} \approx 0.382}$$ **Answer:** $p = \dfrac{3-\sqrt{5}}{2} \approx 0.382$ --- Consecutive Wins Game [medium, Probability] --- https://quantvault.org/problem-76-consecutive-wins-game.html Q: Alice and Bob play a points-based game. Suppose $0 < p < 1$. The game consists of up to $3$ points: - On the first point, Alice wins the point with probability $p$ and Bob wins with probability $1 - p$. - On each subsequent point, the winner of the previous point wins again with probability $p$, and the loser wins with probability $1 - p$. A player wins the game as soon as they win two consecutive points. If all $3$ points are played and no player has won two in a row, the entire game is replayed from scratch under the same rules, repeating until someone wins. Find the probability that Bob wins the overall game when $p = 1/3$. A: **How to Think About It:** This is a momentum game -- whoever just won a point has probability $p$ of winning the next one, so winning streaks are self-reinforcing when $p > 1/2$ and fragile when $p 0$.) Plugging in $p = 1/3$: $$b = \frac{1 - 1/9}{2 - 1/3} = \frac{8/9}{5/3} = \frac{8}{9} \cdot \frac{3}{5} = \frac{8}{15}$$ **Answer:** The probability that Bob wins is $\dfrac{8}{15} \approx 0.533$. --- Dice With All Pairwise Sums Represented [medium, Combinatorics] --- https://quantvault.org/problem-78-dice-with-all-pairwise-sums-represented.html Q: You want to design a 6-sided die whose faces take values from $\{1, 2, 3, 4, 5, 6\}$ (with repeats allowed). The requirement is that when you roll two identical copies of this die, every integer sum from $2$ through $12$ must be achievable with positive probability. Two dice are considered the same if they have the same multiset of face values, i.e., the same number of faces showing each value, regardless of physical orientation. Each of the 6 faces is equally likely on each roll. How many distinct dice satisfy this property? A: **How to Think About It:** Constrain from the extremes inward. The sums at the edges of the range ($2, 3, 11, 12$) can each be made in only one way, so they force specific values onto the die. Once those are fixed, check which middle sums are still missing and count by complement. **Quick Estimate:** After the forced faces $\{1,2,5,6\}$, there are $2$ free faces, i.e. a multiset of size $2$ from $6$ values: $\binom{7}{2} = 21$ options. The ones that fail use only values from $\{1,2,5,6\}$: $\binom{5}{2} = 10$ options. So $21 - 10 = 11$. **Approach:** Determine which face values are forced, find the sums those values miss, then count the ways to fill the remaining two faces so the missing sums are covered. **Formal Solution:** Because the two dice are identical copies, a sum like $4 = 2+2$ only needs one face showing $2$ (that face can come up on both dice). 1. **Extreme sums force $1$ and $6$:** The only way to get $2$ is $1+1$, so $1$ must appear. The only way to get $12$ is $6+6$, so $6$ must appear. 2. **Next extremes force $2$ and $5$:** The only way to get $3$ is $1+2$, so $2$ must appear. The only way to get $11$ is $5+6$, so $5$ must appear. 3. **Check which sums are already covered with $\{1,2,5,6\}$:** $$2 = 1+1,\; 3 = 1+2,\; 4 = 2+2,\; 6 = 1+5,\; 7 = 2+5$$ $$8 = 2+6,\; 10 = 5+5,\; 11 = 5+6,\; 12 = 6+6$$ The missing sums are $5$ and $9$. 4. **Covering 5 and 9:** Using only $\{1,2,5,6\}$ neither is reachable, and $5 = 1+4 = 2+3$, $9 = 3+6 = 4+5$. A single $3$ covers both ($5 = 2+3$, $9 = 3+6$), and a single $4$ covers both ($5 = 1+4$, $9 = 4+5$). So the die works if and only if at least one of $3, 4$ appears. 5. **Counting:** The $4$ forced values use $4$ faces, leaving $2$ free faces, which form an unordered pair (multiset of size $2$) from $\{1,\dots,6\}$. Count pairs containing a $3$ or a $4$ by inclusion-exclusion: - Pairs containing a $3$: $\{3,1\},\{3,2\},\{3,3\},\{3,4\},\{3,5\},\{3,6\}$, that is $6$. - Pairs containing a $4$: likewise $6$. - Pairs containing both: only $\{3,4\}$, that is $1$. $$6 + 6 - 1 = 11$$ Check by complement: $\binom{7}{2} - \binom{5}{2} = 21 - 10 = 11$. **Answer:** There are $\boxed{11}$ distinct dice satisfying the property. --- Even vs Odd Heads and Coin Fairness [medium, Probability] --- https://quantvault.org/problem-79-even-vs-odd-heads-and-coin-fairness.html Q: This is a three-part problem about the parity of heads in coin flips. **Part 1.** You flip a fair coin 10 times. What is the probability of getting an even number of heads? What about an odd number of heads? **Part 2.** You flip a single coin $n \geq 1$ times (where the coin has some unknown probability $p$ of landing heads). You observe that the probability of getting an even number of heads equals the probability of getting an odd number of heads. Does this imply the coin is fair (i.e., $p = 1/2$)? **Part 3.** Now you have 10 different coins with unknown head probabilities $p_1, p_2, \ldots, p_{10}$. You flip each coin once. You observe that the probability of the total number of heads being even equals the probability of it being odd. Does this imply that at least one of the coins is fair? A: **How to Think About It:** The key tool here is a classic trick for separating even and odd terms in a sum: evaluate the generating function at $+1$ and $-1$ and take the difference. For a single coin flip with bias $p$, the probability generating function (PGF) is $p z + (1-p)$. When you plug in $z = -1$ you get $(1 - 2p)$, which measures the "imbalance" between even and odd outcomes. This is the thread that ties all three parts together. **Quick Estimate:** For a fair coin, $(1 - 2p) = 0$, so even and odd are always equally likely regardless of the number of flips. That handles Part 1 immediately. For an unfair coin, $(1 - 2p)^n = 0$ only if $p = 1/2$, and for multiple independent coins, $\prod(1 - 2p_i) = 0$ only if at least one factor is zero. So the answer to all three parts should confirm fairness -- let's verify. **Approach:** Use the generating function evaluation trick $P(\text{even}) - P(\text{odd}) = f(-1)$ where $f(z)$ is the PGF of the total number of heads. **Formal Solution:** **Part 1.** Let $X$ be the number of heads in 10 fair coin flips. The PGF is: $$f(z) = \left(\frac{1 + z}{2}\right)^{10}$$ The difference between even and odd probabilities is: $$P(\text{even}) - P(\text{odd}) = f(-1) = \left(\frac{1 + (-1)}{2}\right)^{10} = 0$$ Since $P(\text{even}) + P(\text{odd}) = 1$ and $P(\text{even}) - P(\text{odd}) = 0$, we get: $$P(\text{even}) = P(\text{odd}) = \frac{1}{2}$$ Alternatively, you can see this directly from the binomial theorem: $\sum_{k=0}^{10} (-1)^k \binom{10}{k} (1/2)^{10} = (1 - 1)^{10} / 2^{10} = 0$. **Part 2.** Now the coin has bias $p$ and is flipped $n$ times. The PGF is: $$f(z) = (pz + (1-p))^n$$ The even-odd difference is: $$P(\text{even}) - P(\text{odd}) = f(-1) = (1 - 2p)^n$$ Setting this to zero: $(1 - 2p)^n = 0$. Since $n \geq 1$, this forces $1 - 2p = 0$, i.e., $p = 1/2$. **Yes, the coin must be fair.** There is no unfair coin that produces equal even/odd probabilities over any positive number of flips. **Part 3.** Now we have 10 independent coins with biases $p_1, \ldots, p_{10}$, each flipped once. The total number of heads $X = X_1 + \cdots + X_{10}$ where each $X_i \sim \text{Bernoulli}(p_i)$, and by independence the PGF is: $$f(z) = \prod_{i=1}^{10} (p_i z + (1-p_i))$$ The even-odd difference is: $$P(\text{even}) - P(\text{odd}) = f(-1) = \prod_{i=1}^{10} (1 - 2p_i)$$ Setting this to zero: $\prod_{i=1}^{10} (1 - 2p_i) = 0$. A product of real numbers is zero if and only if at least one factor is zero, so $1 - 2p_j = 0$ for some $j$, meaning $p_j = 1/2$. **Yes, at least one of the 10 coins must be fair.** **Answer:** - Part 1: $P(\text{even}) = P(\text{odd}) = 1/2$ for a fair coin. - Part 2: Yes, equal even/odd probabilities imply $p = 1/2$. The coin must be fair. - Part 3: Yes, at least one coin must have $p_i = 1/2$. The generating function factorizes as a product, and the product is zero only when a factor is zero. --- Expected Arc Length Between Random Points on a Sphere [medium, Expectation] --- https://quantvault.org/problem-80-expected-arc-length-between-random-points-on-a-sphere.html Q: Two points are chosen uniformly at random on the surface of the unit sphere in $\mathbb{R}^3$. Let $\theta$ be the great-circle (arc) distance between them, i.e., the central angle separating the two points, with $0 \le \theta \le \pi$. What is $E[\theta]$? A: **How to Think About It:** The interview-winning move here is a symmetry trick that needs zero integration. Fix the first point $\mathbf{u}$ and let $\mathbf{v}$ be the second. If $\mathbf{v}$ is uniform on the sphere, so is its antipode $-\mathbf{v}$ (the antipodal map is an isometry of the sphere). But the angle from $\mathbf{u}$ to $-\mathbf{v}$ is exactly $\pi - \theta$. So $\theta$ and $\pi - \theta$ have the same distribution, which forces $E[\theta] = \pi - E[\theta]$, i.e. $E[\theta] = \pi/2$. Lead with this in an interview -- then be ready to back it up with the honest integral below, because the standard follow-up is "prove the distribution facts you just used." **Quick Estimate:** $\theta$ lives in $[0, \pi]$. A naive "uniform angle" guess gives mean $\pi/2 \approx 1.57$. The true density is not uniform -- it is $\tfrac{1}{2}\sin\theta$, peaked at the equator ($\theta = \pi/2$), because a band of the sphere near the equator has more surface area than an equal-width band near a pole. But $\sin\theta$ is symmetric about $\pi/2$ on $[0, \pi]$: the extra weight sits exactly at the center of the interval, so it does not move the mean. Estimate: $3.14 / 2 = 1.57$, and we expect it to be exact. **Approach:** Route 1 (symmetry): the antipodal map gives $E[\theta] = \pi/2$ in two lines. Route 2 (direct): fix the first point at the north pole, prove the key fact $\cos\theta \sim U[-1,1]$ (Archimedes' hat-box theorem), get the density of $\theta$, and integrate. We show both. **Formal Solution:** *Route 1 -- antipodal symmetry (no integration).* Condition on the first point $\mathbf{u}$. The second point $\mathbf{v}$ is uniform on the sphere, and $-\mathbf{v}$ is also uniform (the map $\mathbf{v} \mapsto -\mathbf{v}$ preserves surface area). Since $\mathbf{u} \cdot (-\mathbf{v}) = -\cos\theta$, the central angle from $\mathbf{u}$ to $-\mathbf{v}$ is $\arccos(-\cos\theta) = \pi - \theta$. Hence $\theta$ and $\pi - \theta$ are equal in distribution, so $$E[\theta] = E[\pi - \theta] = \pi - E[\theta] \quad\Longrightarrow\quad \boxed{E[\theta] = \frac{\pi}{2}}$$ *Route 2 -- direct integration (and why $\cos\theta \sim U[-1,1]$).* Step 1: Reduce to one random point. By rotational invariance, condition on the first point and rotate it to the north pole $(0, 0, 1)$. The second point is still uniform, $\theta$ is now its polar angle, and $\cos\theta = Z$, its $z$-coordinate. Step 2: $Z \sim U[-1,1]$ (Archimedes' hat-box theorem). The band of the sphere with polar angle in $[\theta, \theta + d\theta]$ is a ring of radius $\sin\theta$ and width $d\theta$, so its area is $$dA = 2\pi \sin\theta \, d\theta.$$ Meanwhile $z = \cos\theta$ gives $|dz| = \sin\theta \, d\theta$, so $dA = 2\pi \, |dz|$: a slab of height $dz$ carries the same area no matter where it sits on the sphere. Dividing by the total area $4\pi$, the density of $Z$ is the constant $\tfrac{1}{2}$ on $[-1, 1]$ -- uniform. Step 3: Density of $\theta$. From $dA / 4\pi$ directly, $$f_\theta(\theta) = \tfrac{1}{2} \sin\theta, \qquad 0 \le \theta \le \pi.$$ Step 4: Integrate. Using integration by parts with $v = \theta$, $dw = \sin\theta \, d\theta$ (so $w = -\cos\theta$): $$\int_0^{\pi} \theta \sin\theta \, d\theta = \bigl[-\theta \cos\theta\bigr]_0^{\pi} + \int_0^{\pi} \cos\theta \, d\theta = (\pi - 0) + \bigl[\sin\theta\bigr]_0^{\pi} = \pi + 0 = \pi.$$ Therefore $$E[\theta] = \tfrac{1}{2} \int_0^{\pi} \theta \sin\theta \, d\theta = \boxed{\frac{\pi}{2} \approx 1.5708}$$ Sanity check (simulation): with $2 \times 10^7$ random point pairs, the sample mean of $\theta$ is $1.5709$, and $\cos\theta$ has sample mean $\approx 0$ and variance $\approx 1/3$ -- exactly the moments of $U[-1,1]$. **Answer:** $E[\theta] = \dfrac{\pi}{2} \approx 1.5708$. On the unit sphere the arc length equals the central angle, so the expected arc length is $\pi/2$. --- Expected Number of Local Maxima in a Circle [medium, Expectation] --- https://quantvault.org/problem-89-expected-number-of-local-maxima-in-a-circle.html Q: $n$ people with distinct heights are arranged in a circle in a uniformly random order. A person can "see out" if they are taller than both of their immediate neighbors. What is the expected number of people who can see out? A: **How to Think About It:** Whenever you see "expected number of [things satisfying a condition]," your first instinct should be linearity of expectation. Define an indicator variable for each person, compute the probability that one person can see out, and multiply by $n$. The beauty of linearity is that you do not need to worry about dependencies between neighboring indicators -- the expectation still adds up. **Quick Estimate:** For each person, "seeing out" means being the tallest among themselves and their two neighbors -- a group of 3. Since all heights are distinct and every permutation of those 3 people is equally likely, the probability that a specific person is the tallest of the three is $1/3$. So the expected count is roughly $n/3$. Let's verify this is exact. **Approach:** Use indicator random variables and symmetry. **Formal Solution:** Let $X_i = 1$ if person $i$ can see out (is taller than both neighbors), and $X_i = 0$ otherwise. We want: $$E\left[\sum_{i=1}^{n} X_i\right] = \sum_{i=1}^{n} E[X_i] = \sum_{i=1}^{n} P(X_i = 1)$$ Fix person $i$ and consider the triple $(i, i{-}1, i{+}1)$ where indices wrap around the circle. Since all $n$ people have distinct heights and are arranged in a uniformly random permutation, the relative ranking of any 3 specific people is uniform over all $3! = 6$ orderings. Person $i$ is the tallest of the three in exactly $1$ out of $3$ of these orderings (the tallest of 3 is equally likely to be any of the three). Therefore: $$P(X_i = 1) = \frac{1}{3}$$ By symmetry this holds for every $i$, so: $$E\left[\sum_{i=1}^{n} X_i\right] = n \cdot \frac{1}{3} = \frac{n}{3}$$ **Important subtlety:** One might worry that the events $X_i$ and $X_{i+1}$ are not independent (they share a neighbor). That is true -- they are negatively correlated (if person $i$ is the tallest of their triple, the shared neighbor is not, which makes it harder for person $i+1$). But linearity of expectation does not require independence. The expected value of a sum equals the sum of expected values, period. **Answer:** $$E[\text{number who can see out}] = \frac{n}{3}$$ This holds for any $n \geq 3$ and depends only on the assumption of distinct heights (no ties). --- Expected Number of Loops from Tying Rope Ends [medium, Expectation] --- https://quantvault.org/problem-90-expected-number-of-loops-from-tying-rope-ends.html Q: You have $N$ ropes, each with two free ends, giving $2N$ free ends total. At each step you pick two free ends uniformly at random and tie them together. This reduces the number of free ends by 2, so after $N$ steps every end is tied and the process stops. The result is some collection of closed loops. What is the expected number of loops formed? Express your answer as a closed-form sum and give its asymptotic behavior for large $N$. A: **How to Think About It:** Each tying step either closes a loop (the two chosen ends belong to the same connected piece) or merges two pieces into one. The trick is that at every step, once you pick the first end, there is exactly one "dangerous" partner among the remaining free ends -- the other end of the *same* piece -- that would close a loop. So the probability of forming a loop at that step is just $1/(\text{remaining ends} - 1)$, independent of how the pieces are arranged. Linearity of expectation then turns the whole problem into a sum of these per-step probabilities. Before grinding through algebra, let's get a number. **Quick Estimate:** Take $N = 10$ (so $20$ free ends). The expected number of loops is $$1 + \frac{1}{3} + \frac{1}{5} + \frac{1}{7} + \frac{1}{9} + \frac{1}{11} + \frac{1}{13} + \frac{1}{15} + \frac{1}{17} + \frac{1}{19}.$$ Computing: $1 + 0.333 + 0.200 + 0.143 + 0.111 + 0.091 + 0.077 + 0.067 + 0.059 + 0.053 \approx 2.13$. So from $10$ ropes you expect only about $2$ loops. Because this odd-reciprocal sum grows like $\tfrac{1}{2}\ln N$, the count is logarithmically small: even $N = 1000$ ropes gives only $\approx \tfrac12\ln 1000 + \ln 2 + \tfrac{\gamma}{2} \approx 4.4$ loops. **Approach:** Define an indicator for each step that forms a loop, sum their probabilities by linearity of expectation, recognize the partial sum of odd reciprocals, and read off the large-$N$ asymptotics by writing it in terms of harmonic numbers. **Formal Solution:** Label the steps $k = 1, 2, \ldots, N$. Just before step $k$, the number of free ends remaining is $2(N - k + 1)$. Pick one end. Among the other $2(N - k + 1) - 1$ free ends, exactly one is the opposite end of the *same* connected component, and tying to it closes a loop. (Every connected piece is a simple path, so the chosen end has exactly one same-piece partner -- regardless of how many pieces currently exist.) Hence the probability of closing a loop at step $k$ is $$p_k = \frac{1}{2(N - k + 1) - 1} = \frac{1}{2N - 2k + 1}.$$ Let $L_k$ be the indicator that step $k$ forms a loop and $L = \sum_k L_k$ the total. By linearity of expectation, $$E[L] = \sum_{k=1}^{N} E[L_k] = \sum_{k=1}^{N} \frac{1}{2N - 2k + 1}.$$ Substituting $j = N - k + 1$ (so $j$ runs from $N$ down to $1$), $$E[L] = \sum_{j=1}^{N} \frac{1}{2j - 1} = 1 + \frac{1}{3} + \frac{1}{5} + \cdots + \frac{1}{2N - 1},$$ the partial sum of the reciprocals of the odd numbers. *Asymptotics.* Write the sum in terms of harmonic numbers $H_n = \sum_{m=1}^{n} 1/m$ by removing the even terms from $H_{2N}$: $$\sum_{j=1}^{N} \frac{1}{2j-1} = \sum_{m=1}^{2N} \frac{1}{m} - \sum_{m=1}^{N} \frac{1}{2m} = H_{2N} - \frac{1}{2} H_N.$$ Using $H_n = \ln n + \gamma + o(1)$, $$H_{2N} - \tfrac{1}{2} H_N = \bigl(\ln(2N) + \gamma\bigr) - \tfrac{1}{2}\bigl(\ln N + \gamma\bigr) + o(1) = \tfrac{1}{2}\ln N + \ln 2 + \tfrac{\gamma}{2} + o(1),$$ since $\ln(2N) = \ln N + \ln 2$ and $\gamma - \tfrac{\gamma}{2} = \tfrac{\gamma}{2}$. Here $\gamma \approx 0.5772$ is the Euler-Mascheroni constant, so the constant term is $\ln 2 + \gamma/2 \approx 0.693 + 0.289 = 0.982$. The leading order is $\tfrac12 \ln N$. (Beware the tempting but wrong shortcut $\tfrac12\ln(2N) + \tfrac{\gamma}{2}$: that equals $\tfrac12\ln N + \tfrac12\ln 2 + \tfrac{\gamma}{2}$, which has only *half* of $\ln 2$ and is off by $\tfrac12\ln 2 \approx 0.347$.) **Answer:** The expected number of loops is $$E[L] = \sum_{j=1}^{N} \frac{1}{2j-1} = 1 + \frac{1}{3} + \frac{1}{5} + \cdots + \frac{1}{2N-1} = H_{2N} - \frac{1}{2}H_N = \frac{1}{2}\ln N + \ln 2 + \frac{\gamma}{2} + o(1),$$ so to leading order $E[L] \sim \tfrac{1}{2}\ln N$ -- the number of loops grows logarithmically in $N$. (For $N = 10$ the exact sum is $\approx 2.13$.) --- Expected Number of Uniforms to Exceed One [easy, Expectation] --- https://quantvault.org/problem-92-expected-number-of-uniforms-to-exceed-one.html Q: Let $U_1, U_2, U_3, \ldots$ be i.i.d. $\text{Uniform}(0,1)$ random variables. Define the stopping time: $$N = \min\{n : U_1 + U_2 + \cdots + U_n > 1\}$$ What is $E[N]$? A: **How to Think About It:** You are adding up random numbers between 0 and 1 until the sum exceeds 1. How many draws does that take on average? Each draw adds about $1/2$ on average, so you might guess $N \approx 2$ or $3$. But the distribution is not symmetric around 2 -- sometimes you get lucky with a single large draw close to 1, and sometimes you need 4 or 5 small draws. The answer turns out to be $e \approx 2.718$, one of the most elegant results in probability. **Quick Estimate:** The expected sum after 2 draws is 1, so roughly half the time 2 draws suffice and half the time you need 3 or more. That puts $E[N]$ somewhere between 2 and 3. Concretely, $P(N = 1) = 0$ because $U_1 \in (0,1)$ can never exceed 1 on its own, and $P(N = 2) = P(U_1 + U_2 > 1) = 1/2$ (the area of the triangle above the diagonal of the unit square). Since $P(U_1 + U_2 \leq 1) = 1/2$, we already have $E[N] \geq 2 + (1/2)(1) = 2.5$, and the remaining tail pushes it into the $2.7$-$2.8$ range. **Approach:** Use the volume of the standard simplex to compute $P(N > n)$, then sum over $n$. **Formal Solution:** The key identity is: $$P(U_1 + U_2 + \cdots + U_n \leq 1) = \frac{1}{n!}$$ This follows because the event $\{U_1 + \cdots + U_n \leq 1, \; U_i \geq 0\}$ is the standard $n$-simplex embedded in the unit $n$-cube $[0,1]^n$. The volume of the standard $n$-simplex is $1/n!$. Now, $N > n$ means $U_1 + \cdots + U_n \leq 1$, so: $$P(N > n) = \frac{1}{n!}$$ Using the tail-sum formula for expectation: $$E[N] = \sum_{n=0}^{\infty} P(N > n) = \sum_{n=0}^{\infty} \frac{1}{n!}$$ Verify the first few terms: - $P(N > 0) = 1 = 1/0!$ $\checkmark$ (you always need at least one draw) - $P(N > 1) = P(U_1 \leq 1) = 1 = 1/1!$ $\checkmark$ - $P(N > 2) = P(U_1 + U_2 \leq 1) = 1/2 = 1/2!$ $\checkmark$ - $P(N > 3) = P(U_1 + U_2 + U_3 \leq 1) = 1/6 = 1/3!$ $\checkmark$ Therefore: $$E[N] = \sum_{n=0}^{\infty} \frac{1}{n!} = e$$ **Answer:** $E[N] = e \approx 2.71828$. --- Expected Number of Uniforms to Exceed a Threshold [medium, Expectation] --- https://quantvault.org/problem-93-expected-number-of-uniforms-to-exceed-a-threshold.html Q: Draw i.i.d. uniform random variables $X_1, X_2, \ldots$ from $U[0,1]$ and let $S_n = X_1 + X_2 + \cdots + X_n$. Define the stopping time $$T = \min\{n : S_n > 1\}.$$ 1. Show that $E[T] = e$. 2. Generalize: for a threshold $c > 0$, let $T_c = \min\{n : S_n > c\}$. Find $E[T_c]$. A: **How to Think About It:** You are drawing uniform random numbers and adding them up until you pass 1. How many draws does that take on average? Each draw contributes about $1/2$ on average, so you'd guess roughly 2 draws -- but that ignores the fact that you often overshoot or undershoot. The real answer is $e \approx 2.718$, which is surprisingly clean. The key insight is that the probability of needing more than $n$ draws is exactly $1/n!$, because the event $\{T > n\}$ is the same as the event $\{S_n \le 1\}$, and the volume of the region where $n$ uniform random variables sum to at most 1 is the volume of the standard $n$-simplex, which is $1/n!$. **Quick Estimate:** Each $X_i$ has mean $1/2$, so after 2 draws the expected sum is 1. But you need to *exceed* 1, and sometimes you get unlucky with small draws. A rough guess: somewhere between 2.5 and 3. The exact answer $e \approx 2.718$ fits right in that range. You can also simulate: draw a few thousand trials on a computer and you will see the average converge to $e$. **Approach:** Use the tail-sum formula for expectation $E[T] = \sum_{n=0}^{\infty} P(T > n)$ and compute each tail probability as a simplex volume. **Formal Solution:** Define $T = \min\{n : S_n > 1\}$. Note that $\{T > n\}$ is the event $\{S_n \le 1\}$, i.e., the first $n$ draws all contribute to a partial sum that has not yet exceeded 1. The probability $P(S_n \le 1)$ is the volume of the region $$\{(x_1, \ldots, x_n) \in [0,1]^n : x_1 + \cdots + x_n \le 1\}.$$ Since each $x_i \in [0,1]$ and the constraint $\sum x_i \le 1$ already forces each $x_i \le 1$, this region is exactly the standard $n$-simplex $\Delta_n$. Its volume is $$\text{Vol}(\Delta_n) = \frac{1}{n!}.$$ So $P(T > n) = 1/n!$. Applying the tail-sum formula for expectation: $$E[T] = \sum_{n=0}^{\infty} P(T > n) = \sum_{n=0}^{\infty} \frac{1}{n!} = e.$$ **Generalization to threshold $c > 0$:** For $T_c = \min\{n : S_n > c\}$, we need $$P(T_c > n) = P(S_n \le c) = \text{Vol}\{(x_1, \ldots, x_n) \in [0,1]^n : x_1 + \cdots + x_n \le c\}.$$ For $0 0$ (including $c > 1$), the region is no longer a simplex because the $[0,1]^n$ box constraint binds. By inclusion-exclusion, the volume is the Irwin-Hall CDF: $$P(S_n \le c) = \frac{1}{n!} \sum_{j=0}^{\lfloor c \rfloor} (-1)^j \binom{n}{j}(c - j)^n.$$ Then $$E[T_c] = \sum_{n=0}^{\infty} P(S_n \le c).$$ For integer thresholds, closed forms exist. For example: - $c = 1$: $E[T_1] = e \approx 2.718$ - $c = 2$: $E[T_2] = \frac{e^2 + e}{2} \approx 5.053$ In general, for integer $c = k$, $E[T_k]$ can be expressed in terms of powers of $e$ with rational coefficients. **Answer:** $E[T] = e$. For threshold $0 < c \le 1$, $E[T_c] = e^c$. For general $c$, $E[T_c] = \sum_{n=0}^{\infty} P(S_n \le c)$ where each term uses the Irwin-Hall inclusion-exclusion formula. --- Expected Value of a Coin Flip Game [easy, Expectation] --- https://quantvault.org/problem-96-expected-value-of-a-coin-flip-game.html Q: A game pays $\$32$ if you flip 5 heads in a row using a fair coin. How much would you pay to play this game? A: **How to Think About It:** This is a pure expected value calculation. You have a binary outcome -- either you flip 5 heads in a row and collect $\$32$, or you don't and get nothing. The fair price is just the probability of winning times the payoff. Before doing any math, notice that $32 = 2^5$, which is a strong hint that the answer will be clean. **Quick Estimate:** The probability of 5 heads in a row is $(1/2)^5 = 1/32$. So the expected payoff is $32 \times 1/32 = 1$. Done -- the fair price is $\$1$. **Approach:** Straightforward expected value calculation. **Formal Solution:** Each flip is independent with $P(H) = 1/2$. The probability of 5 consecutive heads is: $$P(5H) = \left(\frac{1}{2}\right)^5 = \frac{1}{32}$$ The expected payoff is: $$E[\text{payoff}] = \$32 \times \frac{1}{32} + \$0 \times \frac{31}{32} = \$1$$ **Answer:** A risk-neutral player should pay at most $\$1$ to play this game. A risk-averse player would pay strictly less than $\$1$. The fact that $32 = 2^5$ makes the payoff exactly offset the odds, giving a round-number answer. --- Gambler's Ruin with Biased Dice [medium, Probability] --- https://quantvault.org/problem-101-gambler-s-ruin-with-biased-dice.html Q: Two players each start with 12 tokens. They repeatedly roll three dice. If the sum is 14, player $A$ gives a token to player $B$; if the sum is 11, player $B$ gives a token to player $A$. All other sums are ignored, and they keep rolling. The game ends when one player has all 24 tokens. What is the probability that player $A$ wins? A: **How to Think About It:** This is a classic Gambler's Ruin problem in disguise. Strip away the dice and tokens -- the core question is: in a biased random walk on $\{0, 1, \ldots, 24\}$ with absorbing barriers at 0 and 24, starting at position 12, what is the probability of reaching 24 before 0? The only thing you need from the dice is the probability $p$ that $A$ gains a token on each relevant roll. **Quick Estimate:** Rolling three dice gives $6^3 = 216$ equally likely outcomes; sum $11$ occurs $27$ ways and sum $14$ occurs $15$ ways (both derived below). On each relevant roll, $A$ wins the token with probability $p = 27/(27+15) = 27/42 = 9/14 \approx 0.643$. Since $A$ has a substantial edge and needs to win a net 12 tokens from a symmetric start, $A$ should win with very high probability. Think of it as a biased coin that lands heads 64% of the time -- reaching +12 before $-12$ is nearly certain. **Approach:** Apply the standard Gambler's Ruin formula for a biased random walk. **Formal Solution:** Let $x$ denote $A$'s token count minus 12, so $x \in \{-12, \ldots, 12\}$ with $x = 0$ at the start. $A$ wins when $x = 12$, loses when $x = -12$. At each step, $x$ increases by 1 with probability $p$ or decreases by 1 with probability $q = 1 - p$. *Counting the dice outcomes.* The number of ways three dice total $k$ is the coefficient of $z^k$ in $(z + z^2 + \cdots + z^6)^3$. Equivalently, substituting $y_i = x_i - 1$ turns "solve $x_1 + x_2 + x_3 = k$ with $1 \le x_i \le 6$" into "solve $y_1 + y_2 + y_3 = k - 3$ with $0 \le y_i \le 5$", which inclusion-exclusion on the upper bounds counts as $$N(k) = \sum_{j \ge 0} (-1)^j \binom{3}{j}\binom{k - 3 - 6j + 2}{2}.$$ - $k = 11$: $\binom{10}{2} - 3\binom{4}{2} = 45 - 18 = 27$. - $k = 14$: $\binom{13}{2} - 3\binom{7}{2} = 78 - 63 = 15$. Cross-check by symmetry: replacing every die $x_i$ by $7 - x_i$ is a bijection sending total $k$ to total $21 - k$, so the distribution is symmetric about $10.5$ and $N(14) = N(7) = \binom{6}{2} = 15$ (no correction term, since $7 - 3 = 4 < 6$). That matches, and the $27$ for sum $11$ is likewise $N(10)$, the joint peak of the distribution. So on each relevant roll, $p = 27/42 = 9/14$ and $q = 5/14$. The Gambler's Ruin probability starting at position $x$ (measuring $A$'s surplus tokens, so starting at 0) with absorbing barriers at $-a$ and $+a$ is: $$R_x = \frac{1 - (q/p)^{x+a}}{1 - (q/p)^{2a}}$$ With $a = 12$, $q/p = 5/9$, and $x = 0$: $$R_0 = \frac{1 - (5/9)^{12}}{1 - (5/9)^{24}}$$ Since $(5/9)^{12}$ is very small, we can simplify. Note that $1 - (5/9)^{24} = (1 - (5/9)^{12})(1 + (5/9)^{12})$, so: $$R_0 = \frac{1}{1 + (5/9)^{12}}$$ Now compute $(5/9)^{12}$. We have $\ln(5/9) = \ln 5 - \ln 9 \approx 1.609 - 2.197 = -0.588$, so $12 \times (-0.588) = -7.06$, giving $(5/9)^{12} \approx e^{-7.06} \approx 0.00086$. **Answer:** $$P(A \text{ wins}) = \frac{1}{1 + (5/9)^{12}} \approx 0.99914$$ Player $A$ wins with probability approximately $99.91\%$. --- Guaranteed Birth Month Match: Pigeonhole Principle [easy, Combinatorics] --- https://quantvault.org/problem-103-guaranteed-birth-month-match-pigeonhole-principle.html Q: How many people must be in a room to guarantee that at least 5 of them share the same birth month? A: **How to Think About It:** This is a classic worst-case reasoning problem. The question is not 'what is the probability that 5 people share a birth month?' -- it asks for certainty. The way to guarantee something in the worst case is to ask: how could an adversary arrange the birth months to avoid the outcome for as long as possible? They would spread people as evenly as possible across all 12 months. **Quick Estimate:** If we could put people evenly across 12 months, we can have at most $4 \times 12 = 48$ people with no month having 5 or more. The 49th person must go into some month, pushing it to 5. So the answer is 49. **Approach:** Apply the pigeonhole principle: with $n$ people (pigeons) and 12 months (holes), the most-crowded month has at least $\lceil n/12 \rceil$ people. **Formal Solution:** We want the minimum $n$ such that, regardless of birth month distribution: $$\max_{\text{month } m} (\text{count in month } m) \geq 5$$ The adversary's best strategy is to distribute as evenly as possible: - With $n = 48 = 4 \times 12$ people: place exactly 4 in each month. Every month has 4 -- no month reaches 5. Not guaranteed yet. - With $n = 49$: by pigeonhole, at least one month must have $\lceil 49/12 \rceil = \lceil 4.08... \rceil = 5$ people. Guaranteed. $$\boxed{49 \text{ people}}$$ **Answer:** 49. The general formula is $4 \times 12 + 1 = 49$ for "at least 5 in the same month." For "at least $k$ in the same month among $h$ categories," it is $(k-1) \times h + 1$. --- HTH vs HH: Which Pattern Appears First? [medium, Stochastic Process] --- https://quantvault.org/problem-104-hth-vs-hh-which-pattern-appears-first.html Q: A fair coin is tossed repeatedly. The game ends the moment either the pattern $HH$ or the pattern $HTH$ appears as consecutive flips -- whichever comes first. What is the probability that $HTH$ appears before $HH$? Model the process as a Markov chain over states that track the relevant suffix of the flip sequence. Set up and solve the system of linear equations exactly. A: **How to Think About It:** Competing-pattern races are solved by **first-step / suffix-state analysis**: the only history that matters is the longest current suffix that is a prefix of a target pattern. The structural asymmetry is the whole story — $HH$ finishes in $2$ symbols, $HTH$ in $3$, and crucially they *share the leading $H$*. Every time you are one flip into $HTH$ (state $H$), the very next $H$ completes $HH$ and kills you. So $HH$ is always "closer," and the naive guess that $HTH$ wins less than half the time is correct — the trap would be to expect symmetry. **Quick Estimate:** Reason from the first useful flip. You only care once an $H$ appears (state $H$). From there, one flip decides a lot: with prob $\tfrac12$ the next flip is $H$ and $HH$ wins immediately (you lose the race for $HTH$). So *just from reaching state $H$*, you already forfeit half the time on the spot — an upper bound of $\tfrac12$ on winning, and you must still complete the extra third symbol afterward, dragging it below $\tfrac12$. A one-level refinement: after the surviving $HT$ (prob $\tfrac12$), the next flip wins with prob $\tfrac12$, else you reset. Rough win $\approx \tfrac12\cdot\tfrac12 \approx \tfrac14$ per attempt from state $H$ — landing right on the exact $\tfrac13$ once resets are folded in. Estimate: **around one-third**, comfortably under a half. **Approach:** Suffix states, then a three-equation balance solved by back-substitution. **Formal Solution:** States by relevant suffix; $p_s$ = probability $HTH$ wins from state $s$: - $S_0$: no useful progress (start, or a trailing $T$ off a reset) - $S_H$: suffix $H$ (one step into both patterns) - $S_{HT}$: suffix $HT$ (two steps into $HTH$, $HH$ derailed) - absorbing $WIN$ ($HTH$), $LOSE$ ($HH$) Transitions (each flip prob $\tfrac12$): $$p_0 = \tfrac12 p_H + \tfrac12 p_0 \;\Rightarrow\; p_0 = p_H,$$ $$p_H = \tfrac12\cdot 0 + \tfrac12 p_{HT} = \tfrac12 p_{HT}\quad(H\to HH\ \text{loses};\ T\to HT),$$ $$p_{HT} = \tfrac12\cdot 1 + \tfrac12 p_0\quad(H\to HTH\ \text{wins};\ T\to \text{reset to } S_0).$$ Back-substitute. From the third, $p_{HT} = \tfrac12 + \tfrac12 p_0$. Feed into the second: $p_H = \tfrac12\big(\tfrac12 + \tfrac12 p_0\big) = \tfrac14 + \tfrac14 p_0$. Using $p_0 = p_H$: $$p_0 = \tfrac14 + \tfrac14 p_0 \;\Rightarrow\; \tfrac34 p_0 = \tfrac14 \;\Rightarrow\; p_0 = \tfrac13.$$ Then $p_{HT} = \tfrac12 + \tfrac16 = \tfrac23$ and $p_H = p_0 = \tfrac13$. The game starts in $S_0$, so the win probability is $p_0 = \boxed{\tfrac13}$. **Answer:** $P(HTH\ \text{before}\ HH) = 1/3$. --- Jointly Normal and Uncorrelated Implies Independent [medium, Probability] --- https://quantvault.org/problem-105-jointly-normal-and-uncorrelated-implies-independent.html Q: Part 1: Prove that if $(X, Y)$ are jointly normal and uncorrelated, then $X$ and $Y$ are independent. Part 2: Give an example of two random variables that are each marginally normal and uncorrelated, but are not independent. Explain why this does not contradict Part 1. A: **How to Think About It:** The statement "uncorrelated implies independent" is false in general -- correlation only captures linear dependence. But for jointly normal random variables, the entire distribution is parameterized by the mean vector and covariance matrix. Setting the off-diagonal covariance to zero forces the joint pdf to factor, which is independence. The catch is the word "jointly" -- it is doing all the heavy lifting. If the pair is not jointly normal (even if each marginal is normal), the covariance matrix no longer determines the distribution, and the implication breaks. **Formal Solution:** **Part 1: Proof that jointly normal + uncorrelated $\Rightarrow$ independent.** Let $(X, Y) \sim N(\boldsymbol{\mu}, \Sigma)$ where $\boldsymbol{\mu} = (\mu_X, \mu_Y)^T$ and: $$\Sigma = \begin{pmatrix} \sigma_X^2 & \sigma_{XY} \\\ \sigma_{XY} & \sigma_Y^2 \end{pmatrix}$$ Uncorrelated means $\sigma_{XY} = \text{Cov}(X, Y) = 0$, so $\Sigma$ is diagonal: $$\Sigma = \begin{pmatrix} \sigma_X^2 & 0 \\\ 0 & \sigma_Y^2 \end{pmatrix}$$ The joint density of $(X, Y)$ is: $$f_{X,Y}(x, y) = \frac{1}{2\pi |\Sigma|^{1/2}} \exp\!\left(-\frac{1}{2} (\mathbf{z} - \boldsymbol{\mu})^T \Sigma^{-1} (\mathbf{z} - \boldsymbol{\mu})\right)$$ With $\Sigma$ diagonal, $|\Sigma| = \sigma_X^2 \sigma_Y^2$ and $\Sigma^{-1} = \text{diag}(1/\sigma_X^2,\, 1/\sigma_Y^2)$. The exponent becomes: $$-\frac{(x - \mu_X)^2}{2\sigma_X^2} - \frac{(y - \mu_Y)^2}{2\sigma_Y^2}$$ So the joint density factors: $$f_{X,Y}(x, y) = \underbrace{\frac{1}{\sqrt{2\pi}\sigma_X} \exp\!\left(-\frac{(x - \mu_X)^2}{2\sigma_X^2}\right)}_{f_X(x)} \cdot \underbrace{\frac{1}{\sqrt{2\pi}\sigma_Y} \exp\!\left(-\frac{(y - \mu_Y)^2}{2\sigma_Y^2}\right)}_{f_Y(y)}$$ Since $f_{X,Y}(x, y) = f_X(x) \cdot f_Y(y)$ for all $(x, y)$, the variables $X$ and $Y$ are independent. $\square$ **Alternative proof via MGFs:** The joint MGF of $(X, Y) \sim N(\boldsymbol{\mu}, \Sigma)$ is: $$M(t_1, t_2) = \exp\!\left(\boldsymbol{\mu}^T \mathbf{t} + \frac{1}{2} \mathbf{t}^T \Sigma \mathbf{t}\right)$$ With $\sigma_{XY} = 0$, the cross term $2t_1 t_2 \sigma_{XY}$ vanishes, and the MGF factors: $M(t_1, t_2) = M_X(t_1) \cdot M_Y(t_2)$. Factorization of the MGF is equivalent to independence. **Part 2: Counterexample -- normal marginals, uncorrelated, but dependent.** Let $Z \sim N(0, 1)$ and define: $$W = \begin{cases} Z & \text{if } |Z| \le c \\\ -Z & \text{if } |Z| > c \end{cases}$$ where $c > 0$ is chosen so that $E[ZW] = 0$. **$W$ is standard normal:** Since $|W| = |Z|$ and the sign of $W$ is symmetric (the density of $Z$ is symmetric about 0, and the sign-flipping preserves this symmetry), $W \sim N(0, 1)$. **$Z$ and $W$ are uncorrelated:** We need: $$E[ZW] = E[Z^2 \cdot \mathbf{1}_{|Z| \le c}] - E[Z^2 \cdot \mathbf{1}_{|Z| > c}] = 0$$ This requires $E[Z^2 \cdot \mathbf{1}_{|Z| \le c}] = E[Z^2 \cdot \mathbf{1}_{|Z| > c}] = 1/2$ (since $E[Z^2] = 1$). The function $g(c) = E[Z^2 \cdot \mathbf{1}_{|Z| \le c}]$ is continuous, equals 0 at $c = 0$ and 1 at $c = \infty$, so by the intermediate value theorem there exists a $c$ where $g(c) = 1/2$. (Numerically, $c \approx 1.54$.) **$Z$ and $W$ are dependent:** They are clearly dependent because $|W| = |Z|$ always. Knowing $|Z| = 2$ tells you $|W| = 2$ with certainty. **Why no contradiction:** This does not contradict Part 1 because $(Z, W)$ is not jointly normal. The joint distribution is concentrated on the union of lines $w = z$ and $w = -z$, which is not a bivariate Gaussian. **Answer:** - Part 1: For jointly normal $(X, Y)$, zero covariance makes $\Sigma$ diagonal, which forces the joint density to factor into the product of marginals. Hence $X \perp Y$. - Part 2: Let $Z \sim N(0,1)$ and $W = Z \cdot \text{sign}(c - |Z|)$ with appropriate $c$. Both marginals are $N(0,1)$ and $\text{Cov}(Z, W) = 0$, but $(Z, W)$ is not jointly normal, so the pair is dependent despite being uncorrelated. --- Limiting Distribution of Fixed Points of a Random Function [medium, Probability] --- https://quantvault.org/problem-107-limiting-distribution-of-fixed-points-of-a-random-function.html Q: Pick a function $f_n: [n] \to [n]$ uniformly at random from the set of all $n^n$ functions, where $[n] = \{1, 2, \dots, n\}$. A fixed point is an index $i$ such that $f_n(i) = i$. Let $F_n$ denote the number of fixed points of $f_n$. 1. What is the exact distribution of $F_n$ for finite $n$? 2. What is the limiting distribution of $F_n$ as $n \to \infty$? That is, for any non-negative integer $k$, what does $P(F_n = k)$ converge to? 3. Compute $\displaystyle \lim_{n \to \infty} P(F_n = 3)$ to the nearest thousandth. A: **How to Think About It:** The key observation is that for random functions (as opposed to random permutations), the fixed-point indicators are independent. Each element $i$ maps to a uniformly random target in $[n]$, and since any number of elements can map to the same target, whether $f_n(i) = i$ tells you nothing about whether $f_n(j) = j$. This independence is what makes the problem clean. Once you see that, you have $n$ independent Bernoulli trials each with success probability $1/n$ -- a textbook setup for the Poisson limit theorem. **Quick Estimate:** Each of $n$ elements is a fixed point with probability $1/n$, so the expected number of fixed points is $n \cdot (1/n) = 1$, regardless of $n$. If the limit is Poisson(1), then $P(F = 3) = e^{-1}/3! = e^{-1}/6 \approx 0.368/6 \approx 0.061$. We can sanity-check: $P(F = 0) \approx 0.368$, $P(F = 1) \approx 0.368$, $P(F = 2) \approx 0.184$, $P(F = 3) \approx 0.061$ -- these sum to about 0.981, leaving small probability in the tail. Looks right. **Approach:** Show $F_n \sim \text{Bin}(n, 1/n)$ exactly, then apply the Poisson limit theorem. **Formal Solution:** Define indicator variables $X_i = \mathbf{1}[f_n(i) = i]$ for $i = 1, 2, \dots, n$. Then $F_n = X_1 + X_2 + \cdots + X_n$. - Each $X_i$ is Bernoulli with $P(X_i = 1) = 1/n$, since $f_n(i)$ is uniform on $[n]$. - The $X_i$ are mutually independent. This is because $f_n(i)$ and $f_n(j)$ are chosen independently for $i \neq j$ -- each output is drawn independently from $[n]$. (This is the crucial difference from random permutations, where the outputs must be distinct and the indicators become negatively correlated.) Therefore $F_n$ is a sum of $n$ independent $\text{Bernoulli}(1/n)$ random variables: $$F_n \sim \text{Bin}(n, 1/n)$$ Now apply the **Poisson limit theorem**: if $X_n \sim \text{Bin}(n, p_n)$ with $n p_n \to \lambda$, then $X_n \xrightarrow{d} \text{Poisson}(\lambda)$. Here $n \cdot (1/n) = 1$ for all $n$, so $\lambda = 1$ and: $$F_n \xrightarrow{d} \text{Poisson}(1)$$ For any non-negative integer $k$: $$\lim_{n \to \infty} P(F_n = k) = \frac{e^{-1}}{k!}$$ For $k = 3$: $$\lim_{n \to \infty} P(F_n = 3) = \frac{e^{-1}}{3!} = \frac{e^{-1}}{6} \approx \frac{0.36788}{6} \approx 0.06131$$ **Answer:** The limiting distribution is $\text{Poisson}(1)$, and $\displaystyle \lim_{n \to \infty} P(F_n = 3) = \frac{e^{-1}}{6} \approx 0.061$. --- Matching Colors from Two Jars [easy, Probability] --- https://quantvault.org/problem-109-matching-colors-from-two-jars.html Q: Bob has two jars of marbles: - **Jar A:** $4$ blue and $6$ red marbles ($10$ total) - **Jar B:** $5$ blue and $3$ red marbles ($8$ total) Bob draws one marble uniformly at random from each jar. What is the probability that the two marbles are the same color? A: **How to Think About It:** The two draws are independent (one from each jar), so we just need to add the probability of matching on red and the probability of matching on blue. This is a quick computation -- no tricks, just careful bookkeeping. **Quick Estimate:** Jar A is $60\%$ red, Jar B is $37.5\%$ red. The probability of both red is roughly $0.6 \times 0.375 = 0.225$. Jar A is $40\%$ blue, Jar B is $62.5\%$ blue, so both blue is roughly $0.4 \times 0.625 = 0.25$. Total match probability is about $0.475$, so just under $50\%$. **Formal Solution:** The draws are independent, so: $$P(\text{match}) = P(\text{both red}) + P(\text{both blue})$$ **Both red:** $$P(\text{red from A}) \cdot P(\text{red from B}) = \frac{6}{10} \cdot \frac{3}{8} = \frac{18}{80} = \frac{9}{40}$$ **Both blue:** $$P(\text{blue from A}) \cdot P(\text{blue from B}) = \frac{4}{10} \cdot \frac{5}{8} = \frac{20}{80} = \frac{10}{40}$$ **Total:** $$P(\text{match}) = \frac{9}{40} + \frac{10}{40} = \frac{19}{40} = 0.475$$ **Answer:** $\dfrac{19}{40} = 0.475$, or $47.5\%$. --- Monte Carlo Backtest of Kelly and Fractional Kelly Strategies [medium, Expectation] --- https://quantvault.org/problem-112-monte-carlo-backtest-of-kelly-and-fractional-kelly-strategie.html Q: You run a Monte Carlo backtest of a Kelly-style betting strategy on a sequence of independent favorable bets. Each bet wins with probability $p > 1/2$ and pays net odds of $b:1$ on a win (you gain $b$ times your bet on a win and lose your bet on a loss). (a) Derive the Kelly-optimal fraction $f^{*}$ of wealth to bet each round to maximize expected log-wealth. (b) In practice, you bet a fraction $cf^{*}$ with $c \in (0, 1)$ ("fractional Kelly"). How do the long-run expected log-growth rate and the volatility of log-wealth depend on $c$? Derive explicit expressions. (c) Describe how you would use Monte Carlo simulation to compare the distribution of terminal wealth across different choices of $c$ over a finite horizon of $T$ rounds. Why do interviewers (and practitioners) emphasize the tradeoff between theoretical optimality and finite-horizon risk? A: **How to Think About It:** This problem connects the Kelly Criterion theory to practical implementation. Part (a) is the standard Kelly derivation. The interesting parts are (b) and (c): in theory, full Kelly ($c = 1$) maximizes long-run growth, but over any finite number of bets, the path can be extremely volatile. Fractional Kelly ($c 1-p$, i.e., when the expected return per dollar bet is positive. The maximum growth rate is: $$G^{*} = G(f^{*}) = p \ln\left(1 + bf^{*}\right) + (1-p) \ln\left(1 - f^{*}\right)$$ **Part (b): Fractional Kelly -- Growth Rate and Volatility as Functions of $c$** When you bet $f = cf^{*}$ with $c \in (0, 1)$, the log-growth rate per round is: $$G(c) = p \ln(1 + bcf^{*}) + (1-p) \ln(1 - cf^{*})$$ **Growth rate:** $G(c)$ is a concave function of $c$ that peaks at $c = 1$ (full Kelly). For small deviations from Kelly, the growth rate is approximately: $$G(c) \approx G^{*} - \frac{1}{2}|G''(f^{*})|(f^{*})^2(1 - c)^2$$ So the growth loss from fractional Kelly is quadratic in $(1-c)$: halving to $c = 0.5$ costs only about $25\%$ of the maximum growth rate, not $50\%$. **Volatility of log-wealth:** The variance of the single-round log-return when betting $cf^{*}$ is: $$\sigma^2(c) = p(1-p)\left[\ln\left(\frac{1 + bcf^{*}}{1 - cf^{*}}\right)\right]^2$$ This is approximately proportional to $c^2$ for small $cf^{*}$: $$\sigma^2(c) \approx c^2 (f^{*})^2 (b+1)^2 p(1-p)$$ So the standard deviation scales linearly with $c$. Over $T$ rounds (independent bets), the total log-wealth has: - Mean: $T \cdot G(c)$ (roughly linear in $c$ near $c = 0$) - Standard deviation: $\sqrt{T} \cdot \sigma(c)$ (linear in $c$) The Sharpe ratio of log-returns (growth rate divided by volatility) is approximately: $$\text{SR}(c) \approx \frac{G(c)}{\sigma(c)}$$ Since $G(c)$ is roughly linear in $c$ for small $c$ and $\sigma(c)$ is also linear in $c$, the Sharpe ratio is approximately constant for small $c$. But for larger $c$, the concavity of $G(c)$ means the Sharpe ratio actually decreases -- full Kelly has the highest growth but not the highest risk-adjusted return over finite horizons. **Part (c): Monte Carlo Simulation Design** **Simulation procedure:** 1. Fix parameters $p$, $b$, initial wealth $W_0 = 1$, horizon $T$ (e.g., $T = 1000$ rounds). 2. Choose a grid of $c$ values: e.g., $c \in \{0.1, 0.2, 0.3, \ldots, 1.0, 1.5, 2.0\}$. 3. For each $c$, run $M$ independent simulations (e.g., $M = 10{,}000$): - In each simulation, generate $T$ independent Bernoulli($p$) outcomes. - Track wealth: $W_{t+1} = W_t \cdot (1 + bcf^{*})$ on a win, $W_{t+1} = W_t \cdot (1 - cf^{*})$ on a loss. - Record terminal wealth $W_T$. 4. For each $c$, compute: - Median terminal wealth (more informative than mean for multiplicative processes). - 5th and 95th percentiles of terminal wealth. - Probability that terminal wealth finishes below a threshold, e.g. $P(W_T = 1.0: # staking the whole bankroll: one loss wipes you out, log-wealth is -inf results[c] = {'note': 'f >= 1: ruin on the first loss'} continue up, down = np.log(1 + b * f), np.log(1 - f) wins = rng.random((M, T)) = 1` guard is not pedantic: the $c$ grid deliberately runs past $c = 1$ to show over-betting, and once $c f^{*} \ge 1$ the loss branch $\log(1 - f)$ is undefined, which is the arithmetic telling you a single loss is terminal. And drawdown must come from `log_paths`, not from terminal wealth -- the two answer different questions, and it is the path statistic that gets traders fired. **Why the tradeoff matters:** Full Kelly ($c = 1$) maximizes the median terminal wealth as $T \to \infty$. But over finite horizons: - The distribution of terminal wealth under full Kelly is extremely right-skewed (log-normal with high variance). The mean is enormous, but the median can still be disappointing for moderate $T$. - Drawdowns under full Kelly can be severe: losing 50-80% of wealth before recovering is common. - Fractional Kelly ($c = 0.5$) gives up only about 25% of the growth rate but cuts the volatility of log-wealth in half, dramatically reducing the probability of large drawdowns. - Over-Kelly ($c > 1$) is catastrophic: expected log-growth decreases, and the probability of severe drawdown increases. At $c = 2$, expected log-growth is approximately zero. Interviewers emphasize this because it tests whether you understand that theoretical optimality (infinite-horizon, log-utility) does not equal practical optimality (finite horizon, real constraints, career risk). A trader who bets full Kelly will eventually go through drawdowns that get them fired, even if the strategy is positive-EV. **Answer:** (a) $f^{*} = (pb - q)/b$. (b) Growth rate $G(c)$ is concave in $c$, peaking at $c = 1$; volatility $\sigma(c)$ scales linearly with $c$. Half-Kelly sacrifices about 25% growth but halves volatility. (c) Simulate $M$ paths of $T$ rounds for each $c$, compare median wealth, percentiles, and drawdown probabilities. The key lesson: full Kelly is theoretically optimal but practically too aggressive for finite horizons and real-world constraints. --- Monty Hall With 100 Doors [easy, Probability] --- https://quantvault.org/problem-113-monty-hall-with-100-doors.html Q: There are 100 doors. Behind one door is a car; behind each of the other 99 doors is a goat. You pick one door at random. The host, who knows exactly which door hides the car, then opens 98 of the remaining 99 doors, every one of which reveals a goat. Two doors are now closed: yours and one other. 1. Should you switch to the remaining unopened door? 2. What is the probability you win the car if you switch? What if you stick with your original choice? 3. Generalize: with $n$ doors, the host opens $n - 2$ goat doors. What is your win probability if you switch? A: **How to Think About It:** This is the classic Monty Hall problem scaled up to make the logic unmistakable. The key is that the host is not acting randomly -- he *knows* where the car is and is forced to open only goat doors. That means his action carries information. When you first picked, you had a 1-in-100 shot. The host then systematically eliminated 98 losing doors from the other 99. All of the probability mass that was spread across those 99 doors gets funneled into the single door the host chose *not* to open. Your door is still stuck at its original 1/100. **Quick Estimate:** Before any math, think about it this way: if you always switch, you win whenever your original pick was wrong. Your original pick is wrong 99 times out of 100. So switching wins with probability 99/100 = 0.99. Done. No Bayes' theorem needed for the answer -- the logic is that clean. **Approach:** We can formalize this with conditional probability to confirm the intuition. **Formal Solution:** Label the doors $1, 2, \ldots, 100$. Suppose you pick door 1. Let $C$ be the door hiding the car, uniformly distributed: $P(C = i) = 1/100$ for each $i$. - **Case 1: Car is behind door 1 (your pick).** This happens with probability $1/100$. The host opens 98 of the remaining 99 doors (all goats). If you switch, you lose. - **Case 2: Car is behind some other door $j \neq 1$.** This happens with probability $99/100$. The host *must* open all 98 remaining goat doors, leaving door $j$ as the only other closed door. If you switch, you land on door $j$ and win. So the probability of winning by switching is: $$P(\text{win} \mid \text{switch}) = P(C \neq 1) = \frac{99}{100}$$ And sticking gives: $$P(\text{win} \mid \text{stick}) = P(C = 1) = \frac{1}{100}$$ **Generalization to $n$ doors:** The same argument works. You pick one door out of $n$. The host opens $n - 2$ goat doors. Switching wins whenever your original pick was wrong: $$P(\text{win} \mid \text{switch}) = \frac{n - 1}{n}$$ For the standard 3-door Monty Hall, this gives $2/3$. For 100 doors, $99/100$. As $n \to \infty$, switching is essentially guaranteed to win. **Answer:** Always switch. The probability of winning by switching is $99/100 = 0.99$, while sticking wins with probability $1/100 = 0.01$. In the general $n$-door version, switching wins with probability $(n-1)/n$. --- Optimal Kelly Bet Sizing for a Mean-Reversion Signal [medium, Optimization] --- https://quantvault.org/problem-117-optimal-kelly-bet-sizing-for-a-mean-reversion-signal.html Q: You have an opportunity each day to trade an ETF mean-reversion signal. Each day the trade either wins or loses: - With probability $p = 0.54$, you earn $+1\%$ on the capital you deploy. - With probability $q = 0.46$, you lose $-1\%$ on the capital you deploy. You choose a fraction $f \in [0, 1]$ of your total bankroll to invest in the trade each day. The remaining fraction $1 - f$ sits in cash and earns nothing. 1. Derive the fraction $f^{*}$ that maximizes the expected log-growth of your bankroll over one day, $E[\log(W_1 / W_0)]$. 2. What is the maximum expected log-growth at $f^{*}$? A: **How to Think About It:** This is a textbook Kelly criterion setup. You have a small edge (54% vs. 46%) on a bet that pays 1:1 in percentage terms. The question is: how much of your bankroll should you deploy each day to maximize long-run compounded growth? The Kelly framework says bet $f^{*} = (p - q)/b$ where $b$ is the gain-per-unit-bet. But here the gain-per-unit-bet is only $0.01$ (you earn 1% on deployed capital), so the Kelly fraction blows up well past 1. Since you are constrained to $f \in [0,1]$, the answer is to go all in -- the per-trade risk is so small relative to the edge that even full deployment barely scratches the bankroll in a bad outcome. **Quick Estimate:** If you bet everything ($f = 1$), your bankroll grows by $+1\%$ with probability 0.54 and shrinks by $-1\%$ with probability 0.46. Expected log-growth per day: $$g(1) = 0.54 \ln(1.01) + 0.46 \ln(0.99)$$ Using $\ln(1+x) \approx x - x^2/2$ for small $x$: $$g(1) \approx 0.54(0.01 - 0.00005) + 0.46(-0.01 - 0.00005) = 0.005397 - 0.004623 = 0.000774$$ So roughly $7.5$ basis points of log-growth per day. Over 252 trading days that compounds to about $0.000750 \times 252 \approx 0.189$, or roughly $19\%$ annual log-growth. Not bad for a tiny daily edge. **Approach:** Write the expected log-growth as a function of $f$, take the derivative, solve for the unconstrained optimum, then apply the constraint $f \leq 1$. **Formal Solution:** After one day, your wealth is: $$W_1 = W_0 \cdot \begin{cases} 1 + 0.01 f & \text{with probability } 0.54 \\ 1 - 0.01 f & \text{with probability } 0.46 \end{cases}$$ The expected log-growth rate is: $$g(f) = 0.54 \ln(1 + 0.01 f) + 0.46 \ln(1 - 0.01 f)$$ Differentiate with respect to $f$: $$g'(f) = \frac{0.54 \times 0.01}{1 + 0.01 f} - \frac{0.46 \times 0.01}{1 - 0.01 f} = \frac{0.0054}{1 + 0.01 f} - \frac{0.0046}{1 - 0.01 f}$$ Setting $g'(f) = 0$: $$0.54(1 - 0.01 f) = 0.46(1 + 0.01 f)$$ $$0.54 - 0.0054 f = 0.46 + 0.0046 f$$ $$0.08 = 0.01 f$$ $$f_{\text{unconstrained}}^{*} = 8$$ Since $g(f)$ is strictly concave (you can verify $g''(f) < 0$) and the unconstrained maximizer $f = 8$ lies far above the feasible region $[0,1]$, the function $g(f)$ is strictly increasing on $[0,1]$. Therefore the constrained optimum is: $$f^{*} = 1$$ This makes intuitive sense: the unconstrained Kelly fraction is $f_{\text{Kelly}} = (p - q) / r = 0.08 / 0.01 = 8$, meaning you would want to lever up 8x if you could. Capped at $f = 1$, you invest everything. The maximum expected log-growth at $f^{*} = 1$: $$g(1) = 0.54 \ln(1.01) + 0.46 \ln(0.99)$$ Computing precisely: $\ln(1.01) = 0.0099503\ldots$ and $\ln(0.99) = -0.0100503\ldots$ $$g(1) = 0.54 \times 0.0099503 + 0.46 \times (-0.0100503)$$ $$= 0.0053732 - 0.0046231 = 0.0007501$$ Using the second-order approximation as a check: $g \approx E[r] - \text{Var}(r)/2$ where $r$ is the portfolio return. With $f = 1$: $E[r] = 0.01(0.54 - 0.46) = 0.0008$ and $\text{Var}(r) = (0.01)^2 \cdot 0.54 \cdot 0.46 \cdot 4 = 0.00009936$, giving $g \approx 0.0008 - 0.0000497 = 0.000750$. Consistent. **Answer:** The optimal fraction is $f^{*} = 1$ (invest your entire bankroll). The unconstrained Kelly fraction is $f = (p - q)/r = 0.08/0.01 = 8$, which exceeds the constraint, so the boundary solution applies. The maximum expected log-growth per day is: $$g(f^{*}) = 0.54 \ln(1.01) + 0.46 \ln(0.99) \approx 7.50 \times 10^{-4}$$ or about $0.075\%$ (0.75 basis points) per day. --- Optimal Stopping for Maximum of Uniforms [hard, Expectation] --- https://quantvault.org/problem-120-optimal-stopping-for-maximum-of-uniforms.html Q: You observe $n$ i.i.d. random variables $X_1, X_2, \ldots, X_n$, each drawn from $\text{Unif}(0, 1)$, one at a time. After seeing each $X_t$, you must immediately decide whether to stop and collect payoff $X_t$, or continue to the next observation. If you reach $X_n$ without stopping, you must take $X_n$. Your goal is to maximize $E[X_{\tau}]$, where $\tau$ is your stopping time. 1. Show that the optimal policy is a **threshold rule**: at each step $t$, stop if and only if $X_t \geq c_t$ for some sequence of thresholds $c_1, c_2, \ldots, c_n$. 2. Derive the recursion that determines the thresholds $c_t$, and compute $c_n$ and $c_{n-1}$ explicitly. A: **How to Think About It:** This is a classic sequential decision problem. At each step you see a draw and must decide: take it or gamble that something better comes along. The key insight is that your decision should only depend on the current value $X_t$ and how many observations remain -- the past draws (which you already rejected) are irrelevant. This immediately suggests a threshold structure: for each time step, there is a cutoff below which you should keep looking and above which you should stop. The threshold should decrease as you run out of observations, because the option to continue becomes less valuable. **Quick Estimate:** With $n$ observations, the expected payoff of the naive strategy (just take the last one) is $1/2$. The optimal strategy must do better. For $n = 2$: you stop at $X_1$ if $X_1 \geq c_1$, otherwise take $X_2$. A quick calculation (below) gives $c_1 = 1/2$ and expected payoff $\tfrac{1}{2}\cdot\tfrac{1}{2} + \int_{1/2}^{1} x\,dx = 5/8$. For large $n$, the optimal expected payoff approaches 1 roughly like $1 - 2/n$ (you are very likely to see something close to 1 among $n$ uniform draws, and the threshold policy captures most of that). **Approach:** Define the value function by backward induction, then show the optimal policy is a threshold rule by examining the structure of the continuation value. **Formal Solution:** *Part 1: Threshold structure* Let $V_t$ denote the expected payoff under the optimal policy when you are at step $t$ and have not yet stopped. Define $V_{n+1} = 0$ (if you somehow pass all observations, you get nothing -- but in practice you are forced to take $X_n$). At step $n$ (the last observation), you must take $X_n$, so: $$V_n = E[X_n] = \frac{1}{2}$$ But more precisely, $V_n$ is the expected payoff *before* seeing $X_n$. The decision at step $t < n$ is: stop and receive $X_t$, or continue and receive expected payoff $V_{t+1}$. The optimal action is: $$\text{Stop at } t \iff X_t \geq V_{t+1}$$ This is a threshold rule with $c_t = V_{t+1}$ -- you stop whenever the current draw exceeds the value of continuing. The threshold structure follows because $V_{t+1}$ is a constant (it does not depend on $X_t$ or any past observations), so the comparison $X_t \geq V_{t+1}$ is simply checking whether the current observation exceeds a fixed cutoff. *Part 2: Recursion for the thresholds* At step $t$, under the optimal policy you stop if $X_t \geq c_t = V_{t+1}$ and continue otherwise. The expected payoff at step $t$ is: $$V_t = E[\max(X_t, V_{t+1})]$$ Since $X_t \sim \text{Unif}(0,1)$: $$V_t = \int_0^{V_{t+1}} V_{t+1} \, dx + \int_{V_{t+1}}^{1} x \, dx$$ $$= V_{t+1} \cdot V_{t+1} + \left[\frac{x^2}{2}\right]_{V_{t+1}}^{1}$$ $$= V_{t+1}^2 + \frac{1}{2} - \frac{V_{t+1}^2}{2}$$ $$= \frac{1}{2}(1 + V_{t+1}^2)$$ So the recursion is: $$\boxed{V_t = \frac{1 + V_{t+1}^2}{2}}$$ with the threshold at step $t$ being $c_t = V_{t+1}$. *Computing $c_n$ and $c_{n-1}$:* At step $n$, you are forced to take $X_n$, so $c_n = 0$ (you always stop -- equivalently, the continuation value after step $n$ is 0, and any non-negative draw beats it). More usefully: $$V_n = \frac{1}{2}$$ Thus $c_{n-1} = V_n = \frac{1}{2}$. At step $n-1$, you stop if $X_{n-1} \geq 1/2$. For completeness: $$V_{n-1} = \frac{1 + (1/2)^2}{2} = \frac{1 + 1/4}{2} = \frac{5}{8}$$ So $c_{n-2} = 5/8$, confirming the thresholds are increasing as we move earlier in the sequence (you are pickier when you have more chances remaining). **Answer:** The optimal policy is a threshold rule: stop at step $t$ if $X_t \geq c_t$, where $c_t = V_{t+1}$ and the value function satisfies $V_t = (1 + V_{t+1}^2)/2$ with $V_n = 1/2$. Explicitly, $c_n = 0$ (always take the last draw) and $c_{n-1} = 1/2$. --- Optimal Stopping in a Blue-Red Ball Draw [hard, Game Theory] --- https://quantvault.org/problem-122-optimal-stopping-in-a-blue-red-ball-draw.html Q: A box contains 4 blue balls and 3 red balls. You draw balls one at a time without replacement. Each blue ball earns you $\$1$ and each red ball costs you $\$1$. After each draw you see the ball's color, collect (or pay) the dollar, and then decide: stop and keep your running total, or draw again. You may stop at any point -- including before drawing at all (locking in $\$0$). What is the optimal stopping strategy, and what is the expected profit under that strategy? A: **How to Think About It:** This is a classic optimal-stopping problem on a finite urn. The key feature is that you draw *without* replacement, so seeing a red ball is actually informative -- it shifts the remaining composition in your favor. The obvious wrong instinct is "stop when I'm ahead." The right instinct is "stop only when drawing next has negative expected continuation value." Because the urn composition changes with each draw, you track the state as $(b, r)$ -- blues and reds remaining -- and compute the value of the game from each state via backward induction. Quick gut check before the math: you have 4 blues and 3 reds. If you drew every ball you would net $4 - 3 = +1$. You can do strictly better by stopping early in the rare states where the future is bad, so the answer should exceed $1$. **Quick Estimate:** Think about the boundary states. If you ever exhaust all reds, the remaining blues are free money. If only reds remain, you stop immediately and collect $0$ more. The crucial -- and easy to get wrong -- observation is that the stopping boundary is NOT "stop when reds $\ge$ blues." Even when you are behind, one more draw is often worth it because a red draw *improves* the remaining mix. In fact, for this urn the continuation value is positive in almost every reachable state; you only refuse to draw from states with no blue left or from $(1,3)$. So the value sits a bit above the drew-everything baseline of $+1$, landing near $1.6$. **Approach:** Backward induction. Let $V(b, r)$ be the optimal expected *additional* profit when $b$ blue and $r$ red balls remain. At each state you choose to stop (lock in $\$0$ more) or draw one ball and play optimally from the resulting state. Importantly, the base case for an all-blue state is $V(b,0)=b$ (you draw every remaining blue), so after drawing the *last* red out of a state you must still credit the blues that remain. **Formal Solution:** Base cases: $$V(b, 0) = b \quad \text{(only blues left -- draw them all)}$$ $$V(0, r) = 0 \quad \text{(only reds left -- stop immediately)}$$ Recurrence: at state $(b, r)$ with $n=b+r$, drawing yields blue w.p. $b/n$ and red w.p. $r/n$: $$V(b, r) = \max\!\left(0,\; \frac{b}{n}\bigl(1 + V(b-1, r)\bigr) + \frac{r}{n}\bigl(-1 + V(b, r-1)\bigr)\right)$$ Build the table from small states. The subtle step is $V(1,1)$: drawing a red from $(1,1)$ moves you to $(1,0)$, where one blue remains, so $V(1,0)=1$, NOT $0$. - $V(1, 1) = \max\!\big(0,\; \tfrac12(1+V(0,1)) + \tfrac12(-1+V(1,0))\big) = \max\!\big(0,\; \tfrac12(1) + \tfrac12(0)\big) = \tfrac12$ - $V(1, 2) = \max\!\big(0,\; \tfrac13(1+0) + \tfrac23(-1+V(1,1))\big) = \max\!\big(0,\; \tfrac13 + \tfrac23(-\tfrac12)\big) = \max(0,0) = 0$ - $V(1, 3) = \max\!\big(0,\; \tfrac14(1+0) + \tfrac34(-1+V(1,2))\big) = \max\!\big(0,\; \tfrac14 - \tfrac34\big) = 0$ - $V(2, 1) = \max\!\big(0,\; \tfrac23(1+V(1,1)) + \tfrac13(-1+V(2,0))\big) = \tfrac23\cdot\tfrac32 + \tfrac13\cdot 1 = \tfrac43$ - $V(2, 2) = \max\!\big(0,\; \tfrac12(1+V(1,2)) + \tfrac12(-1+V(2,1))\big) = \tfrac12(1) + \tfrac12\big(\tfrac13\big) = \tfrac23$ - $V(2, 3) = \max\!\big(0,\; \tfrac25(1+V(1,3)) + \tfrac35(-1+V(2,2))\big) = \tfrac25 + \tfrac35\big(-\tfrac13\big) = \tfrac15$ - $V(3, 1) = \max\!\big(0,\; \tfrac34(1+V(2,1)) + \tfrac14(-1+V(3,0))\big) = \tfrac34\cdot\tfrac73 + \tfrac14\cdot 2 = \tfrac{9}{4}$ - $V(3, 2) = \max\!\big(0,\; \tfrac35(1+V(2,2)) + \tfrac25(-1+V(3,1))\big) = \tfrac35\cdot\tfrac53 + \tfrac25\cdot\tfrac54 = 1 + \tfrac12 = \tfrac32$ - $V(3, 3) = \max\!\big(0,\; \tfrac12(1+V(2,3)) + \tfrac12(-1+V(3,2))\big) = \tfrac12\cdot\tfrac65 + \tfrac12\cdot\tfrac12 = \tfrac{17}{20}$ - $V(4, 1) = \max\!\big(0,\; \tfrac45(1+V(3,1)) + \tfrac15(-1+V(4,0))\big) = \tfrac45\cdot\tfrac{13}{4} + \tfrac15\cdot 3 = \tfrac{16}{5}$ - $V(4, 2) = \max\!\big(0,\; \tfrac46(1+V(3,2)) + \tfrac26(-1+V(4,1))\big) = \tfrac23\cdot\tfrac52 + \tfrac13\cdot\tfrac{11}{5} = \tfrac53 + \tfrac{11}{15} = \tfrac{12}{5}$ - $V(4, 3) = \max\!\big(0,\; \tfrac47(1+V(3,3)) + \tfrac37(-1+V(4,2))\big) = \tfrac47\cdot\tfrac{37}{20} + \tfrac37\cdot\tfrac75 = \tfrac{37}{35} + \tfrac{3}{5} = \tfrac{58}{35}$ Since the continuation value is positive in every reachable state except the all-red states and $(1,3)$ (with $(1,2)$ exactly indifferent), the optimal rule is: **keep drawing as long as at least one blue remains, except stop at $(1,3)$** -- equivalently, stop only when continuation value $\le 0$. **Answer:** The optimal expected profit is $V(4,3) = \dfrac{58}{35} \approx 1.657$. The optimal policy is to keep drawing whenever the DP continuation value is positive; the only states where you stop are those with no blue left and the state $(1,3)$ (state $(1,2)$ is a knife-edge where continuing is exactly break-even). --- Order Statistics: Expectations and Densities for Uniform Samples [medium, Expectation] --- https://quantvault.org/problem-126-order-statistics-expectations-and-densities-for-uniform-samp.html Q: Let $X_1, X_2, \ldots, X_n$ be $n$ i.i.d. uniform random variables on $[0, 1]$. Denote their order statistics $X_{(1)} \leq X_{(2)} \leq \cdots \leq X_{(n)}$. (a) Prove that $E[X_{(k)}] = \dfrac{k}{n+1}$ for any $k \in \{1, 2, \ldots, n\}$. (b) Derive the probability density function of $X_{(k)}$ directly -- without first finding the CDF -- and identify the resulting distribution by name and parameters. A: **How to Think About It:** Drop $n$ points uniformly on $[0,1]$. By symmetry they create $n + 1$ gaps of equal expected length $1/(n+1)$ each. The $k$-th order statistic is the right endpoint of the $k$-th gap, so its expected value is $k/(n+1)$. That is the intuition for part (a) -- no calculation required. For part (b), the direct approach to the PDF uses a counting argument: how many ways can you assign the $n$ points so that the $k$-th smallest lands in a small interval $[x, x + dx]$? **(a) Proof of $E[X_{(k)}] = k/(n+1)$:** **Symmetry/gap argument:** The $n$ points partition $[0,1]$ into $n+1$ gaps. By symmetry of the uniform distribution, all $n+1$ gap lengths are exchangeable (identically distributed). Their expected values are therefore equal, and since they sum to 1: $$E[\text{gap length}] = \frac{1}{n+1}$$ The $k$-th order statistic equals the sum of the first $k$ gaps, so: $$E[X_{(k)}] = k \cdot \frac{1}{n+1} = \frac{k}{n+1} \quad \square$$ **Formal verification via distribution:** As shown in part (b), $X_{(k)} \sim \text{Beta}(k, n-k+1)$. The mean of a Beta$(a,b)$ is $a/(a+b)$, giving: $$E[X_{(k)}] = \frac{k}{k + (n-k+1)} = \frac{k}{n+1} \quad \square$$ **(b) PDF of $X_{(k)}$ without using the CDF:** Consider the event $X_{(k)} \in [x, x+dx]$ for small $dx > 0$ and $x \in (0,1)$. For the $k$-th order statistic to land in $[x, x + dx]$, we need exactly: - $k - 1$ of the $n$ points fall in $[0, x)$ - exactly $1$ point falls in $[x, x + dx)$ - $n - k$ points fall in $(x + dx, 1]$ The multinomial coefficient for choosing which points go where is $\dbinom{n}{k-1, 1, n-k} = \dfrac{n!}{(k-1)! \cdot 1! \cdot (n-k)!}$. The probabilities are: $(x)^{k-1}$ for the $k-1$ points below $x$, $(dx)$ for the 1 point in the interval, and $(1-x)^{n-k}$ for the $n-k$ points above. Therefore: $$P(X_{(k)} \in [x, x+dx]) = \frac{n!}{(k-1)!(n-k)!} x^{k-1}(1-x)^{n-k} \, dx$$ This gives the PDF: $$f_{X_{(k)}}(x) = \frac{n!}{(k-1)!(n-k)!} x^{k-1}(1-x)^{n-k}, \quad x \in [0,1]$$ This is exactly the **Beta$(k, n-k+1)$** density, since $B(k, n-k+1) = \frac{(k-1)!(n-k)!}{n!}$: $$f_{X_{(k)}}(x) = \frac{x^{k-1}(1-x)^{(n-k+1)-1}}{B(k, n-k+1)}, \quad x \in [0,1]$$ **Answer:** (a) $E[X_{(k)}] = k/(n+1)$ by the symmetry of the $n+1$ uniform gaps. (b) $X_{(k)} \sim \text{Beta}(k, n-k+1)$ with PDF $f(x) = \frac{n!}{(k-1)!(n-k)!} x^{k-1}(1-x)^{n-k}$. --- Pattern Race: HTH vs HHT [medium, Probability] --- https://quantvault.org/problem-127-pattern-race-hth-vs-hht.html Q: You and a friend are flipping a fair coin repeatedly. You win if the pattern $HTH$ appears before $HHT$; your friend wins if $HHT$ appears first. What is the probability that you win? A: **How to Think About It:** Both target patterns start with $H$, so nothing interesting happens until the first head appears. Once you see that first $H$, the next two flips determine which pattern gets completed or whether the game resets. This is a classic Markov chain / pattern-matching problem, but you can solve it with a clever shortcut: condition on the first $H$ and enumerate the four possible two-flip continuations. **Quick Estimate:** The two patterns $HTH$ and $HHT$ are not symmetric -- $HHT$ has a structural advantage because $HH$ is a "self-reinforcing" prefix (if you see $HHH$, you are still in position for $HHT$ once a tail comes). So we should expect the probability of $HTH$ winning to be less than $1/2$. Rough guess: somewhere around $1/3$ to $2/5$. **Approach:** Condition on the first $H$ and analyze the four possible outcomes of the next two flips. **Formal Solution:** Eventually, a head must appear. Once we see the first $H$, consider the four equally likely outcomes for the next two flips: | Next two flips | Sequence so far | Result | |---|---|---| | $TH$ | $H \to HTH$ | You win | | $HT$ | $H \to HHT$ | Friend wins | | $HH$ | $H \to HHH$ | Friend will win (see below) | | $TT$ | $H \to HTT$ | Game resets | Let's verify each case: 1. **$TH$ (prob $1/4$):** The sequence is $HTH$ -- you win immediately. 2. **$HT$ (prob $1/4$):** The sequence is $HHT$ -- your friend wins immediately. 3. **$HH$ (prob $1/4$):** The sequence is $HHH\ldots$ We now have a run of heads. Eventually a tail must appear, giving us $HH\ldots HT$. The last three characters are $HHT$ -- your friend wins. (The key insight: once you see $HH$, the $HHT$ pattern is guaranteed to complete before $HTH$ can appear, because the next tail creates $HHT$.) 4. **$TT$ (prob $1/4$):** The sequence is $HTT$. Neither pattern overlaps with the suffix $TT$, so the game is completely reset -- we are back to waiting for the first $H$. Since case 4 resets the game, the eventual winner must come from cases 1, 2, or 3. These three outcomes are equally likely (each has probability $1/4$), and only case 1 is a win for you. Therefore: $$P(\text{you win}) = \frac{P(\text{case 1})}{P(\text{case 1}) + P(\text{case 2}) + P(\text{case 3})} = \frac{1/4}{1/4 + 1/4 + 1/4} = \frac{1}{3}$$ **Answer:** The probability that $HTH$ appears before $HHT$ is $\dfrac{1}{3}$. --- Pirate Game and Coupon Collector's Problem [easy, Game Theory] --- https://quantvault.org/problem-128-pirate-game-and-coupon-collector-s-problem.html Q: Solve these two classic brain teasers: **(a) The Pirate Game:** Five pirates (ranked 1 through 5 by seniority, with 5 being the most senior) must divide 100 gold coins. The most senior pirate proposes a split. All pirates vote, and the proposal passes if at least half vote in favor (the proposer can vote for himself). If it fails, the proposer is thrown overboard and the next most senior pirate proposes. Each pirate is perfectly rational, wants to maximize gold, and prefers survival above all else. What split does Pirate 5 propose? **(b) The Coupon Collector:** There are $n$ distinct coupon types. Each time you buy a product, you receive one coupon uniformly at random. What is the expected number of purchases needed to collect all $n$ types? A: **How to Think About It:** These are two very different problems bundled together. The pirate game is pure backward induction -- you start from the simplest case (2 pirates) and work forward. The coupon collector is a linearity-of-expectation problem -- break the collection process into phases and compute each phase's expected duration separately. **Part (a): The Pirate Game** **Quick Estimate:** Pirate 5 needs 3 votes (out of 5, including his own). He can buy 2 votes cheaply by offering those pirates slightly more than they would get if he were eliminated. The key is figuring out what each pirate gets in the "next" scenario. **Backward induction:** **2 pirates (Pirate 2 proposes):** Pirate 2 needs 1 vote (himself). He takes everything. - Split: Pirate 1 gets 0, Pirate 2 gets 100. **3 pirates (Pirate 3 proposes):** Pirate 3 needs 2 votes. He votes for himself. Pirate 1 would get 0 if Pirate 3 is eliminated, so offering Pirate 1 just 1 coin buys his vote. - Split: (1, 0, 99). **4 pirates (Pirate 4 proposes):** Pirate 4 needs 2 votes. He votes for himself. If Pirate 4 is eliminated, the 3-pirate split is (1, 0, 99). Pirate 2 gets 0 in that scenario, so offering Pirate 2 just 1 coin buys his vote. - Split: (0, 1, 0, 99). **5 pirates (Pirate 5 proposes):** Pirate 5 needs 3 votes. He votes for himself. If Pirate 5 is eliminated, the 4-pirate split is (0, 1, 0, 99). Pirates 1 and 3 each get 0 in that scenario, so offering each of them 1 coin buys their votes. - Split: **(1, 0, 1, 0, 98)**. Pirate 5 proposes: Pirate 1 gets 1, Pirate 2 gets 0, Pirate 3 gets 1, Pirate 4 gets 0, Pirate 5 keeps 98. This passes 3-2. **Part (b): The Coupon Collector** **Quick Estimate:** For $n = 10$: the answer is $10 \cdot H_{10} = 10(1 + 1/2 + \cdots + 1/10) \approx 10 \times 2.93 = 29.3$. It takes about 3 times as many purchases as there are coupon types. For large $n$, $E[T] \approx n \ln n + \gamma n \approx n \ln n + 0.577n$. **Formal Solution:** Divide the collection process into $n$ phases. Phase $k$ starts when you have exactly $k - 1$ distinct types and ends when you get the $k$-th new type. During phase $k$, each purchase gives a new type with probability $(n - k + 1)/n$. The number of purchases in phase $k$ is geometric with success probability $(n - k + 1)/n$, so the expected duration is: $$E[T_k] = \frac{n}{n - k + 1}$$ By linearity of expectation, the total expected number of purchases is: $$E[T] = \sum_{k=1}^{n} E[T_k] = \sum_{k=1}^{n} \frac{n}{n - k + 1} = n \sum_{j=1}^{n} \frac{1}{j} = n \cdot H_n$$ where $H_n = 1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n}$ is the $n$-th harmonic number. Using the asymptotic expansion of the harmonic series: $$E[T] = n\left(\ln n + \gamma + \frac{1}{2n} + O(n^{-2})\right)$$ where $\gamma \approx 0.5772$ is the Euler-Mascheroni constant. **Answer:** (a) Pirate 5 proposes the split (1, 0, 1, 0, 98), which passes 3-2 with Pirates 1, 3, and 5 voting yes. (b) The expected number of purchases is $E[T] = n H_n = n(\ln n + \gamma + O(1/n))$, where $H_n$ is the $n$-th harmonic number. --- Probability No One Gets Their Own Hat [medium, Probability] --- https://quantvault.org/problem-131-probability-no-one-gets-their-own-hat.html Q: $n$ men attend a formal party where they check their distinct hats into a closet at the entrance. At the end of the night the lights go out, and each man grabs a hat uniformly at random (without replacement, so the result is a random permutation of all $n$ hats). Let $p(n)$ be the probability that *none* of the $n$ men ends up with his own hat. 1. Derive a closed-form expression for $p(n)$. 2. Evaluate $p(5)$. A: **How to Think About It:** This is the classic derangement problem -- counting permutations with no fixed points. The first instinct is to think about it person-by-person, but the selections are coupled (if person 1 grabs person 2's hat, person 2 can no longer get it), so direct counting is messy. The clean move is inclusion-exclusion on the complementary events: instead of counting arrangements where *nobody* matches, count those where *at least one* person matches and subtract from 1. A useful gut check before any algebra: the probability that a single person matches is $1/n$. There are $n$ people, so by linearity the expected number of matches is exactly 1, regardless of $n$. That suggests the probability of zero matches should stabilize quickly -- and it does, converging to $1/e \approx 0.368$ almost immediately. **Quick Estimate:** For $n = 5$, the answer is close to $1/e \approx 0.3679$. Since the derangement formula is the partial sum of the Taylor series for $e^{-1}$, the error from truncation at $n = 5$ is at most $1/6! \approx 0.0014$. So we expect something very close to $0.367$. In fact $11/30 \approx 0.3667$, which is spot on. **Approach:** Define the "match" events and apply inclusion-exclusion, exploiting exchangeability to collapse the sum. **Formal Solution:** Let $H_i$ be the event that person $i$ gets their own hat back. We want $$p(n) = P\!\left(\bigcap_{i=1}^{n} H_i^c\right) = 1 - P\!\left(\bigcup_{i=1}^{n} H_i\right).$$ By inclusion-exclusion, $$P\!\left(\bigcup_{i=1}^{n} H_i\right) = \sum_{k=1}^{n} (-1)^{k+1} \sum_{|S|=k} P\!\left(\bigcap_{i \in S} H_i\right).$$ The key simplification: by exchangeability, $P\!\left(\bigcap_{i \in S} H_i\right)$ depends only on $|S| = k$, not on which $k$ people are in $S$. If $k$ specific people must each get their own hat, the remaining $n - k$ hats are permuted freely among the remaining $n - k$ people, giving $$P\!\left(\bigcap_{i \in S} H_i\right) = \frac{(n-k)!}{n!}.$$ There are $\binom{n}{k}$ choices of $S$, and $\binom{n}{k} \cdot \frac{(n-k)!}{n!} = \frac{1}{k!}$. So $$P\!\left(\bigcup_{i=1}^{n} H_i\right) = \sum_{k=1}^{n} \frac{(-1)^{k+1}}{k!}.$$ Therefore, $$p(n) = 1 - \sum_{k=1}^{n} \frac{(-1)^{k+1}}{k!} = \sum_{k=0}^{n} \frac{(-1)^k}{k!}.$$ This is exactly the first $n+1$ terms of the Taylor expansion of $e^{-1}$, which is why $p(n) \to 1/e$ as $n \to \infty$. For $n = 5$: $$p(5) = 1 - 1 + \frac{1}{2} - \frac{1}{6} + \frac{1}{24} - \frac{1}{120} = \frac{60 - 20 + 5 - 1}{120} = \frac{44}{120} = \frac{11}{30}.$$ **Sanity checks:** - $p(1) = 1 - 1 = 0$. Correct -- one person always gets their own hat. - $p(2) = 1 - 1 + 1/2 = 1/2$. Correct -- two people swap with probability $1/2$. - $p(5) = 11/30 \approx 0.3667$, close to $1/e \approx 0.3679$. Checks out. **Answer:** $p(n) = \displaystyle\sum_{k=0}^{n} \frac{(-1)^k}{k!}$, and $p(5) = \dfrac{11}{30}$. --- Probability Two Friends Sit Adjacent in a Row [easy, Probability] --- https://quantvault.org/problem-132-probability-two-friends-sit-adjacent-in-a-row.html Q: Ten people are seated randomly in a row of 10 chairs. Two of them are friends. What is the probability that the two friends end up seated next to each other? A: **How to Think About It:** Fix one friend's position and ask: what fraction of the remaining seats are adjacent? That gives you the answer immediately, no combinatorics needed. Alternatively, you can count favorable arrangements over total arrangements -- but the "fix one person" approach is cleaner and less error-prone. **Quick Estimate:** There are 10 seats in a row, so 9 adjacent pairs. There are $\binom{10}{2} = 45$ total pairs of seats. So the probability two specific people are adjacent is $9/45 = 1/5$. Done. **Approach:** Let us verify this with two different methods. **Formal Solution:** **Method 1 -- Fix one person:** Fix person A in some seat. Person B is equally likely to be in any of the 9 remaining seats. How many of those are adjacent to A? - If A is in seats 2 through 9 (8 positions): 2 adjacent seats each. - If A is in seat 1 or seat 10 (2 positions): 1 adjacent seat each. Average number of adjacent seats, averaged over A's position: $$\frac{8 \times 2 + 2 \times 1}{10} = \frac{18}{10} = 1.8$$ $$P(\text{adjacent}) = \frac{1.8}{9} = \frac{1}{5}$$ **Method 2 -- Direct counting:** Total arrangements: $10!$ Favorable: Treat the two friends as a glued block. There are 9 positions for this block in a row of 10 chairs (positions $(1,2), (2,3), \ldots, (9,10)$). Within the block, the friends can swap, giving $2!$ orderings. The remaining 8 people fill the other 8 seats in $8!$ ways. $$P = \frac{9 \times 2 \times 8!}{10!} = \frac{9 \times 2}{10 \times 9} = \frac{2}{10} = \frac{1}{5}$$ **Answer:** The probability is $\dfrac{1}{5} = 0.2$. --- Probability a Random Triangle Contains the Center of a Circle [medium, Probability] --- https://quantvault.org/problem-133-probability-a-random-triangle-contains-the-center-of-a-circl.html Q: Pick three points independently and uniformly at random on the unit circle. They form a triangle $T$. 1. What is the probability that $T$ contains the center of the circle? 2. Give a clean argument using circular orderings and complementary events. 3. Now suppose the three points are drawn i.i.d. uniformly on the circumference of an ellipse instead. Does the answer change? Why or why not? A: **How to Think About It:** The triangle formed by three points on a circle contains the center if and only if the center is "inside" the triangle -- meaning the three arcs between consecutive points each subtend less than $\pi$ (i.e., each arc is less than a semicircle). Equivalently, the triangle contains the center if and only if no semicircle contains all three points. This complementary framing is the key insight: it is much easier to compute the probability that all three points lie in some semicircle, then subtract from 1. Before doing any math, here is a quick sanity check: by symmetry, the triangle is "small" (not containing the center) more often than not, so we expect the probability to be less than $1/2$. For three random points the answer turns out to be exactly $1/4$. **Quick Estimate:** Fix one point at angle $0$ (by rotational symmetry this is free). The other two points have angles $\theta_1, \theta_2$ uniform on $[0, 2\pi)$. The triangle contains the center roughly when the points are "spread out" around the circle. If you imagine throwing two more darts at random, about $3/4$ of the time they cluster enough that some semicircle catches all three. So $P(\text{contains center}) \approx 1/4$. That is the exact answer. **Approach:** We use the complementary event: $P(\text{contains center}) = 1 - P(\text{all three points lie in some semicircle})$. **Formal Solution:** Label the three points $P_1, P_2, P_3$ with angles $\theta_1, \theta_2, \theta_3$ drawn i.i.d. uniformly on $[0, 2\pi)$. *Step 1: Complementary event.* The triangle contains the center if and only if no semicircle (arc of length $\pi$) contains all three points. So: $$P(\text{contains center}) = 1 - P(\text{some semicircle contains all three points}).$$ *Step 2: Compute $P(\text{all in some semicircle})$.* For each point $P_i$, define the event $A_i$ = "all three points lie in the semicircle starting at $P_i$ and going clockwise for $\pi$." By symmetry, $P(A_i) = (1/2)^2 = 1/4$ for each $i$, because the other two points each independently must land in a specific half of the circle. The events $A_1, A_2, A_3$ are not disjoint, but the event "all three in some semicircle" equals $A_1 \cup A_2 \cup A_3$. However, notice that if all three points lie in a semicircle, then the point that is the "most clockwise" boundary of the smallest arc containing all three defines a unique $A_i$ (the semicircle anchored at that point covers the other two). More precisely, at most one $A_i$ can hold unless two points coincide (probability 0). So the events are almost surely mutually exclusive: $$P(A_1 \cup A_2 \cup A_3) = P(A_1) + P(A_2) + P(A_3) = 3 \times \frac{1}{4} = \frac{3}{4}.$$ *Step 3: Final answer.* $$P(\text{contains center}) = 1 - \frac{3}{4} = \frac{1}{4}.$$ *Part 3: Extension to an ellipse.* The answer does not change: $P = 1/4$. Resist the quick affine argument. An ellipse is an affine image of a circle, and affine maps do preserve "this point is inside this triangle," but they distort arc length, so they do NOT carry the uniform law on the circle to the uniform law on the ellipse. What the circle proof actually used is central symmetry, and every ellipse has it: for a boundary point $P$ the antipode $-P$ is also on the ellipse, so the line through the center cuts the perimeter into two arcs of equal length. Each of the other two points therefore lands in an anchored half with probability $1/2$, each anchored event again has probability $1/4$, and the same disjointness gives $1 - 3/4 = 1/4$. Central symmetry is what matters, not roundness: on the boundary of an equilateral triangle, where central symmetry fails, simulation gives about $0.245$ instead. **Answer:** The probability that the triangle contains the center is $\boxed{1/4}$. The same holds for an ellipse -- not by affine invariance, but because an ellipse is centrally symmetric, which is all the anchoring argument needs. --- Probability a Three-Legged Disk Table Stands [medium, Probability] --- https://quantvault.org/problem-134-probability-a-three-legged-disk-table-stands.html Q: You have a perfectly round disk. You attach three legs to the disk at three points chosen independently and uniformly at random on the circumference. The legs are perpendicular to the disk and firmly attached. You flip the disk over so it rests on the three legs. The table stands upright if and only if the center of the disk lies inside the triangle formed by the three leg positions. Otherwise, the table tips over. What is the probability that the table stands? A: **How to Think About It:** The disk's center of mass is at its geometric center. The table stays upright exactly when that center point lies inside the triangle formed by the three legs. So this is a pure geometry question: if you pick three points uniformly at random on a circle, what is the probability the center of the circle is enclosed by the resulting triangle? Before doing any math, think about what configurations fail -- the triangle misses the center whenever all three points are bunched on one side of some diameter. That happens more often than you might guess. **Quick Estimate:** Think about it with a symmetry argument. Fix one point at the top of the circle. The other two points each land on the left or right semicircle with equal probability. If both land on the same semicircle, the triangle cannot contain the center (all three points lie in a half-plane that does not contain the center). That happens with probability $1/2$, so the table stands at most half the time. But even when the two points land on opposite semicircles, the triangle still might not contain the center if one of the arcs is too wide. So the answer should be noticeably less than $1/2$. A reasonable guess is somewhere around $1/4$. **Approach:** We use the arc-length characterization: three points on a circle enclose the center if and only if no arc between consecutive points exceeds $\pi$ (a semicircle). We then compute the probability by integration. **Formal Solution:** Place three points independently and uniformly on a circle of circumference $2\pi$. Fix one point at angle $0$ (by rotational symmetry, this costs nothing). Let the other two points have angles $U$ and $V$, each uniform on $[0, 2\pi)$. Sort the three points around the circle. The three arcs between consecutive points sum to $2\pi$. The center lies inside the triangle if and only if every arc is strictly less than $\pi$. Condition on $U \pi$. Combined with the second, $V < U + \pi$. So the feasible region is: $$0 < U < \pi, \quad \pi < V < U + \pi$$ The joint density is $1/(2\pi)^2$. The area of this region is: $$\int_0^{\pi} \int_{\pi}^{U + \pi} dV\, dU = \int_0^{\pi} U\, dU = \frac{\pi^2}{2}$$ So the probability in the $U < V$ case is: $$\frac{\pi^2 / 2}{(2\pi)^2} = \frac{\pi^2}{2 \cdot 4\pi^2} = \frac{1}{8}$$ By symmetry, the $V < U$ case contributes another $1/8$. **Answer:** $$P(\text{table stands}) = \frac{1}{8} + \frac{1}{8} = \boxed{\frac{1}{4}}$$ A three-legged table with legs placed uniformly at random on the rim of a circular disk stands with probability $1/4$. --- Simpson's Paradox with Poisson Scoring [hard, Probability] --- https://quantvault.org/problem-137-simpson-s-paradox-with-poisson-scoring.html Q: In a basketball game, teams A and B each play two halves. Let $X_{A,1}$ and $X_{B,1}$ be the points scored by A and B in the first half, and $X_{A,2}$ and $X_{B,2}$ in the second half. Assume each $X_{i,j} \sim \text{Poisson}(\lambda_{i,j})$ and all four are independent. 1. Construct specific parameters $\lambda_{A,1}, \lambda_{B,1}, \lambda_{A,2}, \lambda_{B,2}$ such that A is more likely to outscore B in each individual half, but B is more likely to win the overall game. (This is Simpson's paradox.) 2. Prove that your construction works by computing the relevant probabilities. You may use numerical approximations. 3. Interpret this phenomenon in the context of evaluating trading strategies on conditional vs. aggregate performance metrics. A: **How to Think About It:** Simpson's paradox says you can win every battle but lose the war. The question is whether you can pick Poisson rates so that A outscores B in each half (with probability exceeding 0.5) yet B wins the overall game. Because independent Poisson variables are closed under addition, the total scores are themselves Poisson, and the pairwise win probability is monotonic in the rate parameter. This creates a fundamental constraint that makes the strict version of the paradox impossible under these assumptions -- but the conceptual lesson is exactly the point of the problem. **Quick Estimate:** For independent Poisson variables, $P(X > Y) > P(Y > X)$ if and only if $\lambda_X > \lambda_Y$. If A has a higher rate in both halves, then $\lambda_{A,1} + \lambda_{A,2} > \lambda_{B,1} + \lambda_{B,2}$, so A also dominates in total. There is no room for the rates to "reverse" upon aggregation the way proportions can in the classical Simpson's paradox. **Formal Solution:** Define the pairwise win probability: $$P(X > Y) = \sum_{k=0}^{\infty} \sum_{j=0}^{k-1} \frac{e^{-(\lambda_X + \lambda_Y)} \lambda_X^{k} \lambda_Y^{j}}{k! \, j!}$$ where $X \sim \text{Poisson}(\lambda_X)$ and $Y \sim \text{Poisson}(\lambda_Y)$ are independent. **Claim:** For independent Poisson variables, $P(X > Y) > P(Y > X)$ if and only if $\lambda_X > \lambda_Y$. **Proof sketch:** The difference $X - Y$ has a Skellam distribution with parameters $(\lambda_X, \lambda_Y)$. Its probability mass function is symmetric about zero when $\lambda_X = \lambda_Y$, and shifts rightward as $\lambda_X / \lambda_Y$ increases. More precisely, the distribution satisfies $P(X - Y = k) / P(X - Y = -k) = (\lambda_X / \lambda_Y)^{k}$ for $k > 0$, so the ratio exceeds 1 for every positive $k$ if and only if $\lambda_X > \lambda_Y$. Summing over all $k > 0$ gives $P(X > Y) > P(Y > X)$. **Why strict Simpson's paradox fails here:** If $\lambda_{A,j} > \lambda_{B,j}$ for $j = 1, 2$ (required for A to win each half), then $\lambda_{A,1} + \lambda_{A,2} > \lambda_{B,1} + \lambda_{B,2}$. Since totals $X_{A,1} + X_{A,2} \sim \text{Poisson}(\lambda_{A,1} + \lambda_{A,2})$ and $X_{B,1} + X_{B,2} \sim \text{Poisson}(\lambda_{B,1} + \lambda_{B,2})$ by closure under addition, the monotonicity result implies A also wins the total game with higher probability. The additive structure of both the rates and the Poisson family prevents the reversal that drives Simpson's paradox in non-additive settings (like contingency tables with proportions). **Conceptual construction (the pedagogical point):** Simpson's paradox does arise if you measure performance by a non-additive metric -- for instance, win rate per half vs. expected total margin. Consider: - Half 1: $\lambda_{A,1} = 2, \lambda_{B,1} = 1$ -- A wins this half about 56% of the time, but the expected margin is only $+1$. - Half 2: $\lambda_{A,2} = 12, \lambda_{B,2} = 11$ -- A wins this half about 52% of the time, expected margin $+1$. A wins both halves more often than B. But now imagine evaluating by a magnitude-weighted metric: when B wins a half, B tends to win by a larger margin in the high-intensity half (due to higher variance, $\text{Var} = \lambda$). The Sharpe-like ratio of A's edge relative to the noise is $1/\sqrt{2 + 1} \approx 0.58$ in half 1 and $1/\sqrt{12 + 11} \approx 0.21$ in half 2. A's per-half win probability is positive, but the signal degrades as intensity grows. **Trading Interpretation:** This is the core lesson for evaluating strategies. A strategy can have a higher conditional win rate in every regime (trending, mean-reverting, high-vol, low-vol) yet underperform on aggregate P\&L if: 1. It wins often but small in the regime that dominates calendar time. 2. It loses rarely but loses big when it does lose. 3. The regime mix -- how much time is spent in each regime -- weights the losses more heavily than the wins. This is why quant desks decompose performance by regime AND magnitude, not just win rate. A 60% hit rate means nothing if the average loss is three times the average win. Conditional metrics (win rate per regime) and aggregate metrics (total Sharpe, total P\&L) can tell opposite stories. The fix is to always pair hit rate with payoff ratio and to weight conditional statistics by regime frequency before drawing conclusions. **Answer:** A strict Simpson's paradox (A wins both halves with $P > 0.5$ but loses the total game with $P > 0.5$) is impossible with independent Poisson scoring, because Poisson is closed under addition and the pairwise win probability is monotonic in the rate. The paradox does emerge when conditional win rates are compared against magnitude-weighted aggregate metrics -- exactly the trap that arises in evaluating trading strategies across market regimes. --- Splitting One Uniform Into Two Independent Uniforms [medium, Probability] --- https://quantvault.org/problem-139-splitting-one-uniform-into-two-independent-uniforms.html Q: You have a single random variable $U \sim \text{Unif}(0,1)$. Can you find measurable functions $f, g : [0,1] \to [0,1]$ such that $f(U)$ and $g(U)$ are **independent** and each is $\text{Unif}(0,1)$, without using any additional source of randomness? Provide a rigorous proof of your answer. If the answer is no, then additionally give an explicit construction of $f$ and $g$ such that $f(U) \sim \text{Unif}(0,1)$ and $g(U) \sim \text{Unif}(0,1)$ (not necessarily independent), and quantify the dependence between them -- for example, compute $\text{Cov}(f(U), g(U))$ for your construction. A: **How to Think About It:** Your first instinct is probably "no way -- one source of randomness can't produce two independent copies." That intuition is wrong, and understanding why is the whole point of this problem. The key insight: $U$ is not "one coin flip" of randomness. Its binary expansion $U = 0.b_1 b_2 b_3 \ldots$ gives you an *infinite* sequence of i.i.d. fair coin flips. An infinite i.i.d. sequence can be partitioned into two infinite i.i.d. subsequences. That is the construction. The common wrong "proof" of impossibility goes: $(f(U), g(U))$ is a deterministic function of $U$, so it traces a curve in the unit square, curves have 2D Lebesgue measure zero, so the joint can't be uniform on $[0,1]^2$. The flaw: this argument assumes the map is continuous (or at least sufficiently regular). The bit-interleaving map is wildly discontinuous -- it is a measurable bijection (a.e.) from $[0,1]$ to $[0,1]^2$ that pushes $\lambda_1$ to $\lambda_2$. No contradiction. **Answer: Yes, it is possible.** **Construction (Bit-Interleaving):** For almost every $u \in [0,1]$, write $u$ in its unique binary expansion (excluding the countable, measure-zero set of dyadic rationals): $$u = 0.b_1 b_2 b_3 b_4 b_5 b_6 \ldots \quad (\text{base 2})$$ Define: $$f(u) = 0.b_1 b_3 b_5 b_7 \ldots \quad \text{(odd-indexed bits)}$$ $$g(u) = 0.b_2 b_4 b_6 b_8 \ldots \quad \text{(even-indexed bits)}$$ Equivalently, $f(u) = \sum_{k=1}^{\infty} b_{2k-1} \cdot 2^{-k}$ and $g(u) = \sum_{k=1}^{\infty} b_{2k} \cdot 2^{-k}$. **Proof of correctness:** *Step 1 -- Marginals are uniform.* Since $U \sim \text{Unif}(0,1)$, the binary digits $b_1, b_2, b_3, \ldots$ are i.i.d. $\text{Bernoulli}(1/2)$. The subsequence $(b_1, b_3, b_5, \ldots)$ is also i.i.d. $\text{Bernoulli}(1/2)$, so $f(U) = \sum_{k=1}^{\infty} b_{2k-1} \cdot 2^{-k}$ has the distribution of a uniform on $[0,1]$ (a random variable whose binary digits are i.i.d. fair coins is uniform). By the same argument, $g(U) \sim \text{Unif}(0,1)$. *Step 2 -- Independence.* The odd-indexed bits $(b_1, b_3, b_5, \ldots)$ and the even-indexed bits $(b_2, b_4, b_6, \ldots)$ are disjoint sub-collections of an i.i.d. sequence, hence independent. Since $f(U)$ is $\sigma(b_1, b_3, b_5, \ldots)$-measurable and $g(U)$ is $\sigma(b_2, b_4, b_6, \ldots)$-measurable, the two random variables are independent. $\square$ *Step 3 -- The map is a measure-preserving bijection (a.e.).* The map $h : [0,1] \to [0,1]^2$ defined by $h(u) = (f(u), g(u))$ is an injection on the set of non-dyadic rationals (which has full measure). Its inverse "interleaves" the binary digits of two numbers back into one. For any measurable rectangle $[0, a] \times [0, c] \subseteq [0,1]^2$: $$P(f(U) \leq a,\, g(U) \leq c) = P(f(U) \leq a) \cdot P(g(U) \leq c) = a \cdot c = \lambda_2([0,a] \times [0,c])$$ So $h$ pushes $\lambda_1$ forward to $\lambda_2$. This is why the "curves have measure zero" argument fails: $h$ is not a curve in the topological sense. It is a highly discontinuous, space-filling, measure-preserving map. **Why the dimension argument fails:** The statement "a map from $\mathbb{R}$ to $\mathbb{R}^2$ has image of 2D measure zero" is true for *Lipschitz* maps (by the area formula) and even for continuous maps with bounded variation. But it is false for arbitrary measurable maps. The bit-interleaving map is nowhere continuous -- nearby values of $u$ can map to completely different points in the square (flipping one early bit of $u$ rearranges many bits of $f(u)$ and $g(u)$). Measure-theoretic structure and topological structure are different things, and this problem is all about that distinction. **Bonus: A dependent construction.** The problem asks for a dependent pair if the answer were "no." Since the answer is yes, we provide these for comparison. The simplest dependent construction: $f(U) = U$ and $g(U) = 1 - U$. Both are marginally $\text{Unif}(0,1)$, but they are maximally negatively dependent: $$\text{Cov}(f(U), g(U)) = \text{Cov}(U, 1-U) = -\text{Var}(U) = -\frac{1}{12}$$ Another instructive construction: $f(U) = U$ and $g(U) = (U + 1/2) \bmod 1$. Both marginals are $\text{Unif}(0,1)$ and $\text{Cov}(f(U), g(U)) = -\frac{1}{24}$, only half the magnitude of the antithetic pair -- yet $g(U)$ is a deterministic function of $f(U)$, so the two are as dependent as two random variables can possibly be. A small covariance is not evidence of near-independence. **Answer:** Yes, it is possible. Write $U$ in binary as $0.b_1 b_2 b_3 \ldots$, set $f(U) = 0.b_1 b_3 b_5 \ldots$ (odd bits) and $g(U) = 0.b_2 b_4 b_6 \ldots$ (even bits). Both are $\text{Unif}(0,1)$ and independent. The construction works because the binary digits of a uniform are i.i.d. $\text{Bernoulli}(1/2)$, and partitioning an i.i.d. sequence into disjoint subsequences preserves independence. --- Tightest Moment Bound on a Tail Probability [hard, Probability] --- https://quantvault.org/problem-145-tightest-moment-bound-on-a-tail-probability.html Q: A positive random variable $X$ satisfies $E[X] = 1$ and $E[X^2] = 3$. Using only these two moments: 1. Find the tightest possible upper bound on $P(X \geq 5)$. State which inequality you used. 2. Construct (or describe) a distribution on $[0, \infty)$ that achieves the bound, proving it is tight. A: **How to Think About It:** You have two moments and you want to bound a tail probability. The first instinct is Markov's inequality, but you have a second moment -- you should be able to do better. The real question is: what is the sharpest bound you can get from both moments simultaneously? This is a classic moment problem, and the answer turns out to be Cantelli's inequality (the one-sided Chebyshev bound). To show it is tight, you construct a two-point distribution that saturates the bound. You do not need to have memorized the name: Cantelli is Markov's inequality applied to a shifted square $(X - x_0)^2$, and the Formal Solution below derives it from scratch. The lesson [Moment Generating Functions & Probability Bounds](learn.html?lecture=lec-q-prob-slides-07&track=quant&topic=probability) covers it on the Chebyshev's Inequality slide. **Quick Estimate:** Start with the easy bounds. Markov on $X$ gives $P(X \geq 5) \leq 1/5 = 0.20$. Markov on $X^2$ gives $P(X^2 \geq 25) \leq 3/25 = 0.12$. The variance is $\text{Var}(X) = 3 - 1 = 2$. Cantelli's inequality says $P(X - \mu \geq t) \leq \sigma^2/(\sigma^2 + t^2)$. Here $\mu = 1$, $\sigma^2 = 2$, $t = 4$, so $P(X \geq 5) \leq 2/(2 + 16) = 1/9 \approx 0.111$. This beats both Markov bounds, and as we will show, it is the best possible. **Approach:** We solve the dual of the moment problem -- find the lowest-lying quadratic that dominates the indicator $\mathbf{1}_{\{x \geq 5\}}$ on $[0, \infty)$ -- and then verify sharpness by exhibiting an extremal distribution. **Formal Solution:** For any function $g(x) \geq \mathbf{1}_{\{x \geq 5\}}$ on $[0, \infty)$, we have $P(X \geq 5) \leq E[g(X)]$. Restrict to quadratics of the form $g(x) = c(x - x_0)^2$ with $c > 0$ and $x_0 < 5$. A square is never negative, so $g \geq 0$ everywhere. Setting $g(5) = 1$ gives $c = 1/(5 - x_0)^2$, and because $x_0 < 5$, $g$ is increasing past $5$, so $g(x) \geq 1$ for all $x \geq 5$. Thus $g$ dominates the indicator. (This step is just Markov applied to $(X - x_0)^2$.) Then the bound becomes: $$P(X \geq 5) \leq E[g(X)] = \frac{E[(X - x_0)^2]}{(5 - x_0)^2} = \frac{x_0^2 - 2x_0 + 3}{(5 - x_0)^2}$$ Minimize over $x_0$. Setting the derivative to zero: $$\frac{d}{dx_0}\left[\frac{x_0^2 - 2x_0 + 3}{(5 - x_0)^2}\right] = \frac{(2x_0 - 2)(5 - x_0) + 2(x_0^2 - 2x_0 + 3)}{(5 - x_0)^3} = \frac{8x_0 - 4}{(5 - x_0)^3} = 0$$ This gives $x_0 = 1/2$, and the minimum value is: $$\frac{(1/4) - 1 + 3}{(5 - 1/2)^2} = \frac{9/4}{81/4} = \frac{1}{9}$$ This is exactly Cantelli's inequality applied with $\mu = 1$, $\sigma^2 = 2$, $t = 4$. **Extremal distribution:** Place mass at $x_0 = 1/2$ and at $x = 5$. Let $P(X = 5) = p$ and $P(X = 1/2) = 1 - p$. The moment constraints pin down $p$: - $E[X] = \frac{1-p}{2} + 5p = \frac{1}{2} + \frac{9p}{2} = 1 \implies p = \frac{1}{9}$ - Check: $E[X^2] = \frac{1 - 1/9}{4} + 25 \cdot \frac{1}{9} = \frac{2}{9} + \frac{25}{9} = 3$ \checkmark This distribution satisfies all constraints and achieves $P(X \geq 5) = 1/9$, so the bound is tight. **Answer:** The tightest upper bound is $P(X \geq 5) \leq \dfrac{1}{9}$, obtained via Cantelli's (one-sided Chebyshev) inequality. It is achieved by the two-point distribution $P(X = 1/2) = 8/9$, $P(X = 5) = 1/9$. --- Total Score of a Random Partitioning Game [medium, Combinatorics] --- https://quantvault.org/problem-146-total-score-of-a-random-partitioning-game.html Q: You start with a single set containing $n$ items. At each step, you pick one of the current sets (of size $k \geq 2$) and randomly split it into two non-empty subsets of sizes $k_1$ and $k_2$ (where $k_1 + k_2 = k$). You score $k_1 \times k_2$ points for that split. You keep going until every set is a singleton. What is the total score at the end of the game? Show that the answer is the same no matter how the random partitions happen to fall. A: **How to Think About It:** The scoring rule $k_1 \times k_2$ looks arbitrary at first, but it is actually counting something natural. When you split a set of size $k$ into pieces of size $k_1$ and $k_2$, how many pairs of items just got separated? Exactly $k_1 \times k_2$ -- one item from each side. That realization turns a complicated random process into a simple bookkeeping exercise. Every pair of items starts together and ends apart, and each pair gets separated exactly once. So the total score is just the total number of pairs. **Quick Estimate:** For $n = 4$, there are $\binom{4}{2} = 6$ pairs. Try a specific partition sequence: split $\{1,2,3,4\}$ into $\{1,2\}$ and $\{3,4\}$ -- that scores $2 \times 2 = 4$. Then split $\{1,2\}$ into singletons -- scores $1 \times 1 = 1$. Then split $\{3,4\}$ -- scores $1 \times 1 = 1$. Total: $4 + 1 + 1 = 6$. Now try a different sequence: split $\{1,2,3,4\}$ into $\{1\}$ and $\{2,3,4\}$ -- scores $1 \times 3 = 3$. Then split $\{2,3,4\}$ into $\{2\}$ and $\{3,4\}$ -- scores $1 \times 2 = 2$. Then split $\{3,4\}$ -- scores $1$. Total: $3 + 2 + 1 = 6$. Same answer both times. For general $n$, we expect $\binom{n}{2}$. **Approach:** Use an indicator variable argument -- track each pair of items separately and show each pair contributes exactly 1 to the total score. **Formal Solution:** For each pair $(i, j)$ with $i < j$, define the indicator: $$X_{ij} = \begin{cases} 1 & \text{at the step when } i \text{ and } j \text{ are first placed in different subsets} \\ 0 & \text{otherwise} \end{cases}$$ The key observation is that the score $k_1 \times k_2$ from splitting a set of size $k$ into pieces of size $k_1$ and $k_2$ equals exactly the number of pairs $(i,j)$ that are separated by that split. So the total score across all steps is: $$\text{Total} = \sum_{\text{all steps}} k_1 k_2 = \sum_{i < j} X_{ij}$$ Now, every pair of items starts in the same set (the original set of $n$ items) and ends in different sets (singletons). At some step along the way, each pair must be separated for the first time. This happens exactly once per pair. Therefore $X_{ij} = 1$ for every pair $(i,j)$, and: $$\text{Total} = \sum_{i < j} 1 = \binom{n}{2} = \frac{n(n-1)}{2}$$ This holds for every possible sequence of partitions -- the randomness does not matter. **Answer:** The total score is always $\binom{n}{2} = \dfrac{n(n-1)}{2}$, regardless of how the partitions are chosen. The result is deterministic because each of the $\binom{n}{2}$ pairs contributes exactly 1 point at the moment it is separated. --- Unbiased Coin from a Biased Coin [medium, Expectation] --- https://quantvault.org/problem-148-unbiased-coin-from-a-biased-coin.html Q: You have a coin whose probability of landing heads is some unknown $p \in (0,1)$. You don't know $p$, and you can't measure it. Design a procedure that uses only this biased coin to produce outcomes that are exactly fair -- each outcome should have probability exactly $\frac{1}{2}$, regardless of the value of $p$. Once you have a procedure, derive the expected number of coin flips needed to generate one fair outcome, as a function of $p$. A: **How to Think About It:** The challenge is that you don't know $p$, so you can't "correct" for the bias directly. You need a trick that makes the bias cancel out. The key observation: if you flip the coin twice, the probability of getting HT is $p(1-p)$, and the probability of getting TH is $(1-p)p$. Those are identical -- the unknown bias appears symmetrically and cancels. So any time you see HT vs TH, you are looking at two equally likely events, regardless of $p$. That is your source of fairness. **Key Insight:** Symmetry kills the bias. The pair HT and the pair TH always have the same probability, so mapping one to "heads" and the other to "tails" gives you a perfect fair coin. **The Method (Von Neumann's Trick):** 1. Flip the coin twice. 2. If the result is HT, output "Heads." 3. If the result is TH, output "Tails." 4. If the result is HH or TT, discard both flips and go back to step 1. **Why it works:** The four possible outcomes of two flips have probabilities: $$P(HH) = p^2, \quad P(HT) = p(1-p), \quad P(TH) = (1-p)p, \quad P(TT) = (1-p)^2$$ Since $P(HT) = P(TH)$, conditioning on a "usable" pair (one where we don't discard) gives: $$P(\text{output Heads} \mid \text{usable}) = \frac{p(1-p)}{p(1-p) + (1-p)p} = \frac{1}{2}$$ This holds for every $p \in (0,1)$. The bias cancels exactly. **Expected Number of Flips:** The probability of getting a usable pair on any given round is: $$P(\text{usable}) = P(HT) + P(TH) = 2p(1-p)$$ Each round that fails (HH or TT) is independent, so the number of rounds until the first usable pair is geometric with success probability $2p(1-p)$. Each round costs 2 flips, so: $$E[\text{flips}] = \frac{2}{2p(1-p)} = \frac{1}{p(1-p)}$$ **Quick Estimate:** For a mildly biased coin with $p = 0.6$: $E[\text{flips}] = 1/(0.6 \times 0.4) = 1/0.24 \approx 4.2$. For a heavily biased coin with $p = 0.9$: $E[\text{flips}] = 1/(0.9 \times 0.1) \approx 11.1$. The closer $p$ is to 0 or 1, the more flips you waste on same-outcome pairs. At $p = 0.5$ (already fair), you need $E = 4$ flips -- which is the best case, and also the least useful since the coin is already fair. **Practical Considerations:** - The method is simple but wasteful for extreme biases. When $p$ is near 0 or 1, almost all pairs are discarded. - Peres (1992) showed you can recycle the discarded pairs: apply the same trick recursively to the sequence of discards. This extracts more fair bits per flip, approaching the information-theoretic limit of $H(p) = -p\log_2 p - (1-p)\log_2(1-p)$ fair bits per biased flip. - In practice, if you know $p$ approximately, there are more efficient schemes. Von Neumann's trick is the go-to when $p$ is truly unknown. **Answer:** Flip the coin in pairs: HT maps to Heads, TH maps to Tails, and same-outcome pairs are discarded. This produces an exactly fair coin for any $p \in (0,1)$. The expected number of flips per fair outcome is $\frac{1}{p(1-p)}$. --- Unknown Die with Uniform Sum [easy, Probability] --- https://quantvault.org/problem-150-unknown-die-with-uniform-sum.html Q: Alan has two fair 6-sided dice -- each of the six faces of each die is equally likely. One is a standard die with faces labeled $1$ through $6$. The other die has unknown face values -- they need not be the integers $1$-$6$ and may be repeated -- but all values are positive integers. Whenever Alan rolls both dice together, each integer sum from $2$ to $13$ (inclusive) is equally likely. What is the sum of all six face values on the unknown die? A: **How to Think About It:** We don't know the individual faces of the mystery die, but we can pin down their SUM using only expected values: the sum of a die's faces equals six times its mean. The condition that the total is uniform on $\{2,\dots,13\}$ fixes $E[\text{sum}]$, and linearity of expectation then fixes $E[\text{mystery die}]$. **Quick Estimate:** A uniform distribution on $\{2,\dots,13\}$ has mean $\frac{2+13}{2}=7.5$. A standard die has mean $3.5$. So the mystery die must have mean $7.5-3.5=4$, and with $6$ equally-likely faces its face-sum is $6\times4=24$. **Approach:** Both dice are fair, so each die's mean is the plain average of its six faces and the face-sum is $6$ times the mean. That turns the question into a one-line application of linearity of expectation -- no need to identify the faces themselves. We then confirm separately that a die meeting the constraint really exists, which is what licenses the expectation argument in the first place. **Formal Solution:** Let $D_1$ be the standard die and $D_2$ the mystery die, with $S=D_1+D_2$ uniform on $\{2,\dots,13\}$. Then $$E[S]=\frac{2+13}{2}=\frac{15}{2},\qquad E[D_1]=\frac{1+2+3+4+5+6}{6}=\frac{7}{2},$$ so by linearity $E[D_2]=E[S]-E[D_1]=\frac{15}{2}-\frac{7}{2}=4$. Since $D_2$ has six equally-likely faces, the sum of its face values is $$6\,E[D_2]=6\times4=24.$$ **Does such a die exist?** Yes -- and the expected-value argument is only valid because it does. Matching generating functions, $(x+\cdots+x^6)\,G_2(x)=3(x^2+\cdots+x^{13})$ forces $G_2(x)=3x+3x^7$: the mystery die has three faces showing $1$ and three showing $7$, i.e. $\{1,1,1,7,7,7\}$. Check: the mystery die is equally likely $1$ or $7$, so sums $2$–$7$ (mystery $=1$) and $8$–$13$ (mystery $=7$) each occur with probability $\tfrac{1}{12}$ -- uniform. Its face-sum is $3(1)+3(7)=24$. **Answer:** The sum of all six face values on the mystery die is $\mathbf{24}$. --- Variance and Expectation Inequality [easy, Random Variables] --- https://quantvault.org/problem-152-variance-and-expectation-inequality.html Q: Which is larger, $E[X^2]$ or $(E[X])^2$? Prove your answer and state precisely when they are equal. A: **How to Think About It:** This is one of the most fundamental inequalities in probability. The answer comes straight from the definition of variance: $\text{Var}(X) = E[X^2] - (E[X])^2$. Since variance is always non-negative, $E[X^2]$ must be at least as large as $(E[X])^2$. You can also see this as a special case of Jensen's inequality applied to the convex function $f(x) = x^2$. **Approach:** Use the non-negativity of variance and characterize the equality case. **Formal Solution:** By definition of variance: $$\text{Var}(X) = E[X^2] - (E[X])^2$$ Since variance is non-negative for any random variable (it is the expected squared deviation from the mean): $$\text{Var}(X) \geq 0$$ Therefore: $$E[X^2] \geq (E[X])^2$$ **Alternative proof via Jensen's inequality:** The function $f(x) = x^2$ is convex (its second derivative is $2 > 0$). Jensen's inequality states that for any convex function $f$ and random variable $X$: $$E[f(X)] \geq f(E[X])$$ Applying this with $f(x) = x^2$: $$E[X^2] \geq (E[X])^2$$ **Equality condition:** $E[X^2] = (E[X])^2$ if and only if $\text{Var}(X) = 0$, which holds if and only if $X$ is constant almost surely -- meaning $P(X = c) = 1$ for some constant $c$. To see why: $\text{Var}(X) = E[(X - \mu)^2] = 0$ requires $(X - \mu)^2 = 0$ almost surely (since a non-negative random variable has zero expectation only if it is zero a.s.), which means $X = \mu$ almost surely. **Answer:** $E[X^2] \geq (E[X])^2$ always, with equality if and only if $X$ is constant almost surely. --- Wallet Bid: Conditional Expected Total [medium, Expectation] --- https://quantvault.org/problem-153-wallet-bid-conditional-expected-total.html Q: $n$ people each have an amount of money in their wallets, where each amount is independently drawn from $U(0,1)$. The person with the most money wins everyone's money. You are person 1. You see $\$x_1$ in your wallet and are told that you won (i.e., your amount was the largest). What is your estimate of the total amount of money across all $n$ wallets? More precisely, find: $$E\left[\sum_{i=1}^n X_i \;\middle|\; X_1 = x_1,\; X_1 = \max(X_1, \ldots, X_n)\right]$$ Report the numerical answer when $x_1 = 0.75$ and $n = 15$. A: **How to Think About It:** You know two things: your amount is $x_1$, and it is the largest. Conditioning on being the maximum shrinks the support of every other wallet from $U(0,1)$ to $U(0, x_1)$. Each other person's amount is uniformly distributed between 0 and your amount. So the expected total is your amount plus $(n-1)$ copies of the expected value of $U(0, x_1)$, which is $x_1/2$. **Quick Estimate:** With $x_1 = 0.75$ and $n = 15$: your contribution is $0.75$, and each of the other 14 people contributes on average $0.75/2 = 0.375$. Total: $0.75 + 14 \times 0.375 = 0.75 + 5.25 = 6.0$. **Approach:** Derive the conditional distribution of $X_i \mid X_1 = x_1, X_1 = \max$ and compute the conditional expectation. **Formal Solution:** Let $X_1, X_2, \ldots, X_n \overset{\text{iid}}{\sim} U(0,1)$ and let $T = X_1 + X_2 + \cdots + X_n$. We want: $$E[T \mid X_1 = x_1, X_1 = \max_i X_i]$$ **Step 1: Conditional distribution of each $X_i$ for $i \geq 2$.** Given $X_1 = x_1$ and $X_1 \geq X_i$ for all $i$, each $X_i$ (for $i \geq 2$) must satisfy $0 \leq X_i \leq x_1$. Since $X_i \sim U(0,1)$ conditioned on $X_i \leq x_1$, the conditional distribution is: $$X_i \mid X_i \leq x_1 \sim U(0, x_1)$$ This follows because for $U(0,1)$, conditioning on $X_i \leq x_1$ gives a uniform distribution on $[0, x_1]$. **Step 2: Conditional independence.** Since the $X_i$'s are originally independent, and we condition each on $X_i \leq x_1$ (with $x_1$ fixed), the conditional variables $X_2, \ldots, X_n \mid X_1 = x_1, X_1 = \max$ are still independent, each $U(0, x_1)$. **Step 3: Compute the expectation.** $$E[X_i \mid X_i \leq x_1] = \frac{x_1}{2} \quad \text{for } i = 2, \ldots, n$$ $$E[T \mid X_1 = x_1, X_1 = \max] = x_1 + (n-1) \cdot \frac{x_1}{2} = x_1\left(1 + \frac{n-1}{2}\right) = \frac{(n+1)x_1}{2}$$ **Step 4: Compare with the unconditional case.** If you only knew $X_1 = x_1$ but did NOT know you were the maximum: $$E[T \mid X_1 = x_1] = x_1 + (n-1) \cdot \frac{1}{2} = x_1 + \frac{n-1}{2}$$ The conditional-on-winning estimate $\frac{(n+1)x_1}{2}$ is always less than the unconditional estimate $x_1 + \frac{n-1}{2}$ (for $x_1 < 1$). This makes sense: knowing you have the maximum tells you everyone else has less than $x_1$, pulling down their expected contributions. **Step 5: Plug in numbers.** With $x_1 = 0.75$ and $n = 15$: $$E[T] = \frac{(15+1)(0.75)}{2} = \frac{16 \times 0.75}{2} = \frac{12}{2} = 6$$ **Answer:** The expected total is $\dfrac{(n+1)x_1}{2}$. For $x_1 = 0.75$ and $n = 15$, the answer is $\mathbf{6}$. --- Weekend Rain Bounds via Frechet-Hoeffding [medium, Probability] --- https://quantvault.org/problem-154-weekend-rain-bounds-via-frechet-hoeffding.html Q: Let $A$ be the event "rain on Saturday" with $P(A) = p$, and let $B$ be the event "rain on Sunday" with $P(B) = q$. You know the marginal probabilities but nothing about the joint distribution. 1. Using the Frechet-Hoeffding bounds, determine the tight range for $P(A \cap B)$. 2. Suppose you are told that "rain usually lasts at least two days." Without assuming independence, argue whether $P(A \cap B)$ tends toward the upper or lower end of that range, and explain why. 3. Now suppose instead that "rain rarely lasts more than one day." Argue the direction of $P(A \cap B)$ within the range and explain your reasoning. A: **How to Think About It:** This is a classic question about what you can say about a joint probability when you only know the marginals. In practice, this comes up constantly -- you know the probability of two individual events but the dependence structure is unknown. The Frechet-Hoeffding bounds give you the tightest possible interval for $P(A \cap B)$ over all possible couplings of $A$ and $B$. The qualitative parts test whether you understand what positive and negative dependence mean in plain terms. **Quick Estimate:** Take $p = 0.4$, $q = 0.3$. Independence gives $P(A \cap B) = 0.12$. The maximum possible overlap is $\min(0.4, 0.3) = 0.3$ (Sunday rain implies Saturday rain). The minimum is $\max(0.4 + 0.3 - 1, 0) = \max(-0.3, 0) = 0$. So for these marginals, $P(A \cap B) \in [0, 0.3]$, with independence sitting at $0.12$. Positive dependence pushes toward $0.3$; negative dependence pushes toward $0$. **Approach:** Apply inclusion-exclusion and the constraint that all probabilities lie in $[0,1]$. **Formal Solution:** **Part 1: Frechet-Hoeffding bounds.** We need two constraints on $P(A \cap B)$: - *Upper bound:* Since $A \cap B \subseteq A$ and $A \cap B \subseteq B$, we have $P(A \cap B) \leq \min(p, q)$. - *Lower bound:* By inclusion-exclusion, $P(A \cup B) = p + q - P(A \cap B)$. Since $P(A \cup B) \leq 1$, we get $P(A \cap B) \geq p + q - 1$. Also $P(A \cap B) \geq 0$. Combining: $P(A \cap B) \geq \max(p + q - 1, 0)$. Both bounds are tight -- they are achieved by specific joint distributions: - The upper bound $\min(p, q)$ is achieved when one event is a subset of the other (maximal positive dependence). - The lower bound $\max(p + q - 1, 0)$ is achieved when the events are as "anti-aligned" as possible (maximal negative dependence, or mutual exclusivity when $p + q \leq 1$). So the tight range is: $$\max(p + q - 1, \, 0) \;\leq\; P(A \cap B) \;\leq\; \min(p, q)$$ **Part 2: "Rain usually lasts at least two days."** This statement means that if it rains on one day, it very likely rains on the adjacent day as well. In probabilistic terms, this is strong positive dependence: $P(B \mid A)$ is high (close to 1), and similarly $P(A \mid B)$ is high. Since $P(A \cap B) = P(B \mid A) \cdot p$, a high conditional probability pushes $P(A \cap B)$ upward. In the extreme case, rain on one day guarantees rain on the other, giving $P(A \cap B) = \min(p, q)$, the upper Frechet bound. So $P(A \cap B)$ tends toward the **upper end** of the range, near $\min(p, q)$. **Part 3: "Rain rarely lasts more than one day."** This means rain events tend to be isolated -- if it rains on Saturday, it is unlikely to rain on Sunday, and vice versa. This is negative dependence: $P(B \mid A)$ is small relative to $P(B)$. This pushes $P(A \cap B)$ downward. In the extreme case where rain never lasts two consecutive days, $A$ and $B$ are mutually exclusive (if $p + q \leq 1$), giving $P(A \cap B) = 0$. More generally, $P(A \cap B) \approx \max(p + q - 1, 0)$. So $P(A \cap B)$ tends toward the **lower end** of the range, near $\max(p + q - 1, 0)$. **Answer:** $$\max(p + q - 1, \, 0) \;\leq\; P(A \cap B) \;\leq\; \min(p, q)$$ "Rain lasts two days" implies strong positive dependence, pushing $P(A \cap B)$ toward the upper bound $\min(p, q)$. "Rain rarely lasts more than one day" implies negative dependence, pushing $P(A \cap B)$ toward the lower bound $\max(p + q - 1, 0)$. --- Absorption Probability on a Finite Random Walk [hard, Stochastic Process] --- https://quantvault.org/problem-155-absorption-probability-on-a-finite-random-walk.html Q: You start at position 17 on an integer line with absorbing barriers at positions 0 and 100. At each step, you move left or right with equal probability $p = \frac{1}{2}$. What is the probability that you return to position 17 at least once before being absorbed at 0 or 100? A: **How to Think About It:** This is a symmetric random walk on $\{0, 1, \ldots, 100\}$ with absorbing barriers at 0 and 100. The walker starts at 17 and we want the probability of ever revisiting 17 before absorption. On the infinite line, a symmetric 1D walk is recurrent -- it returns to every state with probability 1. But absorbing barriers change everything: the walk will eventually hit 0 or 100, and it might do so without ever coming back to 17. **Quick Estimate:** The probability of being absorbed at 0 (rather than 100) from position 17 is $83/100 = 0.83$ by the classical gambler's ruin. Starting at 17, the walk must first step to 16 or 18. From either neighbor, there is some probability the walk gets absorbed before revisiting 17. The probability of NOT returning should be related to $1/\min(17, 83)$. So roughly $P(\text{return}) \approx 1 - 1/17 \approx 0.94$. **Approach:** Use the connection between return probability and absorption probability via the gambler's ruin. **Formal Solution:** Let $r$ be the probability of returning to position 17 before absorption. Consider the first step: from 17, you move to 16 or 18 each with probability $1/2$. From position 16, let $\alpha$ be the probability of reaching 17 before reaching 0. By the gambler's ruin on $\{0, \ldots, 17\}$ with a symmetric walk, $\alpha = 16/17$. From position 18, let $\beta$ be the probability of reaching 17 before reaching 100. By the gambler's ruin on $\{17, \ldots, 100\}$ (now position 18 is 1 step from the left boundary 17, with the right boundary 100 being 82 steps away), $\beta = 82/83$. But we need the probability of reaching 17 before being absorbed at either barrier. From position 16: - The walk could hit 17 first (success) or hit 0 first (absorbed, failure). Since 17 and 0 are the relevant boundaries when starting at 16, $P(\text{hit 17 before 0} \mid \text{start at 16}) = 16/17$. - However, the walk could also be absorbed at 100 from position 16 without revisiting 17 -- but to reach 100 from 16 you must pass through 17. So the only way to be absorbed without revisiting 17, starting from 16, is to be absorbed at 0. Thus $P(\text{revisit 17} \mid \text{first step to 16}) = 16/17$. Similarly, from position 18, to reach 0 you must pass through 17, so the only way to be absorbed without revisiting 17 is to be absorbed at 100. $P(\text{revisit 17} \mid \text{first step to 18}) = P(\text{hit 17 before 100} \mid \text{start at 18}) = 82/83$. Combining: $$r = \frac{1}{2} \cdot \frac{16}{17} + \frac{1}{2} \cdot \frac{82}{83} = \frac{1}{2}\left(\frac{16}{17} + \frac{82}{83}\right)$$ $$= \frac{1}{2}\left(\frac{16 \cdot 83 + 82 \cdot 17}{17 \cdot 83}\right) = \frac{1}{2}\left(\frac{1328 + 1394}{1411}\right) = \frac{1}{2} \cdot \frac{2722}{1411} = \frac{1361}{1411}$$ Simplifying: $\gcd(1361, 1411) = 1$ (both are prime -- $1361$ and $1411$ are indeed prime), so: $$r = \frac{1361}{1411} \approx 0.9646$$ **Answer:** The probability of returning to position 17 before absorption is: $$P(\text{return to 17}) = \frac{1}{2}\left(\frac{16}{17} + \frac{82}{83}\right) = \frac{1361}{1411} \approx 0.9646$$ --- Bayesian Coin Selection and Sequential Betting [medium, Probability] --- https://quantvault.org/problem-156-bayesian-coin-selection-and-sequential-betting.html Q: You have two coins. Coin 1 has $P(H) = 2/3$ and Coin 2 has $P(H) = 1/3$. One coin is chosen uniformly at random and flipped 3 times, producing 3 heads. (a) What is the probability that the next flip (with the same coin) is heads? (b) Counterparty A offers you a bet: if the next flip is heads, you win $\$2$; if tails, you lose $\$2$. Should you take it? If offered repeatedly (always using the same coin), should you keep taking it? (c) Counterparty B offers a different bet: if heads, you lose $\$2$; if tails, you win $\$3$. Should you take it? (d) If you can take bet B at most once and bet A up to 100 times, what is your optimal strategy? A: **How to Think About It:** You observed 3 heads in a row from a coin that is either $2/3$-biased or $1/3$-biased. Three heads is $8\times$ more likely under Coin 1 than Coin 2, so the posterior heavily favors Coin 1. Once you have the posterior, every bet question is just an expected value calculation. The subtle point in part (d) is whether the negative-EV bet B can be worth taking as a hedge or information play -- but since bet B's outcome does not change the coin or affect future bet A payoffs, it cannot. **Quick Estimate:** Posterior odds for Coin 1: $(2/3)^3 / (1/3)^3 = 8$. So $P(\text{Coin 1} \mid HHH) = 8/9 \approx 0.889$. Next-flip heads probability: roughly $0.889 \times 0.667 + 0.111 \times 0.333 \approx 0.593 + 0.037 = 0.630$. This is above $0.5$, so a symmetric bet on heads has positive EV. **Approach:** Bayes' theorem for the posterior, then law of total probability for part (a), and direct EV calculations for the bets. **Formal Solution:** **(a) Posterior and next-flip probability:** Prior: $P(C_1) = P(C_2) = 1/2$. $$P(C_1 \mid HHH) = \frac{(2/3)^3 \cdot (1/2)}{(2/3)^3 \cdot (1/2) + (1/3)^3 \cdot (1/2)} = \frac{8/27}{8/27 + 1/27} = \frac{8}{9}$$ $$P(\text{next } H) = \frac{8}{9} \cdot \frac{2}{3} + \frac{1}{9} \cdot \frac{1}{3} = \frac{16}{27} + \frac{1}{27} = \frac{17}{27} \approx 0.630$$ **(b) Bet A: win $\$2$ if heads, lose $\$2$ if tails.** $$EV_A = \frac{17}{27} \cdot 2 + \frac{10}{27} \cdot (-2) = \frac{34 - 20}{27} = \frac{14}{27} \approx \$0.519$$ Positive EV -- **take the bet**. If offered repeatedly with the same coin, the edge persists. In fact, it gets stronger: each additional flip updates the posterior further. If you keep seeing heads, you become even more confident it is Coin 1, and $P(H)$ increases. If you see a tail, the posterior shifts toward Coin 2, but $P(H)$ stays above $1/2$ unless you see many tails (because the posterior already strongly favors Coin 1). The expected value of each subsequent bet A remains positive, so **take it every time**. **(c) Bet B: lose $\$2$ if heads, win $\$3$ if tails.** $$EV_B = \frac{17}{27} \cdot (-2) + \frac{10}{27} \cdot 3 = \frac{-34 + 30}{27} = \frac{-4}{27} \approx -\$0.148$$ Negative EV -- **do not take bet B**. **(d) Optimal strategy with both bets available (B at most once, A up to 100 times):** Bet A has positive EV ($\$0.519$ per play). Bet B has negative EV ($-\$0.148$). Since the bets are on separate flips (or at least independent conditional on the coin), taking bet B does not provide any information or hedging benefit that affects bet A's value. They are additive. Optimal strategy: **take bet A all 100 times, skip bet B**. Expected profit: $100 \times \frac{14}{27} \approx \$51.85$. **Answer:** (a) $P(\text{next } H) = 17/27 \approx 0.630$. (b) Take bet A -- EV is $+14/27 \approx \$0.52$ per play; take it repeatedly. (c) Do not take bet B -- EV is $-4/27 \approx -\$0.15$. (d) Take all 100 bet A's, skip bet B. Expected profit $\approx \$51.85$. --- Breaking Even in a Coin Game [medium, Probability] --- https://quantvault.org/problem-160-breaking-even-in-a-coin-game.html Q: Lewis and Lilly are playing a game with a fair coin. They each start with $\$5$. The coin will be tossed up to $10$ times. On each toss, if it lands heads, Lewis pays Lilly $\$1$; if tails, Lilly pays Lewis $\$1$. The game ends early if either player's balance hits $\$0$. Otherwise, it continues for all $10$ tosses. What is the probability that both players finish the game with exactly $\$5$ -- i.e., they break even? A: **How to Think About It:** This is a symmetric random walk on $\{0, 1, \ldots, 10\}$ with absorbing barriers at $0$ and $10$, starting at $5$. Lewis's fortune is the walk position. We want the probability that the walk returns to $5$ after exactly $10$ steps without ever hitting $0$ or $10$ along the way. The first instinct: count the paths of length $10$ that end at $5$, subtract the ones that touch a barrier. **Quick Estimate:** Total outcomes: $2^{10} = 1024$. Paths ending at $5$ need exactly $5$ heads and $5$ tails: $\binom{10}{5} = 252$ such paths. Now, how many of those hit $0$ or $10$? The walk starts at $5$ and moves $\pm 1$ each step. To reach $0$ or $10$ from $5$, you need $5$ consecutive moves in the same direction. That is extremely constrained -- the only way is $5$ straight heads or $5$ straight tails right from the start (with the opposite $5$ filling the rest). So we expect very few bad paths -- maybe just $2$. That gives roughly $250/1024 \approx 0.244$, about a $1$ in $4$ chance. **Approach:** We count the sequences of $10$ fair coin flips with exactly $5$ heads that never cause Lewis's or Lilly's balance to hit $\$0$, then divide by $2^{10}$. **Formal Solution:** Let $H_k$ denote the number of heads in the first $k$ tosses. Lewis's fortune after $k$ tosses is: $$S_k = 5 - H_k + (k - H_k) = 5 + k - 2H_k$$ The game ends if $S_k = 0$ (Lewis broke) or $S_k = 10$ (Lilly broke) at any step $k \leq 10$. *When can Lewis hit $0$?* We need $5 + k - 2h = 0$, i.e., $h = (5 + k)/2$. This requires $k$ odd. Checking each odd $k$ with the constraint that we need exactly $5$ heads total in $10$ tosses: - $k = 1$: $h = 3$ -- impossible (only $1$ toss so far) - $k = 3$: $h = 4$ -- impossible (only $3$ tosses) - $k = 5$: $h = 5$ -- possible. All $5$ heads come in the first $5$ tosses: HHHHHTTTTT - $k = 7$: $h = 6$ -- impossible (only $5$ heads total) - $k = 9$: $h = 7$ -- impossible *When can Lilly hit $0$?* We need $S_k = 10$, i.e., $h = (k - 5)/2$. Again $k$ must be odd and $k \geq 5$: - $k = 5$: $h = 0$ -- possible. All $5$ tails come first: TTTTTHHHHH - $k = 7$: $h = 1$ -- need $4$ heads in last $3$ tosses, impossible - $k = 9$: $h = 2$ -- need $3$ heads in last $1$ toss, impossible So exactly $2$ sequences out of $\binom{10}{5} = 252$ hit a barrier. The valid count is $252 - 2 = 250$. **Answer:** $$P(\text{break even}) = \frac{250}{1024} = \frac{125}{512} \approx 0.2441$$ --- Comparing Pi to the e and e to the Pi [easy, Brain Teaser] --- https://quantvault.org/problems.html?id=164 Q: Which is larger: $\pi^e$ or $e^{\pi}$? Prove your answer. A: **How to Think About It:** You have two expressions with mixed bases and exponents. Direct computation is messy -- $\pi^e \approx 22.46$ and $e^{\pi} \approx 23.14$ are close enough that you cannot eyeball it. The right move is to reduce the comparison to something cleaner. Since the logarithm is strictly increasing, comparing $\pi^e$ vs. $e^{\pi}$ is equivalent to comparing $\ln(\pi^e) = e \ln \pi$ vs. $\ln(e^{\pi}) = \pi$. Dividing both sides by $e\pi$, this becomes: is $\frac{\ln \pi}{\pi}$ greater or less than $\frac{\ln e}{e} = \frac{1}{e}$? **Quick Estimate:** $\ln \pi \approx 1.145$, so $\ln(\pi)/\pi \approx 1.145/3.14 \approx 0.365$. And $1/e \approx 0.368$. Very close, but $1/e$ is slightly larger, suggesting $e^{\pi} > \pi^e$. **Approach:** Study the function $f(x) = \ln(x)/x$ and find its maximum. **Formal Solution:** Define $f(x) = \dfrac{\ln x}{x}$ for $x > 0$. Take the derivative: $$f'(x) = \frac{1 - \ln x}{x^2}$$ Setting $f'(x) = 0$ gives $\ln x = 1$, i.e., $x = e$. Since $f'(x) > 0$ for $x e$, the function $f$ has a global maximum at $x = e$. Since $\pi > e$, we have $\pi \neq e$ and: $$f(\pi) = \frac{\ln \pi}{\pi} 0$: $$e \ln \pi 0$ with $x \neq e$. The function $f(x) = \ln(x)/x$ is uniquely maximized at $x = e$, so $e$ is the "best base" in the sense that $e^{1/e} \geq x^{1/x}$ for all $x > 0$. --- Dice Sum vs. Coin Flip Count [medium, Probability] --- https://quantvault.org/problem-170-dice-sum-vs-coin-flip-count.html Q: You roll a fair six-sided die 100 times and let $X$ be the total. Separately, you flip a fair coin 600 times and let $Y$ be the number of heads. What is the probability that $X > Y$? A: **How to Think About It:** Before touching any formulas, get a feel for the two numbers you are comparing. A single die averages 3.5, so 100 dice average 350. A fair coin in 600 flips gives 300 heads on average. So on average the dice sum beats the head count by 50. The question is whether random fluctuations can close that 50-point gap. Both quantities are sums of independent trials, so CLT applies and each is roughly normal. You just need to check how many standard deviations that 50-point cushion is worth. **Quick Estimate:** The dice sum $X$ has mean 350 and variance $100 \times 35/12 \approx 291.7$, so $\sigma_X \approx 17.1$. The head count $Y$ has mean 300 and variance $600 \times 0.25 = 150$, so $\sigma_Y \approx 12.2$. The difference $W = X - Y$ has mean 50 and variance $291.7 + 150 = 441.7$, so $\sigma_W \approx 21.0$. The gap of 50 is about $50/21.0 \approx 2.38$ standard deviations above zero. Glancing at a z-table, $\Phi(2.38) \approx 0.991$. So we expect roughly a 99% chance that the dice sum wins. That is a big cushion -- the 50-point head start is more than two sigma. **Approach:** Formalize by writing $W = X - Y$ as a sum of independent random variables, apply CLT, and read off the probability from the standard normal CDF. **Formal Solution:** Let $D_1, \dots, D_{100}$ be i.i.d. $\text{Uniform}\{1,2,3,4,5,6\}$ rolls. Each has: $$E[D_i] = \frac{7}{2}, \quad \text{Var}(D_i) = \frac{35}{12}$$ So for $X = \sum_{i=1}^{100} D_i$: $$E[X] = 350, \quad \text{Var}(X) = \frac{3500}{12} = \frac{875}{3} \approx 291.67$$ Let $Y \sim \text{Binomial}(600,\, 1/2)$: $$E[Y] = 300, \quad \text{Var}(Y) = 150$$ Since $X$ and $Y$ are independent, define $W = X - Y$: $$E[W] = 350 - 300 = 50$$ $$\text{Var}(W) = \frac{875}{3} + 150 = \frac{875 + 450}{3} = \frac{1325}{3} \approx 441.67$$ $$\sigma_W = \sqrt{\frac{1325}{3}} \approx 21.02$$ By the CLT, $W$ is approximately $N(50,\, 441.67)$. Then: $$P(X > Y) = P(W > 0) = P\!\left(Z > \frac{0 - 50}{21.02}\right) = P(Z > -2.38) = \Phi(2.38)$$ $$\approx 0.9913$$ **Answer:** The probability that the dice sum exceeds the head count is $\Phi(2.38) \approx 0.991$, or about $99.1\%$. --- Distance Between Two Random Cutpoints [medium, Probability] --- https://quantvault.org/problem-171-distance-between-two-random-cutpoints.html Q: Lewis is cutting a piece of wood of length $L$ into three pieces. He independently and uniformly at random chooses two cutpoints -- one on the left half of the wood and one on the right half. Let $X_1 \sim \text{Uniform}(0, L/2)$ be the left cutpoint and $X_2 \sim \text{Uniform}(L/2, L)$ be the right cutpoint. What is the probability that the distance between the two cutpoints is less than $L/3$? Compute the answer for $L = 2$ (though note the answer does not depend on $L$). A: **How to Think About It:** The value of $L$ cancels out -- all distances scale with $L$, so we can set $L = 1$ without loss of generality. Then $X_1 \sim \text{Uniform}(0, 1/2)$ and $X_2 \sim \text{Uniform}(1/2, 1)$, independently. We need $P(X_2 - X_1 1/2$ and $X_1 0$, so this is just $P(X_2 - X_1 < 1/3)$. **Quick Estimate:** The distance $X_2 - X_1$ has mean $E[X_2] - E[X_1] = 3/4 - 1/4 = 1/2$. The threshold is $1/3$, which is below the mean, so the probability should be less than $1/2$. The distance ranges from just above $0$ (when both cutpoints are near the midpoint) to just below $1$ (when they are at opposite ends). A rough guess: maybe $1/5$ to $1/4$. **Approach:** Condition on $X_1$ and integrate using the law of total probability. **Formal Solution:** With $L = 1$: $X_1$ has density $f_{X_1}(x) = 2$ on $(0, 1/2)$ and $X_2$ has density $f_{X_2}(y) = 2$ on $(1/2, 1)$. $$P(X_2 - X_1 < 1/3) = \int_0^{1/2} P(X_2 < 1/3 + x) \cdot 2 \, dx$$ Since $X_2 \sim \text{Uniform}(1/2, 1)$: $$P(X_2 < 1/3 + x) = \begin{cases} 0 & \text{if } 1/3 + x \leq 1/2, \text{ i.e., } x \leq 1/6 \\ 2(1/3 + x - 1/2) = 2(x - 1/6) & \text{if } 1/2 < 1/3 + x \leq 1, \text{ i.e., } 1/6 < x \leq 1/2 \end{cases}$$ Note: for $x \leq 1/2$, we have $1/3 + x \leq 5/6 < 1$, so the CDF does not cap at 1 in our range. Substituting: $$P(X_2 - X_1 < 1/3) = \int_{1/6}^{1/2} 2(x - 1/6) \cdot 2 \, dx = 4 \int_{1/6}^{1/2} \left(x - \frac{1}{6}\right) dx$$ $$= 4 \left[ \frac{(x - 1/6)^2}{2} \right]_{1/6}^{1/2} = 4 \cdot \frac{(1/2 - 1/6)^2}{2} = 4 \cdot \frac{(1/3)^2}{2} = 4 \cdot \frac{1}{18} = \frac{2}{9}$$ **Answer:** The probability is $\dfrac{2}{9}$. --- Distinct Outcomes in Dice Rolls [medium, Probability] --- https://quantvault.org/problem-172-distinct-outcomes-in-dice-rolls.html Q: A fair six-sided die is rolled six times. What is the probability of getting exactly four distinct numbers among the six outcomes? A: **How to Think About It:** Six rolls, six faces -- if you got all distinct values that would be $6!/6^6$, a birthday-problem style calculation. But we want exactly four distinct values, meaning two of the six faces never show up, and the four that do show up must cover all six rolls. The trick is counting how many ways to distribute six rolls across exactly four values so that every value appears at least once. This is a classic surjection (onto function) count, and the standard tool is inclusion-exclusion or Stirling numbers of the second kind. **Quick Estimate:** There are $\binom{6}{4} = 15$ ways to choose which four faces appear. For each choice, we need to distribute 6 rolls onto 4 bins with no bin empty. The total unrestricted ways to fill 4 bins with 6 balls is $4^6 = 4096$. By inclusion-exclusion, subtract the arrangements that miss at least one bin. Roughly, $4^6 - \binom{4}{1}3^6 + \binom{4}{2}2^6 - \binom{4}{3}1^6 = 4096 - 2916 + 384 - 4 = 1560$. So the favorable count is $15 \times 1560 = 23{,}400$ out of $6^6 = 46{,}656$. That gives $23400/46656 \approx 0.5015$. So roughly a coin flip -- makes sense, since with 6 rolls and 6 faces, getting exactly 4 distinct is the most common outcome. **Approach:** We count the favorable outcomes using the "choose then surject" method: pick which 4 faces appear, then count onto functions from 6 rolls to those 4 faces via inclusion-exclusion. **Formal Solution:** 1. **Choose the 4 faces** that appear among the six rolls: $$\binom{6}{4} = 15$$ 2. **Count surjections** from 6 rolls onto 4 chosen values. A surjection ensures each of the 4 values appears at least once. By inclusion-exclusion, the number of onto functions from an $n$-set to a $k$-set is: $$\sum_{j=0}^{k} (-1)^j \binom{k}{j}(k-j)^n$$ With $n = 6$, $k = 4$: $$\sum_{j=0}^{4} (-1)^j \binom{4}{j}(4-j)^6 = 4^6 - 4 \cdot 3^6 + 6 \cdot 2^6 - 4 \cdot 1^6 + 0$$ $$= 4096 - 4(729) + 6(64) - 4(1)$$ $$= 4096 - 2916 + 384 - 4 = 1560$$ (Equivalently, this equals $4! \cdot S(6,4)$ where $S(6,4) = 65$ is a Stirling number of the second kind.) 3. **Total favorable outcomes:** $$15 \times 1560 = 23{,}400$$ 4. **Total outcomes:** $6^6 = 46{,}656$. 5. **Probability:** $$P(\text{exactly 4 distinct}) = \frac{23{,}400}{46{,}656} = \frac{325}{648}$$ **Answer:** The probability is $\dfrac{325}{648} \approx 0.5015$. --- Expected Sum of Dice Rolls Until a Target Face [medium, Expectation] --- https://quantvault.org/problem-180-expected-sum-of-dice-rolls-until-a-target-face.html Q: You repeatedly roll a fair 6-sided die and keep a running total of every value you roll. The process stops the moment you roll a 1. What is the expected value of the running total at that point (including the final 1)? **Follow-up:** Suppose instead you stop the first time you roll a 6, or any other specific face $k \in \{1, 2, 3, 4, 5, 6\}$. Does the expected total change? A: **How to Think About It:** Each roll is either the stopping roll (you hit 1) or a non-stopping roll. The non-stopping rolls each contribute a random value drawn uniformly from $\{2, 3, 4, 5, 6\}$, and you keep rolling until you finally hit 1. So the total sum decomposes into: (sum of all the non-stopping rolls) + 1. The number of non-stopping rolls follows a geometric distribution, and the conditional expectation of each non-stopping roll is just the average of the five non-stopping faces. Multiply those together and add the final 1. **Quick Estimate:** On average, how many times do you roll before hitting 1? Each roll has a $1/6$ chance of being a 1, so you expect $5$ non-stopping rolls before the first 1 appears (the mean of a Geometric($1/6$) distribution, counting only failures). Each non-stopping roll averages $(2+3+4+5+6)/5 = 4$. So the total is roughly $5 \times 4 + 1 = 21$. **Approach:** Use the decomposition $S = (\text{sum of non-stopping rolls}) + (\text{stopping value})$ with Wald's identity. **Formal Solution:** Let $N$ be the number of rolls before the first 1 appears. Each roll independently lands on 1 with probability $1/6$, so $N \sim \text{Geometric}(1/6)$ counting failures only: $$E[N] = \frac{1 - 1/6}{1/6} = 5$$ Each of the $N$ non-stopping rolls is uniformly distributed on $\{2, 3, 4, 5, 6\}$, independent of $N$. The conditional mean of a single non-stopping roll is: $$E[X_i \mid X_i \neq 1] = \frac{2+3+4+5+6}{5} = 4$$ By Wald's identity, the expected sum of the $N$ non-stopping rolls is: $$E\left[\sum_{i=1}^{N} X_i\right] = E[N] \cdot E[X_i \mid X_i \neq 1] = 5 \times 4 = 20$$ Adding the final roll of 1: $$E[S] = 20 + 1 = 21$$ **Follow-up:** Now suppose the stopping face is $k$ instead of 1. The number of non-stopping rolls is still $\text{Geometric}(1/6)$ with $E[N] = 5$, since each face has probability $1/6$. The conditional mean of a non-stopping roll is: $$E[X_i \mid X_i \neq k] = \frac{21 - k}{5}$$ where 21 is the sum of all faces $1 + 2 + \cdots + 6$. So: $$E[S] = 5 \cdot \frac{21 - k}{5} + k = (21 - k) + k = 21$$ The expected total is always 21, regardless of which face stops the process. **Answer:** The expected sum is $\boxed{21}$, and this holds no matter which face you designate as the stopping face. The cancellation is exact: a higher stopping face contributes more on the final roll but less on average per non-stopping roll, and vice versa. The two effects cancel perfectly because every face has the same probability $1/6$. --- Generalized Monty Hall: n Doors [medium, Probability] --- https://quantvault.org/problem-185-generalized-monty-hall-n-doors.html Q: You are a contestant on a game show. There are $n \geq 3$ doors. Behind one door is a prize; the other $n - 1$ doors are empty. The prize is placed uniformly at random. You pick a door. The host -- who knows where the prize is -- then opens one of the other $n - 1$ doors, always revealing an empty one. You are now offered a chance to switch to any one of the remaining $n - 2$ unopened doors (not your original pick, not the one the host opened). (a) What is the probability of winning if you switch? (b) For $n = 8$, what is the probability you win by switching? Express as a fraction. A: **How to Think About It:** The classic Monty Hall trap is forgetting that the host's action is not random -- he always reveals an empty door. This means opening a door carries information. Your initial pick is correct with probability $1/n$. The remaining $n - 1$ probability mass was "spread" across the other doors, and the host just collapsed it down to $n - 2$ doors. Switching distributes that $1/n$ mass over $n - 2$ remaining doors, giving you a better-than-$1/n$ chance. The rule of thumb: switching is always better, regardless of $n$. **Quick Estimate:** For $n = 8$: staying wins with probability $1/8 = 12.5\%$. Switching should win more often. Quick upper bound: at most $1/(n-2) = 1/6 \approx 16.7\%$ if the whole conditional probability landed on the remaining doors. The exact answer is somewhere in that range. **Formal Solution:** To win by switching, two things must happen: 1. Your initial pick was wrong (probability $(n-1)/n$). 2. Given your pick was wrong, the prize is behind the specific door you switch to. After the host opens one empty door, there are $n - 2$ remaining doors (all non-initial, non-opened), and the prize is uniformly among them. So the conditional probability of picking it is $1/(n-2)$. These events are independent given the setup, so: $$P(\text{win by switching}) = \frac{n-1}{n} \cdot \frac{1}{n-2} = \frac{n-1}{n(n-2)}$$ **Comparison to staying:** $P(\text{win by staying}) = 1/n$. Since $\frac{n-1}{n(n-2)} = \frac{1}{n} \cdot \frac{n-1}{n-2} > \frac{1}{n}$ for all $n \geq 3$, switching is always strictly better. **For $n = 8$:** $$P(\text{win by switching}) = \frac{7}{8 \cdot 6} = \frac{7}{48}$$ **Answer:** Switching wins with probability $\dfrac{n-1}{n(n-2)}$. For $n = 8$, this is $\dfrac{7}{48} \approx 14.6\%$, compared to $12.5\%$ for staying. --- Last Roll of a Cumulative Sum Game [easy, Expectation] --- https://quantvault.org/problem-188-last-roll-of-a-cumulative-sum-game.html Q: You roll a standard fair 6-sided die repeatedly, accumulating the sum of all rolls. You stop the moment the running total reaches or exceeds 1000. Which face value is most likely to be your final roll? A: **How to Think About It:** Ask what must be true just before the last roll. If the final roll is $k$, the running sum immediately before it sat somewhere in $\{1000-k, 1000-k+1, \ldots, 999\}$ -- a window of exactly $k$ values. So bigger faces get wider "capture windows," and the question reduces to weighing those windows. The one renewal-theory fact you need: far from the start, the running sum hits any particular integer $s$ with the same probability $1/\mu = 1/3.5 = 2/7$ (the average step is $3.5$, so the walk lands on about one integer in every $3.5$). Watch the trap, though: it is the *visit* probability that is flat. The pre-terminal sum itself is **not** uniform on $\{994, \ldots, 999\}$ -- sums near $999$ are likelier launch points precisely because more faces terminate from them. **Quick Estimate:** The die averages $3.5$ per roll, so on the way to $1000$ the running sum lands on roughly $1000 / 3.5 \approx 286$ of the $1000$ integers -- about $2$ of every $7$. So for each $s$ near $1000$, $P(\text{sum ever hits } s) \approx 2/7$. The last roll is $k$ exactly when the walk stops on one of the $k$ values in $\{1000-k, \ldots, 999\}$ and then rolls $k$ (probability $1/6$): $$P(\text{last roll} = k) \approx k \cdot \frac{2}{7} \cdot \frac{1}{6} = \frac{k}{21}$$ Sanity check: $\sum_{k=1}^{6} k/21 = 21/21 = 1$. So face $6$ comes in at $6/21 = 2/7 \approx 28.6\%$, six times as likely as face $1$ at $1/21 \approx 4.8\%$. **Formal Solution:** Let $v(s)$ be the probability the running sum ever equals $s$. It satisfies $v(0) = 1$ and the renewal recursion $$v(s) = \frac{1}{6} \sum_{j=1}^{\min(6, s)} v(s - j)$$ By the renewal theorem (Blackwell), for an aperiodic bounded step, $v(s) \to 1/\mu = 2/7$ as $s \to \infty$, with exponentially fast convergence. At $s \approx 1000$ the convergence is complete for all practical purposes: exact dynamic programming gives $v(994) = v(995) = \cdots = v(999) = 0.285714\ldots$, matching $2/7$ to more than $100$ decimal places. Now decompose the event by the pre-terminal sum. The last roll equals $k$ with pre-terminal sum $s$ iff (i) the walk visits $s \le 999$, (ii) the roll taken from $s$ equals $k$, and (iii) $s + k \ge 1000$, i.e. $s \ge 1000 - k$. Conditions (i) and (ii) are independent -- the next roll does not depend on the path that reached $s$ -- and the events are disjoint across $s$ because the pre-terminal sum is unique. Hence, exactly, $$P(\text{last roll} = k) = \sum_{s=1000-k}^{999} v(s) \cdot \frac{1}{6} = k \cdot \frac{2}{7} \cdot \frac{1}{6}$$ $$\boxed{P(\text{last roll} = k) = \frac{k}{21}, \qquad k = 1, \ldots, 6}$$ (Exact DP at threshold $1000$ agrees with $k/21$ to within $10^{-138}$; a $2{,}000{,}000$-trial simulation matches every $k/21$ to within $0.001$.) One pitfall worth naming: the pre-terminal sum $S$ itself has the triangular law $P(S = s) = (s - 993)/21$ for $994 \le s \le 999$, not a uniform one -- writing $p_k = \sum_s P(S = s) \cdot \frac{1}{6}$ would double-count the termination condition. Condition on *visiting* $s$, not on stopping there. The resulting law $k/21$ is the size-biased version of the uniform die distribution: the step that carries the walk across the fixed level $1000$ is picked with probability proportional to its length. **Answer:** The most likely final roll is $\boxed{6}$, with probability $6/21 = 2/7 \approx 28.6\%$. More generally, $P(\text{last roll} = k) = k/21$: the probabilities are proportional to the face values. --- Matching Multiset of Three Dice Rolls [hard, Probability] --- https://quantvault.org/problem-191-matching-multiset-of-three-dice-rolls.html Q: You roll three standard fair 6-sided dice and observe the values on the upfaces. Then you roll all three dice again. What is the probability that the second roll produces the same multiset of values as the first roll? (The values on the second roll can appear in any order -- only the collection of numbers matters, not which die shows which number.) A: **How to Think About It:** The second roll needs to produce the same multiset of values -- the same numbers with the same multiplicities, but in any order. The key insight is that the number of favorable outcomes on the second roll depends on how many distinct values appeared on the first roll. If all three dice showed the same value, there is only one way to reproduce it. If two values appeared, there are a few permutations. If all three were distinct, there are $3! = 6$ orderings. So we condition on the number of distinct values and use the law of total probability. **Quick Estimate:** There are $6^3 = 216$ equally likely outcomes for each roll. The "best case" for matching is all distinct values, which gives 6 favorable outcomes out of 216, roughly a $1/36 \approx 2.8\%$ chance of matching conditional on that case. Since all-distinct is the most common pattern (probability $5/9$), we expect the overall probability to be somewhat above $1\%$ but well below $3\%$. A rough guess: around $2\%$. **Approach:** Condition on the number of distinct values $V_i$ ($i = 1, 2, 3$) on the first roll, compute $P(V_i)$ and $P(M \mid V_i)$ where $M$ is the matching event, then apply the law of total probability. **Formal Solution:** Let $V_i$ denote the event that exactly $i$ distinct values appear on the first roll. *Step 1: Compute $P(V_i)$.* All three dice the same ($V_1$): there are 6 such outcomes (one per face value), so $$P(V_1) = \frac{6}{216} = \frac{1}{36}$$ All three dice distinct ($V_3$): the first die has 6 options, the second has 5, the third has 4, giving $$P(V_3) = \frac{6 \cdot 5 \cdot 4}{216} = \frac{120}{216} = \frac{5}{9}$$ Exactly two distinct values ($V_2$): by complement, $$P(V_2) = 1 - \frac{1}{36} - \frac{5}{9} = \frac{36 - 1 - 20}{36} = \frac{15}{36} = \frac{5}{12}$$ *Step 2: Compute $P(M \mid V_i)$.* Each of the 216 equally likely outcomes on the second roll is equally probable. - If the first roll is $(a, a, a)$: the only matching outcome is $(a, a, a)$, so $P(M \mid V_1) = \frac{1}{216}$. - If the first roll has pattern $(a, a, b)$ with $a \neq b$: the matching outcomes are the permutations of two $a$'s and one $b$ among three dice. There are $\binom{3}{1} = 3$ such permutations (choose which die shows $b$), so $P(M \mid V_2) = \frac{3}{216} = \frac{1}{72}$. - If the first roll has pattern $(a, b, c)$ with all distinct: there are $3! = 6$ permutations of three distinct values, so $P(M \mid V_3) = \frac{6}{216} = \frac{1}{36}$. *Step 3: Law of total probability.* $$P(M) = P(M \mid V_1)P(V_1) + P(M \mid V_2)P(V_2) + P(M \mid V_3)P(V_3)$$ $$= \frac{1}{216} \cdot \frac{1}{36} + \frac{1}{72} \cdot \frac{5}{12} + \frac{1}{36} \cdot \frac{5}{9}$$ $$= \frac{1}{7776} + \frac{5}{864} + \frac{5}{324}$$ Converting to a common denominator of 7776: $$= \frac{1}{7776} + \frac{45}{7776} + \frac{120}{7776} = \frac{166}{7776} = \frac{83}{3888}$$ **Answer:** The probability that the second roll matches the multiset of the first roll is $\dfrac{83}{3888} \approx 2.14\%$. --- Maximize Red Marble Probability with Two Drawers [medium, Probability] --- https://quantvault.org/problem-192-maximize-red-marble-probability-with-two-drawers.html Q: You have 100 marbles -- 50 red and 50 blue. You must distribute all 100 marbles between two drawers, subject to two constraints: no marbles left outside, and each drawer must contain at least one marble. Once the marbles are placed, you pick one of the two drawers uniformly at random, then draw one marble uniformly at random from that drawer. How should you distribute the marbles to maximize the probability of drawing a red marble? What is that maximum probability? A: **How to Think About It:** You are choosing how to split 50 red and 50 blue marbles across two drawers to maximize the probability of drawing red. The key observation is that each drawer gets equal weight (probability $1/2$) regardless of how many marbles it holds. That means a drawer with 1 marble counts just as much as a drawer with 99. This immediately suggests an extreme allocation: if you can make one drawer a sure thing for red, you lock in a full $1/2$ from that drawer, and then you just need to maximize the red fraction in the other drawer. **Quick Estimate:** Put 1 red marble in drawer 1, everything else in drawer 2. Drawer 1 gives red with probability 1. Drawer 2 has 49 red out of 99, so roughly $49/99 \approx 0.495$. Total: $(1/2)(1) + (1/2)(0.495) \approx 0.748$. That is way above the naive $0.50$ from any balanced split. Can we do better? If we put 2 red marbles in drawer 1, it is still a guaranteed red draw, but drawer 2 now has $48/98 \approx 0.490$, giving total $\approx 0.745$ -- slightly worse. So the minimum sacrifice (1 marble) is best. **Approach:** Write the probability as a function of the allocation and show that the $(1, 0)$-$(49, 50)$ split is optimal. **Formal Solution:** Let drawer 1 contain $r$ red and $b$ blue marbles, with $r + b \geq 1$. Drawer 2 then contains $50 - r$ red and $50 - b$ blue, with $(50 - r) + (50 - b) \geq 1$, i.e., $r + b \leq 99$. The probability of drawing red is: $$P(\text{red}) = \frac{1}{2} \cdot \frac{r}{r + b} + \frac{1}{2} \cdot \frac{50 - r}{100 - r - b}$$ To maximize this, note the two terms are independent decisions: - **First term** $\frac{r}{r+b}$: This is the red fraction in drawer 1. It is maximized at 1 when $b = 0$ (drawer 1 has only red marbles). Among all-red choices, taking $r = 1$ minimizes the damage to drawer 2. - **Second term** $\frac{50 - r}{100 - r - b}$: Given $b = 0$ and $r = 1$, this becomes $49/99$. Any larger $r$ (with $b = 0$) gives $\frac{50 - r}{100 - r}$, which is decreasing in $r$ for $r \geq 1$ since the numerator drops by 1 for each unit increase while the denominator also drops by 1 from a larger base (formally, $\frac{d}{dr}\frac{50-r}{100-r} = \frac{-50}{(100-r)^2} 0$ blue, the first term drops below 1, and we lose more than we could gain back in the second term. For instance, $(25, 0)$-$(25, 50)$: $P = (1/2)(1) + (1/2)(25/75) = 0.5 + 0.167 = 0.667$, much worse. So the optimal allocation is: - Drawer 1: 1 red marble, 0 blue marbles - Drawer 2: 49 red marbles, 50 blue marbles $$P(\text{red}) = \frac{1}{2} \cdot 1 + \frac{1}{2} \cdot \frac{49}{99} = \frac{1}{2} + \frac{49}{198} = \frac{99 + 49}{198} = \frac{148}{198} = \frac{74}{99}$$ **Answer:** Place 1 red marble alone in one drawer and all remaining 99 marbles (49 red, 50 blue) in the other. The maximum probability of drawing red is $\frac{74}{99} \approx 0.7475$. --- Maximum of Multiple Dice Rolls [easy, Probability] --- https://quantvault.org/problem-193-maximum-of-multiple-dice-rolls.html Q: You roll $k \geq 1$ fair $n$-sided dice, each showing a value in $\{1, 2, \ldots, n\}$. Let $M_k$ denote the maximum value among all $k$ rolls. Find a closed-form expression for $P(M_k = r)$, the probability that the maximum equals $r$, for any $1 \leq r \leq n$. Then compute the answer explicitly for $k = 3$, $r = 4$, and $n = 6$. A: **How to Think About It:** The maximum is one of those quantities that is awkward to handle directly but clean to handle via its CDF. If you ask "what is the chance the max equals exactly 4?", it is hard to count directly. But if you ask "what is the chance the max is at most 4?", that is just the chance every single die shows 4 or less -- which factors immediately by independence. From there, $P(M_k = r) = P(M_k \leq r) - P(M_k \leq r-1)$ is a one-line subtraction. This CDF-then-difference trick is the standard tool for any discrete order statistic problem. **Quick Estimate:** With $k = 3$, $r = 4$, $n = 6$: the max is at most 4 roughly $(4/6)^3 \approx 0.296$ of the time. The max is at most 3 roughly $(3/6)^3 = 0.125$ of the time. So the max equals exactly 4 about $0.296 - 0.125 = 0.171$. Sanity check: roughly 1-in-6, which feels about right -- out of six possible max values on a 6-sided die, equal weighting would be 1/6 $\approx 0.167$, so 0.171 is in the right ballpark. **Approach:** Compute $P(M_k \leq r)$ by independence, then use the discrete difference $P(M_k = r) = P(M_k \leq r) - P(M_k \leq r-1)$. **Formal Solution:** Let $X_1, \ldots, X_k$ be the $k$ independent rolls. The CDF of the maximum is: $$P(M_k \leq r) = P(X_1 \leq r, \ldots, X_k \leq r)$$ Since $M_k \leq r$ if and only if every die shows at most $r$, and the dice are independent: $$P(M_k \leq r) = \prod_{i=1}^{k} P(X_i \leq r) = \left(\frac{r}{n}\right)^k$$ where $P(X_i \leq r) = r/n$ because the die is fair and there are $r$ values in $\{1, \ldots, r\}$ out of $n$ equally likely outcomes. For the PMF, use the discrete difference: $$P(M_k = r) = P(M_k \leq r) - P(M_k \leq r-1) = \left(\frac{r}{n}\right)^k - \left(\frac{r-1}{n}\right)^k$$ For $k = 3$, $r = 4$, $n = 6$: $$P(M_3 = 4) = \frac{4^3 - 3^3}{6^3} = \frac{64 - 27}{216} = \frac{37}{216}$$ **Answer:** $P(M_k = r) = \left(\dfrac{r}{n}\right)^k - \left(\dfrac{r-1}{n}\right)^k$. For $k=3$, $r=4$, $n=6$: $\dfrac{37}{216} \approx 0.171$. --- Non-Transitive Dice [easy, Probability] --- https://quantvault.org/problem-195-non-transitive-dice.html Q: Three fair 6-sided dice $A$, $B$, and $C$ have the following faces: - Die $A$: $\{2, 2, 6, 6, 7, 7\}$ - Die $B$: $\{1, 1, 5, 5, 9, 9\}$ - Die $C$: $\{3, 3, 4, 4, 8, 8\}$ Each value appears on exactly two faces, so each die shows each of its three values with probability $1/3$. Find the probability that die $A$ shows a strictly higher value than die $B$. A: **How to Think About It:** Each die has three equally likely outcomes (each with probability $1/3$), so this is a $3 \times 3$ grid of cases. Condition on what $B$ rolls and count how often $A$ beats it. This is a brute-force-friendly problem -- there are only 9 cases to check. **Quick Estimate:** Die $A$ has values $\{2, 6, 7\}$ and die $B$ has values $\{1, 5, 9\}$. On average, $A$ has mean $5$ and $B$ has mean $5$, so the dice are roughly evenly matched in expectation. But the question is about $P(A > B)$, not about means. Let's count: $B$ rolls low (1) a third of the time, and $A$ always beats 1. $B$ rolls medium (5) a third of the time, and $A$ beats it with 6 or 7 (2 out of 3). $B$ rolls high (9) a third of the time, and $A$ never beats it. So roughly $P \approx (1/3)(1) + (1/3)(2/3) + (1/3)(0) = 5/9 \approx 0.556$. **Approach:** Enumerate all 9 equally likely pairs and count victories. **Formal Solution:** Since each die shows each of its three distinct values with probability $1/3$, we have 9 equally likely outcome pairs. Condition on the value of $B$: | $B$ rolls | Prob | $A$ values that beat $B$ | $P(A > B \mid B)$ | |-----------|------|--------------------------|--------------------| | $1$ | $1/3$ | $2, 6, 7$ (all three) | $3/3 = 1$ | | $5$ | $1/3$ | $6, 7$ (two of three) | $2/3$ | | $9$ | $1/3$ | none | $0$ | By the law of total probability: $$P(A > B) = \frac{1}{3} \cdot 1 + \frac{1}{3} \cdot \frac{2}{3} + \frac{1}{3} \cdot 0 = \frac{1}{3} + \frac{2}{9} = \frac{5}{9}$$ **Answer:** $P(A > B) = \dfrac{5}{9} \approx 0.556$. Note: This problem is part of the "non-transitive dice" family. One can verify that $P(B > C) > 1/2$ and $P(C > A) > 1/2$ as well, creating a cycle: $A$ beats $B$, $B$ beats $C$, and $C$ beats $A$ -- each with probability $5/9$. This violates transitivity, which is the surprising punch line. --- Optimal Card Betting With a Known Deck [hard, Game Theory] --- https://quantvault.org/problem-196-optimal-card-betting-with-a-known-deck.html Q: You have a standard deck of 52 cards (26 red, 26 black), shuffled randomly. Cards are revealed one at a time from the top. Before each reveal, you may bet any amount up to your current bankroll that the next card will be red. If you're right, you win that amount; if you're wrong, you lose it. You start with $\$2$. What strategy maximizes your expected final bankroll, and what is the expected value under optimal play? A: **How to Think About It:** Read the objective carefully: you maximize expected final **dollars**, not the growth rate of wealth. That distinction is what makes the answer counterintuitive. Because the payoff is linear in wealth, the value function is linear in your bankroll, so at every step the optimal bet is *bang-bang* — shove everything or bet nothing, never an interior fraction. The tempting rule is "shove whenever the next card is more likely red," i.e. on one-step edge. **That rule is wrong here.** The correct test compares *continuation values*, because betting risks capital that is far more valuable saved for the endgame: once the last black card is gone, every remaining card is a certain red and can be doubled risk-free. That endgame value turns out to be so large that you should wait until every black is gone — no matter how favorable the immediate odds look. **Quick Estimate:** You start with $\$2$ and a balanced deck. A red bet only ever pays when reds outnumber blacks, and the one situation with *guaranteed* edge is when every black is already gone: then each remaining card is certainly red, so shoving doubles your money risk-free. Doubling through a run of $j$ guaranteed reds multiplies wealth by $2^j$. The chance of a long all-red tail is small, but the $2^j$ payoff is huge, and these rare tails dominate the expectation. Since $\binom{52}{26}\approx4.96\times10^{14}$ and $2^{52}\approx4.50\times10^{15}$, we have $2^{52}/\binom{52}{26}\approx9.08$, pointing to a final answer a little above $\$10$. **Approach:** Set up the exact dynamic program over deck compositions $(r,b)=$ (reds remaining, blacks remaining). Because the objective is linear, the value function is linear in wealth, so we track the optimal expected multiplier $V(r,b)$ on the current bankroll, determine the optimal bet at each state, and solve the recursion in closed form. **Formal Solution:** Let $V(r,b)$ be the optimal expected final wealth *per dollar* of current bankroll when $r$ reds and $b$ blacks remain. Betting a fraction $f\in[0,1]$ on red, with $p=r/(r+b)$: on a win the bankroll becomes $(1+f)W$ and the state moves to $(r-1,b)$; on a loss it becomes $(1-f)W$ and moves to $(r,b-1)$. By linearity the expected multiplier is $$g(f)=p(1+f)V(r-1,b)+(1-p)(1-f)V(r,b-1),$$ linear in $f$ with slope $$g'(f)=pV(r-1,b)-(1-p)V(r,b-1)=\frac{rV(r-1,b)-bV(r,b-1)}{r+b}.$$ A linear function on $[0,1]$ is maximized at an endpoint, so $$f^*=\begin{cases}1 & \text{if } rV(r-1,b) > bV(r,b-1),\\ 0 & \text{otherwise.}\end{cases}$$ **The criterion is *not* one-step EV** (whether $r>b$); it is the sign of $rV(r-1,b)-bV(r,b-1)$, which weights each branch by its *continuation* value. The boundaries are $V(r,0)=2^r$ (all remaining cards are sure reds — shove and double $r$ times) and $V(0,b)=1$ (no reds left, so no red bet can ever win). While a black remains you bet nothing, so $V$ satisfies the plain averaging recursion $V(r,b)=\frac{r}{r+b}V(r-1,b)+\frac{b}{r+b}V(r,b-1)$ — which is just conditioning on the **top** card. **Key identity (why you always wait while $b\ge1$).** Condition instead on the **bottom** card. With probability $\frac{b}{r+b}$ it is black, which forces the trailing all-red run to be empty and contributes a multiplier of $1$; with probability $\frac{r}{r+b}$ it is red, a guaranteed double sitting beneath the remaining $(r-1,b)$ deck, contributing $2V(r-1,b)$. Hence $$(r+b)\,V(r,b)=b\cdot 1+r\cdot 2V(r-1,b)=b+2rV(r-1,b).$$ Subtracting the top-card recursion $(r+b)V(r,b)=rV(r-1,b)+bV(r,b-1)$ gives, for **every** state with $b\ge1$, $$\boxed{\,rV(r-1,b)-bV(r,b-1)=-b\,.}$$ So the slope is $g'(f)=-\dfrac{b}{r+b}<0$ whenever a black remains: betting is *strictly* worse than waiting, hence $f^*=0$. Concretely, at $(26,1)$ the next card is red with probability $\tfrac{26}{27}$ — a one-step edge of $+0.93$ per dollar — yet the slope is $-\tfrac{1}{27}<0$, so you *still* bet nothing. The only positive-slope regime is $b=0$, where you shove every card. **Optimal strategy:** bet $\$0$ while any black card remains; once the last black is dealt, bet your entire bankroll on each remaining (certainly red) card. **Closed form.** With $f^*=0$ for $b\ge1$, unrolling the recursion — equivalently, your $\$2$ is untouched until the tail, then multiplied by $2^J$ where $J$ is the number of reds after the last black — gives $$V(r,b)=\frac{1}{\binom{r+b}{b}}\sum_{j=0}^{r}2^{j}\binom{r+b-1-j}{b-1}\qquad(b\ge1),$$ and for the balanced full deck $r=b=26$, $$V(26,26)=\tfrac12\!\left(1+\frac{2^{52}}{\binom{52}{26}}\right).$$ Starting from $\$2$, the expected final bankroll is $$E[W]=2V(26,26)=1+\frac{2^{52}}{\binom{52}{26}}.$$ With $2^{52}=4{,}503{,}599{,}627{,}370{,}496$ and $\binom{52}{26}=495{,}918{,}532{,}948{,}104$, $$E[W]=1+\frac{2^{52}}{\binom{52}{26}}\approx 1+9.0813=\$10.0813.$$ **Verification (small decks, exact DP).** - **2-card deck $(1R,1B)$ from $\$2$.** First card: bet $\$0$. If **black** shows first, the last card is a sure red, so you shove and double to $\$4$. If **red** shows first, the last card is *black* — which you cannot bet on — so you finish at $\$2$. Hence $E[W]=\tfrac12(\$4)+\tfrac12(\$2)=\$3.00$, matching $1+2^2/\binom{2}{1}=3$. (You do **not** double in both branches.) - **4-card deck $(2R,2B)$ from $\$2$:** the same wait-then-shove rule gives $E[W]=1+2^4/\binom{4}{2}=1+\tfrac{16}{6}\approx\$3.67$. Both exact values differ from any Kelly/proportional policy, confirming the expected-wealth objective is genuinely all-or-nothing. **Answer:** Bet nothing while any black card remains; once the last black is gone, bet your entire bankroll on every remaining (guaranteed-red) card. Starting from $\$2$, $$E[W]=1+\frac{2^{52}}{\binom{52}{26}}\approx\$10.08.$$ --- Perfectly Balanced Dice Rolls [medium, Probability] --- https://quantvault.org/problem-200-perfectly-balanced-dice-rolls.html Q: You roll a fair six-sided die 36 times. What is the probability that each face (1 through 6) appears exactly 6 times? A: **How to Think About It:** You roll a fair die 36 times, so each face shows up $6$ times on average. The question asks how likely it is that every face hits its expected count exactly. Your gut should say "pretty unlikely" -- even though $6$ is the mean count per face, the variance is large enough that perfect balance across all six faces simultaneously is rare. This is a textbook multinomial probability calculation. **Quick Estimate:** Each face count $X_i \sim \text{Binomial}(36, 1/6)$ with mean $6$ and standard deviation $\sqrt{36 \cdot \frac{1}{6} \cdot \frac{5}{6}} = \sqrt{5} \approx 2.24$. The probability a single face lands exactly on its mean is roughly $1/(\sqrt{2\pi} \cdot 2.24) \approx 0.18$. But we need all six faces to hit their means simultaneously, and the counts are dependent (they sum to 36). A multinomial normal approximation gives a probability on the order of $10^{-4}$, which lines up with the exact answer. **Approach:** Apply the multinomial distribution directly. Count the ways to arrange 36 rolls so each face appears exactly 6 times, then multiply by the probability of any single such sequence. **Formal Solution:** The 36 rolls follow a $\text{Multinomial}(36;\, 1/6,\, 1/6,\, 1/6,\, 1/6,\, 1/6,\, 1/6)$ distribution. The probability that face $i$ appears exactly $n_i$ times is: $$P(X_1 = n_1, \ldots, X_6 = n_6) = \frac{36!}{n_1! \cdot n_2! \cdots n_6!} \left(\frac{1}{6}\right)^{36}$$ With $n_1 = n_2 = \cdots = n_6 = 6$: $$P = \frac{36!}{(6!)^{6}} \cdot \left(\frac{1}{6}\right)^{36}$$ The multinomial coefficient evaluates to: $$\binom{36}{6,6,6,6,6,6} = \frac{36!}{(6!)^{6}} \approx 2.67 \times 10^{24}$$ and $6^{36} \approx 1.031 \times 10^{28}$, so: $$P = \frac{2.67 \times 10^{24}}{1.031 \times 10^{28}} \approx 0.000259$$ **Answer:** $$P = \frac{36!}{(6!)^{6} \cdot 6^{36}} \approx 0.000259 \approx \frac{1}{3{,}863}$$ About a 0.026% chance -- roughly 1 in 3,863 attempts will produce a perfectly balanced outcome. --- Pirate Hat Puzzle: Cooperative Parity Strategy [medium, Game Theory] --- https://quantvault.org/problem-201-pirate-hat-puzzle-cooperative-parity-strategy.html Q: Ten pirates are each given a hat -- either red or blue -- uniformly at random. Each pirate can see everyone else's hat but not their own. They must simultaneously and independently announce a guess for their own hat color. If every single pirate guesses correctly, they are all freed; if even one is wrong, they are all executed. Before the hats are assigned, the pirates can meet and agree on any strategy they like. No communication is allowed once the hats go on. 1. What is the survival probability of the naive strategy where everyone just guesses randomly? 2. Design an optimal cooperative strategy. What survival probability does it achieve? 3. Prove that no strategy can exceed this probability. A: **How to Think About It:** The naive approach -- everyone guesses randomly -- gives $(1/2)^{10} \approx 0.1\%$, useless. The temptation is to imagine one pirate "announcing" a parity bit that the others then decode from his guess. **That framing is illegal here:** the guesses are made *simultaneously and independently*, and reading another pirate's guess would be communication, which is forbidden. No pirate ever learns anyone else's guess. Every pirate may use *only* the 9 hats he sees. The right idea is therefore fully symmetric: the group fixes one global parity convention in advance, and each pirate independently guesses the color that makes the observed-plus-own count obey that convention. With this rule all ten pirates are correct together or all wrong together, and they win exactly on the configurations whose true parity matches the convention -- which is half of them. **Quick Estimate:** There are $2^{10} = 1024$ equally likely hat configurations. Any cooperative strategy is just a collection of functions, one per pirate, from "the 9 hats I see" to a guess. Focus on any single pirate, say pirate 1: his guess is a function of the other 9 hats, while his own hat is an independent fair coin he cannot see. So no matter how clever the strategy, pirate 1's own hat is equally likely to match or mismatch his guess -- he is correct with probability exactly $1/2$. Survival requires *everyone* correct, hence in particular pirate 1 correct, so $P(\text{survive}) \le 1/2$. The question is whether $1/2$ is actually reachable; parity shows it is. **Approach:** (1) Naive: independence gives $(1/2)^{10}$. (2) Fix a global parity target for the total red-hat count; have *every* pirate independently guess the color that makes the total match the target, using only the hats he sees. Show all guesses are simultaneously right iff the true total has the target parity, an event of probability $1/2$. (3) Upper bound: any single pirate is right with probability exactly $1/2$ on his own hat, capping the group at $1/2$. **Formal Solution:** *Part 1 -- Naive strategy.* If each pirate guesses his own color by an independent fair coin, his guess matches his (independent, fair) hat with probability $1/2$. The ten events are independent, so $$P(\text{survive}) = \left(\tfrac12\right)^{10} = \frac{1}{1024} \approx 0.098\%.$$ *Part 2 -- Optimal strategy (symmetric parity).* Let $\mathrm{hat}_k \in \{0,1\}$ with $1 =$ red, and let $R = \sum_{k=1}^{10}\mathrm{hat}_k$ be the total number of red hats. Before the game the pirates agree on a target parity -- say, **the total number of reds should be even**. Now each pirate acts *independently*: > Pirate $k$ counts the reds among the 9 hats he sees, namely $s_k = R - \mathrm{hat}_k$, and guesses the color of his own hat that would make the total even. Concretely he guesses red if $s_k$ is odd (so that adding his red makes the total even) and blue if $s_k$ is even. No pirate observes any guess; each uses only his own observation $s_k$. Examine the two cases: - **$R$ even.** For pirate $k$: if his hat is red then $s_k = R-1$ is odd, so he guesses red -- correct; if his hat is blue then $s_k = R$ is even, so he guesses blue -- correct. **Every** pirate is correct, so the group survives. - **$R$ odd.** For pirate $k$: if his hat is red then $s_k = R-1$ is even, so he guesses blue -- wrong; if his hat is blue then $s_k = R$ is odd, so he guesses red -- wrong. **Every** pirate is wrong. Thus the outcome is all-or-nothing and survival happens **exactly when the true total $R$ is even**. Since $R$ is a sum of 10 independent fair bits, its parity is uniform: $P(R\text{ even}) = 1/2$. Hence $$P(\text{survive}) = P(R \text{ even}) = \frac{1}{2}.$$ (Using the "odd" convention instead would make the group win exactly on the odd-parity configurations -- also probability $1/2$.) *Part 3 -- Optimality (no strategy exceeds $1/2$).* Fix any cooperative strategy. Pirate 1's guess is some deterministic function $g_1$ of the 9 hats he sees, $g_1(\mathrm{hat}_2,\dots,\mathrm{hat}_{10})$. His own hat $\mathrm{hat}_1$ is generated by an independent fair coin and is invisible to him, hence statistically independent of everything $g_1$ depends on. Conditioning on the other 9 hats fixes the guess $g_1$ but leaves $\mathrm{hat}_1$ uniform on $\{$red, blue$\}$, so $$P(\text{pirate 1 correct}) = \tfrac12 \quad\text{for every strategy.}$$ Survival requires every pirate correct, in particular pirate 1, so $P(\text{survive}) \le P(\text{pirate 1 correct}) = 1/2$. (Counting check: for each of the $512$ assignments of the other 9 hats, $g_1$ is fixed, and exactly one of the two values of $\mathrm{hat}_1$ makes pirate 1 right; so pirate 1 is right in exactly $512$ of the $1024$ configurations.) The parity strategy attains this bound, so $1/2$ is optimal. **Answer:** The naive strategy survives with probability $(1/2)^{10} = \boxed{1/1024}$. The optimal cooperative strategy has **every** pirate independently guess the color that makes the total number of red hats match a pre-agreed parity (e.g. even); all ten are then correct exactly on the even-parity configurations, giving survival probability $\boxed{1/2}$, and no strategy can do better because any single pirate is correct with probability only $1/2$ on his own hat. --- Poisson Limit of Lottery Winners [medium, Probability] --- https://quantvault.org/problem-202-poisson-limit-of-lottery-winners.html Q: A total of $2N$ people play a lottery each week. Each person is independently assigned a uniformly random integer from $\{1, 2, \dots, N\}$. One of the $N$ integers is then selected uniformly at random as the winning value. Let $W_N$ be the number of people who win in a given week. Compute $\displaystyle \lim_{N \to \infty} P(W_N \leq 3)$ to the nearest thousandth. A: **How to Think About It:** Each of the $2N$ people independently wins with probability $1/N$, so $W_N \sim \text{Bin}(2N, 1/N)$. As $N$ grows, you have more and more trials with a smaller and smaller success probability, but the expected number of winners stays fixed at $2N \cdot 1/N = 2$. That is the textbook setup for a Poisson limit. In the first 10 seconds of the interview, you should say: "This converges to a Poisson(2), so I need the CDF up to 3." **Quick Estimate:** For $X \sim \text{Poisson}(2)$, the PMF is $P(X = k) = e^{-2} \cdot 2^k / k!$. We need $P(X \leq 3)$. A quick mental computation: $e^{-2} \approx 0.135$. Then $P(X=0) \approx 0.135$, $P(X=1) \approx 0.271$, $P(X=2) \approx 0.271$, $P(X=3) \approx 0.180$. Summing: $0.135 + 0.271 + 0.271 + 0.180 \approx 0.857$. That is our answer. **Approach:** Apply the Poisson limit theorem to the sequence of Binomial random variables, then evaluate the Poisson CDF exactly. **Formal Solution:** For fixed $N$, each person wins if their assigned number matches the winning value. This happens with probability $1/N$, independently across people. So $$W_N \sim \text{Bin}(2N,\; 1/N).$$ As $N \to \infty$, the number of trials $2N \to \infty$ and the success probability $1/N \to 0$, while the mean is $$E[W_N] = 2N \cdot \frac{1}{N} = 2.$$ By the Poisson limit theorem, $W_N \xrightarrow{d} X$ where $X \sim \text{Poisson}(2)$. Therefore $$\lim_{N \to \infty} P(W_N \leq 3) = P(X \leq 3) = \sum_{k=0}^{3} \frac{e^{-2} \cdot 2^k}{k!}.$$ Evaluating term by term: - $P(X = 0) = e^{-2}$ - $P(X = 1) = 2e^{-2}$ - $P(X = 2) = \frac{4}{2} e^{-2} = 2e^{-2}$ - $P(X = 3) = \frac{8}{6} e^{-2} = \frac{4}{3} e^{-2}$ Summing: $$P(X \leq 3) = e^{-2}\left(1 + 2 + 2 + \frac{4}{3}\right) = \frac{19}{3} e^{-2} = \frac{19}{3e^2}.$$ Numerically, $e^2 \approx 7.389$, so $P(X \leq 3) \approx 19 / 22.167 \approx 0.857$. **Answer:** $\displaystyle \lim_{N \to \infty} P(W_N \leq 3) = \frac{19}{3e^2} \approx 0.857$. --- Posterior Probability of a Double-Headed Coin [easy, Probability] --- https://quantvault.org/problem-204-posterior-probability-of-a-double-headed-coin.html Q: A jar contains 12 coins: 10 are fair (equal chance of heads or tails), and 2 are double-headed (always land heads). You pick a coin uniformly at random from the jar and flip it 5 times. All 5 flips come up heads. What is the posterior probability that the coin you picked is double-headed? A: **How to Think About It:** This is a textbook Bayes' theorem problem. You observed something (5 heads in a row) that is much more likely under one hypothesis (double-headed coin) than the other (fair coin). Before you compute anything, build intuition: a fair coin produces 5 heads with probability $1/32 \approx 3\%$, while a double-headed coin does it with certainty. So 5 heads is strong evidence for the double-headed hypothesis, but the prior odds are 10-to-2 against it. The posterior should swing heavily toward double-headed, but not all the way. **Quick Estimate:** Prior odds of double-headed vs. fair: $2:10 = 1:5$. Likelihood ratio: $1 / (1/32) = 32$. Posterior odds: $32 \times 1/5 = 32/5 \approx 6.4$ to 1 in favor of double-headed. So the posterior probability is roughly $6.4 / 7.4 \approx 0.865$, or about 86-87%. That is a quick number you can give in an interview before writing anything down. **Approach:** Apply Bayes' theorem directly. Define $D$ as the event that the chosen coin is double-headed, and $H_5$ as the event of observing 5 heads. **Formal Solution:** The prior probabilities are: $$P(D) = \frac{2}{12} = \frac{1}{6}, \quad P(D^c) = \frac{10}{12} = \frac{5}{6}$$ The likelihoods are: $$P(H_5 \mid D) = 1, \quad P(H_5 \mid D^c) = \left(\frac{1}{2}\right)^5 = \frac{1}{32}$$ The total probability of observing 5 heads is: $$P(H_5) = P(H_5 \mid D) \cdot P(D) + P(H_5 \mid D^c) \cdot P(D^c) = 1 \cdot \frac{1}{6} + \frac{1}{32} \cdot \frac{5}{6} = \frac{1}{6} + \frac{5}{192}$$ Finding a common denominator: $$P(H_5) = \frac{32}{192} + \frac{5}{192} = \frac{37}{192}$$ Applying Bayes' theorem: $$P(D \mid H_5) = \frac{P(H_5 \mid D) \cdot P(D)}{P(H_5)} = \frac{1 \cdot \frac{1}{6}}{\frac{37}{192}} = \frac{\frac{1}{6}}{\frac{37}{192}} = \frac{192}{6 \times 37} = \frac{32}{37}$$ **Answer:** The posterior probability that the chosen coin is double-headed is $\frac{32}{37} \approx 0.8649$, or about 86.5%. --- Posterior for a Double-Headed Coin [easy, Probability] --- https://quantvault.org/problem-205-posterior-for-a-double-headed-coin.html Q: A box contains 100 coins: 98 fair coins, 1 double-headed coin (heads on both sides), and 1 double-tailed coin (tails on both sides). You pick a coin uniformly at random and flip it. 1. The first flip shows heads. Compute $P(\text{double-headed} \mid \text{heads})$, $P(\text{fair} \mid \text{heads})$, and $P(\text{double-tailed} \mid \text{heads})$. 2. You flip the same coin again and it shows heads again. Update your posterior: compute $P(\text{double-headed} \mid \text{two heads})$. A: **How to Think About It:** This is a straightforward Bayes' theorem problem with three hypotheses: the coin is fair ($F$), double-headed ($HH$), or double-tailed ($TT$). The prior is determined by the composition of the box. After observing heads, the double-headed coin becomes more likely (it always shows heads), the fair coin stays plausible (50% chance of heads), and the double-tailed coin becomes impossible (it can never show heads). After a second heads, the double-headed coin becomes even more likely. **Quick Estimate:** Before any flip, $P(HH) = 1/100$. The double-headed coin is 2x more likely to produce heads than a fair coin. After one heads, the posterior should roughly double the prior odds of $HH$ vs. $F$: from $1:98$ to about $2:98 = 1:49$, giving $P(HH \mid H) \approx 2/100 = 2\%$. After two heads, it doubles again: about $4:98$, so roughly $4\%$. **Approach:** Apply Bayes' theorem sequentially. **Formal Solution:** **Part (i): One flip shows heads.** Priors: $$P(HH) = \frac{1}{100}, \quad P(F) = \frac{98}{100}, \quad P(TT) = \frac{1}{100}$$ Likelihoods: $$P(H \mid HH) = 1, \quad P(H \mid F) = \frac{1}{2}, \quad P(H \mid TT) = 0$$ Total probability of heads: $$P(H) = 1 \cdot \frac{1}{100} + \frac{1}{2} \cdot \frac{98}{100} + 0 \cdot \frac{1}{100} = \frac{1}{100} + \frac{49}{100} = \frac{50}{100} = \frac{1}{2}$$ Posteriors after one heads: $$P(HH \mid H) = \frac{P(H \mid HH) \cdot P(HH)}{P(H)} = \frac{1 \cdot 1/100}{1/2} = \frac{2}{100} = \frac{1}{50}$$ $$P(F \mid H) = \frac{P(H \mid F) \cdot P(F)}{P(H)} = \frac{(1/2)(98/100)}{1/2} = \frac{98}{100} = \frac{49}{50}$$ $$P(TT \mid H) = \frac{P(H \mid TT) \cdot P(TT)}{P(H)} = \frac{0 \cdot 1/100}{1/2} = 0$$ Sanity check: $1/50 + 49/50 + 0 = 1$. **Part (ii): Second flip also shows heads (same coin).** Use the posteriors from part (i) as the new priors: $$P(HH \mid H) = \frac{1}{50}, \quad P(F \mid H) = \frac{49}{50}, \quad P(TT \mid H) = 0$$ Likelihoods for the second heads: $$P(H_2 \mid HH) = 1, \quad P(H_2 \mid F) = \frac{1}{2}, \quad P(H_2 \mid TT) = 0$$ Total probability of second heads given first heads: $$P(H_2 \mid H_1) = 1 \cdot \frac{1}{50} + \frac{1}{2} \cdot \frac{49}{50} + 0 = \frac{1}{50} + \frac{49}{100} = \frac{2}{100} + \frac{49}{100} = \frac{51}{100}$$ Posterior after two heads: $$P(HH \mid HH_{\text{obs}}) = \frac{1 \cdot 1/50}{51/100} = \frac{2/100}{51/100} = \frac{2}{51}$$ $$P(F \mid HH_{\text{obs}}) = \frac{(1/2)(49/50)}{51/100} = \frac{49/100}{51/100} = \frac{49}{51}$$ Sanity check: $2/51 + 49/51 = 1$. **Note the pattern:** After $k$ consecutive heads, the posterior odds of $HH$ to $F$ are: $$\frac{P(HH \mid k \text{ heads})}{P(F \mid k \text{ heads})} = \frac{1}{98} \cdot 2^k$$ This is because each heads observation doubles the likelihood ratio of $HH$ vs. $F$. **Answer:** 1. After one heads: $P(HH \mid H) = 1/50 = 0.02$, $P(F \mid H) = 49/50 = 0.98$, $P(TT \mid H) = 0$. 2. After two heads: $P(HH \mid \text{two heads}) = 2/51 \approx 0.0392$. --- Probability That 1 Was Selected Given Increasing Order [hard, Probability] --- https://quantvault.org/problem-206-probability-that-1-was-selected-given-increasing-order.html Q: You draw $r$ integers one at a time, without replacement, uniformly at random from $\{1, 2, \dots, n\}$. Someone tells you that the numbers came out in increasing order. Given that information, what is the probability that $1$ was among the numbers selected? Derive a general formula in terms of $r$ and $n$, then compute the answer for $r = 5$ and $n = 100$. A: **How to Think About It:** The conditioning event -- "the draws came out in increasing order" -- sounds like it changes things dramatically, but it is actually just a clean way to select a random $r$-element subset. There is exactly one increasing arrangement for any subset, so conditioning on increasing order is equivalent to asking: pick a uniformly random $r$-element subset of $\{1, \dots, n\}$; what is the chance $1$ is in it? That collapses the whole problem into a single counting ratio. **Quick Estimate:** For $r = 5$, $n = 100$: you are picking 5 elements out of 100 at random. The chance any specific element (like $1$) gets picked is roughly $5/100 = 0.05 = 1/20$. So the answer should be $1/20$ -- and the formal derivation will confirm this exactly. **Approach:** Use counting rather than manipulating probabilities directly. Since sampling is uniform, $P(A) = |A| / |\Omega|$ for any event $A$, so we just need to count favorable vs. total outcomes. **Formal Solution:** Let $I$ be the event that the $r$ draws come out in increasing order, and $O$ be the event that $1$ is among the selected numbers. We want $P(O \mid I)$. By definition: $$P(O \mid I) = \frac{P(O \cap I)}{P(I)}$$ Since every subset of size $r$ has exactly one increasing arrangement, the total number of increasing sequences is $\binom{n}{r}$ -- one per subset. So: $$P(I) = \frac{\binom{n}{r}}{n^{\underline{r}}}$$ where $n^{\underline{r}} = n(n-1)\cdots(n-r+1)$ is the number of ordered draws. But these factors cancel cleanly -- we can work directly in the space of ordered draws and count. Equivalently, think of it this way: in the space where we condition on $I$, every $r$-element subset of $\{1, \dots, n\}$ is equally likely. The denominator is $\binom{n}{r}$ (all size-$r$ subsets). For the numerator, count subsets that include $1$: fix $1$, then choose the remaining $r-1$ elements from $\{2, 3, \dots, n\}$, giving $\binom{n-1}{r-1}$ subsets. Therefore: $$P(O \mid I) = \frac{\binom{n-1}{r-1}}{\binom{n}{r}} = \frac{\dfrac{(n-1)!}{(r-1)!\,(n-r)!}}{\dfrac{n!}{r!\,(n-r)!}} = \frac{r}{n}$$ **Answer:** $P(1 \text{ selected} \mid \text{increasing order}) = \dfrac{r}{n}$. For $r = 5$, $n = 100$: $\dfrac{5}{100} = \dfrac{1}{20}$. --- Probability of a Void Suit in a Card Hand [medium, Probability] --- https://quantvault.org/problem-211-probability-of-a-void-suit-in-a-card-hand.html Q: You are dealt $13$ cards from a standard deck of $52$ cards (four suits of $13$ cards each). A hand is **void** in a suit if it contains zero cards from that suit. What is the probability that your hand is void in at least one suit? Round your answer to the nearest thousandth. A: **How to Think About It:** "At least one" screams inclusion-exclusion. Label the four suits $1, 2, 3, 4$ and let $A_k$ be the event that your hand has no cards from suit $k$. You want $P(A_1 \cup A_2 \cup A_3 \cup A_4)$. By symmetry every suit looks the same, so $P(A_1) = P(A_2) = \cdots$, and similarly for intersections. That makes the inclusion-exclusion formula very clean. Gut feeling: being void in a single suit means all 13 cards come from 39 cards -- that should be a small probability, so the union will be dominated by the first-order term $4 \cdot P(A_1)$. **Quick Estimate:** $P(A_1)$ is the chance all 13 cards come from the 39 non-suit-1 cards. Think of it sequentially: the first card avoids suit 1 with probability $39/52 = 3/4$, the second with $38/51$, and so on. Roughly $(3/4)^{13} \approx 0.75^{13}$. Since $0.75^4 \approx 0.316$ and $0.75^{13} \approx 0.75^{12} \cdot 0.75 \approx (0.316)^3 \cdot 0.75 \approx 0.032 \cdot 0.75 \approx 0.024$. That is a rough upper bound (the actual hypergeometric probability is smaller because we draw without replacement from a finite deck). The exact value is about $0.0128$, and $4 \times 0.0128 \approx 0.051$. The higher-order corrections ($P(A_1 \cap A_2)$, etc.) are tiny by comparison, so the answer is approximately $0.051$. **Approach:** Apply inclusion-exclusion with four symmetric events, computing each intersection probability via hypergeometric counting. **Formal Solution:** By inclusion-exclusion: $$P\!\left(\bigcup_{k=1}^{4} A_k\right) = \binom{4}{1}P(A_1) - \binom{4}{2}P(A_1 \cap A_2) + \binom{4}{3}P(A_1 \cap A_2 \cap A_3) - \binom{4}{4}P(A_1 \cap A_2 \cap A_3 \cap A_4)$$ Compute each probability by counting favorable hands over total hands $\binom{52}{13}$: - $P(A_1)$: all 13 cards come from the 39 cards not in suit 1. $$P(A_1) = \frac{\binom{39}{13}}{\binom{52}{13}}$$ - $P(A_1 \cap A_2)$: all 13 cards come from the 26 cards in suits 3 and 4. $$P(A_1 \cap A_2) = \frac{\binom{26}{13}}{\binom{52}{13}}$$ - $P(A_1 \cap A_2 \cap A_3)$: all 13 cards come from suit 4 alone -- there are exactly 13 such cards, so there is exactly one such hand. $$P(A_1 \cap A_2 \cap A_3) = \frac{\binom{13}{13}}{\binom{52}{13}} = \frac{1}{\binom{52}{13}}$$ - $P(A_1 \cap A_2 \cap A_3 \cap A_4) = 0$, since you cannot avoid all four suits with 13 cards. Substituting: $$P = 4 \cdot \frac{\binom{39}{13}}{\binom{52}{13}} - 6 \cdot \frac{\binom{26}{13}}{\binom{52}{13}} + 4 \cdot \frac{1}{\binom{52}{13}}$$ Numerically: - $4 \cdot P(A_1) \approx 0.05116$ - $6 \cdot P(A_1 \cap A_2) \approx 0.0000983$ - $4 \cdot P(A_1 \cap A_2 \cap A_3) \approx 6.3 \times 10^{-12}$ (negligible) $$P \approx 0.05116 - 0.0000983 + 0 \approx 0.05107$$ **Answer:** The probability of being void in at least one suit is approximately $\boxed{0.051}$. --- Secure Message Delivery with Padlocks [medium, Brain Teaser] --- https://quantvault.org/problems.html?id=213 Q: You need to send a confidential document to a colleague via an untrusted messenger service. You can send items in a padlock box. The rules: - Anything inside an **unlocked** box will be stolen by the messenger (including any locks you place inside). - You and your colleague each own one high-security padlock. Each padlock has exactly one key, held only by the person who placed that lock. - Keys cannot be sent through the messenger (they would be stolen from an unlocked box, and you cannot lock a key inside a box whose lock requires that same key to open). Design a protocol that guarantees your colleague receives the document, even though the messenger is completely untrusted. A: **How to Think About It:** This is a logic puzzle about secure communication without shared secrets -- essentially the physical analogue of a key-exchange problem. The naive approach fails: you lock the box and send it, but your colleague has no key to open it. You cannot send the key either, because the messenger steals anything in an unlocked box. The trick is realizing that the box can make multiple trips, and both parties can add and remove their own locks. **Key Insight:** The box does not have to be opened by the recipient on the first delivery. You can layer locks and peel them off in the right order over multiple round trips. **The Method:** 1. **You** place the document in the box, lock it with **your** padlock, and send it to your colleague. (The messenger cannot open the locked box.) 2. **Your colleague** receives the locked box. He cannot open it (he does not have your key). Instead, he adds **his own** padlock to the box alongside yours, and sends it back to you. (The box now has two locks; it is still secure.) 3. **You** receive the doubly-locked box. You remove **your** padlock (you have the key) and send the box back. (The box now has only your colleague's padlock.) 4. **Your colleague** receives the box with only his own padlock. He unlocks it with his key and retrieves the document. At every stage the box is locked, so the messenger never has access to the contents. **Practical Considerations:** - This requires **three** deliveries (you to colleague, colleague to you, you to colleague). - The protocol assumes the messenger does not tamper with or destroy the box -- only that he steals unlocked contents. - The box must support multiple padlocks simultaneously (both can be attached at once). **Answer:** Send the document in a box locked with your padlock. Your colleague adds his padlock and returns it. You remove your padlock and send it back. Your colleague removes his padlock and has the document. Three trips, zero shared secrets. --- Simulating a 2:3 Odds Ratio with Fair Coins [medium, Probability] --- https://quantvault.org/problem-214-simulating-a-2-3-odds-ratio-with-fair-coins.html Q: You have two fair coins. A colleague asks you to simulate a random outcome where heads occurs with probability $\frac{2}{5}$ and tails with probability $\frac{3}{5}$ -- a 2:3 ratio -- using only these coins. Design a procedure using only flips of these two fair coins that produces a binary outcome (call it H or T) with exactly $P(H) = \frac{2}{5}$ and $P(T) = \frac{3}{5}$. Your procedure may use as many flips as needed, but each flip must be of a fair coin. A: **How to Think About It:** The core tension here is that fair coins generate probabilities that are powers of $\frac{1}{2}$, but we want $\frac{2}{5}$, which has a 5 in the denominator. You cannot directly produce fifths from halves. The key insight is rejection sampling -- flip coins to generate outcomes over a larger space, identify a subset where the valid outcomes split in a 2:3 ratio, and discard (and reflip) when you land outside that subset. The question is: what is the smallest flip sequence that gives you enough equally-likely outcomes to carve out a 2:3 split? **Quick Estimate:** Two coins give $2^2 = 4$ equally likely outcomes. We need 5 equally likely outcomes to split 2:3. Four does not divide into five evenly, so two coins in one shot cannot work. Three coins give $2^3 = 8$ outcomes. We can assign 2 to H and 3 to T -- that is 5 outcomes used, 3 rejected. The acceptance probability is $\frac{5}{8}$, so on average we need $\frac{8}{5} = 1.6$ rounds of three flips, or about 4.8 flips total in expectation. That is pretty efficient. **Approach:** Use rejection sampling over three-flip blocks (two coins and one extra flip -- or equivalently, three flips of either coin). Assign 2 of the 8 equally likely outcomes to H, 3 to T, and reject the other 3. **Formal Solution:** Flip coin A twice and coin B once (or any three fair flips in a fixed order). There are $2^3 = 8$ equally likely outcomes, each with probability $\frac{1}{8}$: $$\text{HHH, HHT, HTH, HTT, THH, THT, TTH, TTT}$$ Assign outcomes as follows: - HHH, HHT $\to$ result H (2 outcomes) - HTH, HTT, THH $\to$ result T (3 outcomes) - THT, TTH, TTT $\to$ reflip (3 outcomes) Given that we do not reflip (probability $\frac{5}{8}$), the conditional probabilities are: $$P(H \mid \text{no reflip}) = \frac{2/8}{5/8} = \frac{2}{5}, \quad P(T \mid \text{no reflip}) = \frac{3/8}{5/8} = \frac{3}{5}$$ The expected number of three-flip rounds is $\frac{1}{5/8} = \frac{8}{5} = 1.6$, so the expected total number of flips is $3 \times \frac{8}{5} = \frac{24}{5} = 4.8$. **Answer:** Flip three fair coins per round. Map 2 of the 8 outcomes to H, 3 to T, and reject-and-reflip on the remaining 3. This produces $P(H) = \frac{2}{5}$ exactly, with an expected 4.8 flips per result. --- Triangle Sharing a Side with a Regular Polygon [medium, Probability] --- https://quantvault.org/problem-222-triangle-sharing-a-side-with-a-regular-polygon.html Q: Fix $n \geq 4$. You pick three distinct vertices uniformly at random from a regular $n$-gon and connect them to form a triangle. What is the probability that at least one side of this triangle is also a side (edge) of the $n$-gon? Compute the answer for $n = 10$. A: **How to Think About It:** Every set of 3 vertices out of $n$ is equally likely, so this is pure counting: favorable triangles over total triangles. The key observation is that a triangle can share 0, 1, or 2 sides with the polygon (never 3, since three consecutive edges of an $n$-gon with $n \geq 4$ don't close into a triangle). So count the triangles sharing exactly 1 side and exactly 2 sides separately, then add. **Quick Estimate:** For $n = 10$, total triangles: $\binom{10}{3} = 120$. Triangles with 2 shared sides require 3 consecutive vertices -- there are 10 of those. Triangles with 1 shared side: pick one of 10 edges, then the third vertex can't be adjacent to that edge (or you'd get 3 consecutive), so you exclude 4 vertices, leaving $10 - 4 = 6$ choices, giving $10 \times 6 = 60$. Total favorable: $10 + 60 = 70$. Probability: $70/120 = 7/12 \approx 0.583$. **Approach:** Count by cases -- triangles sharing exactly 2 sides and exactly 1 side with the $n$-gon -- then combine. **Formal Solution:** The total number of triangles is $\binom{n}{3}$. *Case 1: Exactly 2 shared sides.* This requires three consecutive vertices of the polygon. There are exactly $n$ such triples (one for each starting vertex, cycling around). *Case 2: Exactly 1 shared side.* Pick the shared edge: there are $n$ edges. The third vertex must not be adjacent to either endpoint of that edge (otherwise we'd get 3 consecutive vertices, which is Case 1). That rules out the 2 endpoints themselves plus the 2 vertices adjacent to the endpoints on the outside, so 4 vertices are excluded. This leaves $n - 4$ choices for the third vertex. So there are $n(n - 4)$ such triangles. Adding both cases: $$\text{Favorable} = n + n(n-4) = n(n-3)$$ The probability is: $$P = \frac{n(n-3)}{\binom{n}{3}} = \frac{n(n-3)}{\frac{n(n-1)(n-2)}{6}} = \frac{6(n-3)}{(n-1)(n-2)}$$ **Answer:** For general $n$, the probability is $\dfrac{6(n-3)}{(n-1)(n-2)}$. For $n = 10$: $$P = \frac{6 \cdot 7}{9 \cdot 8} = \frac{42}{72} = \frac{7}{12}$$ --- Winning From Deuce With a Weaker Serve [medium, Probability] --- https://quantvault.org/problem-226-winning-from-deuce-with-a-weaker-serve.html Q: You are playing a ping pong game, and the score is tied 10-10 (deuce). From this point, you must win by 2 clear points to take the game. Your probability of winning any individual point is $p = 0.4$. What is the probability that you win the game? A: **How to Think About It:** This is a classic Markov chain / recursive probability setup. At deuce, neither player has an advantage in score, but the weaker player (you, at 40%) is at a disadvantage. The game from deuce is memoryless -- every time you return to deuce, it is as if you started over. So the whole problem reduces to: what is the probability of going on a net +2 run before a net -2 run, where each point is an independent Bernoulli trial? Before any math, your gut should say: since you win each point less than half the time, your chance of winning must be well below 50%. And because the "win by 2" rule gives the stronger player repeated chances to reassert dominance, it should be noticeably below the naive $p^2 = 16\%$ that ignores the possibility of returning to deuce. **Quick Estimate:** The two immediate paths from deuce are: you win the next two points outright (prob $0.4^2 = 0.16$), or the opponent wins both ($0.6^2 = 0.36$). The remaining $2 \times 0.4 \times 0.6 = 0.48$ of the time you split the points and return to deuce. So roughly, conditional on the game not returning to deuce, you win with probability $\frac{0.16}{0.16 + 0.36} = \frac{0.16}{0.52} \approx 0.308$. That shortcut actually gives the exact answer here because the deuce reset is memoryless. **Approach:** Model the game as a three-state Markov chain (Deuce, Advantage You, Advantage Opponent) and solve the resulting linear equation for the win probability. **Formal Solution:** Let $p = 0.4$, $q = 1 - p = 0.6$, and let $P$ be your probability of winning from deuce. Define three states: - **Deuce:** score is tied. - **Advantage You (A+):** you lead by 1 point. - **Advantage Opponent (A-):** you trail by 1 point. From deuce, with probability $p$ you move to A+, with probability $q$ you move to A-. From A+: with probability $p$ you win the game (score +2), with probability $q$ you return to deuce. From A-: with probability $p$ you return to deuce, with probability $q$ you lose (score -2). Let $P_{+}$ be the probability of winning from A+ and $P_{-}$ from A-: $$P_{+} = p + q \cdot P$$ $$P_{-} = p \cdot P$$ The main equation: $$P = p \cdot P_{+} + q \cdot P_{-} = p(p + qP) + q(pP)$$ $$P = p^2 + pqP + pqP = p^2 + 2pqP$$ Solving: $$P - 2pqP = p^2$$ $$P(1 - 2pq) = p^2$$ $$P = \frac{p^2}{1 - 2pq}$$ Plugging in $p = 0.4$, $q = 0.6$: $$P = \frac{0.16}{1 - 2(0.24)} = \frac{0.16}{1 - 0.48} = \frac{0.16}{0.52} = \frac{4}{13} \approx 0.3077$$ **Answer:** The probability of winning from deuce is $P = \dfrac{p^2}{1 - 2pq} = \dfrac{4}{13} \approx 30.8\%$. --- Age Whose Square Is the Year [easy, Brain Teaser] --- https://quantvault.org/problem-228-age-whose-square-is-the-year.html Q: A man once noticed something remarkable: the square of his current age equaled the current year. He wrote this down in a journal, but the ink was smudged and the entry is now unreadable. You know two things: - He died in 1970. - He did not live longer than 100 years. What was his age when he made the journal entry? A: **How to Think About It:** We need an integer age $x$ such that $x^2$ is a valid year during this man's lifetime. He died in 1970 and lived at most 100 years, so he was born no earlier than 1870. That means $x^2$ must fall in the range $[1870, 1970]$. We just need to check which perfect squares land in that interval. **Quick Estimate:** $\sqrt{1870} \approx 43.2$ and $\sqrt{1970} \approx 44.4$. So the only integer in that range is $x = 44$, since $44^2 = 1936$. Quick sanity check: if he was 44 in 1936, he was born in 1892, which is between 1870 and 1970, and he would have been 78 at death in 1970 -- well under 100. Works. **Approach:** Systematically check nearby perfect squares. **Formal Solution:** Let $x$ be his age when he made the entry, so the year of the entry is $x^2$. - He died in 1970, so $x^2 \leq 1970$. - He lived at most 100 years, so he was born no earlier than 1870, meaning $x^2 - x \geq 1870$ (birth year = year of entry minus age). - Also $x^2 \geq 1870$ (the entry must be within his lifetime). Check the candidates: | $x$ | $x^2$ | Birth year ($x^2 - x$) | Age at death ($1970 - x^2 + x$) | Valid? | |-----|--------|------------------------|----------------------------------|--------| | 43 | 1849 | 1806 | 164 | No (before 1870, age > 100) | | 44 | 1936 | 1892 | 78 | Yes | | 45 | 2025 | 1980 | -- | No ($x^2 > 1970$) | Only $x = 44$ satisfies all constraints. **Answer:** His age was $44$ (the year was 1936, he was born in 1892, and he died at age 78 in 1970). --- All Evens Before Any Odd on a Fair Die [medium, Probability] --- https://quantvault.org/problem-229-all-evens-before-any-odd-on-a-fair-die.html Q: You roll a fair six-sided die repeatedly. What is the probability that you see all three even numbers (2, 4, and 6) before you see any odd number (1, 3, or 5)? In other words, when the first odd number finally appears, all three even numbers must have already shown up at least once. A: **How to Think About It:** Picture rolling the die over and over. Each roll is equally likely to be any of {1, 2, 3, 4, 5, 6}. The process "ends" the moment an odd number appears. For us to win, we need to have collected all three even values -- 2, 4, and 6 -- before that happens. This is a race between collecting all even faces and hitting any odd face. The elegant way to think about it: forget about repeated values entirely. Focus only on the order in which the six distinct values first appear. By symmetry (each face has probability $1/6$), every ordering of first-appearance times is equally likely -- it is a uniformly random permutation of $\{1, 2, 3, 4, 5, 6\}$. **Quick Estimate:** There are $\binom{6}{3} = 20$ ways to choose which 3 of the 6 values appear first. We need the first 3 to be exactly $\{2, 4, 6\}$. So we should expect something around $1/20 = 0.05$. Let's verify. **Approach:** Use the fact that the sequence of distinct values encountered is a uniformly random permutation. **Formal Solution:** Label the six die faces. Define $T_i$ as the roll on which face $i$ first appears. By symmetry of the fair die, the relative ordering of $(T_1, T_2, T_3, T_4, T_5, T_6)$ is a uniformly random permutation of 6 elements. We need: every even value appears before every odd value. Equivalently, in the random permutation of first-appearance order, positions 1, 2, 3 must be occupied by $\{2, 4, 6\}$ (in any order). We can compute this step by step using the sequence of new (previously unseen) values: - The 1st new value: probability it is even $= 3/6 = 1/2$ (3 even faces out of 6 total). - The 2nd new value, given the 1st was even: probability it is even $= 2/5$ (2 remaining evens out of 5 remaining values). - The 3rd new value, given the first two were even: probability it is even $= 1/4$ (1 remaining even out of 4 remaining values). $$P = \frac{3}{6} \cdot \frac{2}{5} \cdot \frac{1}{4} = \frac{6}{120} = \frac{1}{20}$$ **Equivalently**, count favorable permutations of the 6 first-appearance positions: the 3 evens can fill the first 3 slots in $3!$ ways, and the 3 odds fill the last 3 slots in $3!$ ways, out of $6!$ total permutations: $$P = \frac{3! \cdot 3!}{6!} = \frac{6 \cdot 6}{720} = \frac{36}{720} = \frac{1}{20}$$ Both methods agree. **Answer:** $P = \dfrac{1}{20} = 0.05$ --- Bayesian Coin Identification [easy, Probability] --- https://quantvault.org/problem-235-bayesian-coin-identification.html Q: You have two coins. Coin A is fair: each flip lands heads with probability $0.5$. Coin B is biased toward heads: each flip lands heads with probability $p = 0.7$ (tails with probability $0.3$). You pick one uniformly at random and flip it 5 times, observing 4 heads and 1 tail. What is the posterior probability that you are holding Coin B? A: **How to Think About It:** Before flipping, you have a 50-50 chance of holding either coin. The data -- 4 heads in 5 flips -- is more likely under Coin B ($p = 0.7$) than Coin A ($p = 0.5$), so the posterior should shift toward B. The question is by how much. Intuitively, 4/5 heads (80%) is much closer to 70% than to 50%, so the update should be substantial but not overwhelming -- you should end up somewhere around 65-75% probability for Coin B. **Quick Estimate:** The likelihood ratio is $P(D | B) / P(D | A) = (0.7^4 \times 0.3) / (0.5^4 \times 0.5)$. Numerically, $0.7^4 \times 0.3 \approx 0.2401 \times 0.3 = 0.0720$ and $0.5^4 \times 0.5 = 0.5^5 = 0.03125$, so the likelihood ratio $\approx 0.0720 / 0.03125 \approx 2.3$. Starting from 50-50 prior odds, posterior odds for B are $2.3:1$, so $P(B|D) \approx 2.3/3.3 \approx 0.70$. **Approach:** Apply Bayes' theorem with binomial likelihoods. **Formal Solution:** Let $D$ denote the observed data (4 heads, 1 tail in 5 flips). **Prior:** $P(A) = P(B) = 1/2$. **Likelihoods** (the $\binom{5}{4}$ factor cancels in the ratio, but include for clarity): $$P(D | A) = \binom{5}{4}(0.5)^4(0.5)^1 = 5 \times \frac{1}{32} = \frac{5}{32} \approx 0.15625$$ $$P(D | B) = \binom{5}{4}(0.7)^4(0.3)^1 = 5 \times 0.2401 \times 0.3 \approx 0.36015$$ **Marginal likelihood:** $$P(D) = P(D|A) \cdot P(A) + P(D|B) \cdot P(B) = \frac{1}{2}(0.15625 + 0.36015) = \frac{0.5164}{2} \approx 0.25820$$ **Posterior:** $$P(B|D) = \frac{P(D|B) \cdot P(B)}{P(D)} = \frac{0.36015 \times 0.5}{0.25820} \approx \frac{0.18008}{0.25820} \approx \boxed{0.697}$$ The posterior shifts from 50% to about 69.7% -- consistent with our quick estimate of ~70%. **Answer:** $P(B | \text{4H, 1T}) = \frac{7203}{10328} \approx 0.697$. The biased coin is now roughly 2.3 times more likely than the fair coin. --- Biased Random Walk Reaching Position 1 [medium, Probability] --- https://quantvault.org/problem-238-biased-random-walk-reaching-position-1.html Q: A particle starts at position $0$ on the integer number line. At each step, it moves right (from $i$ to $i+1$) with probability $p$ and left (from $i$ to $i-1$) with probability $1 - p$. If $p = 3/4$, what is the probability the particle ever reaches position $1$? For full credit, derive the answer for general $p$ and then specialize. A: **How to Think About It:** This is a classic first-passage problem for a biased random walk. Before doing any algebra, think about it economically: the particle has a $3/4$ chance of stepping right and only $1/4$ chance of stepping left. It is strongly biased toward moving right. So intuitively, you would expect it to reach position $1$ with very high probability -- probably certainty. The key technique is conditioning on the first step: either you step right and you are already at $1$, or you step left to $-1$ and now you need to climb back two positions. By translation invariance of the random walk, the probability of reaching $1$ from $0$ is the same as reaching $0$ from $-1$, which lets you write a self-consistent equation. **Quick Estimate:** With $p = 3/4$, the drift is $E[\text{step}] = p - (1-p) = 1/2 > 0$. The walk drifts to the right at rate $1/2$ per step. By the law of large numbers, the particle goes to $+\infty$, so it certainly passes through $1$. That strongly suggests the probability is $1$. For a symmetric walk ($p = 1/2$), we know from classical theory that the walk is recurrent -- it hits every integer with probability $1$. Since $p = 3/4$ is even more biased toward the right, the probability of reaching $1$ should remain $1$. In fact, the answer will be less than $1$ only when the walk drifts away from the target, i.e., when $p 1/2$, we get $\alpha = 1$. Plugging in $p = 3/4$: $$\alpha = \min\!\left(1,\;\frac{3/4}{1/4}\right) = \min(1, 3) = 1$$ **Answer:** The probability is $1$. The particle reaches position $1$ with certainty. More generally, the hitting probability is $\min(1,\, p/(1-p))$, which equals $1$ whenever $p \geq 1/2$. --- Corner Absorption From a 3x3 Center [medium, Stochastic Process] --- https://quantvault.org/problem-241-corner-absorption-from-a-3x3-center.html Q: On a $5 \times 5$ grid, label the columns $0$-$4$ and the rows $0$ (bottom) to $4$ (top). Pick a starting square uniformly at random from the central $3 \times 3$ block (columns $1$-$3$, rows $1$-$3$). At each step the token moves diagonally upward: up-left or up-right, each with probability $\tfrac{1}{2}$. If one of those moves would take the token off the board, only the legal move is made (with probability $1$). The walk ends when the token reaches row $4$. What is the probability that the walk finishes in one of the two top corners -- column $0$ or column $4$ of row $4$? Show how you aggregate over all nine equally likely starting squares. A: **How to Think About It:** This is a bounded random walk on a 5-wide grid, and the token always moves upward, so it reaches row 4 in a fixed number of steps (depending on its starting row). The key is that boundary reflections break the usual diffusion symmetry -- the edges funnel probability toward or away from the corners. Rather than setting up a big system of equations, just compute the corner-absorption probability $P(c, r)$ for every cell, working backward from row 4. **Quick Estimate:** By symmetry, columns 1 and 3 are mirror images, so their corner probabilities match. The center column (column 2) "sees" both corners equally and the boundary effects cancel. A rough guess: about half the starting squares feed into a corner and about half do not, so the average should be somewhere around $\tfrac{1}{4}$ to $\tfrac{1}{3}$. The exact answer will be $\tfrac{5}{18} \approx 0.278$. **Approach:** Define $P(c, r)$ as the probability of finishing in column 0 or column 4 at row 4 when starting from $(c, r)$. Work backward row by row from the terminal conditions. **Formal Solution:** *Terminal row (row 4):* $$P(0,4) = 1,\quad P(1,4) = 0,\quad P(2,4) = 0,\quad P(3,4) = 0,\quad P(4,4) = 1$$ *Row 3 (one step from the top):* - $P(0,3)$: left edge, forced right $\to (1,4)$. $P = 0$. - $P(1,3) = \tfrac{1}{2}P(0,4) + \tfrac{1}{2}P(2,4) = \tfrac{1}{2}$. - $P(2,3) = \tfrac{1}{2}P(1,4) + \tfrac{1}{2}P(3,4) = 0$. - $P(3,3) = \tfrac{1}{2}P(2,4) + \tfrac{1}{2}P(4,4) = \tfrac{1}{2}$. - $P(4,3)$: right edge, forced left $\to (3,4)$. $P = 0$. *Row 2:* - $P(0,2)$: forced right $\to (1,3)$. $P = \tfrac{1}{2}$. - $P(1,2) = \tfrac{1}{2}P(0,3) + \tfrac{1}{2}P(2,3) = 0$. - $P(2,2) = \tfrac{1}{2}P(1,3) + \tfrac{1}{2}P(3,3) = \tfrac{1}{2}$. - $P(3,2) = \tfrac{1}{2}P(2,3) + \tfrac{1}{2}P(4,3) = 0$. - $P(4,2)$: forced left $\to (3,3)$. $P = \tfrac{1}{2}$. *Row 1:* - $P(0,1)$: forced right $\to (1,2)$. $P = 0$. - $P(1,1) = \tfrac{1}{2}P(0,2) + \tfrac{1}{2}P(2,2) = \tfrac{1}{2}$. - $P(2,1) = \tfrac{1}{2}P(1,2) + \tfrac{1}{2}P(3,2) = 0$. - $P(3,1) = \tfrac{1}{2}P(2,2) + \tfrac{1}{2}P(4,2) = \tfrac{1}{2}$. - $P(4,1)$: forced left $\to (3,2)$. $P = 0$. *Aggregation over the central $3 \times 3$ block:* | Cell | $P$ | |---|---| | $(1,1)$ | $\tfrac{1}{2}$ | | $(2,1)$ | $0$ | | $(3,1)$ | $\tfrac{1}{2}$ | | $(1,2)$ | $0$ | | $(2,2)$ | $\tfrac{1}{2}$ | | $(3,2)$ | $0$ | | $(1,3)$ | $\tfrac{1}{2}$ | | $(2,3)$ | $0$ | | $(3,3)$ | $\tfrac{1}{2}$ | Five of the nine cells have probability $\tfrac{1}{2}$ and four have probability $0$. The overall probability is: $$\frac{1}{9}\sum_{\text{block}} P(c,r) = \frac{1}{9}\left(5 \cdot \frac{1}{2} + 4 \cdot 0\right) = \frac{5}{18}$$ **Answer:** The probability that the walk is absorbed in either top corner is $\dfrac{5}{18} \approx 0.2778$. --- Day of the Week for January 1, 2078 [medium, Brain Teaser] --- https://quantvault.org/problem-244-day-of-the-week-for-january-1-2078.html Q: January 1, 2024, was a Monday. What day of the week is January 1, 2078? Express your answer as a number $0$-$6$, where $0$ = Sunday, $1$ = Monday, ..., $6$ = Saturday. A: **How to Think About It:** The day of the week shifts forward by 1 each year (since $365 = 52 \times 7 + 1$), except after a leap year, when it shifts forward by 2 (since $366 = 52 \times 7 + 2$). So the total shift over 54 years is the number of years plus the number of leap years in that span, all mod 7. **Quick Estimate:** From 2024 to 2078 is 54 years. Roughly $54/4 \approx 13$ or $14$ leap years in there. Total shift is around $54 + 14 = 68$ days. $68/7 = 9$ remainder $5$. So we shift 5 days forward from Monday, landing on Saturday ($6$). Let me verify the leap year count to be sure. **Approach:** Count exact leap years, compute total day shift mod 7. **Formal Solution:** From January 1, 2024 to January 1, 2078 is exactly 54 years. Leap years in the range 2024-2077 (these are the years whose extra day affects the Jan 1 shift for the following year; equivalently, these are the leap years we "pass through"): $$2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056, 2060, 2064, 2068, 2072, 2076$$ That is $14$ leap years. (None of these are century years, so no Gregorian correction needed.) Total day shift: $$\Delta = 54 + 14 = 68 \text{ days forward}$$ Reduce mod 7: $$68 = 9 \times 7 + 5$$ So the day shifts forward by 5 from Monday: - Monday + 1 = Tuesday - Monday + 2 = Wednesday - Monday + 3 = Thursday - Monday + 4 = Friday - Monday + 5 = Saturday **Answer:** January 1, 2078 is a **Saturday**, which corresponds to $\boxed{6}$. --- Edit Distance (Levenshtein Distance) [medium, Coding] --- https://quantvault.org/problem-245-edit-distance-levenshtein-distance.html Q: You are given two strings $s_1$ and $s_2$. You can transform $s_1$ into $s_2$ using three operations, each costing 1: - **Insert** a character at any position - **Delete** a character at any position - **Substitute** one character for another Write a function that returns the minimum total number of operations needed to convert $s_1$ into $s_2$. This quantity is known as the Levenshtein (edit) distance. **Constraints:** - $0 \leq |s_1|, |s_2| \leq 500$ - Both strings consist of lowercase English letters **Examples:** 1. `s1 = "kitten"`, `s2 = "sitting"` → `3` - kitten → sitten (substitute 'k' with 's') - sitten → sittin (substitute 'e' with 'i') - sittin → sitting (insert 'g' at end) 2. `s1 = "horse"`, `s2 = "ros"` → `3` - horse → rorse (substitute 'h' with 'r') - rorse → rose (delete 'r') - rose → ros (delete 'e') 3. `s1 = ""`, `s2 = "abc"` → `3` (3 insertions) A: **How to Think About It:** The naive move -- search over every sequence of edits -- is a trap: it branches exponentially. The heuristic that cracks it is **optimal substructure via last-character alignment**. Compare only the *last* characters of the two prefixes $s_1[0..i-1]$ and $s_2[0..j-1]$. Exactly one of four things is optimal for that final position: the two last characters already match (align them for free), or you paid for a delete, an insert, or a substitute. Each choice hands you back a *strictly smaller* prefix pair. That single recurrence is the whole problem; everything else is bookkeeping in an $(m{+}1)\times(n{+}1)$ table. This is the canonical **DP / first-step-decomposition** pattern -- decide the last operation, recurse on what remains. **Quick Estimate:** Before writing code, bound the answer in your head. The edit distance is squeezed between $\big|\,|s_1|-|s_2|\,\big|$ (you must at least insert/delete the length difference) and $\max(|s_1|,|s_2|)$ (worst case: rewrite everything). For `kitten` (6) $\to$ `sitting` (7): lower bound $|6-7|=1$, upper bound $7$. The true answer $3$ sits comfortably inside. A sharper mental count: line the words up, `sitt` is shared in the middle, so you expect only a *handful* of edits -- 'k'$\to$'s', 'e'$\to$'i', and one trailing insert = $3$. For `horse`$\to$`ros`: length drop $5-3=2$ forces at least 2 deletes, and one letter differs, so $\approx 3$ -- exactly right. The bound-plus-eyeball is the sanity check an interviewer wants before you commit to the $O(mn)$ table. **Approach:** Fill $dp[i][j]$ = edit distance of the first $i$ chars of $s_1$ and first $j$ chars of $s_2$, row by row, using the last-character recurrence. **Formal Solution:** Define $dp[i][j]$ = edit distance between $s_1[0..i-1]$ and $s_2[0..j-1]$. *Base cases* (one string empty): - $dp[i][0] = i$ -- delete all $i$ characters of $s_1$. - $dp[0][j] = j$ -- insert all $j$ characters of $s_2$. *Recurrence*, for $i,j \ge 1$: $$dp[i][j] = \begin{cases} dp[i-1][j-1] & \text{if } s_1[i-1] = s_2[j-1] \\[4pt] 1 + \min\!\big(dp[i-1][j],\; dp[i][j-1],\; dp[i-1][j-1]\big) & \text{otherwise} \end{cases}$$ where the three inner terms are **delete** from $s_1$, **insert** into $s_1$, and **substitute** respectively. The answer is $\boxed{dp[m][n]}$ with $m=|s_1|,\, n=|s_2|$. ```python def edit_distance(s1: str, s2: str) -> int: m, n = len(s1), len(s2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i in range(m + 1): dp[i][0] = i for j in range(n + 1): dp[0][j] = j for i in range(1, m + 1): for j in range(1, n + 1): if s1[i - 1] == s2[j - 1]: dp[i][j] = dp[i - 1][j - 1] else: dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) return dp[m][n] ``` **Space optimization:** each row depends only on the row above, so keep two rows and reduce space to $O(\min(m,n))$: ```python def edit_distance_optimized(s1: str, s2: str) -> int: if len(s1) < len(s2): s1, s2 = s2, s1 m, n = len(s1), len(s2) prev = list(range(n + 1)) for i in range(1, m + 1): curr = [i] + [0] * n for j in range(1, n + 1): if s1[i - 1] == s2[j - 1]: curr[j] = prev[j - 1] else: curr[j] = 1 + min(prev[j], curr[j - 1], prev[j - 1]) prev = curr return prev[n] ``` **Complexity:** Time $O(mn)$ (each of the $mn$ cells is $O(1)$); space $O(mn)$ for the full table, $O(\min(m,n))$ with two rows. **Answer:** Edit distance is computed in $O(mn)$ time by DP; the key insight is that aligning the last characters of each prefix reduces the problem to three strictly smaller subproblems. On the examples: `kitten`$\to$`sitting` $=3$, `horse`$\to$`ros` $=3$, `""`$\to$`abc` $=3$. --- Even Heads with Mixed Fair and Unfair Coins [medium, Probability] --- https://quantvault.org/problem-249-even-heads-with-mixed-fair-and-unfair-coins.html Q: You flip 10 coins: 5 are fair (each heads with probability $\frac{1}{2}$) and 5 are unfair with some unknown fixed bias $q \in [0, 1]$. All flips are independent. What is the probability that the total number of heads is even? A: **How to Think About It:** Before calculating anything, ask yourself: does the bias of the unfair coins even matter? You have 5 fair coins in the mix. Each fair coin is perfectly symmetric -- heads and tails are equally likely. Intuitively, that symmetry might dominate. Your first move at a whiteboard should be to check whether the answer is just $\frac{1}{2}$ regardless of $q$, and if so, explain why. **Quick Estimate:** Start with an extreme case to build intuition. Suppose the 5 unfair coins are all double-headed ($q = 1$), so they always show heads. Then you always get exactly 5 heads from the unfair coins, and the total parity is determined entirely by the 5 fair coins. The probability of an even number of heads from 5 fair coins is $\frac{1}{2}$ by symmetry (equal chance of any number from 0 to 5, and exactly half the configurations sum to even). So the answer is $\frac{1}{2}$ in this extreme case. Try the other extreme: $q = 0$ (all tails always). Same logic -- parity is again determined by the 5 fair coins, giving $\frac{1}{2}$. The answer looks like $\frac{1}{2}$ no matter what. **Approach:** Use the standard generating-function identity for even-head probabilities. For $n$ independent coins with head probabilities $p_1, \ldots, p_n$, the probability of an even number of heads is: $$P(\text{even heads}) = \frac{1 + \prod_{i=1}^{n}(1 - 2p_i)}{2}$$ This follows from evaluating the probability generating function at $-1$: setting $z = -1$ in $\prod_{i=1}^{n}(1 - p_i + p_i z)$ gives $\prod_{i=1}^{n}(1 - 2p_i)$, and combining with the evaluation at $z = 1$ (which gives 1) yields the result. **Formal Solution:** Label the fair coins $i = 1, \ldots, 5$ with $p_i = \frac{1}{2}$, and the unfair coins $i = 6, \ldots, 10$ with $p_i = q$. Apply the identity: $$P(\text{even heads}) = \frac{1 + \prod_{i=1}^{10}(1 - 2p_i)}{2}$$ For the fair coins, each factor contributes: $$1 - 2 \cdot \frac{1}{2} = 0$$ So the product over all 10 coins contains $(0)^5 = 0$ as a factor, regardless of $q$: $$\prod_{i=1}^{10}(1 - 2p_i) = (0)^5 \cdot (1 - 2q)^5 = 0$$ Therefore: $$P(\text{even heads}) = \frac{1 + 0}{2} = \frac{1}{2}$$ **Answer:** $$\boxed{\frac{1}{2}}$$ The bias $q$ of the unfair coins is completely irrelevant. --- Expected Rolls to Reach Position 10 in a Symmetric Random Walk [medium, Expectation] --- https://quantvault.org/problem-258-expected-rolls-to-reach-position-10-in-a-symmetric-random-wa.html Q: You start at position $0$ on the number line. Each turn, you roll a fair six-sided die and get a result $X$. - If $X \in \{1, 2, 3\}$, you move $X$ steps to the right. - If $X \in \{4, 5, 6\}$, you move $X - 3$ steps to the left (i.e., 1, 2, or 3 steps left). The process ends when you first reach position $10$ or higher. How many rolls does it take on average? A: **How to Think About It:** Each roll gives a displacement drawn uniformly from $\{-3, -2, -1, +1, +2, +3\}$, so the mean step is $0$: this is a symmetric (driftless) random walk with variable step sizes. The question asks for the expected time to first reach $+10$, and there is no lower boundary -- the walk may wander arbitrarily far negative. That last fact is the whole problem. Two different things are true at once and it is easy to conflate them: a driftless walk on $\mathbb{Z}$ is *recurrent*, so it hits $+10$ with probability $1$; but the time it takes has a heavy $t^{-3/2}$ tail, so the *expectation* of that time diverges. "Certain to happen" and "happens in finite expected time" are not the same statement. The trap the question is built around is reaching for the diffusion scale $d^2/\sigma^2$, which silently answers a bounded problem instead. **Quick Estimate:** For a symmetric random walk, the variance per step is: $$\sigma^2 = E[\Delta^2] = \frac{1}{6}(1 + 4 + 9 + 1 + 4 + 9) = \frac{28}{6} = \frac{14}{3} \approx 4.67$$ By diffusion approximation, the position after $n$ steps is approximately $N(0, n\sigma^2)$. The probability of being at or above 10 after $n$ steps is roughly $\Phi(-10/\sqrt{n \cdot 14/3})$. For this to be significant, we need $n$ of order $100/\sigma^2 \approx 21$. That $\approx 21$ figure is exactly the trap. The formula $d^2/\sigma^2$ prices an *exit* time from a bounded region, so it answers a two-barrier problem, not the one-sided problem posed here. **Approach:** Set up a system of linear equations. Let $E_i$ be the expected number of rolls to reach position $\ge 10$ starting from position $i$. For $i \ge 10$, $E_i = 0$. For $i < 10$: $$E_i = 1 + \frac{1}{6}\bigl(E_{i+1} + E_{i+2} + E_{i+3} + E_{i-1} + E_{i-2} + E_{i-3}\bigr)$$ The problem is that $i$ can be arbitrarily negative, giving infinitely many states. For a symmetric walk with no lower barrier, $E_0 = \infty$ (this is a well-known result: the expected first-passage time to a one-sided barrier for a mean-zero random walk is infinite, even though the walk will reach the barrier with probability 1). **Diffusion approximation (finite answer):** If we instead treat this as a diffusion and ask for the expected time conditional on reaching $+10$ before $-10$ (a symmetric two-barrier problem), then by the standard result for a Brownian motion between barriers at $-a$ and $+a$: $$E[\tau \mid \text{hit } +a \text{ first}] = \frac{a^2}{\sigma^2}$$ With $a = 10$ and $\sigma^2 = 14/3$: $$E[\tau] \approx \frac{100}{14/3} = \frac{300}{14} \approx 21.4$$ **Finite-state solutions (what a lower boundary actually buys you):** Add a boundary at $0$ and the expectation becomes finite and computable. States $0, \ldots, 9$ with everything $\geq 10$ absorbing gives a $10 \times 10$ linear system; solving it in exact rational arithmetic: - clamped at $0$ (a step that would go below $0$ leaves you at $0$): $E_0 = \tfrac{95956}{3315} \approx 28.95$ - reflecting at $0$ (position $\mapsto |{\cdot}|$): $E_0 = \tfrac{7085976}{294881} \approx 24.03$ - symmetric two-barrier, absorbing at $+10$ and $-10$: $E_0 = \tfrac{7085976}{294881} \approx 24.03$ as well -- the same number, as it must be, since folding the symmetric two-barrier walk about the origin *is* the reflecting walk All of these exceed the diffusion estimate $300/14 \approx 21.4$, which ignores overshoot past the barrier. None of them is the answer to the question as posed -- they are the answers to the neighbouring questions the trap quietly substitutes. **Answer:** No -- the candidate's "about $21$ rolls" is wrong for the game as stated. For a mean-zero walk with no lower barrier, the walk reaches $10$ with probability $1$, but the expected first-passage time is **infinite**: $E[\tau] = \infty$. The diffusion figure $\frac{300}{14} \approx 21.4$ (from $E[\tau] \approx d^2/\sigma^2$ with $\sigma^2 = 14/3$, $d = 10$) answers a *different* problem -- one with a second absorbing barrier or a reflecting barrier at the origin -- and quoting it here is precisely the pitfall this question probes. Simulation confirms the divergence: the truncated mean $E[\min(\tau, T)]$ grows like $\sqrt{T}$ without bound (roughly $220$, $460$, $910$ for $T = 10^3,\ 4\times 10^3,\ 1.6\times 10^4$). --- Expected Sum of Two Dice with Cancellation on Doubles [medium, Expectation] --- https://quantvault.org/problem-259-expected-sum-of-two-dice-with-cancellation-on-doubles.html Q: You roll two fair six-sided dice. If both dice show the same number, the outcome is "cancelled" and you record a sum of $0$. Otherwise, you record the sum of the two dice. What is the expected value of the recorded sum? A: **How to Think About It:** This is a clean conditional expectation problem. Before doing any math, think about what's happening: five-sixths of the time you get a normal dice sum, and one-sixth of the time you get zero. So the answer should be roughly $(5/6) \times (\text{average sum when dice differ})$. The average sum of two dice is $7$, and the cancelled doubles ($2, 4, 6, 8, 10, 12$) are themselves symmetric about $7$, so throwing them out leaves the average of the surviving rolls at exactly $7$. Quick gut: the answer is exactly $(5/6) \times 7 \approx 5.83$. **Quick Estimate:** There are 36 equally likely outcomes. Doubles occur 6 times (probability $1/6$), contributing 0 to the expectation. The remaining 30 outcomes contribute their sums. The total sum across all 36 outcomes is $36 \times 7 = 252$ (by symmetry, each die averages $3.5$). The doubles contribute $2 + 4 + 6 + 8 + 10 + 12 = 42$. So the non-doubles contribute $252 - 42 = 210$, giving expected recorded sum $= 210 / 36 = 35/6 \approx 5.833$. **Approach:** Use the decomposition $E[\text{result}] = E[S] - E[S \cdot \mathbf{1}_{\text{same}}]$ where $S$ is the ordinary sum. **Formal Solution:** Let $S = X_1 + X_2$ be the sum of two fair dice, and let $D$ be the event that $X_1 = X_2$. The recorded result is $R = S \cdot \mathbf{1}_{D^c}$, so: $$E[R] = E[S] - E[S \cdot \mathbf{1}_D]$$ We know $E[S] = 7$. For the doubles term: $$E[S \cdot \mathbf{1}_D] = \sum_{k=1}^{6} P(X_1 = X_2 = k) \cdot 2k = \sum_{k=1}^{6} \frac{1}{36} \cdot 2k = \frac{2}{36} \sum_{k=1}^{6} k = \frac{2 \times 21}{36} = \frac{42}{36} = \frac{7}{6}$$ Therefore: $$E[R] = 7 - \frac{7}{6} = \frac{35}{6}$$ **Answer:** The expected recorded sum is $\frac{35}{6} \approx 5.833$. --- Half-Kelly Betting With Equal Wins and Losses [medium, Expectation] --- https://quantvault.org/problem-270-half-kelly-betting-with-equal-wins-and-losses.html Q: You start with $\$2$ and make 6 consecutive bets on independent fair coin flips. Each time, you bet exactly half of your current bankroll: if you win, you gain your stake (your bankroll is multiplied by $3/2$); if you lose, you lose your stake (your bankroll is multiplied by $1/2$). Suppose that across the 6 flips you win exactly 3 times and lose exactly 3 times. What is your final bankroll, and what is your net profit or loss? A: **How to Think About It:** The crucial insight is that the order of wins and losses does not matter. Each win multiplies your bankroll by $3/2$ and each loss multiplies it by $1/2$. Multiplication is commutative, so whether you win the first three and lose the last three, or alternate, or any other pattern -- the final bankroll is the same. This is a key property of multiplicative betting strategies. **Quick Estimate:** After 3 wins and 3 losses, your bankroll is multiplied by $(3/2)^3 \times (1/2)^3$. Let's compute: $(3/2)^3 = 27/8 = 3.375$ and $(1/2)^3 = 1/8 = 0.125$. The product is $3.375 \times 0.125 = 0.421875$. Starting from $\$2$: $2 \times 0.421875 = 0.84375$. So you end up with less than a dollar despite winning half your bets. That's a loss of about $\$1.16$. **Approach:** Compute the multiplicative growth factor directly. **Formal Solution:** Let $B_0 = 2$ be the initial bankroll. At each step, you bet half: - **Win:** bankroll goes from $B$ to $B + B/2 = (3/2)B$ - **Loss:** bankroll goes from $B$ to $B - B/2 = (1/2)B$ After $w$ wins and $\ell$ losses, the final bankroll is: $$B_{\text{final}} = B_0 \cdot \left(\frac{3}{2}\right)^w \cdot \left(\frac{1}{2}\right)^\ell$$ With $w = 3$, $\ell = 3$: $$B_{\text{final}} = 2 \cdot \left(\frac{3}{2}\right)^3 \cdot \left(\frac{1}{2}\right)^3 = 2 \cdot \frac{27}{8} \cdot \frac{1}{8} = 2 \cdot \frac{27}{64} = \frac{27}{32}$$ Net profit/loss: $$\text{Net} = \frac{27}{32} - 2 = \frac{27 - 64}{32} = -\frac{37}{32} \approx -\$1.15625$$ **Answer:** - Final bankroll: $\frac{27}{32} = \$0.84375$ - Net loss: $\frac{37}{32} \approx \$1.16$ Despite winning exactly half the bets, you lose more than half your initial stake. --- Optimal Guess in a Dice Payout Game [medium, Expectation] --- https://quantvault.org/problem-278-optimal-guess-in-a-dice-payout-game.html Q: You pick a number $n$ from $1$ to $6$. I roll a fair die. If the die shows $n$ or higher, you get paid $\$n$. If the die shows less than $n$, you get nothing. What number should you pick, and what is the fair price to play this game? A: **How to Think About It:** This is a classic expected value optimization. You are choosing a threshold $n$ that controls a trade-off: higher $n$ means a bigger payout when you win, but a lower probability of winning. Your gut should say that the extremes are bad -- picking $n = 1$ guarantees a win but only pays $\$1$, while picking $n = 6$ pays $\$6$ but you only have a $1/6$ chance. The sweet spot is somewhere in the middle. The fair price is whatever the optimal expected payoff turns out to be. **Quick Estimate:** At the extremes, $n = 1$ gives $\$1 \cdot 1 = \$1$ and $n = 6$ gives $\$6 \cdot 1/6 = \$1$. Both are $\$1$. In the middle, try $n = 3$: you win with probability $4/6$ and get $\$3$, so the expected payoff is $3 \times 4/6 = \$2$. Try $n = 4$: you win with probability $3/6$ and get $\$4$, so $4 \times 3/6 = \$2$. The optimal expected payoff looks like $\$2$. **Approach:** Compute $E_n = n \cdot P(\text{die} \ge n)$ for each $n$ and maximize. **Formal Solution:** If you choose $n$, the die shows $n$ or higher with probability $(7 - n)/6$, so your expected payoff is: $$E_n = n \cdot \frac{7 - n}{6}$$ Evaluating for each choice: - $n = 1$: $1 \cdot 6/6 = 1.00$ - $n = 2$: $2 \cdot 5/6 \approx 1.67$ - $n = 3$: $3 \cdot 4/6 = 2.00$ - $n = 4$: $4 \cdot 3/6 = 2.00$ - $n = 5$: $5 \cdot 2/6 \approx 1.67$ - $n = 6$: $6 \cdot 1/6 = 1.00$ The expected payoff is symmetric around $n = 3.5$ (since $E_n = n(7-n)/6$ is a downward-opening parabola with vertex at $n = 3.5$). The two integer values flanking the vertex, $n = 3$ and $n = 4$, both achieve the maximum of $\$2$. **Answer:** Pick $n = 3$ or $n = 4$. The maximum expected payoff is $\$2$, so the fair price to play is $\$2$. --- Probability That Uniform Plus Exponential Exceeds One [medium, Probability] --- https://quantvault.org/problem-290-probability-that-uniform-plus-exponential-exceeds-one.html Q: Let $U \sim \text{Uniform}(0, 1)$ and $V \sim \text{Exponential}(1)$ be independent random variables. Find $P(U + V > 1)$ to the nearest thousandth. A: **How to Think About It:** $U$ is bounded on $[0, 1]$ while $V$ is unbounded on $[0, \infty)$. Since $V$ has no upper bound, conditioning on $U$ is the cleaner move -- once $U = u$ is fixed, we need $V > 1 - u$, and tail probabilities of the exponential are trivial to compute. The law of total probability handles the conditioning cleanly. **Quick Estimate:** The expected value of $U + V$ is $E[U] + E[V] = 0.5 + 1 = 1.5$. The sum exceeds its own mean about half the time for symmetric distributions, but $U + V$ is skewed right (exponential has a long right tail). So we expect the probability to be noticeably above $0.5$. A rough guess: around $0.6$-$0.7$. **Approach:** Condition on $U$ and integrate using the exponential tail probability. **Formal Solution:** By the law of total probability, conditioning on $U = u$: $$P(U + V > 1) = \int_0^1 P(V > 1 - u) \cdot f_U(u) \, du$$ Since $U \sim \text{Uniform}(0,1)$, the density is $f_U(u) = 1$ on $[0, 1]$. For $V \sim \text{Exponential}(1)$, the survival function is $P(V > x) = e^{-x}$ for $x > 0$. Since $u \in [0, 1]$, we have $1 - u \in [0, 1] \subset [0, \infty)$, so: $$P(V > 1 - u \mid U = u) = e^{-(1-u)}$$ Substituting: $$P(U + V > 1) = \int_0^1 e^{-(1-u)} \, du$$ Let $w = 1 - u$, $dw = -du$; limits go from $w = 1$ to $w = 0$: $$= \int_1^0 e^{-w} (-dw) = \int_0^1 e^{-w} \, dw = \left[-e^{-w}\right]_0^1 = -e^{-1} + e^0 = 1 - e^{-1}$$ Alternatively, directly: $$\int_0^1 e^{-(1-u)} \, du = e^{-1}\int_0^1 e^{u} \, du = e^{-1}[e^u]_0^1 = e^{-1}(e - 1) = 1 - e^{-1}$$ Numerically: $$P(U + V > 1) = 1 - e^{-1} \approx 1 - 0.3679 = \boxed{0.632}$$ **Answer:** $P(U + V > 1) = 1 - e^{-1} \approx 0.632$. --- Probability That a Uniform Quotient Lies in an Interval [medium, Probability] --- https://quantvault.org/problem-291-probability-that-a-uniform-quotient-lies-in-an-interval.html Q: Let $a$ and $b$ be independent uniform random variables on $[0, 1]$. What is the probability that $a/b$ falls in the interval $[1, 3]$? A: **How to Think About It:** You have two independent draws from $[0,1]$ and you want the ratio $a/b$ to land between 1 and 3. The condition $1 \le a/b \le 3$ is equivalent to $b \le a \le 3b$ (assuming $b > 0$, which happens with probability 1). So this is a geometric probability problem -- you are computing the area of a region inside the unit square. Quick gut check: the region is a wedge-shaped slice, so the answer should be a clean fraction, probably somewhere around $1/4$ to $1/3$. **Quick Estimate:** The condition $a/b \in [1,3]$ means $a \ge b$ (so we are in the upper triangle of the unit square) and $a \le 3b$ (which cuts off a small corner near $b = 0$). The upper triangle has area $1/2$. The constraint $a \le 3b$ removes the sliver where $b < a/3$, which is a small triangle. So the answer is somewhat less than $1/2$. A rough guess: about $1/3$. **Approach:** Condition on $a$ and integrate over the feasible range of $b$. **Formal Solution:** The condition $1 \le a/b \le 3$ is equivalent to $a/3 \le b \le a$. Since $b \in [0,1]$ and $a \in [0,1]$, we need $a/3 \le b \le a$, and both bounds are automatically in $[0,1]$ when $a \in [0,1]$. Condition on $a$: $$P\left(\frac{a}{3} \le b \le a \;\middle|\; a\right) = a - \frac{a}{3} = \frac{2a}{3}$$ Now integrate over $a$: $$P\left(1 \le \frac{a}{b} \le 3\right) = \int_0^1 \frac{2a}{3} \, da = \frac{2}{3} \cdot \frac{1}{2} = \frac{1}{3}$$ **Verification (Monte Carlo):** ```python import random count = 0 N = 1_000_000 for _ in range(N): a = random.uniform(0, 1) b = random.uniform(0, 1) if 1 <= a / b <= 3: count += 1 print(count / N) # should be close to 0.333 ``` **Answer:** $P(1 \le a/b \le 3) = \dfrac{1}{3}$. --- Probability That the Product of Three Dice Is Prime [easy, Probability] --- https://quantvault.org/problem-293-probability-that-the-product-of-three-dice-is-prime.html Q: Three standard 6-sided dice are rolled. What is the probability that their product is a prime number? A: **How to Think About It:** A prime number has exactly two positive divisors: 1 and itself. For a product of three positive integers to be prime, exactly one of them must equal that prime and the other two must equal 1. There is no other way -- if two or more factors are $\ge 2$, the product is composite (or at least 4). So the question reduces to: how many ways can one die show a prime and the other two show 1? **Quick Estimate:** The primes on a die are $\{2, 3, 5\}$ -- three values. One die shows a prime (3 choices), the other two must each show 1 (probability $1/6$ each). There are 3 ways to pick which die shows the prime. So the count is $3 \times 3 = 9$ favorable outcomes out of $6^3 = 216$ total. That gives $9/216 = 1/24 \approx 4.2\%$. **Approach:** Enumerate the favorable outcomes using the factorization constraint. **Formal Solution:** Each die shows a value in $\{1, 2, 3, 4, 5, 6\}$. Let the three results be $(d_1, d_2, d_3)$. For $d_1 \cdot d_2 \cdot d_3$ to be prime, the product must have the form $p \cdot 1 \cdot 1$ for some prime $p$. The only primes achievable on a standard die are $2, 3, 5$. The favorable outcomes are all permutations of $(p, 1, 1)$ where $p \in \{2, 3, 5\}$: - Choose which die shows the prime: $\binom{3}{1} = 3$ ways - Choose which prime: $3$ choices - The remaining two dice must each show $1$: $1$ way each Total favorable outcomes: $3 \times 3 = 9$. Total outcomes: $6^3 = 216$. $$P(\text{product is prime}) = \frac{9}{216} = \frac{1}{24}$$ **Answer:** $\dfrac{1}{24} \approx 4.17\%$. --- Probability a Group Sits Together at a Circular Table [hard, Probability] --- https://quantvault.org/problem-298-probability-a-group-sits-together-at-a-circular-table.html Q: Fix a positive integer $n$. There are $3n$ people who sit around a circular table with $3n$ seats, uniformly at random. The $3n$ people are divided into 3 groups of $n$ people each. Find the probability that **at least one** of the three groups has all $n$ of its members seated adjacent to one another. Rotations of a seating arrangement are considered identical. Report the answer for $n = 5$, rounded to the nearest ten-thousandth. A: **How to Think About It:** "At least one" screams inclusion-exclusion. Let $G_i$ be the event that all $n$ people in group $i$ sit in $n$ consecutive seats. We want $P(G_1 \cup G_2 \cup G_3)$. The three events are not independent (knowing one group sits together constrains the remaining seats), but they are symmetric (all groups have the same size), so inclusion-exclusion is clean. The trick for circular permutation problems: when a group sits together, treat them as a single "block" and count circular permutations of the blocks and remaining individuals. **Quick Estimate:** For $n = 5$, we have 15 people. The chance that a specific group of 5 sits together in a circle of 15 is roughly $5! \cdot 10! / 14! \approx 0.005$, so by the union bound, $P \leq 3 \times 0.005 = 0.015$. The actual answer should be slightly below this due to the inclusion-exclusion corrections. We will see it comes out to about $0.0147$. **Approach:** Apply inclusion-exclusion with three symmetric events, counting circular permutations with blocks. **Formal Solution:** The total number of distinct circular seating arrangements of $3n$ people is $(3n - 1)!$ (fix one person's position to account for rotational equivalence). By symmetry of the three groups: $$P(G_1 \cup G_2 \cup G_3) = 3 P(G_1) - 3 P(G_1 \cap G_2) + P(G_1 \cap G_2 \cap G_3)$$ *Computing $P(G_1)$:* Treat group 1 as a single block. We now arrange $2n + 1$ objects (the block plus $2n$ individuals) in a circle: $(2n)!$ circular permutations. The $n$ people within the block can be arranged in $n!$ ways. So: $$P(G_1) = \frac{(2n)! \cdot n!}{(3n-1)!}$$ *Computing $P(G_1 \cap G_2)$:* Treat groups 1 and 2 as two blocks. We arrange $n + 2$ objects ($n$ remaining individuals plus 2 blocks) in a circle: $(n+1)!$ circular permutations. Each block has $n!$ internal arrangements. So: $$P(G_1 \cap G_2) = \frac{(n+1)! \cdot (n!)^2}{(3n-1)!}$$ *Computing $P(G_1 \cap G_2 \cap G_3)$:* All three groups are blocks. We arrange 3 blocks in a circle: $2!$ circular permutations. Each block has $n!$ internal arrangements. So: $$P(G_1 \cap G_2 \cap G_3) = \frac{2 \cdot (n!)^3}{(3n-1)!}$$ *Combining:* $$P(G_1 \cup G_2 \cup G_3) = \frac{3(2n)! \cdot n! - 3(n+1)!(n!)^2 + 2(n!)^3}{(3n-1)!}$$ *Sanity check ($n = 1$):* $$P = \frac{3 \cdot 2! \cdot 1! - 3 \cdot 2! \cdot 1 + 2 \cdot 1}{2!} = \frac{6 - 6 + 2}{2} = 1$$ Correct: with $n = 1$, each person is their own group, so every group trivially sits "together." *Evaluation at $n = 5$:* - $(3n-1)! = 14!$ - $3(2n)! \cdot n! = 3 \cdot 10! \cdot 5! = 3 \cdot 3628800 \cdot 120 = 1{,}306{,}368{,}000$ - $3(n+1)!(n!)^2 = 3 \cdot 720 \cdot 14400 = 31{,}104{,}000$ - $2(n!)^3 = 2 \cdot 120^3 = 3{,}456{,}000$ Numerator: $1{,}306{,}368{,}000 - 31{,}104{,}000 + 3{,}456{,}000 = 1{,}278{,}720{,}000$ Denominator: $14! = 87{,}178{,}291{,}200$ $$P = \frac{1{,}278{,}720{,}000}{87{,}178{,}291{,}200} \approx 0.01467$$ Rounded to the nearest ten-thousandth: $\approx 0.0147$. **Answer:** The probability is $$P = \frac{3(2n)! \cdot n! - 3(n+1)!(n!)^2 + 2(n!)^3}{(3n-1)!}$$ For $n = 5$, this evaluates to approximately $0.0147$. --- Probability of Exactly One Win Before Bankruptcy [medium, Probability] --- https://quantvault.org/problem-299-probability-of-exactly-one-win-before-bankruptcy.html Q: Fred is playing a game that costs $\$1$ per round. Each round, he wins with probability $p$ (where $0 1$), but he does not get his $\$1$ entry fee back. If he loses, he simply loses his $\$1$. Fred starts with $\$s$ in his bank (an integer with $s \geq 1$). He keeps playing until he goes bankrupt (hits $\$0$). Find the probability that Fred wins exactly once before going bankrupt. Evaluate your answer for $s = 3$, $k = 4$, and $p = 1/3$. Round to the nearest ten-thousandth. A: **How to Think About It:** The key structural observation is about *when* the single win can happen. Fred starts with $s$ dollars and loses $\$1$ per round, so if he never wins, he is bankrupt after round $s$. That means his one win must occur in one of the first $s$ rounds -- otherwise he is already broke. Once you see that constraint, the rest is just counting: how many total rounds does he play, and how many ways can that single win be placed? **Quick Estimate:** With $s = 3$, $k = 4$, $p = 1/3$, Fred's one win must come in rounds 1, 2, or 3. The total game length is $s + k = 7$ rounds (we will verify below). So we need exactly 1 success in 7 trials, but restricted to the first 3 positions. Each valid configuration has probability $p(1-p)^6 = (1/3)(2/3)^6 = 64/2187$, and there are 3 such configurations, giving $3 \times 64/2187 = 192/2187 \approx 0.0878$. That is our target. **Approach:** Count the total rounds played given exactly one win, then compute the probability by summing over the valid win positions. **Formal Solution:** Suppose Fred's single win occurs in round $j$, where $1 \leq j \leq s$. Track his bankroll: - Before round $j$: he has lost $j - 1$ rounds, so his bankroll is $s - (j - 1) = s - j + 1$. - He pays $\$1$ to play round $j$ and wins $\$k$, so his bankroll becomes $s - j + k$. - He then loses every subsequent round. Starting from $s - j + k$ dollars, he goes bankrupt after exactly $s - j + k$ more losing rounds. The total number of rounds played is $j + (s - j + k) = s + k$, regardless of when the win occurs. This is an important simplification: every valid outcome has the same length. Since the game lasts $s + k$ rounds with exactly one win and $s + k - 1$ losses, each such outcome has probability: $$p \cdot (1 - p)^{s + k - 1}$$ The win must fall in one of the first $s$ rounds (otherwise Fred goes bankrupt before winning). There are $s$ such positions, so the total probability is: $$P(\text{exactly one win before bankruptcy}) = s \cdot p \cdot (1 - p)^{s + k - 1}$$ Plugging in $s = 3$, $k = 4$, $p = 1/3$: $$3 \cdot \frac{1}{3} \cdot \left(\frac{2}{3}\right)^{6} = 1 \cdot \frac{64}{729} = \frac{64}{729} \approx 0.0878$$ **Answer:** The probability of exactly one win before bankruptcy is $s \cdot p \cdot (1 - p)^{s+k-1}$. For $s = 3$, $k = 4$, $p = 1/3$, this equals $64/729 \approx 0.0878$. --- Probability of Three of a Kind in a 5-Card Hand [medium, Probability] --- https://quantvault.org/problem-301-probability-of-three-of-a-kind-in-a-5-card-hand.html Q: A standard 52-card deck has 13 ranks (Ace through King) and 4 suits. A five-card hand is a **three-of-a-kind** if it contains exactly 3 cards of one rank and exactly 1 card each of two other distinct ranks (not a full house, not four-of-a-kind). To the nearest ten-thousandth, what is the probability that a randomly dealt 5-card hand is a three-of-a-kind? A: **How to Think About It:** Count the number of valid three-of-a-kind hands and divide by the total number of 5-card hands. The counting breaks into three independent choices: (1) which rank appears three times, (2) which 3 of the 4 suits appear for that rank, and (3) which two other ranks and which suits fill the remaining two slots. The only subtlety is making sure the two 'kicker' cards are from distinct ranks -- otherwise you would be counting full houses. **Quick Estimate:** There are 13 ranks and for each we choose 3 suits from 4: $\binom{4}{3} = 4$ ways. Then we need 2 kicker ranks from the remaining 12, and each kicker has 4 suit choices: $\binom{12}{2} \times 4^2 = 66 \times 16 = 1056$. Total three-of-a-kind hands $\approx 13 \times 4 \times 1056 = 54{,}912$. Over $\binom{52}{5} = 2{,}598{,}960$ total hands, that is about $2.1\%$. **Formal Solution:** **Total 5-card hands:** $$\binom{52}{5} = \frac{52!}{5! \cdot 47!} = 2{,}598{,}960$$ **Counting three-of-a-kind hands:** **Step 1 -- Choose the rank for the triple:** 13 ways. **Step 2 -- Choose 3 suits for the triple:** $\binom{4}{3} = 4$ ways. **Step 3 -- Choose 2 kicker ranks** (from the remaining 12 ranks, must be distinct to avoid full house): $\binom{12}{2} = 66$ ways. **Step 4 -- Choose the suit of each kicker** (4 choices each, independently): $4 \times 4 = 16$ ways. **Total three-of-a-kind hands:** $$13 \times \binom{4}{3} \times \binom{12}{2} \times 4^2 = 13 \times 4 \times 66 \times 16 = 54{,}912$$ **Probability:** $$P(\text{three-of-a-kind}) = \frac{54{,}912}{2{,}598{,}960} \approx 0.0211$$ **Answer:** $\approx \mathbf{0.0211}$ (to the nearest ten-thousandth). --- Probability via Area: Point Above a Parabola in a Unit Circle [medium, Probability] --- https://quantvault.org/problem-304-probability-via-area-point-above-a-parabola-in-a-unit-circle.html Q: A point $(x, y)$ is chosen uniformly at random from within the unit disk (the region $x^2 + y^2 \leq 1$). What is the probability that the point lies above the parabola $y = x^2$? In other words, compute $P(y > x^2)$ where $(x, y)$ is uniform on the unit disk. A: **How to Think About It:** For a uniform distribution on a region, probability is just area divided by total area. The unit disk has area $\pi$. You need the area of the region inside the disk and above $y = x^2$. This means finding where $y = x^2$ intersects $x^2 + y^2 = 1$, then integrating to find the area between the parabola and the part of the disk above it. Quick sanity check: the parabola $y = x^2$ passes through the origin and curves upward. The region above it (inside the disk) is the upper part of the disk minus the area under the parabola. Since the parabola is below $y = 0$ only for... wait, $y = x^2 \geq 0$ always, so the parabola only touches the lower half indirectly. The region $y > x^2$ is a subset of the upper half of the disk (since $x^2 \geq 0$). Rough guess: maybe 30-40% of the disk area. **Quick Estimate:** The region $y > x^2$ inside the unit disk is bounded above by the circle and below by the parabola. The circle area is $\pi \approx 3.14$. The area of the upper semicircle is $\pi/2 \approx 1.57$. The parabola cuts a chunk from below this region. The integral of $x^2$ from $-1$ to $1$ is $2/3$, so very roughly the region has area $\pi/2 - 2/3 \approx 1.57 - 0.67 = 0.90$ (very rough), giving probability $\approx 0.90/3.14 \approx 28\%$. The actual answer will be close. **Approach:** Find the intersection points of $y = x^2$ and $x^2 + y^2 = 1$, then integrate. The probability is the area of the region $\{(x,y): x^2 + y^2 \leq 1,\, y > x^2\}$ divided by $\pi$. **Formal Solution:** **Step 1 -- Intersection points:** Substitute $y = x^2$ into $x^2 + y^2 = 1$: $$x^2 + x^4 = 1 \implies x^4 + x^2 - 1 = 0$$ Let $u = x^2$: $u^2 + u - 1 = 0 \implies u = \dfrac{-1 + \sqrt{5}}{2}$ (taking the positive root). So $x^2 = \dfrac{\sqrt{5}-1}{2} \approx 0.618$ (the golden ratio minus 1), giving $x = \pm x_0$ where $x_0 = \sqrt{(\sqrt{5}-1)/2} \approx 0.786$. At the intersection, $y_0 = x_0^2 = (\sqrt{5}-1)/2 \approx 0.618$. **Step 2 -- Set up the area integral:** For $x \in [-x_0, x_0]$, the region $y > x^2$ inside the circle has $y$ ranging from $x^2$ up to $\sqrt{1-x^2}$. Outside $[-x_0, x_0]$, the parabola $y = x^2$ lies outside (or on) the circle, so there is no region to contribute. $$A = \int_{-x_0}^{x_0} \left(\sqrt{1-x^2} - x^2\right) dx$$ By symmetry in $x$: $$A = 2\int_{0}^{x_0} \left(\sqrt{1-x^2} - x^2\right) dx$$ **Step 3 -- Evaluate the integral:** Split into two parts: **Part I:** $\displaystyle 2\int_0^{x_0} \sqrt{1-x^2}\,dx$ Using the formula $\int_0^a \sqrt{1-x^2}\,dx = \dfrac{1}{2}\left(a\sqrt{1-a^2} + \arcsin(a)\right)$: With $a = x_0$ and $x_0^2 = (\sqrt{5}-1)/2$, so $1 - x_0^2 = (3-\sqrt{5})/2$ and $\sqrt{1-x_0^2} = \sqrt{(3-\sqrt{5})/2} = y_0'$ (the $y$-coordinate at intersection is $y_0 = x_0^2$; note $\sqrt{1-x_0^2} \neq y_0$). Actually $\sqrt{1-x_0^2} = \sqrt{1-(\sqrt{5}-1)/2} = \sqrt{(3-\sqrt{5})/2}$. Also $\arcsin(x_0) = \arcsin\!\left(\sqrt{(\sqrt{5}-1)/2}\right)$. So Part I $= 2 \cdot \dfrac{1}{2}\left(x_0\sqrt{1-x_0^2} + \arcsin(x_0)\right) = x_0\sqrt{1-x_0^2} + \arcsin(x_0)$. **Part II:** $\displaystyle 2\int_0^{x_0} x^2\,dx = 2 \cdot \frac{x_0^3}{3} = \frac{2x_0^3}{3}$ So: $$A = x_0\sqrt{1-x_0^2} + \arcsin(x_0) - \frac{2x_0^3}{3}$$ Numerically: $x_0 \approx 0.7862$, $\sqrt{1-x_0^2} \approx \sqrt{0.3820} \approx 0.6181$, $\arcsin(0.7862) \approx 0.9046$ rad. $$A \approx 0.7862 \times 0.6181 + 0.9046 - \frac{2 \times 0.4864}{3} \approx 0.4861 + 0.9046 - 0.3243 \approx 1.0664$$ $$P(y > x^2) = \frac{A}{\pi} \approx \frac{1.0664}{3.1416} \approx \boxed{0.3394}$$ **Answer:** $P(y > x^2) = \dfrac{1}{\pi}\left(x_0\sqrt{1-x_0^2} + \arcsin(x_0) - \dfrac{2x_0^3}{3}\right) \approx 33.9\%$, where $x_0 = \sqrt{(\sqrt{5}-1)/2}$. --- Random Walk on a Telephone Pad: Divisibility by 2 [medium, Probability] --- https://quantvault.org/problem-309-random-walk-on-a-telephone-pad-divisibility-by-2.html Q: Consider a standard telephone number pad arranged as: ``` 1 2 3 4 5 6 7 8 9 ``` You press a random digit (uniformly from 1-9), write it down, then move to a uniformly random orthogonally adjacent digit (up, down, left, or right -- no diagonals), write that down, then move again to a random adjacent digit and write it down. The three digits form a 3-digit number (e.g., if you press 5, then 6, then 9, the number is 569). What is the probability that this 3-digit number is divisible by 2? A: **How to Think About It:** A number is divisible by 2 iff its last digit is even. So we need $P(d_3 \text{ is even})$, where $d_3$ is the third digit in the random walk. The pad has a beautiful structure: lay out the digits and check their adjacencies. Odd digits $\{1, 3, 5, 7, 9\}$ only connect to even digits $\{2, 4, 6, 8\}$, and vice versa. The graph is bipartite! This means the parity of the digit alternates with every step. **Quick Estimate:** Since the grid is bipartite (odd connects only to even and vice versa), the parity of $d_3$ equals the parity of $d_1$. There are 5 odd digits and 4 even digits among $\{1, \dots, 9\}$, so $P(d_1 \text{ even}) = 4/9$ and $P(d_1 \text{ odd}) = 5/9$. Since odd $\to$ even $\to$ odd and even $\to$ odd $\to$ even, we get $P(d_3 \text{ even}) = P(d_1 \text{ even}) = 4/9$. **Approach:** Exploit the bipartite structure of the adjacency graph on the telephone pad. **Formal Solution:** First, write out the adjacency list for orthogonal neighbors: | Digit | Neighbors | Parity | Neighbor parity | |-------|-----------|--------|-----------------| | 1 | 2, 4 | odd | all even | | 2 | 1, 3, 5 | even | all odd | | 3 | 2, 6 | odd | all even | | 4 | 1, 5, 7 | even | all odd | | 5 | 2, 4, 6, 8 | odd | all even | | 6 | 3, 5, 9 | even | all odd | | 7 | 4, 8 | odd | all even | | 8 | 5, 7, 9 | even | all odd | | 9 | 6, 8 | odd | all even | The adjacency graph is bipartite: every odd digit has only even neighbors, and every even digit has only odd neighbors. This means the parity alternates deterministically at each step: - $d_1$ odd $\Rightarrow$ $d_2$ even $\Rightarrow$ $d_3$ odd - $d_1$ even $\Rightarrow$ $d_2$ odd $\Rightarrow$ $d_3$ even So $d_3$ is even if and only if $d_1$ is even. Since $d_1$ is chosen uniformly from $\{1, 2, \dots, 9\}$: $$P(d_3 \text{ even}) = P(d_1 \text{ even}) = P(d_1 \in \{2, 4, 6, 8\}) = \frac{4}{9}$$ **Answer:** The probability that the 3-digit number is divisible by 2 is $\boxed{\dfrac{4}{9}} \approx 0.444$. --- Two Eggs, d Drops: Maximum Floors [medium, Optimization] --- https://quantvault.org/problem-317-two-eggs-d-drops-maximum-floors.html Q: You have $2$ identical eggs and a building with $n$ floors. You want to find the highest floor from which an egg can be dropped without breaking -- call this the critical floor. The critical floor could be any value from $0$ (the egg breaks even on floor $1$) up to $n$ (the egg never breaks). The rules are simple: if an egg survives a drop, you can reuse it. If it breaks, it's gone. If it breaks on floor $k$, it breaks on every floor above $k$ too, and if it survives floor $k$, it survives every floor below $k$. What is the maximum value of $n$ such that you can always identify the critical floor using at most $d$ drops? Give the answer for $d = 10$. A: **How to Think About It:** This is a classic minimax search problem. You have two eggs and a budget of $d$ drops, and you want to cover as many floors as possible. With only one egg, you would have to search linearly from the bottom, checking one floor at a time -- so one egg and $d$ drops covers $d$ floors. The second egg lets you skip ahead, but once the first egg breaks, you are back to linear search with the second egg and however many drops you have left. The key tension: the higher you go for the first drop, the more floors you skip, but the more floors you need to linearly check if it breaks. You need to balance these. **Quick Estimate:** With 10 drops and 1 egg, you cover 10 floors. With 2 eggs, the second egg lets you "binary-search-ish" but not quite since you only have 2 eggs, not $\log n$. Intuitively, you can do much better than 10 but not as well as $2^{10} = 1024$. A natural strategy is to drop the first egg every $k$ floors. If it breaks at floor $k$, you linearly search floors $1$ through $k-1$ with the remaining egg. With a fixed gap of $k$ and $d$ total drops, you cover about $k \cdot (d - k + 1)$ floors, which is maximized when $k \approx d/2$, giving roughly $d^2/4 \approx 25$ floors. But we can do better by shrinking the gap as we go up. With a decreasing gap strategy, we should get roughly $d(d+1)/2 = 55$ floors. **Approach:** The optimal strategy uses decreasing gaps. On your first drop, if the egg breaks, you need to linearly search the floors below with your remaining $d-1$ drops using the second egg. So the first drop should be at floor $d$, leaving you enough drops to cover the gap. **Formal Solution:** Suppose you have $d$ drops remaining and both eggs intact. Drop the first egg at floor $f_1$. - If it breaks, you have $1$ egg and $d-1$ drops left, so you can search at most $d-1$ floors below $f_1$ (linear search). So set $f_1 = d$. - If it survives, you have $2$ eggs and $d-1$ drops left. Now drop the first egg at floor $f_1 + (d-1)$. - If it breaks, you have $1$ egg and $d-2$ drops, so you can search at most $d-2$ floors. The gap of $d-1$ floors (minus the one you already tested) leaves exactly $d-2$ floors to check. This works. - If it survives, repeat with gap $d-2$, then $d-3$, and so on. The floors tested by the first egg are: $$f_1 = d, \quad f_2 = d + (d-1), \quad f_3 = d + (d-1) + (d-2), \quad \ldots$$ The highest reachable floor is: $$f_d = d + (d-1) + (d-2) + \cdots + 1 = \frac{d(d+1)}{2}$$ If the egg survives every first-egg drop including floor $\frac{d(d+1)}{2}$, then the critical floor is $\frac{d(d+1)}{2}$ (the building's top floor). Every possible critical floor from $0$ to $\frac{d(d+1)}{2}$ is uniquely identifiable. For a building with $n = \frac{d(d+1)}{2} + 1$ floors, if the egg survives all drops through floor $\frac{d(d+1)}{2}$, you cannot distinguish whether the critical floor is $\frac{d(d+1)}{2}$ or $\frac{d(d+1)}{2} + 1$. So $\frac{d(d+1)}{2}$ is the maximum. For $d = 10$: $$n = \frac{10 \times 11}{2} = 55$$ **Answer:** The maximum number of floors is $n = \frac{d(d+1)}{2}$. For $d = 10$, the answer is $\boxed{55}$. --- Two-Digit Number Whose Digit Product Equals Half Itself [easy, Brain Teaser] --- https://quantvault.org/problem-320-two-digit-number-whose-digit-product-equals-half-itself.html Q: Find a two-digit number with the following property: when you multiply its two digits together, the result equals half the original number. For example, for the number $36$, the digits are $3$ and $6$, and $3 \times 6 = 18 = 36/2$. Does $36$ satisfy the condition? Verify, and determine if there are other solutions. A: **How to Think About It:** Write the two-digit number algebraically: if the tens digit is $x$ and the ones digit is $y$, the number equals $10x + y$. The condition becomes $xy = (10x + y)/2$. From here it is a small Diophantine search -- clear denominators, rearrange, and use divisibility constraints to narrow down candidates. **Quick Estimate:** The product $xy$ grows quadratically, while the number $10x + y$ grows linearly in the digits. For large digits like $x = y = 9$, the product ($81$) far exceeds half the number ($49.5$) -- so solutions, if any, will involve moderate digit sizes. For $x = 1$, $y = 8$: product $= 8$, half of $18 = 9$ -- close but no. For $x = 3$, $y = 6$: product $= 18$, half of $36 = 18$ -- yes. So $36$ works. Expect at most a couple solutions. **Approach:** Set up the Diophantine equation and use divisibility arguments to enumerate all solutions. **Formal Solution:** Let the tens digit be $x$ ($1 \leq x \leq 9$) and the ones digit be $y$ ($0 \leq y \leq 9$), so the number is $10x + y$. The condition is: $$xy = \frac{10x + y}{2} \implies 2xy = 10x + y \implies y(2x - 1) = 10x \implies y = \frac{10x}{2x-1}$$ For $y$ to be a positive integer, $(2x-1)$ must divide $10x$. Write: $$10x = 5(2x-1) + 5 \implies y = 5 + \frac{5}{2x-1}$$ So $(2x-1)$ must divide $5$. The positive odd divisors of $5$ are $1$ and $5$: - $2x - 1 = 1 \implies x = 1$: $y = 5 + 5 = 10$. Not a valid digit. - $2x - 1 = 5 \implies x = 3$: $y = 5 + 1 = 6$. Valid. Number: $36$. Verification: $3 \times 6 = 18 = 36/2$. Confirmed. **Answer:** The unique two-digit solution is $\boxed{36}$. --- Aces Before Kings: Probability via Exchangeability [medium, Probability] --- https://quantvault.org/problem-325-aces-before-kings-probability-via-exchangeability.html Q: You shuffle a standard 52-card deck uniformly at random and reveal cards one by one without replacement. Let $T$ be the first time you have seen all four aces or all four kings (whichever group is completed first). Compute $P(\text{aces finish first})$ -- that is, the probability that all four aces appear before all four kings are complete -- and justify your answer rigorously using a symmetry or exchangeability argument. No lengthy enumeration. A: **How to Think About It:** Two heuristics crack this without any enumeration. First, *discard irrelevant randomness*: the 44 cards that are neither aces nor kings only pad the sequence — they never change which of the 8 special cards comes before which. By exchangeability of a uniform shuffle, the relative order of the 4 aces and 4 kings is a uniformly random arrangement of 4 A's and 4 K's. Second, *reframe a race as a statement about the last runner*: "aces finish first" means the 4th ace appears before the 4th king — equivalently, the very last of the 8 special cards is a **king**. The stopping time $T$ in the problem statement is a red herring: you never need its distribution, only the relative order of the specials. **Quick Estimate:** Pure symmetry, runnable in one breath. Swapping the labels ace $\leftrightarrow$ king is a bijection on shuffles that preserves the uniform measure (relabeling cards doesn't change "uniformly random") and maps the event "aces finish first" onto "kings finish first." So the two events have equal probability. Ties are impossible — cards are revealed one at a time and the 4th ace and 4th king are different physical cards — so the two events partition everything: each has probability $\tfrac12$. Sanity check on the smallest case: 1 ace vs. 1 king in a deck — the ace "group" completes first iff the ace precedes the king, which is $\tfrac12$ by symmetry. Consistent. **Approach:** Reduce to the 8 special cards by exchangeability, then ask which group owns the last special card. **Formal Solution:** **Step 1 — reduction to the specials.** Condition on the set of 8 positions in the 52-card deck occupied by the aces and kings. Under a uniform shuffle, every assignment of the 4 ace labels and 4 king labels to those 8 positions is equally likely — there are $\binom{8}{4} = 70$ equally likely assignments. The other 44 cards are irrelevant to which group completes first. **Step 2 — reframe via the last special card.** "Aces finish first" holds iff the 4th (last) ace occurs strictly before the 4th (last) king among the specials, which holds iff the special card in the last of the 8 special positions is a king. **Step 3 — symmetry.** By exchangeability, the last special position is equally likely to hold any of the 8 special cards, and 4 of them are kings: $$P(\text{last special card is a king}) = \frac{4}{8} = \frac{1}{2}$$ Ties are impossible (the two groups are disjoint and cards are revealed one at a time), so "aces first" and "kings first" partition the sample space, and the label-swap bijection confirms each has probability $\tfrac12$. $$\boxed{P(\text{aces finish first}) = \frac{1}{2}}$$ **Generalization:** If one group has $a$ cards and the other $b$, the group of size $a$ finishes first with probability $\dfrac{b}{a+b}$ — the identical argument asks whether the last of the $a+b$ specials belongs to the *other* group. (Check: $a = b = 4$ gives $4/8 = 1/2$.) **Answer:** $P(\text{aces finish first}) = \dfrac{1}{2}$. --- Bayesian Coin Inference and Kelly Betting with Beta Prior [hard, Expectation] --- https://quantvault.org/problem-334-bayesian-coin-inference-and-kelly-betting-with-beta-prior.html Q: A coin has an unknown bias $p$. You start with a $\text{Beta}(a, b)$ prior on $p$, then observe $H$ heads and $T$ tails. 1. Compute the posterior distribution of $p$ and the posterior predictive probability that the next single flip is heads. 2. Generalize: what is the posterior predictive probability that exactly $h$ of the next $k$ flips are heads? 3. You are offered an even-money bet: you pay $\$1$ to play and receive $\$2$ if the next flip matches your call (heads or tails). You want to maximize expected logarithmic utility (Kelly criterion). Which side should you bet on, what fraction of your bankroll should you wager, and what is the expected log growth rate per bet? A: **How to Think About It:** This is the canonical Bayesian conjugate setup -- Beta prior meets Binomial likelihood and produces a Beta posterior. The posterior predictive for future flips is a Beta-Binomial, which accounts for your remaining uncertainty about $p$. The Kelly part is a direct application: given your posterior belief about the coin's bias, Kelly tells you to bet on the more likely side with a specific fraction. **Quick Estimate:** Suppose $a = b = 1$ (uniform prior), $H = 7$, $T = 3$. Posterior is $\text{Beta}(8, 4)$, posterior mean $\hat{p} = 8/12 = 2/3$. Kelly fraction for an even-money bet on heads: $f^{*} = 2\hat{p} - 1 = 1/3$, so bet one-third of your bankroll on heads. **Formal Solution:** **Part (i): Posterior distribution** The Beta-Binomial conjugacy gives: $$p \mid \text{data} \sim \text{Beta}(a + H, \, b + T)$$ Let $a' = a + H$ and $b' = b + T$ for convenience. The posterior mean is: $$E[p \mid \text{data}] = \frac{a'}{a' + b'}$$ The posterior predictive probability that the next flip is heads: $$P(\text{next flip} = H \mid \text{data}) = E[p \mid \text{data}] = \frac{a + H}{a + b + H + T}$$ This is just the posterior mean -- a standard result from exchangeability. **Part (ii): Posterior predictive for $k$ flips** The posterior predictive distribution for the number of heads $h$ in the next $k$ flips is Beta-Binomial: $$P(h \mid k, a', b') = \binom{k}{h} \frac{B(a' + h, \, b' + k - h)}{B(a', b')}$$ where $B(\cdot, \cdot)$ is the Beta function. This can be written using rising factorials: $$P(h \mid k, a', b') = \binom{k}{h} \frac{\prod_{i=0}^{h-1}(a' + i) \cdot \prod_{j=0}^{k-h-1}(b' + j)}{\prod_{m=0}^{k-1}(a' + b' + m)}$$ The mean of this distribution is $k \cdot a'/(a' + b')$ and the variance is larger than the Binomial variance because of the extra uncertainty in $p$ (over-dispersion). **Part (iii): Kelly criterion** Let $q = P(\text{next flip} = H \mid \text{data}) = a'/(a' + b')$ be your posterior probability of heads. **Decision rule:** Bet on heads if $q > 1/2$ (i.e., $a' > b'$, equivalently $a + H > b + T$). Bet on tails if $q 1/2\text{)}$$ $$f^{*} = 1 - 2q \quad \text{(if betting tails, i.e., } q 1/2$. **Derivation:** You wager fraction $f$ of your bankroll. If you win (probability $q$ for heads), your bankroll multiplies by $(1 + f)$. If you lose (probability $1 - q$), it multiplies by $(1 - f)$. The expected log growth rate is: $$G(f) = q \ln(1 + f) + (1 - q) \ln(1 - f)$$ Maximizing: $G'(f) = q/(1+f) - (1-q)/(1-f) = 0$, which gives $f^{*} = 2q - 1$ (assuming $q > 1/2$, so we are betting heads). **Expected log growth rate at the Kelly fraction:** $$G(f^{*}) = q \ln(2q) + (1 - q) \ln(2(1-q))$$ $$= \ln 2 + q \ln q + (1-q) \ln(1-q)$$ $$= \ln 2 - \mathcal{H}(q)$$ where $\mathcal{H}(q) = -q \ln q - (1-q) \ln(1-q)$ is the binary entropy. The growth rate equals $\ln 2$ minus the entropy of your belief -- the more certain you are, the faster you grow. **Answer:** 1. Posterior: $p \mid \text{data} \sim \text{Beta}(a+H, b+T)$. Posterior predictive for next flip being heads: $(a+H)/(a+b+H+T)$. 2. Posterior predictive for $h$ heads in $k$ flips: $\text{Beta-Binomial}(k, a+H, b+T)$. 3. Bet on whichever side has posterior probability $> 1/2$. Kelly fraction: $f^{*} = |2q - 1|$ where $q = (a+H)/(a+b+H+T)$. Expected log growth: $\ln 2 - \mathcal{H}(q)$. --- Conditional Marble Draw with Three Colors [medium, Probability] --- https://quantvault.org/problem-349-conditional-marble-draw-with-three-colors.html Q: A bag contains $r$ red, $y$ yellow, and $b$ blue marbles, with $n = r + y + b$ total. You draw 2 marbles without replacement. Given that at least one of the two drawn marbles is blue, what is the probability that neither marble is red? A: **How to Think About It:** This is a conditional probability exercise -- you condition on seeing at least one blue, and want the chance that both marbles come from the non-red colors (blue and yellow). The key move is to set up the fraction correctly: numerator is the count of 2-marble draws that have at least one blue AND no red, denominator is all 2-marble draws with at least one blue. Both are easiest to compute via complements. **Quick Estimate:** Suppose equal counts: $r = y = b = 10$, so $n = 30$. Total draws: $\binom{30}{2} = 435$. Draws with at least one blue: $435 - \binom{20}{2} = 435 - 190 = 245$. Draws with at least one blue and no red: draws from $\{y, b\}$ with at least one blue = $\binom{20}{2} - \binom{10}{2} = 190 - 45 = 145$. Probability: $145/245 = 29/49 \approx 0.592$. **Approach:** Use the complement method for "at least one blue" events in both numerator and denominator. **Formal Solution:** Let $A$ = "at least one blue" and $B$ = "no red marble drawn." We want $P(B \mid A)$. $$P(B \mid A) = \frac{P(A \cap B)}{P(A)}$$ **Denominator** -- $P(A)$: Probability of at least one blue. $$P(A) = 1 - \frac{\binom{n-b}{2}}{\binom{n}{2}} = \frac{\binom{n}{2} - \binom{r+y}{2}}{\binom{n}{2}}$$ **Numerator** -- $P(A \cap B)$: Both marbles are from $\{y, b\}$ (no red) AND at least one is blue. $$P(A \cap B) = \frac{\binom{y+b}{2} - \binom{y}{2}}{\binom{n}{2}}$$ The subtraction removes the case where both are yellow (non-red but also non-blue). So: $$P(B \mid A) = \frac{\binom{y+b}{2} - \binom{y}{2}}{\binom{n}{2} - \binom{r+y}{2}}$$ Expanding the binomial coefficients: $$= \frac{(y+b)(y+b-1) - y(y-1)}{n(n-1) - (r+y)(r+y-1)}$$ $$= \frac{2by + b(b-1)}{2bn - b(b-1) - 2b \cdot 0}$$ Simplifying the numerator: $(y+b)(y+b-1) - y(y-1) = b^2 + 2by - b = b(b + 2y - 1)$. Simplifying the denominator: $n(n-1) - (n-b)(n-b-1) = 2b(n-b) - b + b = b(2n - 2b - 1 + 1) = b(2n - b - 1)$... let's be more careful: $n(n-1) - (r+y)(r+y-1) = n^2 - n - (n-b)^2 + (n-b) = 2nb - b^2 - b = b(2n - b - 1)$. So: $$P(B \mid A) = \frac{b(b + 2y - 1)}{b(2n - b - 1)} = \frac{b + 2y - 1}{2n - b - 1}$$ **Verification with equal counts** ($r = y = b = 10$, $n = 30$): $(10 + 20 - 1)/(60 - 10 - 1) = 29/49 \approx 0.592$. This matches the quick estimate. **Answer:** $P(\text{no red} \mid \text{at least one blue}) = \dfrac{b + 2y - 1}{2n - b - 1}$, where $n = r + y + b$. The answer depends on the composition of the bag; with equal counts ($r = y = b$), it equals $(3b - 1)/(5b - 1)$. --- Conditional Probability Under Sample Space Reduction [easy, Probability] --- https://quantvault.org/problem-350-conditional-probability-under-sample-space-reduction.html Q: You have a set $B$ with $|B| = n$ elements, and a subset $A \subseteq B$ with $|A| = k$. You draw a uniformly random element from $B$. For any element $b \in B \setminus A$, what is the probability that $b$ was selected, given that the selected element is not in $A$? Derive the general formula, then evaluate it for $n = 10$, $k = 4$. A: **How to Think About It:** The heuristic is *conditioning a uniform distribution on a subset gives the uniform distribution on that subset*. Being told "the draw is not in $A$" throws away $k$ of the $n$ equally likely outcomes and cannot break the symmetry among the $n - k$ survivors — no element of $B \setminus A$ is favored over any other, so each must get probability $1/(n-k)$. There is no real trap here, but the interviewer wants to see you know *why* the shortcut is legal: the mechanical reason is renormalization — every surviving outcome keeps its original mass $1/n$ and you divide by the total surviving mass $(n-k)/n$. **Quick Estimate:** For $n = 10$, $k = 4$: the element $b$ starts with mass $\tfrac{1}{10}$; the conditioning event "not in $A$" contains $10 - 4 = 6$ elements, so it has mass $\tfrac{6}{10}$; renormalizing, $b$ gets $\tfrac{1/10}{6/10} = \tfrac{1}{6} \approx 0.167$. Symmetry cross-check: 6 equally-likely survivors must each get $\tfrac16$ — same number, two independent one-line routes. **Approach:** Definition of conditional probability, using that $\{x = b\}$ is a sub-event of $\{x \notin A\}$. **Formal Solution:** Let $x$ denote the selected element. We want $P(x = b \mid x \notin A)$ for a fixed $b \in B \setminus A$. By the definition of conditional probability: $$P(x = b \mid x \notin A) = \frac{P(x = b,\, x \notin A)}{P(x \notin A)}$$ **Numerator.** Since $b \notin A$, the event $\{x = b\}$ already implies $\{x \notin A\}$, so the joint event is just $\{x = b\}$: $$P(x = b,\, x \notin A) = P(x = b) = \frac{1}{n}$$ **Denominator.** $A$ contains $k$ of the $n$ equally likely elements: $$P(x \notin A) = 1 - \frac{k}{n} = \frac{n - k}{n}$$ **Combine:** $$P(x = b \mid x \notin A) = \frac{1/n}{(n-k)/n} = \boxed{\frac{1}{n-k}}$$ Since this value is the same for every $b \in B \setminus A$ and there are $n - k$ such elements summing to $1$, the conditional distribution is exactly uniform on $B \setminus A$ — confirming the symmetry intuition. **Answer:** $P(x = b \mid x \notin A) = \dfrac{1}{n-k}$ in general; for $n = 10$, $k = 4$ this is $\boxed{\dfrac{1}{6}}$. --- Conditional Probability of a Weekend Party Day [easy, Probability] --- https://quantvault.org/problem-354-conditional-probability-of-a-weekend-party-day.html Q: You plan to hold a party on the first sunny day of the weekend. Independently, it rains on Saturday with probability $0.70$ and on Sunday with probability $0.60$. The rule is simple: if Saturday is sunny, the party is Saturday. If Saturday is rainy but Sunday is sunny, the party is Sunday. If both days are rainy, the party doesn't happen. You fast-forward to Monday and learn that the party did happen -- your friends had a great time. Given this information, what is the conditional probability that the party was held on Saturday? A: **How to Think About It:** This is a clean Bayes' theorem problem with a small event space. You have two independent weather draws and a deterministic rule (party on the *first* sunny day). Once you learn the party happened, you need to update your beliefs about which day it was. Before any math, think about it: Saturday has a 30% chance of being sunny, but if Saturday is sunny the party is guaranteed to be Saturday -- it's the first sunny day. Sunday only gets the party if Saturday fails. So Saturday has a slight edge in the posterior even though Sunday is the sunnier day. **Quick Estimate:** P(party on Saturday) = P(Saturday sunny) = $0.30$. P(party on Sunday) = P(Saturday rainy) $\times$ P(Sunday sunny) = $0.70 \times 0.40 = 0.28$. Total probability the party happens = $0.30 + 0.28 = 0.58$. So the posterior for Saturday is roughly $0.30 / 0.58 \approx 0.517$. Just above half -- Saturday has a slight edge because it gets "first dibs." **Approach:** Apply Bayes' theorem directly. Define the events and compute the conditional probability. **Formal Solution:** Let $S$ = Saturday is sunny, $U$ = Sunday is sunny. We are given: $$P(S) = 0.30, \quad P(U) = 0.40$$ with $S$ and $U$ independent. Define: - $A$ = party held on Saturday = $\{S\}$ (Saturday is sunny, so party is Saturday regardless of Sunday) - $B$ = party held on Sunday = $\{S^c \cap U\}$ (Saturday rainy, Sunday sunny) - $H$ = party happened = $A \cup B$ Compute the probabilities: $$P(A) = P(S) = 0.30$$ $$P(B) = P(S^c) \cdot P(U) = 0.70 \times 0.40 = 0.28$$ $$P(H) = P(A) + P(B) = 0.30 + 0.28 = 0.58$$ We can verify: $P(H) = 1 - P(\text{both rainy}) = 1 - 0.70 \times 0.60 = 1 - 0.42 = 0.58$. Consistent. By Bayes' theorem: $$P(A \mid H) = \frac{P(A)}{P(H)} = \frac{0.30}{0.58} = \frac{30}{58} = \frac{15}{29}$$ **Answer:** The conditional probability the party was held on Saturday is $\dfrac{15}{29} \approx 0.5172$. --- Counting Proper Parenthesizations (Catalan Numbers) [medium, Combinatorics] --- https://quantvault.org/problem-357-counting-proper-parenthesizations-catalan-numbers.html Q: A *proper parenthesization* of length $2n$ is a string of $n$ left parentheses and $n$ right parentheses such that at every prefix, the number of right parentheses never exceeds the number of left parentheses. For example, with $n = 3$: the strings $()()()$, $((()))$, and $()(())$ are valid, while $())(()$ and $(((())$ are not. How many proper parenthesizations of length $2n$ are there? Compute the answer for $n = 6$. A: **How to Think About It:** This is the classic Catalan number problem. The trick is to count the *bad* strings and subtract from the total. The total number of strings with $n$ left and $n$ right parentheses is $\binom{2n}{n}$ -- just choose which $n$ of the $2n$ positions are left parens. The hard part is counting invalid strings, and the elegant approach uses a reflection/bijection argument. **Quick Estimate:** For $n = 6$, $\binom{12}{6} = 924$ total strings. The Catalan number is $\frac{1}{n+1}\binom{2n}{n} = \frac{924}{7} = 132$. So about $132/924 \approx 14\%$ of all balanced strings are valid -- which makes sense since the constraint is quite restrictive. **Approach:** Use the reflection bijection (Andre's reflection principle) to count invalid strings. **Formal Solution:** **Step 1: Total count.** There are $2n$ positions, and we choose $n$ of them for left parentheses. The rest are right parentheses. Total: $\binom{2n}{n}$. **Step 2: Count invalid strings.** A string is invalid if at some point the number of right parentheses exceeds the number of left parentheses. Consider the first position where this happens -- at that point, you have seen one more right paren than left paren. Now apply the reflection trick: from that point onward, swap every left paren with a right paren and vice versa. Before the swap point, the parens are unchanged. After the swap, you've flipped one extra left paren to right (and one fewer right to left), so the new string has $(n+1)$ right parentheses and $(n-1)$ left parentheses. This mapping is a bijection between invalid strings of type $(n, n)$ and *all* strings of type $(n-1, n+1)$. The number of such strings is: $$\binom{2n}{n+1}$$ **Step 3: Valid count.** $$C_n = \binom{2n}{n} - \binom{2n}{n+1} = \frac{(2n)!}{n! \, n!} - \frac{(2n)!}{(n+1)! \, (n-1)!}$$ Factor out: $$C_n = \frac{(2n)!}{n! \, n!} \left(1 - \frac{n!}{(n+1)(n-1)!}\cdot\frac{n!}{n!}\right) = \frac{1}{n+1}\binom{2n}{n}$$ This is the $n$-th Catalan number. **Step 4: Evaluate at $n = 6$.** $$C_6 = \frac{1}{7}\binom{12}{6} = \frac{924}{7} = 132$$ **Answer:** The number of proper parenthesizations of length $2n$ is the $n$-th Catalan number $C_n = \frac{1}{n+1}\binom{2n}{n}$. For $n = 6$, the answer is $\boxed{132}$. --- Defective Cat Plushie [medium, Expectation] --- https://quantvault.org/problem-361-defective-cat-plushie.html Q: A factory produces cat plushies one at a time. Each plushie is either good or defective, and the quality of each plushie depends on the one before it: - If the current plushie is **good**, the next one is also good with probability $\frac{4}{5}$ (and defective with probability $\frac{1}{5}$). - If the current plushie is **defective**, the next one is also defective with probability $\frac{3}{5}$ (and good with probability $\frac{2}{5}$). The factory just produced a defective plushie. What is the expected number of plushies produced until the next defective one (including that next defective plushie)? A: **How to Think About It:** This is a first-passage-time problem on a two-state Markov chain. The states are D (defective) and G (good), and you want the expected number of steps to return to D starting from D. The key insight is that if the very next plushie is good, you have shifted into the good state, and from there you need to figure out how long it takes to get back to defective. So you set up two unknowns -- the expected hitting time from each state -- and solve a small linear system. **Quick Estimate:** From state D, there is a $\frac{3}{5}$ chance you get a defective immediately (1 step). If you escape to G, the good state is sticky ($\frac{4}{5}$ self-loop), so you expect to stay there about $\frac{1}{1 - 4/5} = 5$ steps before a defective appears. So a rough estimate is $\frac{3}{5}(1) + \frac{2}{5}(1 + 5) = \frac{3}{5} + \frac{12}{5} = 3$. That is actually exact here because the system is small enough that the quick estimate IS the formal solution. **Approach:** Define $E_D$ and $E_G$ as the expected number of plushies produced until the next defective, given the most recent plushie was defective or good, respectively. Condition on the next plushie and apply the law of total expectation. **Formal Solution:** Let $E_D = E[N \mid \text{current is D}]$ and $E_G = E[N \mid \text{current is G}]$. From state G: $$E_G = \frac{1}{5}(1) + \frac{4}{5}(1 + E_G)$$ The first term: with probability $\frac{1}{5}$ the next plushie is defective, taking 1 step. The second term: with probability $\frac{4}{5}$ the next plushie is good, taking 1 step and then you are still in state G. Solving: $$E_G = 1 + \frac{4}{5} E_G \implies \frac{1}{5} E_G = 1 \implies E_G = 5$$ This makes sense: in state G, each plushie has a $\frac{1}{5}$ chance of being defective, so the waiting time is geometric with mean 5. From state D: $$E_D = \frac{3}{5}(1) + \frac{2}{5}(1 + E_G)$$ With probability $\frac{3}{5}$, the next plushie is defective immediately (1 step). With probability $\frac{2}{5}$, the next plushie is good (1 step), and then you need $E_G$ more steps from state G. Plugging in $E_G = 5$: $$E_D = \frac{3}{5} + \frac{2}{5}(1 + 5) = \frac{3}{5} + \frac{12}{5} = \frac{15}{5} = 3$$ **Answer:** The expected number of plushies produced until the next defective one is $E_D = 3$. --- Even vs. Odd Heads: Does Symmetry Force Fairness? [medium, Probability] --- https://quantvault.org/problem-365-even-vs-odd-heads-does-symmetry-force-fairness.html Q: You flip a coin 10 times. The coin has an unknown probability $p$ of landing heads on each flip, and the flips are independent. Suppose you are told that the probability of getting an even number of heads (0, 2, 4, 6, 8, or 10) is exactly equal to the probability of getting an odd number of heads. Does this guarantee the coin is fair -- i.e., must $p = 1/2$? A: **How to Think About It:** At first glance this might feel like a symmetry argument -- if even and odd are equally likely, maybe the coin is just balanced. But be careful: symmetry in outcomes does not always imply symmetry in the underlying parameter. The question is whether P(even) = P(odd) has a unique solution in $p$, or whether some biased coin could also satisfy it. The cleanest way to attack this is to write out P(even) - P(odd) as a closed-form function of $p$ and ask when it equals zero. If that function has only one root in $[0,1]$, the implication holds. **Approach:** Express the difference P(even) - P(odd) using the binomial theorem, reduce to a power of $(1 - 2p)$, and check for roots. **Formal Solution:** Let $X \sim \text{Binomial}(10, p)$. Define: $$D(p) = P(X \text{ even}) - P(X \text{ odd}) = \sum_{k=0}^{10} (-1)^k \binom{10}{k} p^k (1-p)^{10-k}$$ The key trick: the factor $(-1)^k$ lets us collapse this sum using the binomial theorem. Notice that: $$\sum_{k=0}^{10} (-1)^k \binom{10}{k} p^k (1-p)^{10-k} = \bigl((1-p) + (-1) \cdot p\bigr)^{10} = (1 - 2p)^{10}$$ So: $$D(p) = (1 - 2p)^{10}$$ Setting $D(p) = 0$: $$(1 - 2p)^{10} = 0 \implies 1 - 2p = 0 \implies p = \frac{1}{2}$$ Since $(1 - 2p)^{10}$ is a perfect 10th power, zero is its only root, and it occurs uniquely at $p = 1/2$. **Answer:** Yes -- P(even heads) = P(odd heads) if and only if $p = 1/2$. The condition uniquely forces the coin to be fair. --- Expected Absolute Value of a Normal Random Variable [medium, Expectation] --- https://quantvault.org/problem-366-expected-absolute-value-of-a-normal-random-variable.html Q: Let $X \sim N(0, \sigma^2)$. Compute $E[|X|]$ in terms of $\sigma$, then evaluate at $\sigma^2 = 2\pi$. A: **How to Think About It:** The absolute value kills the sign of $X$ and folds the distribution onto the positive real line. Since $X$ is symmetric around $0$, $E[|X|] = 2 \cdot E[X \cdot \mathbf{1}_{X > 0}]$. The quickest path is to factor out $\sigma$ using the scaling property of Gaussians, reduce to the standard normal case, then evaluate the resulting integral via a $u$-substitution. **Quick Estimate:** A rough bound: $E[|X|] \leq \sqrt{E[X^2]} = \sigma$ by Jensen's inequality (since $|\cdot|$ is concave on the positive reals... actually convex, so Jensen goes the other way -- but $\sqrt{E[X^2]} = \sigma$ gives the right order). For $\sigma^2 = 2\pi$, we get $\sigma = \sqrt{2\pi} \approx 2.51$, so the answer should be somewhat less than $2.51$. A rough mental model: the mean of the half-normal is about $80\%$ of $\sigma$, so roughly $0.8 \times 2.51 \approx 2$. Spoiler: the exact answer is $2$. **Approach:** Factor using $X = \sigma Z$ where $Z \sim N(0,1)$, then apply LOTUS to compute $E[|Z|]$. **Formal Solution:** Since $X = \sigma Z$ with $Z \sim N(0,1)$: $$E[|X|] = E[|\sigma Z|] = \sigma \, E[|Z|]$$ Apply LOTUS with the standard normal density $\phi(x) = \frac{1}{\sqrt{2\pi}} e^{-x^2/2}$: $$E[|Z|] = \int_{-\infty}^{\infty} |x| \cdot \frac{1}{\sqrt{2\pi}} e^{-x^2/2}\, dx$$ Both $|x|$ and $\phi(x)$ are even functions, so the integrand is even. Fold the integral onto $[0, \infty)$: $$E[|Z|] = \frac{2}{\sqrt{2\pi}} \int_0^{\infty} x \, e^{-x^2/2}\, dx$$ Substitute $u = x^2/2$, so $du = x\, dx$: $$E[|Z|] = \frac{2}{\sqrt{2\pi}} \int_0^{\infty} e^{-u}\, du = \frac{2}{\sqrt{2\pi}} \cdot 1 = \sqrt{\frac{2}{\pi}}$$ Therefore: $$E[|X|] = \sigma \sqrt{\frac{2}{\pi}}$$ **Answer:** $E[|X|] = \sigma \sqrt{\dfrac{2}{\pi}}$. For $\sigma^2 = 2\pi$, we have $\sigma = \sqrt{2\pi}$, so: $$E[|X|] = \sqrt{2\pi} \cdot \sqrt{\frac{2}{\pi}} = \sqrt{4} = 2$$ --- Expected Adjacent Couples at a Round Table [medium, Expectation] --- https://quantvault.org/problem-368-expected-adjacent-couples-at-a-round-table.html Q: $n$ married couples are seated uniformly at random around a circular table with $2n$ seats. What is the expected number of couples who end up sitting next to each other? For concreteness, "sitting next to each other" means a husband and wife occupy two consecutive seats in the circle. A couple counts as adjacent regardless of who else sits nearby. A: **How to Think About It:** Whenever you see "expected number of [something] in a random arrangement," your first reflex should be linearity of expectation with indicator variables. Define one indicator per couple, compute a single probability, and multiply. No need to think about joint distributions or complicated combinatorics -- linearity does not care about dependence. The key probability question is: given a random circular seating of $2n$ people, what is the chance that two specific people end up next to each other? **Quick Estimate:** For $n = 4$ (8 people around a table), fix one spouse. She has 2 neighbors out of $2n - 1 = 7$ remaining people, so the probability her partner is adjacent is $2/7$. Expected adjacent couples: $4 \times 2/7 = 8/7 \approx 1.14$. That feels right -- with 4 couples, you expect a bit more than one lucky pair. For large $n$, the answer should approach 1 since $2n/(2n-1) \to 1$. **Approach:** Indicator variables plus a straightforward counting argument for circular adjacency. **Formal Solution:** Define $X_i = \mathbf{1}\{\text{couple } i \text{ sits adjacent}\}$ for $i = 1, 2, \ldots, n$. We want $E\left[\sum_{i=1}^{n} X_i\right]$. By linearity of expectation: $$E\left[\sum_{i=1}^{n} X_i\right] = \sum_{i=1}^{n} E[X_i] = n \cdot P(\text{a specific couple is adjacent})$$ To compute $P(\text{adjacent})$: fix one member of the couple (say the wife) at an arbitrary seat. In a circular arrangement of $2n$ people, fixing one person eliminates the rotational symmetry, and the remaining $2n - 1$ people are equally likely to sit in any of the remaining $2n - 1$ seats. The wife has exactly 2 neighboring seats (one on her left, one on her right). So the probability her husband occupies one of those 2 seats is: $$P(\text{adjacent}) = \frac{2}{2n - 1}$$ Therefore: $$E[\text{adjacent couples}] = n \cdot \frac{2}{2n - 1} = \frac{2n}{2n - 1}$$ **Sanity checks:** - $n = 2$ (4 people): $E = 4/3 \approx 1.33$. Two couples around 4 seats -- more often than not, exactly one couple ends up adjacent, occasionally both do, giving an average above 1. - $n = 4$ (8 people): $E = 8/7 \approx 1.14$, matching our quick estimate. - $n \to \infty$: $E \to 1$. As the table gets huge, each couple has a vanishingly small chance of sitting together, but there are many couples, and these effects balance to give an expected count of about 1. Note: for $n = 1$ (2 people, 2 seats), the couple is always adjacent, so $E = 1$. The formula gives $2/1 = 2$, which is off because with only 2 seats in a circle each person has just 1 distinct neighbor, not 2. The formula $2/(2n-1)$ assumes $n \geq 2$ so that each person genuinely has 2 distinct neighbors. **Answer:** $E[\text{adjacent couples}] = \dfrac{2n}{2n - 1}$ for $n \geq 2$. --- Expected Distance to Escape a Room [medium, Expectation] --- https://quantvault.org/problem-373-expected-distance-to-escape-a-room.html Q: You are stuck in a room with 4 doors. Two doors lead to freedom: one down a path of length 5 miles, the other 7 miles. The other two doors loop you right back to this same room -- one after 2 miles, the other after 4 miles. At every step you pick a door uniformly at random. What is the expected total distance you travel before reaching freedom? A: **How to Think About It:** The key structure here is that when you take a looping door, you end up in exactly the same situation you started in -- same room, same 4 doors, same probabilities. Nothing has changed except you have racked up some extra miles. This memoryless restart is the classic setup for a first-passage expectation equation. Let $E$ be your expected total distance. You write one equation for $E$ by conditioning on the first door chosen, and then solve for $E$ algebraically. **Quick Estimate:** Two of the four doors lead to freedom at distances 5 and 7, averaging 6 miles -- but you do not always escape on the first try. The two looping doors cost you 2 and 4 miles on average, so 3 miles wasted before restarting. You escape with probability $1/2$ each round, so the expected number of rounds is 2. Each round costs either an escape (average 6 miles) or a loop (average 3 miles). A rough estimate: you take about 2 rounds, each costing roughly $\frac{1}{2}(6) + \frac{1}{2}(3) = 4.5$ miles, giving $2 \times 4.5 = 9$ miles. That matches the exact answer, so the intuition is clean. **Approach:** Set up a fixed-point equation using the Law of Total Expectation, conditioning on the first door picked. **Formal Solution:** Let $D$ be the total distance traveled before reaching freedom. Condition on the first door chosen. Each door is selected with probability $\frac{1}{4}$. - Door of length 5: leads to freedom, so $E[D \mid \text{door 5}] = 5$. - Door of length 7: leads to freedom, so $E[D \mid \text{door 7}] = 7$. - Door of length 2: loops back to the same room. After traveling 2 miles you face the same problem from scratch, so $E[D \mid \text{door 2}] = 2 + E[D]$. - Door of length 4: loops back similarly, so $E[D \mid \text{door 4}] = 4 + E[D]$. By the Law of Total Expectation: $$E[D] = \frac{1}{4}(2 + E[D]) + \frac{1}{4}(4 + E[D]) + \frac{1}{4}(5) + \frac{1}{4}(7)$$ Expand: $$E[D] = \frac{1}{4}E[D] + \frac{1}{4}E[D] + \frac{2 + 4 + 5 + 7}{4}$$ $$E[D] = \frac{1}{2}E[D] + \frac{18}{4}$$ $$\frac{1}{2}E[D] = \frac{9}{2}$$ $$E[D] = 9$$ **Answer:** The expected total distance traveled before reaching freedom is $\boxed{9}$ miles. --- Expected Flips for Consecutive Heads [medium, Expectation] --- https://quantvault.org/problem-376-expected-flips-for-consecutive-heads.html Q: You have a fair coin. Let $X_i$ be the number of flips required to see $i$ consecutive heads for the first time. Compute $\mathbb{E}[X_1]$, $\mathbb{E}[X_2]$, and $\mathbb{E}[X_3]$. For each, set up a recursive equation and solve it. Can you spot the pattern and conjecture a formula for general $\mathbb{E}[X_k]$? A: **How to Think About It:** This is a classic first-passage-time problem with a Markov chain flavor. At any point, you are in some state representing "how many consecutive heads you have built up so far." A single tail resets you to zero. The key structural insight is that getting to $k$ consecutive heads means first getting to $k-1$ consecutive heads, then flipping one more head -- but if that extra flip is tails, you have burned all your progress and must restart from scratch. This nesting creates a clean recursive structure. **Quick Estimate:** For $\mathbb{E}[X_1]$, you just need one head -- that is a geometric with $p = 1/2$, so $\mathbb{E}[X_1] = 2$. For $\mathbb{E}[X_2]$, once you have your first head (cost: 2 flips on average), you flip again. Half the time you get a second head and you are done. Half the time you get tails and restart. So the answer should be a bit more than $2 \times 2 = 4$, but each restart costs the full $\mathbb{E}[X_2]$ again, so it will be higher. Guessing around 6. For $\mathbb{E}[X_3]$, by similar logic, somewhere around 14. Let's verify. **Approach:** Define states by the current streak of consecutive heads. Set up recursive equations using the law of total expectation, conditioning on the next flip. **Formal Solution:** *Computing $\mathbb{E}[X_1]$:* Let $E_1 = \mathbb{E}[X_1]$. On any flip, with probability $1/2$ we get heads (done in 1 flip), and with probability $1/2$ we get tails (wasted 1 flip, restart): $$E_1 = \frac{1}{2}(1) + \frac{1}{2}(1 + E_1)$$ $$E_1 = 1 + \frac{1}{2}E_1 \implies \frac{1}{2}E_1 = 1 \implies E_1 = 2$$ *Computing $\mathbb{E}[X_2]$:* Let $E_2 = \mathbb{E}[X_2]$. Condition on the first flip: - Tails (prob $1/2$): wasted 1 flip, restart. Contributes $1 + E_2$. - Heads (prob $1/2$): we have 1 consecutive head. Now flip again: - Heads (prob $1/2$): done in 2 flips total. - Tails (prob $1/2$): wasted 2 flips, restart. Contributes $2 + E_2$. $$E_2 = \frac{1}{2}(1 + E_2) + \frac{1}{2}\left[\frac{1}{2}(2) + \frac{1}{2}(2 + E_2)\right]$$ $$E_2 = \frac{1}{2} + \frac{1}{2}E_2 + \frac{1}{2} + \frac{1}{4}E_2 = 1 + \frac{3}{4}E_2$$ Solving: $\frac{1}{4}E_2 = 1$, so $$\boxed{E_2 = 6}$$ Alternatively, think of it this way: to get 2 consecutive heads, first get 1 consecutive head (expected cost $E_1 = 2$ flips), then flip once more. With probability $1/2$ you get heads and you are done. With probability $1/2$ you get tails and must restart entirely. So: $$E_2 = E_1 + 1 + \frac{1}{2}(0) + \frac{1}{2}(E_2) = E_1 + 1 + \frac{1}{2}E_2$$ $$E_2 = 2(E_1 + 1) = 2(2 + 1) = 6 \checkmark$$ *Computing $\mathbb{E}[X_3]$:* Using the same recursive shortcut: to get 3 consecutive heads, first get 2 consecutive heads (expected cost $E_2 = 6$), then flip once more. With probability $1/2$ you get heads (done). With probability $1/2$ you get tails (restart from scratch): $$E_3 = E_2 + 1 + \frac{1}{2}E_3$$ $$\frac{1}{2}E_3 = E_2 + 1 = 7 \implies E_3 = 14$$ $$\boxed{E_3 = 14}$$ *General formula:* The recurrence $E_k = 2(E_{k-1} + 1)$ with $E_0 = 0$ solves to: $$E_k = 2^{k+1} - 2$$ You can verify: $E_1 = 4 - 2 = 2$, $E_2 = 8 - 2 = 6$, $E_3 = 16 - 2 = 14$, $E_4 = 32 - 2 = 30$. **Answer:** $\mathbb{E}[X_1] = 2$, $\mathbb{E}[X_2] = 6$, $\mathbb{E}[X_3] = 14$. The general formula is $\mathbb{E}[X_k] = 2^{k+1} - 2$. --- Expected Flips for Two Consecutive Heads [medium, Expectation] --- https://quantvault.org/problem-377-expected-flips-for-two-consecutive-heads.html Q: You flip a fair coin repeatedly until you see two heads in a row. What is the expected number of flips? As a follow-up, think about why the sequence $HH$ takes longer on average than $HT$. A: **How to Think About It:** This is a first-passage-time problem on a small Markov chain. Before writing any equations, think about the states you can be in: you either have zero progress toward $HH$, or your last flip was $H$ and you need one more. Every time you flip tails, you lose all your progress. That "restart" penalty is what makes this take longer than you might guess. A naive thought is "two heads, each takes 2 flips on average, so maybe 4" -- but that ignores restarts. The answer will be higher. **Quick Estimate:** On any given pair of consecutive flips, $P(HH) = 1/4$. If the trials were independent (they are not, but as a rough bound), you would need about $1/(1/4) = 4$ "trials" -- but each trial overlaps by one flip, so that suggests something in the range of 4-8 flips. The geometric-style heuristic gives a rough guess around 6. Let's confirm. **Approach:** Set up a system of linear equations by conditioning on the current state of the Markov chain. **Formal Solution:** Define two states based on progress toward $HH$: - **State 0**: no progress (either we just started, or the last flip was $T$). - **State $H$**: the last flip was $H$; one more $H$ finishes the game. Let $e_0$ = expected flips to reach $HH$ from State 0, and $e_H$ = expected flips to reach $HH$ from State $H$. From **State 0**, we flip once: $$e_0 = \frac{1}{2}(1 + e_0) + \frac{1}{2}(1 + e_H)$$ - Tails ($1/2$): we stay in State 0, having used 1 flip. - Heads ($1/2$): we move to State $H$, having used 1 flip. From **State $H$**, we flip once: $$e_H = \frac{1}{2}(1 + e_0) + \frac{1}{2}(1)$$ - Tails ($1/2$): we crash back to State 0, having used 1 flip. - Heads ($1/2$): we are done -- it took 1 flip. Simplify the second equation: $$e_H = \frac{1}{2}e_0 + 1$$ Substitute into the first equation: $$e_0 = 1 + \frac{1}{2}e_0 + \frac{1}{2}e_H = 1 + \frac{1}{2}e_0 + \frac{1}{2}\left(\frac{1}{2}e_0 + 1\right)$$ $$e_0 = 1 + \frac{1}{2}e_0 + \frac{1}{4}e_0 + \frac{1}{2} = \frac{3}{2} + \frac{3}{4}e_0$$ $$\frac{1}{4}e_0 = \frac{3}{2} \implies e_0 = 6$$ **Follow-up -- why $HH$ takes longer than $HT$:** The expected time to first see $HT$ is only 4 flips (you can verify with a similar Markov chain). The key difference: when you are in State $H$ and trying to complete $HT$, a tails *finishes* the pattern. But when you are in State $H$ and trying to complete $HH$, a tails *restarts* you. Meanwhile, when trying for $HT$ and you see another $H$, you have not lost progress -- you are still in State $H$. The $HH$ pattern has no such "free retry." **Answer:** The expected number of flips is $\boxed{6}$. --- Expected Hitting Time of a Birth-Death Chain [hard, Expectation] --- https://quantvault.org/problem-381-expected-hitting-time-of-a-birth-death-chain.html Q: A Markov chain has states $1, 2, \ldots, n$. From state $i$, the chain moves to $i+1$ with probability $p$, to $i-1$ with probability $q$, and stays at $i$ with probability $1 - p - q$. State 1 has a reflecting boundary (a leftward step from state 1 keeps you at state 1). Let $h_i$ denote the expected number of steps to reach state $n$ starting from state $i$. 1. Set up and solve the recurrence for $h_i$ in terms of $p$ and $q$ (with $p \ne q$). 2. Solve the symmetric case $p = q = 1/2$ and express $h_1$ as a closed-form function of $n$. 3. How does the "stay" probability $1 - p - q$ affect the answer? A: **How to Think About It:** This is a classic birth-death chain / gambler's ruin hitting-time problem. Before doing any algebra, think about the physics. You are doing a random walk on $\{1, \ldots, n\}$ and need to reach the far end. In the symmetric case ($p = q$), there is no drift, so you are wandering aimlessly -- the hitting time should grow like $n^2$ (diffusive scaling). In the biased case ($p > q$), you have a net rightward drift of $p - q$ per step, so the hitting time should be roughly $(n-1)/(p-q)$ (ballistic scaling). The "stay" probability just wastes time without changing direction, so it slows you down but the algebra will absorb it naturally. **Quick Estimate:** Take $n = 10$, $p = q = 0.5$. The answer should scale as $n^2$, so roughly $10 \times 9 = 90$ steps. For a biased walk with $p = 0.6$, $q = 0.3$, the drift is $0.3$ per step, so roughly $(10-1)/0.3 = 30$ steps. These ballpark numbers will help us sanity-check the exact formulas. **Approach:** Let $h_i = E[T_n \mid X_0 = i]$. Write the one-step recurrence, simplify, and solve as a first-order recurrence in the differences $d_i = h_i - h_{i+1}$. **Formal Solution:** *Step 1: Recurrence.* For $2 \le i \le n - 1$: $$h_i = 1 + p \cdot h_{i+1} + q \cdot h_{i-1} + (1 - p - q) \cdot h_i$$ The $(1 - p - q) \cdot h_i$ on the right cancels part of the left side: $$(p + q) \cdot h_i = 1 + p \cdot h_{i+1} + q \cdot h_{i-1}$$ Rearranging: $$p(h_{i+1} - h_i) - q(h_i - h_{i-1}) = -1$$ *Step 2: Boundary conditions.* At state $n$: $h_n = 0$. At state 1 (reflecting): a leftward move keeps you at 1, so $$h_1 = 1 + p \cdot h_2 + (1 - p) \cdot h_1$$ which gives $p(h_1 - h_2) = 1$, i.e., $d_1 = 1/p$ where $d_i = h_i - h_{i+1}$. *Step 3: Solve the differences.* Define $d_i = h_i - h_{i+1}$. The recurrence becomes $-p \cdot d_i + q \cdot d_{i-1} = -1$, or $$d_i = \frac{q}{p} \cdot d_{i-1} + \frac{1}{p}$$ Let $r = q/p$. Iterating from $d_1 = 1/p$: $$d_i = \frac{1}{p} \cdot \frac{r^i - 1}{r - 1} \quad \text{for } r \ne 1$$ Verification: $d_1 = \frac{1}{p} \cdot \frac{r - 1}{r - 1} = \frac{1}{p}$. Check. *Step 4: Sum to get $h_1$.* Since $h_1 = \sum_{i=1}^{n-1} d_i$: $$h_1 = \frac{1}{p(r - 1)} \sum_{i=1}^{n-1} (r^i - 1) = \frac{1}{p(r - 1)} \left[ \frac{r(r^{n-1} - 1)}{r - 1} - (n - 1) \right]$$ Substituting $r = q/p$ and $p(r - 1) = q - p$: $$h_1 = \frac{n - 1}{p - q} - \frac{q\left[1 - (q/p)^{n-1}\right]}{(p - q)^2} \quad \text{for } p > q$$ and similarly for $p < q$ (with sign adjustments). *Step 5: Symmetric case $p = q = 1/2$.* When $r = 1$, the difference recurrence becomes $d_i = d_{i-1} + 2$, giving $d_i = 2i$. Therefore: $$h_1 = \sum_{i=1}^{n-1} 2i = 2 \cdot \frac{(n-1)n}{2} = n(n - 1)$$ *Step 6: Role of the stay probability.* The probability $1 - p - q$ of staying put drops out of the recurrence entirely. It does not appear in the final answer. This makes sense: staying in place adds one step but does not change your position, and the recurrence already accounts for the expected number of steps. Formally, the $(1 - p - q) h_i$ terms cancel when you move them to the left side. The expected hitting time depends only on $p$ and $q$ (or equivalently, on their ratio $q/p$ and their sum $p + q$). **Answer:** For $p = q = 1/2$, the expected hitting time from state 1 to state $n$ is $$h_1 = n(n - 1)$$ For general $p \ne q$: $$h_1 = \frac{n - 1}{p - q} - \frac{q\left[1 - (q/p)^{n-1}\right]}{(p - q)^2}$$ The stay probability $1 - p - q$ does not affect the answer. --- Expected Insertions to Pay a Parking Meter [medium, Expectation] --- https://quantvault.org/problem-382-expected-insertions-to-pay-a-parking-meter.html Q: You need to pay $\$1$ at a parking meter that only accepts quarters ($\$0.25$ each), so you need 4 real quarters to complete payment. You have an infinitely large pouch of coins where 60% are real quarters and 40% are fake. The meter has a catch: if you insert a fake quarter, the meter resets to $\$0$ and you lose all progress. You must start over from scratch. You draw coins one at a time from the pouch and insert them. How many coins do you expect to insert before the meter is fully paid? A: **How to Think About It:** This is a consecutive-successes problem. You need 4 real quarters in a row, and each coin is real with probability $p = 0.6$. A single fake coin (probability $0.4$) wipes out all progress and resets you to zero. The question reduces to: what is the expected number of Bernoulli trials to get $r = 4$ consecutive successes? Before doing any math, get a feel for the answer. If there were no reset penalty, you would need exactly 4 coins. But fakes reset you, and at a 40% fake rate, you will get reset frequently. The probability of 4 consecutive reals is $0.6^4 = 0.1296$, roughly 13%. So you would need on the order of $1/0.1296 \approx 7.7$ "attempts" -- but each failed attempt wastes some coins too. Expect something in the low-to-mid teens. **Quick Estimate:** Think of it as a geometric number of "streaks." Each streak attempt either succeeds (probability $0.6^4 \approx 0.13$) or fails partway through. On a failed attempt, you insert on average about 1-2 coins before hitting a fake (roughly $1/0.4 = 2.5$ coins per attempt on average, but failed attempts are shorter since they exclude the successful run). A rough estimate: about $1/0.13 \approx 7.7$ attempts, each costing ~2 coins on average, gives ~15-17 total coins. **Approach:** Let $E_k$ denote the expected number of additional coins needed when you currently have $k$ consecutive real quarters inserted. We want $E_0$. Setting up the recursion and solving gives a clean closed-form formula. **Formal Solution:** Define $E_k$ = expected additional coins needed given $k$ consecutive real quarters so far. We have $E_4 = 0$ (done). For $k < 4$: $$E_k = 1 + p \cdot E_{k+1} + (1 - p) \cdot E_0$$ Each coin costs 1 insertion. With probability $p = 0.6$ it is real and we advance to $k+1$; with probability $1 - p = 0.4$ it is fake and we reset to $E_0$. Solving backwards from $k = 3$: - $E_3 = 1 + 0.6 \cdot 0 + 0.4 \cdot E_0 = 1 + 0.4 E_0$ - $E_2 = 1 + 0.6(1 + 0.4 E_0) + 0.4 E_0 = 1.6 + 0.64 E_0$ - $E_1 = 1 + 0.6(1.6 + 0.64 E_0) + 0.4 E_0 = 1.96 + 0.784 E_0$ - $E_0 = 1 + 0.6(1.96 + 0.784 E_0) + 0.4 E_0 = 2.176 + 0.8704 E_0$ Solving for $E_0$: $$E_0 = \frac{2.176}{1 - 0.8704} = \frac{2.176}{0.1296} \approx 16.79$$ This matches the general formula for $r$ consecutive successes with individual probability $p$: $$E[T_r] = \sum_{i=1}^{r} \frac{1}{p^i} = \frac{1}{p} + \frac{1}{p^2} + \frac{1}{p^3} + \frac{1}{p^4}$$ Plugging in: $$E[T_4] = \frac{1}{0.6} + \frac{1}{0.36} + \frac{1}{0.216} + \frac{1}{0.1296} = 1.667 + 2.778 + 4.630 + 7.716 \approx 16.79$$ Alternatively, the closed-form expression is: $$E[T_r] = \frac{1 - p^r}{(1-p) \, p^r}$$ **Answer:** The expected number of coins you need to insert is approximately $\boxed{16.79}$, or exactly $\dfrac{1 - 0.6^4}{0.4 \times 0.6^4} = \dfrac{0.8704}{0.05184}$. --- Expected Mid Change From Poisson Order Flow [medium, Expectation] --- https://quantvault.org/problem-387-expected-mid-change-from-poisson-order-flow.html Q: In a 1-second interval, buy market orders arrive as $N_b \sim \text{Poisson}(6)$ and sell market orders arrive as $N_s \sim \text{Poisson}(5)$, independently. Each buy order independently moves the mid price up by $+1$ tick with probability $0.3$ (otherwise no impact). Each sell order independently moves the mid price down by $-1$ tick with probability $0.25$ (otherwise no impact). Impacts are independent across all orders and sides. Let $M$ be the total mid price change (the sum of all individual impacts). 1. Compute $E[M]$ and $\text{Var}(M)$. 2. Using a normal approximation, estimate $P(M \ge 2)$. State the mean and variance you plug in. A: **How to Think About It:** This is a compound Poisson problem dressed up in order-flow language. Each side generates a random number of orders, and each order independently either moves the mid or does nothing. The key shortcut is Poisson thinning: if you take a $\text{Poisson}(\lambda)$ number of i.i.d. coin flips, the number of successes is itself Poisson. That collapses the whole problem into the difference of two independent Poisson random variables, which is quick to work with. **Quick Estimate:** Buy-side impact count is $\text{Poisson}(6 \times 0.3) = \text{Poisson}(1.8)$, sell-side impact count is $\text{Poisson}(5 \times 0.25) = \text{Poisson}(1.25)$. So the expected net move is $1.8 - 1.25 = 0.55$ ticks upward, and the variance is $1.8 + 1.25 = 3.05$. Standard deviation is about $1.75$ ticks. The mid drifts slightly up on average but with plenty of noise -- a $\pm 2\sigma$ range of roughly $-2.9$ to $+4.0$ ticks. **Approach:** Use Poisson thinning, then properties of sums of independent random variables. **Formal Solution:** Define the buy-side impact as $B = \sum_{i=1}^{N_b} X_i$ where each $X_i \sim \text{Bernoulli}(0.3)$ takes value $+1$ with probability $0.3$ and $0$ otherwise. Define the sell-side impact as $S = \sum_{j=1}^{N_s} Y_j$ where each $Y_j \sim \text{Bernoulli}(0.25)$ takes value $1$ with probability $0.25$ and $0$ otherwise. Then $M = B - S$. By Poisson thinning: $$B \sim \text{Poisson}(\lambda_b p_b) = \text{Poisson}(6 \times 0.3) = \text{Poisson}(1.8)$$ $$S \sim \text{Poisson}(\lambda_s p_s) = \text{Poisson}(5 \times 0.25) = \text{Poisson}(1.25)$$ Since $N_b$ and $N_s$ are independent, $B$ and $S$ are independent. **Part 1: Mean and Variance** $$E[M] = E[B] - E[S] = 1.8 - 1.25 = 0.55$$ $$\text{Var}(M) = \text{Var}(B) + \text{Var}(S) = 1.8 + 1.25 = 3.05$$ The variance adds because $B$ and $S$ are independent (the minus sign does not matter for variance). **Part 2: Normal Approximation for $P(M \ge 2)$** Approximate $M \sim N(0.55, 3.05)$, so $\sigma = \sqrt{3.05} \approx 1.747$. Without continuity correction: $$P(M \ge 2) \approx P\!\left(Z \ge \frac{2 - 0.55}{1.747}\right) = P(Z \ge 0.830) \approx 1 - \Phi(0.830) \approx 0.203$$ With continuity correction (since $M$ is integer-valued): $$P(M \ge 2) = P(M \ge 1.5) \approx P\!\left(Z \ge \frac{1.5 - 0.55}{1.747}\right) = P(Z \ge 0.544) \approx 1 - \Phi(0.544) \approx 0.293$$ The continuity-corrected version is more accurate here. **Answer:** $E[M] = 0.55$ ticks, $\text{Var}(M) = 3.05$, and $P(M \ge 2) \approx 0.29$ (with continuity correction) or $\approx 0.20$ (without). --- Expected Rolls Until Cumulative Sum Reaches n [medium, Expectation] --- https://quantvault.org/problem-391-expected-rolls-until-cumulative-sum-reaches-n.html Q: You have a fair $n$-sided die with faces numbered $1$ through $n$. You roll the die repeatedly until the cumulative sum of all rolls is greater than or equal to $n$. What is the expected number of rolls? Compute the answer for $n = 6$. A: **How to Think About It:** Let $E_k(n)$ be the expected number of additional rolls needed when your current cumulative sum is $k$. You need $E_0(n)$. Each roll advances your sum by 1 to $n$ uniformly, and once your sum hits $n$ or above, you stop. The trick is to compute $E_{n-1}, E_{n-2}, \ldots$ backwards and spot a pattern. Gut feeling: the mean of one roll is $(n+1)/2$, so you need roughly $n / ((n+1)/2) = 2n/(n+1)$ rolls. For $n = 6$ that is about $12/7 \approx 1.71$. But that is just the ratio of target to mean roll -- the actual answer should be a bit higher because you overshoot. **Quick Estimate:** For $n = 6$, mean roll is 3.5, so the naive estimate is $6/3.5 \approx 1.71$ rolls. But this ignores the integer structure. We expect the answer to be somewhat above 2 since you often need 2 rolls to reach 6. **Approach:** Build the recurrence $E_k(n) = 1 + \frac{1}{n}\sum_{j=k+1}^{\min(k+n, n-1)} E_j(n)$ and solve by working backwards from $E_{n-1}$. **Formal Solution:** Define $E_k(n)$ as the expected number of rolls to reach a cumulative sum $\ge n$, given current sum $k$. For $k \ge n$, $E_k(n) = 0$. For $k < n$: $$E_k(n) = 1 + \frac{1}{n}\sum_{j=1}^{n} E_{k+j}(n) = 1 + \frac{1}{n}\sum_{j=k+1}^{k+n} E_j(n)$$ Since $E_j(n) = 0$ for $j \ge n$, only terms with $j < n$ survive: $$E_k(n) = 1 + \frac{1}{n}\sum_{j=k+1}^{n-1} E_j(n)$$ **Working backwards:** $E_{n-1}(n) = 1$ (any roll completes the game). $E_{n-2}(n) = 1 + \frac{1}{n} E_{n-1}(n) = 1 + \frac{1}{n}$. $E_{n-3}(n) = 1 + \frac{1}{n}(E_{n-2} + E_{n-1}) = 1 + \frac{1}{n}\left(1 + \frac{1}{n} + 1\right) = 1 + \frac{2}{n} + \frac{1}{n^2}$. $E_{n-4}(n) = 1 + \frac{1}{n}(E_{n-3} + E_{n-2} + E_{n-1}) = 1 + \frac{3}{n} + \frac{3}{n^2} + \frac{1}{n^3}$. **Spotting the pattern:** The coefficients are binomial: $$E_{n-k}(n) = \sum_{i=0}^{k-1} \binom{k-1}{i} \frac{1}{n^i} = \left(1 + \frac{1}{n}\right)^{k-1}$$ by the binomial theorem. **Shortcut (subtract consecutive equations):** For $0 \le k \le n-2$, write the recurrence at $k$ and at $k+1$: $$E_k(n) = 1 + \frac{1}{n}\sum_{j=k+1}^{n-1} E_j(n), \qquad E_{k+1}(n) = 1 + \frac{1}{n}\sum_{j=k+2}^{n-1} E_j(n).$$ Subtracting cancels every term except $E_{k+1}(n)$: $$E_k(n) - E_{k+1}(n) = \frac{1}{n} E_{k+1}(n) \implies E_k(n) = \left(1 + \frac{1}{n}\right) E_{k+1}(n).$$ Starting from $E_{n-1}(n) = 1$ and stepping down $n-1$ times gives $E_0(n) = (1 + 1/n)^{n-1}$ at once; for $n = 6$, $E_0 = (7/6)^5 = 16807/7776$. This works because from any sum $k \ge 0$ one roll can reach every state up to $n-1$, so both sums run to the same upper limit. (With a target above $n+1$ the sums would be cut off at $k+n$ and the ratio would break.) The induction below confirms the same closed form. **Proof by induction:** Assume $E_{n-m}(n) = (1 + 1/n)^{m-1}$ for $m = 1, \ldots, k-1$. Then: $$E_{n-k}(n) = 1 + \frac{1}{n}\sum_{m=1}^{k-1} E_{n-m}(n) = 1 + \frac{1}{n}\sum_{m=1}^{k-1} \left(1 + \frac{1}{n}\right)^{m-1}$$ $$= 1 + \frac{1}{n} \cdot \frac{(1+1/n)^{k-1} - 1}{1/n} = 1 + (1+1/n)^{k-1} - 1 = \left(1 + \frac{1}{n}\right)^{k-1}. \quad \checkmark$$ Setting $k = n$ to get $E_0(n)$: $$E_0(n) = \left(1 + \frac{1}{n}\right)^{n-1}$$ For $n = 6$: $$E_0(6) = \left(\frac{7}{6}\right)^5 = \frac{7^5}{6^5} = \frac{16807}{7776} \approx 2.161$$ **Answer:** The expected number of rolls is $\left(1 + \dfrac{1}{n}\right)^{n-1}$. For $n = 6$, this equals $\dfrac{16807}{7776} \approx 2.161$. --- Expected Rolls for a Consecutive 4-5 Sequence [medium, Expectation] --- https://quantvault.org/problem-393-expected-rolls-for-a-consecutive-4-5-sequence.html Q: You roll a fair six-sided die repeatedly. What is the expected number of rolls until you first see a 4 immediately followed by a 5? A: **How to Think About It:** This is a pattern-matching problem on a sequence of die rolls. You are waiting for the specific consecutive pair (4, 5). The trick is that the problem has memory: if you just rolled a 4, your next roll matters a lot -- a 5 finishes the game, another 4 keeps you in a "promising" state, and anything else resets you. Set up states based on whether your most recent roll was a 4 or not. **Quick Estimate:** The probability of any two consecutive rolls being (4, 5) is $1/36$. If the attempts were independent, you would need about 36 rolls. But attempts overlap (each new roll starts a new potential pair), and rolling a 4 after a 4 keeps you in the game rather than resetting. So the answer should be close to 36, maybe exactly 36. **Approach:** Define states and use the law of total expectation. **Formal Solution:** Let $E$ be the expected number of rolls from the start (no useful recent roll) and $E_4$ be the expected additional rolls given that you just rolled a 4. **From the start state:** On the first roll, with probability $1/6$ you roll a 4 (entering state $E_4$), and with probability $5/6$ you roll something else (staying in the start state). Each roll costs 1: $$E = 1 + \frac{5}{6}E + \frac{1}{6}E_4$$ **From the "just rolled 4" state:** On the next roll: - With probability $1/6$, you roll a 5 -- done. Cost: 1 more roll. - With probability $1/6$, you roll another 4 -- stay in state $E_4$. Cost: 1 more roll. - With probability $4/6$, you roll something other than 4 or 5 -- reset to start. Cost: 1 more roll. $$E_4 = 1 + \frac{1}{6}(0) + \frac{1}{6}E_4 + \frac{4}{6}E$$ Note: the $0$ is because if you roll a 5, you need 0 additional rolls after this one (the 1 at the front already counts this roll). So: $E_4 = 1 + \frac{1}{6}E_4 + \frac{4}{6}E$ Solving for $E_4$: $$\frac{5}{6}E_4 = 1 + \frac{4}{6}E \implies E_4 = \frac{6}{5} + \frac{4}{5}E$$ Substituting into the equation for $E$: $$E = 1 + \frac{5}{6}E + \frac{1}{6}\left(\frac{6}{5} + \frac{4}{5}E\right)$$ $$E = 1 + \frac{5}{6}E + \frac{1}{5} + \frac{4}{30}E$$ $$E = \frac{6}{5} + \frac{5}{6}E + \frac{2}{15}E$$ $$E = \frac{6}{5} + \left(\frac{25}{30} + \frac{4}{30}\right)E = \frac{6}{5} + \frac{29}{30}E$$ $$E - \frac{29}{30}E = \frac{6}{5}$$ $$\frac{1}{30}E = \frac{6}{5}$$ $$E = 36$$ **Answer:** The expected number of rolls is $E = 36$. This matches the naive estimate of $1/(1/36) = 36$ -- in this particular case, the overlapping structure and the "4 after 4" effect exactly cancel out, giving the same answer as if attempts were independent. --- Expected Steps Between Circle Intersections [medium, Expectation] --- https://quantvault.org/problem-397-expected-steps-between-circle-intersections.html Q: Two unit circles are centered at $(0,0)$ and $(1,0)$. They meet at exactly two points: call them $A$ (the upper one) and $B$ (the lower one). A particle starts at $A$. At each step, it picks one of the two circles uniformly at random (probability $\frac{1}{2}$ each), then jumps to one of that circle's two intersection points with the other circle, choosing each with probability $\frac{1}{2}$ (it may land back where it is). The process stops when the particle lands at $B$. What is the expected number of steps to reach $B$? A: **How to Think About It:** The heuristic is **strip away the irrelevant mechanism**. The circle choice looks important, but both circles pass through both $A$ and $B$, so whichever circle is picked, the particle lands on $A$ or $B$ with probability $\frac{1}{2}$ each. Once you see the circle label is noise, each step is a fair coin flip for "reached $B$", and the question becomes a geometric waiting time. (Why the jump is restricted to the intersection points: if the particle instead jumped to a uniformly random point anywhere on a circle, the chance of landing exactly on the single point $B$ would be $0$ on every step, and the particle would never stop. The intersection-point version is the meaningful one.) **Quick Estimate:** At each step you pick a circle (each with prob $\frac{1}{2}$) and then land at either intersection point with equal probability. So regardless of which circle you pick, you jump to $A$ or $B$ with prob $\frac{1}{2}$ each. The probability of reaching $B$ on any given step from $A$ is $\frac{1}{2}$. That is a Geometric$(\frac{1}{2})$ distribution, so the expected number of steps is $\frac{1}{1/2} = 2$. The answer feels right -- you expect to flip a fair coin twice before getting heads. **Approach:** Set up a first-step equation using the state structure, then solve. **Formal Solution:** First, find the intersection points. Solve $x^2 + y^2 = 1$ and $(x-1)^2 + y^2 = 1$ simultaneously: $$x^2 = (x-1)^2 \implies 2x = 1 \implies x = \frac{1}{2}$$ $$y = \pm\frac{\sqrt{3}}{2}$$ So $A = \left(\frac{1}{2}, \frac{\sqrt{3}}{2}\right)$ and $B = \left(\frac{1}{2}, -\frac{\sqrt{3}}{2}\right)$. At each step the particle chooses one of the two circles (each with prob $\frac{1}{2}$) and lands at one of the two intersection points with equal probability $\frac{1}{2}$. Since both circles pass through both $A$ and $B$, the choice of circle does not affect the outcome distribution: the particle moves to $A$ with prob $\frac{1}{2}$ and to $B$ with prob $\frac{1}{2}$, independently of where it currently is. Let $E$ be the expected number of steps to reach $B$ starting from $A$. By the first-step equation: $$E = \frac{1}{2} \cdot 1 + \frac{1}{2} \cdot (1 + E)$$ $$E = \frac{1}{2} + \frac{1}{2} + \frac{1}{2}E$$ $$\frac{1}{2}E = 1$$ $$E = 2$$ Alternatively, the number of steps is Geometric$\left(\frac{1}{2}\right)$, so $E[T] = \frac{1}{p} = 2$ directly. **Answer:** The expected number of steps is $\boxed{2}$. --- First Repeated Die Roll [medium, Probability] --- https://quantvault.org/problem-415-first-repeated-die-roll.html Q: You roll a fair six-sided die repeatedly until you see a number that has already appeared. Let $r$ be the total number of rolls, and let $p_r$ be the probability that the game lasts exactly $r$ rolls. **Part 1.** Calculate $p_3$. **Part 2.** What is $p_1 + p_2 + \cdots + p_{10}$? **Part 3.** What is $P(r = 5 \mid r \geq 3)$? A: **How to Think About It:** This is a birthday-problem variant on a six-sided die. Each roll is like a new person arriving -- you keep going until you get a "collision" with a previously seen face. The key observation is that on roll $k$, there are $k - 1$ faces already seen, so the probability of matching is $(k-1)/6$, and the probability of not matching is $(7-k)/6$. That gives you a clean product formula for $p_r$. Before doing any algebra, notice that with only 6 faces you must get a repeat by roll 7 at the latest (pigeonhole), so $p_r = 0$ for $r \geq 8$ and for $r = 1$. **Quick Estimate:** The expected number of rolls is roughly the birthday-problem threshold for $n = 6$. A quick approximation: you expect a collision after about $\sqrt{\pi \cdot 6 / 2} \approx 3.1$ rolls. So the probability mass should be centered around $r = 3$ or $r = 4$. For Part 1, $p_3$ should be a decent chunk -- maybe around $5/18 \approx 0.28$. For Part 2, the sum over all possible outcomes must be 1 since the game always terminates. For Part 3, conditioning on $r \geq 3$ just removes $p_2 = 1/6$, so we are re-normalizing over a probability mass of $5/6$. **Approach:** Build the product formula for $p_r$ using sequential independence, then apply conditional probability for Part 3. **Formal Solution:** **Part 1: Computing $p_3$** For the game to last exactly 3 rolls: - Roll 1: any face. Probability $= 1$. - Roll 2: must differ from roll 1. Probability $= 5/6$. - Roll 3: must match one of the 2 faces already seen. Probability $= 2/6$. Since rolls are independent: $$p_3 = 1 \cdot \frac{5}{6} \cdot \frac{2}{6} = \frac{10}{36} = \frac{5}{18}$$ More generally, for $2 \leq r \leq 7$: $$p_r = \left(\prod_{k=1}^{r-1} \frac{7-k}{6}\right) \cdot \frac{r-1}{6}$$ The first product ensures rolls $1$ through $r-1$ are all distinct, and the last factor is the probability that roll $r$ matches one of the $r-1$ previously seen faces. **Part 2: $p_1 + p_2 + \cdots + p_{10}$** A six-sided die has only 6 faces, so by the pigeonhole principle the game must end by roll 7 at the latest: after 6 rolls you have seen at most 6 distinct faces, and the 7th roll is guaranteed to repeat one. Therefore $p_r = 0$ for $r = 1$ (no face to repeat on the first roll) and for $r \geq 8$ (impossible). The events $\{r = 2\}, \{r = 3\}, \ldots, \{r = 7\}$ partition the entire sample space, so: $$p_1 + p_2 + \cdots + p_{10} = p_2 + p_3 + \cdots + p_7 = 1$$ **Part 3: $P(r = 5 \mid r \geq 3)$** This is a direct application of conditional probability: $$P(r = 5 \mid r \geq 3) = \frac{P(r = 5 \cap r \geq 3)}{P(r \geq 3)} = \frac{P(r = 5)}{P(r \geq 3)}$$ since $r = 5$ implies $r \geq 3$. First, compute $p_5$: $$p_5 = \frac{6}{6} \cdot \frac{5}{6} \cdot \frac{4}{6} \cdot \frac{3}{6} \cdot \frac{4}{6} = \frac{5 \cdot 4 \cdot 3 \cdot 4}{6^4} = \frac{240}{1296} = \frac{5}{27}$$ For the denominator, use the complement. The complement of $r \geq 3$ is $r \leq 2$, and since $p_1 = 0$: $$P(r \geq 3) = 1 - p_1 - p_2 = 1 - 0 - \frac{1}{6} = \frac{5}{6}$$ Putting it together: $$P(r = 5 \mid r \geq 3) = \frac{5/27}{5/6} = \frac{5}{27} \cdot \frac{6}{5} = \frac{6}{27} = \frac{2}{9} \approx 0.222$$ **Verification by simulation:** ```python import random def simulate_game(): seen = set() rolls = 0 while True: face = random.randint(1, 6) rolls += 1 if face in seen: return rolls seen.add(face) N = 500_000 count_5_given_ge3 = 0 count_ge3 = 0 for _ in range(N): r = simulate_game() if r >= 3: count_ge3 += 1 if r == 5: count_5_given_ge3 += 1 print(f"p_3 = {sum(1 for _ in range(N) if simulate_game() == 3) / N:.4f}") print(f"P(r=5 | r>=3) = {count_5_given_ge3 / count_ge3:.4f}") # Expected: p_3 ~ 0.2778, P(r=5|r>=3) ~ 0.2222 ``` **Answer:** - **Part 1:** $p_3 = \dfrac{5}{18} \approx 0.278$ - **Part 2:** $p_1 + p_2 + \cdots + p_{10} = 1$ - **Part 3:** $P(r = 5 \mid r \geq 3) = \dfrac{2}{9} \approx 0.222$ --- First-Passage Probabilities and Optimal Barriers for Brownian Motion with Drift [hard, Stochastic Process] --- https://quantvault.org/problem-416-first-passage-probabilities-and-optimal-barriers-for-brownia.html Q: Let $X_t = \mu t + W_t$ where $W_t$ is a standard Brownian motion and $\mu$ is a constant drift. Two absorbing barriers sit at $-L 0$. Define $\tau = \inf\{t \geq 0 : X_t \in \{-L, U\}\}$ as the first time the process hits either barrier. 1. Compute $P(X_\tau = U)$, the probability of hitting the upper barrier before the lower. 2. Compute $E[\tau]$, the expected time to absorption. 3. Describe what happens in the limiting cases $\mu \to 0$ and $U, L \to \infty$. 4. Suppose you can choose a single symmetric barrier $b = U = L > 0$ before trading begins. How should the optimal $b$ change with $|\mu|$ to balance hit probability against expected duration? Discuss the trade-off qualitatively and describe what the optimal $b$ looks like as $|\mu| \to 0$ and $|\mu| \to \infty$. A: **How to Think About It:** Think of $X_t$ as a position that drifts steadily in one direction while also jiggling randomly. You want to know which barrier it touches first and how long that takes. The key tool is the exponential martingale $e^{-2\mu X_t}$ (when $\mu \neq 0$): optional stopping on it pins down the exit probabilities exactly. For the expected duration, the generator gives a clean ODE, and the martingale $X_t - \mu t$ confirms the answer in one line. For the symmetric barrier question, compute the natural objective before assuming it has a maximum: a tight barrier ($b$ small) resolves quickly but pays close to a coin flip, a wide barrier ($b$ large) is reliable but slow, and those two effects turn out to cancel exactly in profit per unit time. **Quick Estimate:** Take $\mu = 0.5$, $U = 2$, $L = 1$. With no drift the answer would be $L/(L+U) = 1/3$, since the nearer barrier is the likelier one. Positive drift should lift that well above $1/3$, but not to certainty, because one unit of noise by time 1 makes an early touch of $-L$ quite possible. Using the exit probability formula $P(\text{hit } U) = (1 - e^{2\mu L}) / (e^{-2\mu U} - e^{2\mu L})$ with $\mu = 0.5$, $U = 2$, $L = 1$: numerator $= 1 - e^{1} \approx -1.718$, denominator $= e^{-2} - e^{1} \approx 0.135 - 2.718 = -2.583$, so $P \approx 0.665$. That is about two-thirds, double the driftless value. **Approach:** Use two martingales. First, $M_t = e^{-2\mu X_t}$ is a martingale (verify by Ito's lemma), giving the exit probabilities via optional stopping. Second, $u(x) = E_x[\tau]$ solves an ODE from the generator $\mathcal{L} = \mu \partial_x + \frac{1}{2} \partial_{xx}$, and the martingale $X_t - \mu t$ gives an independent check. **Formal Solution:** *Part 1: Exit Probabilities* When $\mu \neq 0$, apply Ito's lemma to $f(x) = e^{-2\mu x}$: $$df = -2\mu e^{-2\mu X_t} dX_t + \frac{1}{2}(4\mu^{2}) e^{-2\mu X_t} dt = e^{-2\mu X_t}(-2\mu \, dX_t + 2\mu^{2} \, dt)$$ Substituting $dX_t = \mu \, dt + dW_t$: $$df = e^{-2\mu X_t}(-2\mu(\mu \, dt + dW_t) + 2\mu^{2} \, dt) = e^{-2\mu X_t}(-2\mu \, dW_t)$$ So $M_t = e^{-2\mu X_t}$ is indeed a local martingale (and a true martingale on a bounded domain). By optional stopping at $\tau$: $$E[M_0] = E[M_\tau]$$ $$e^{0} = P(\text{hit } U) \cdot e^{-2\mu U} + P(\text{hit } -L) \cdot e^{2\mu L}$$ Let $p = P(X_\tau = U)$. Then $1-p = P(X_\tau = -L)$, so: $$1 = p \, e^{-2\mu U} + (1-p) e^{2\mu L}$$ $$\boxed{p = \frac{1 - e^{2\mu L}}{e^{-2\mu U} - e^{2\mu L}}}$$ When $\mu > 0$, the numerator is negative and the denominator is negative (since $e^{-2\mu U} 0$ as expected. Multiplying top and bottom by $-e^{-2\mu L}$ gives the equivalent form $p = (1 - e^{-2\mu L})/(1 - e^{-2\mu(L+U)})$, which is the easiest one for taking limits. Drift raises $p$ above the driftless value $L/(L+U)$, though $p$ can still be small if $L$ is much smaller than $U$. When $\mu 0$:** The answer depends on which barriers move, so use $p = (1 - e^{-2\mu L})/(1 - e^{-2\mu(L+U)})$ and $E[\tau] = (pU - (1-p)L)/\mu$. - $L$ fixed, $U \to \infty$: $p \to 1 - e^{-2\mu L} 0$ (for $\mu 0.$$ The objective is flat. It does not go to $0$ as $b \to 0$: the numerator is about $\mu b^{2}$ and the denominator about $b^{2}$, so the ratio stays at $\mu$. There is no interior maximum, so no optimal width follows from this objective. This is not special to the symmetric case. For any stopping rule with $E[\tau] 1/2$.** Since $p$ depends only on $|\mu| b$, you need $b = \dfrac{\ln(p_0/(1-p_0))}{2|\mu|}$, and the wait is $E_b[\tau] = \dfrac{(2p_0 - 1)\ln(p_0/(1-p_0))}{2\mu^{2}}$. As $|\mu| \to 0$, $b \to \infty$ like $1/|\mu|$ and the wait grows like $1/\mu^{2}$: a weak signal needs a wide barrier and a long wait. As $|\mu| \to \infty$, $b \to 0$: a sliver of a barrier already pays reliably. - **Holding-time budget $E_b[\tau] = T$.** Solve $b\tanh(|\mu| b)/|\mu| = T$. As $|\mu| \to 0$, $b \to \sqrt{T}$, the pure diffusion scale. As $|\mu| \to \infty$, $b \approx |\mu| T$, the distance the drift covers in time $T$: here a stronger signal means a wider barrier. So there is no single optimal $b(|\mu|)$. A fixed reliability target says tighten as the drift grows, and a fixed time budget says widen. A good answer states the criterion first. **Answer:** - $P(X_\tau = U) = \dfrac{1 - e^{2\mu L}}{e^{-2\mu U} - e^{2\mu L}}$ - $E[\tau] = \dfrac{1}{\mu}\left[U \cdot \dfrac{e^{2\mu L} - 1}{e^{2\mu L} - e^{-2\mu U}} - L \cdot \dfrac{1 - e^{-2\mu U}}{e^{2\mu L} - e^{-2\mu U}}\right]$ - As $\mu \to 0$: $P(\text{hit } U) \to L/(L+U)$, $E[\tau] \to LU$ (gambler's ruin). - With $\mu > 0$ and $L$ fixed, $U \to \infty$ gives $P(\text{hit } U) \to 1 - e^{-2\mu L} 0$ and does not select a width. An extra criterion is needed: a fixed win rate $p_0$ gives $b = \ln(p_0/(1-p_0))/(2|\mu|)$, wide as $|\mu| \to 0$ and tight as $|\mu| \to \infty$; a holding-time budget $T$ gives $b \to \sqrt{T}$ as $|\mu| \to 0$ and $b \approx |\mu| T$ as $|\mu| \to \infty$. --- First-Passage Time of Brownian Motion [hard, Stochastic Process] --- https://quantvault.org/problem-417-first-passage-time-of-brownian-motion.html Q: Let $B_t$ be a standard Brownian motion with $B_0 = 0$. Define the first-passage time to level $a > 0$ as $$\tau_a = \inf\{t \geq 0 : B_t = a\}.$$ **(i)** Using the reflection principle (or an equivalent martingale argument), derive $P(\tau_a \leq t)$ in closed form and the density of $\tau_a$. Evaluate $P(\tau_1 \leq 1)$, the probability of reaching level 1 within one unit of time. **(ii)** Show whether $E[\tau_a]$ is finite or infinite, and justify your conclusion carefully. **(iii)** Explain what this result tells you about barrier options -- specifically, why the probability of hitting a barrier can be surprisingly large even when the terminal distribution of $B_T$ assigns very little mass near the barrier. A: **How to Think About It:** Before any formulas, think about what a first-passage time question is really asking: how long does a random walk take to reach a fixed level? The reflection principle is a beautiful symmetry trick -- once a Brownian path reaches $a$, its future is symmetric, so continuing above $a$ is equally likely as reflecting below. This immediately doubles probabilities in a clean way. For the expectation, your gut should already suspect something is off: Brownian motion is recurrent, so $\tau_a$ is finite with probability 1, but the path meanders so wildly that the average time is infinite. This is a counterintuitive but crucial fact that shows up repeatedly in exotic options pricing. **Quick Sanity Check:** Before computing, ask: what does $P(\tau_a \leq t)$ look like qualitatively? At $t = 0$, it should be 0 (the path starts at 0, not $a > 0$). As $t \to \infty$, it should go to 1 (Brownian motion is recurrent and will eventually hit every level). And it should be an increasing function of $t$ and a decreasing function of $a$ (higher barriers take longer to hit). Whatever formula we derive had better satisfy these. **Part (i): The Distribution of $\tau_a$** **Reflection Principle Derivation:** The event $\{\tau_a \leq t\}$ means the path touched level $a$ by time $t$. To compute this, note: $$P(\tau_a \leq t) = P\left(\max_{0 \leq s \leq t} B_s \geq a\right).$$ Now apply the reflection principle. Condition on paths that hit $a$ by time $t$ and reflect them after the first hitting time: - Any path with $\tau_a \leq t$ and $B_t \geq a$ contributes as-is. - Any path with $\tau_a \leq t$ and $B_t 0.$$ This is the Levy distribution (the driftless limit of the inverse Gaussian) with heavy tails -- specifically, $f_{\tau_a}(t) \sim t^{-3/2}$ for large $t$. **Part (ii): The Expected Hitting Time is Infinite** We need $$E[\tau_a] = \int_0^{\infty} t \cdot f_{\tau_a}(t)\, dt = \int_0^{\infty} \frac{a}{\sqrt{2\pi}}\, t^{-1/2} \exp\!\left(-\frac{a^2}{2t}\right) dt.$$ For $t \ge a^2$ the exponential factor is at least $e^{-1/2}$, so $$\int_{a^2}^{\infty} t \cdot f_{\tau_a}(t)\, dt \ \ge\ \frac{a\, e^{-1/2}}{\sqrt{2\pi}} \int_{a^2}^{\infty} t^{-1/2}\, dt = +\infty.$$ So $E[\tau_a] = +\infty$. Numerically, $\int_0^T t f_{\tau_a}(t)\,dt$ grows like $a\sqrt{2T/\pi}$ (about $798$ at $T = 10^6$ for $a = 1$) and never levels off. **Martingale check (Wald):** If $E[\tau_a]$ were finite, Wald's identity for Brownian motion would give $E[B_{\tau_a}] = E[B_0] = 0$. But $B_{\tau_a} = a > 0$ on every path, a contradiction. So $E[\tau_a] = +\infty$. **Two-barrier view:** Add a lower barrier at $-b < 0$. The exit time $\tau_{-b,a}$ from $(-b, a)$ has finite mean, and optional stopping on the martingale $B_t^2 - t$ gives $E[\tau_{-b,a}] = ab$. As $b \to \infty$ the exit time increases to $\tau_a$, so by monotone convergence $E[\tau_a] = \lim_{b\to\infty} ab = \infty$. This also shows why the tempting answer $E[\tau_a] = a^2$ (optional stopping on $B_t^2 - t$ with only the upper barrier) is wrong: optional stopping needs exactly the integrability that fails here. **Answer (ii):** $E[\tau_a] = +\infty$. The $t^{-3/2}$ tail of the density is not integrable when multiplied by $t$. **Part (iii): Barrier Option Intuition** For a barrier option that knocks out if $B_T$ (or a GBM analog) hits a barrier at level $a$ before maturity $T$, the relevant quantity is $P(\tau_a \leq T)$, NOT $P(B_T \geq a)$. From our formula: - $P(B_T \geq a) = 1 - \Phi(a/\sqrt{T})$ -- probability the terminal value exceeds the barrier. - $P(\tau_a \leq T) = 2(1 - \Phi(a/\sqrt{T})) = 2 \cdot P(B_T \geq a)$ -- probability the path ever touches the barrier. The hitting probability is exactly TWICE the terminal exceedance probability. This factor of 2 is the whole story: even if the barrier seems remote from the terminal distribution (say $a = 3\sqrt{T}$, so $P(B_T \geq a) \approx 0.135\%$), the hitting probability is still $\approx 0.27\%$ -- not negligible for exotic products. In practice, for barrier options on stocks modeled with GBM, the analog involves the running maximum of a drifted Brownian motion, but the qualitative lesson is the same: paths explore a much wider range than the terminal snapshot suggests. A barrier that looks far out-of-the-money at expiry may have been touched many times during the life of the option. This is why barrier options are materially cheaper (knock-outs) or more expensive (knock-ins) than their vanilla counterparts, and why barrier risk is hard to hedge -- small moves in spot or vol can dramatically change the hitting probability. --- Fixed Point Probability in a Random Permutation [easy, Probability] --- https://quantvault.org/problem-419-fixed-point-probability-in-a-random-permutation.html Q: Pick a uniformly random permutation $f: [n] \rightarrow [n]$, where $[n] = \{1, 2, \ldots, n\}$. We call index $i$ a fixed point if $f(i) = i$ -- it maps to itself. With $n = 10$, what is the probability that $1$ is a fixed point but $2$ is not? A: **How to Think About It:** This is a counting problem in disguise. You have $n!$ equally likely permutations, and you want to count how many have $f(1) = 1$ but $f(2) \neq 2$. The clean way to do this: count the ones with $f(1) = 1$, then subtract the ones where both $f(1) = 1$ and $f(2) = 2$. That is just inclusion-exclusion on two events -- a one-liner once you see it. The answer should be noticeably less than $1/n$ (the probability that $1$ alone is fixed) because we are restricting to a subset. **Quick Estimate:** With $n = 10$, the probability that $1$ is fixed is $1/10 = 0.10$. Given that $1$ is fixed, element $2$ maps to one of the remaining 9 positions, and exactly one of those is $f(2) = 2$, so the conditional probability that $2$ is also fixed is $1/9$. The probability we want is $P(F_1) - P(F_1 \cap F_2) = 1/10 - 1/90 = 9/90 - 1/90 = 8/90 = 4/45 \approx 0.089$. Ballpark: just under 9%. **Approach:** Differencing (inclusion-exclusion on two events), confirmed by a direct combinatorial count. **Formal Solution:** Let $F_i$ denote the event that $i$ is a fixed point of $f$. We want $P(F_1 \cap F_2^c)$. Since $F_1 \cap F_2^c$ and $F_1 \cap F_2$ partition $F_1$: $$P(F_1 \cap F_2^c) = P(F_1) - P(F_1 \cap F_2)$$ All $n!$ permutations are equally likely, so we count favorable outcomes. - $|F_1|$: fix $f(1) = 1$, freely permute the remaining $n-1$ elements. Count: $(n-1)!$ - $|F_1 \cap F_2|$: fix $f(1) = 1$ and $f(2) = 2$, freely permute the remaining $n-2$ elements. Count: $(n-2)!$ Converting to probabilities: $$P(F_1) = \frac{(n-1)!}{n!} = \frac{1}{n}, \qquad P(F_1 \cap F_2) = \frac{(n-2)!}{n!} = \frac{1}{n(n-1)}$$ Therefore: $$P(F_1 \cap F_2^c) = \frac{1}{n} - \frac{1}{n(n-1)} = \frac{n-1}{n(n-1)} - \frac{1}{n(n-1)} = \frac{n-2}{n(n-1)}$$ **Alternative (direct count):** Among all $n(n-1)$ ordered pairs $(f(1), f(2))$ with $f(1) \neq f(2)$, exactly one choice satisfies $f(1) = 1$ (element 1 is fixed), and then $f(2)$ can be any of the $n-2$ values that are not $1$ or $2$. So the probability is directly $\frac{n-2}{n(n-1)}$, confirming the result. **Answer:** With $n = 10$: $$P(F_1 \cap F_2^c) = \frac{10-2}{10 \cdot 9} = \frac{8}{90} = \frac{4}{45}$$ --- Futures Exchange Data Feed Delay [medium, Probability] --- https://quantvault.org/problem-421-futures-exchange-data-feed-delay.html Q: A futures exchange provides two data feeds: a fast direct feed (arriving at time $t$) and a slower consolidated feed (arriving at time $t + \delta$, where $\delta \sim \text{Exp}(\lambda)$ with mean $50$ microseconds). You observe a price change on the fast feed at time $t_0$. 1. What is the probability the consolidated feed has not yet reflected this change after $100$ microseconds? 2. If you can trade on the consolidated feed's stale price, what is your information advantage as a function of your order latency $\tau$? 3. How does this connect to latency arbitrage in practice, and what does it tell you about the value of speed? A: **How to Think About It:** You are sitting on a trading desk with two feeds -- one fast, one slow. The slow feed lags by some random amount that is memoryless (exponential). The moment you see a price move on the fast feed, the consolidated feed is showing a stale quote. That stale quote is a free option: you know the new price, but the rest of the market (those on the slow feed) is still trading at the old one. The question is how long that window stays open, and how much you can extract before it closes. The key parameter is the rate $\lambda = 1/50 = 0.02$ per microsecond. Everything flows from the exponential survival function $P(\delta > t) = e^{-\lambda t}$. **Quick Estimate:** With mean delay $50\mu s$, the consolidated feed takes twice the mean ($100\mu s$) to catch up only $e^{-2} \approx 13.5\%$ of the time. If your order latency is $\tau = 10\mu s$, you are racing to exploit the stale quote, and $e^{-0.2} \approx 82\%$ of the time you get there before it updates. That is a big edge. If it takes you $50\mu s$ (the mean delay itself), you still catch it $e^{-1} \approx 37\%$ of the time. So speed really matters -- every microsecond shifts your capture rate by roughly $\lambda = 2\%$. **Approach:** Use the exponential CDF for part (1), then combine with price impact for parts (2) and (3). **Formal Solution:** **(a) Probability the consolidated feed is still stale after $100\mu s$:** The delay is $\delta \sim \text{Exp}(\lambda)$ with $E[\delta] = 1/\lambda = 50\mu s$, so $\lambda = 0.02$ per $\mu s$. $$P(\delta > 100) = e^{-\lambda \cdot 100} = e^{-2} \approx 0.1353$$ About $13.5\%$ of the time, the consolidated feed has not yet updated after $100\mu s$. **(b) Information advantage:** Suppose the true price moved by $\Delta p$ at time $t_0$. During the delay $\delta$, the consolidated feed shows the old (stale) price. A trader with the fast feed can: - Buy at the stale (low) price if $\Delta p > 0$ - Sell at the stale (high) price if $\Delta p \tau) = e^{-\lambda \tau}$$ So the expected profit per opportunity is: $$E[\Pi] = E[|\Delta p|] \cdot e^{-\lambda \tau}$$ This is your information advantage: you earn $E[|\Delta p|]$ on each opportunity, discounted by the probability that the window is still open when your order hits. **(c) Connection to latency arbitrage:** This is exactly the mechanism behind latency arbitrage. Faster traders exploit the gap between when they observe new information and when slower participants react. The exponential model makes the value of speed transparent: - $\tau = 10\mu s$: $P(\text{stale}) = e^{-0.2} \approx 81.9\%$ - $\tau = 50\mu s$: $P(\text{stale}) = e^{-1} \approx 36.8\%$ - $\tau = 100\mu s$: $P(\text{stale}) = e^{-2} \approx 13.5\%$ The marginal value of a microsecond reduction in latency is: $$\frac{dE[\Pi]}{d\tau} = -\lambda \cdot E[|\Delta p|] \cdot e^{-\lambda \tau}$$ At $\tau = 0$, each microsecond is worth $\lambda \cdot E[|\Delta p|] = 0.02 \cdot E[|\Delta p|]$, i.e., roughly $2\%$ of the expected absolute price move per microsecond. This explains the arms race for co-location, microwave links, and FPGA-based trading systems -- the payoff to shaving microseconds is directly proportional to this exponential survival probability. **Answer:** (a) $P(\delta > 100) = e^{-2} \approx 13.5\%$. (b) Expected profit per opportunity is $E[\Pi] = E[|\Delta p|] \cdot e^{-\lambda \tau}$, where $\lambda = 0.02/\mu s$ and $\tau$ is your order latency. (c) Each microsecond of latency improvement increases your capture rate by approximately $2\%$, which is why firms invest heavily in speed infrastructure. --- Generate Uniform 1-7 from Uniform 1-5 [medium, Probability] --- https://quantvault.org/problem-424-generate-uniform-1-7-from-uniform-1-5.html Q: You are given a function `rand5()` that returns a uniformly random integer from 1 to 5. Using only `rand5()`, build `rand7()` returning a uniformly random integer from 1 to 7, then measure it empirically. Implement the function ```python def rand7_mean(n, seed): ... ``` which must: - create a deterministic generator `rng = random.Random(seed)` and `rand5 = lambda: rng.randint(1, 5)`, - build `rand7()` from `rand5()` by **rejection sampling**: take two `rand5()` calls as `num = 5*(rand5()-1) + (rand5()-1)` (uniform on $\{0,\dots,24\}$); if `num 3.9981` With `seed=1`, the seeded RNG makes the 20000 draws fully reproducible; their average `3.9981` sits just below the true mean `4.0`, and it tightens toward `4.0` as `n` increases. A: **How to Think About It:** Two calls to `rand5()` give you $5 \times 5 = 25$ equally likely outcomes. You need to map these to 7 equally likely outcomes. Since 7 does not divide 25, you cannot use all 25 outcomes -- you must reject some. The largest multiple of 7 that fits in 25 is 21, so you use outcomes 0-20 (mapped to 1-7 via modulo) and reject outcomes 21-24. This is rejection sampling. **What this problem actually asks:** the graded entry point is `rand7_mean(n, seed)`, not `rand7()` by itself. You must (1) build a deterministic `rand5()` from `random.Random(seed)`, (2) build `rand7()` on top of it via rejection sampling, then (3) return the empirical mean of `n` independent `rand7()` draws, `sum(draws) / n`. Because the RNG is seeded, the result is fully reproducible for a given `(n, seed)`, and as `n` grows it converges to the true mean of a uniform draw on $\{1,\dots,7\}$, which is $4.0$. **Algorithm for `rand7()`:** 1. Call `rand5()` twice to generate a uniform random number in $\{0, 1, \ldots, 24\}$ using $5 \cdot (a - 1) + (b - 1)$. 2. If the result is less than 21, return `result % 7 + 1`. 3. Otherwise, reject and repeat from step 1. Each of the values 0-20 maps to exactly one of 1-7 (three values each), so the output is perfectly uniform. **Code:** ```python import random def rand7_mean(n, seed): rng = random.Random(seed) rand5 = lambda: rng.randint(1, 5) def rand7(): while True: num = 5 * (rand5() - 1) + (rand5() - 1) if num < 21: return num % 7 + 1 total = 0 for _ in range(n): total += rand7() return total / n ``` **Complexity:** - Each `rand7()` iteration uses 2 calls to `rand5()`; acceptance probability $\frac{21}{25} = 0.84$. - Expected `rand5()` calls per output: $2 \times \frac{25}{21} = \frac{50}{21} \approx 2.38$. - `rand7_mean` therefore makes $\approx 2.38 n$ calls to `rand5()` and returns a single float. **Why rejection is unavoidable:** a single `rand5()` produces $\log_2 5 \approx 2.32$ bits of entropy while `rand7()` needs $\log_2 7 \approx 2.81$ bits, and since $\log_2 7 / \log_2 5$ is irrational no finite number of `rand5()` calls yields exactly the right entropy. Rejection sampling is the standard fix. **Answer:** Build `rand7()` by rejection sampling on the 25-outcome space (accept 21, reject 4), then average `n` draws. `rand7_mean` returns that empirical mean, which tends to $4.0$. --- Glosten-Milgrom Zero-Profit Bid-Ask Quotes [medium, Market Microstructure] --- https://quantvault.org/problem-425-glosten-milgrom-zero-profit-bid-ask-quotes.html Q: A binary asset has fundamental value $V \in \{0, 1\}$ with prior $P(V = 1) = p$, where $p \in (0, 1)$. A single counterparty arrives to trade one unit. With probability $1 - q$ the counterparty is perfectly informed (buys if $V = 1$, sells if $V = 0$). With probability $q$ the counterparty is a noise trader who buys or sells each with probability $\frac{1}{2}$. You are the market maker. You must post a bid $b$ and an ask $a$ (unit size) such that your expected profit is exactly zero conditional on being hit on that side. 1. Derive the zero-profit ask $a(p, q)$ and bid $b(p, q)$. 2. Compute the implied spread $a - b$ and simplify. 3. Interpret the limiting cases $q \to 0$ (all informed) and $q \to 1$ (all noise). A: **How to Think About It:** You are a market maker posting quotes against a mix of informed and uninformed flow. The informed traders pick you off -- they only buy when the asset is worth 1 and only sell when it is worth 0. Noise traders are your profit center since they trade randomly. The zero-profit condition means you set each quote equal to the expected value of the asset conditional on being hit on that side. This is just Bayes' theorem: a buy tells you it is more likely the counterparty is informed and $V = 1$, while a sell tells you $V = 0$ is more likely. The ask will be above $p$ (adverse selection on the buy side) and the bid will be below $p$ (adverse selection on the sell side), with the gap driven by how much informed flow there is. **Quick Estimate:** Take $p = 0.5$, $q = 0.5$ (half informed, half noise, fair coin prior). If someone buys, the informed component pushes $P(V=1)$ up. By symmetry the ask should be above 0.5 and the bid below by the same amount. Rough guess: $a \approx 0.7$, $b \approx 0.3$, spread $\approx 0.4$. As we will see, the exact answer gives $a = 3/4$, $b = 1/4$, spread $= 1/2$. **Approach:** Apply Bayes' theorem separately to the buy event and the sell event, then set each quote equal to the posterior expectation of $V$. **Formal Solution:** *Step 1: Ask quote (zero-profit on the sell side of the book).* When someone buys from you, compute the conditional probabilities: - $P(\text{buy} \mid V = 1) = (1 - q) \cdot 1 + q \cdot \tfrac{1}{2} = 1 - \tfrac{q}{2}$ - $P(\text{buy} \mid V = 0) = (1 - q) \cdot 0 + q \cdot \tfrac{1}{2} = \tfrac{q}{2}$ By Bayes' theorem: $$P(V = 1 \mid \text{buy}) = \frac{p\,(1 - q/2)}{p\,(1 - q/2) + (1 - p)\,q/2} = \frac{p(2 - q)}{2p + q - 2pq}$$ The zero-profit ask is $a = E[V \mid \text{buy}] = P(V = 1 \mid \text{buy})$: $$\boxed{a(p, q) = \frac{p(2 - q)}{2p + q - 2pq}}$$ With $p = q = \tfrac{1}{2}$: $P(\text{buy}\mid V=1) = 1 - \tfrac{1}{4} = \tfrac{3}{4}$ and $P(\text{buy}\mid V=0) = \tfrac{1}{4}$, so $$a = \frac{\tfrac{1}{2}\cdot\tfrac{3}{4}}{\tfrac{1}{2}\cdot\tfrac{3}{4} + \tfrac{1}{2}\cdot\tfrac{1}{4}} = \frac{3/8}{3/8 + 1/8} = \frac{3/8}{1/2} = \frac{3}{4}.$$ Equivalently from the formula, $a = \frac{(1/2)(3/2)}{1 + 1/2 - 1/2} = \frac{3/4}{1} = \frac{3}{4}$. *Step 2: Bid quote (zero-profit on the buy side of the book).* When someone sells to you: - $P(\text{sell} \mid V = 1) = (1 - q) \cdot 0 + q \cdot \tfrac{1}{2} = \tfrac{q}{2}$ - $P(\text{sell} \mid V = 0) = (1 - q) \cdot 1 + q \cdot \tfrac{1}{2} = 1 - \tfrac{q}{2}$ By Bayes' theorem: $$P(V = 1 \mid \text{sell}) = \frac{p\,q/2}{p\,q/2 + (1 - p)(1 - q/2)} = \frac{pq}{pq + (1 - p)(2 - q)}$$ The zero-profit bid: $$\boxed{b(p, q) = \frac{pq}{pq + (1 - p)(2 - q)}}$$ With $p = q = \tfrac{1}{2}$: $b = \frac{(1/2)(1/2)}{(1/2)(1/2) + (1/2)(3/2)} = \frac{1/4}{1/4 + 3/4} = \frac{1/4}{1} = \frac{1}{4}$. *Step 3: Implied spread.* Let $D_a = 2p + q - 2pq$ and $D_b = pq + (1 - p)(2 - q) = 2 - 2p - q + 2pq$. Note $D_a + D_b = 2$. $$a - b = \frac{p(2 - q)}{D_a} - \frac{pq}{D_b}$$ Putting over the common denominator $D_a D_b$ and expanding the numerator: $$p(2 - q) D_b - pq \, D_a = p\bigl[(2 - q)(2 - 2p - q + 2pq) - q(2p + q - 2pq)\bigr]$$ Expanding and collecting terms, the bracket simplifies to $4(1 - p)(1 - q)$, so: $$\boxed{a - b = \frac{4p(1 - p)(1 - q)}{(2p + q - 2pq)(2 - 2p - q + 2pq)}}$$ With $p = q = \tfrac{1}{2}$: $a - b = \frac{4(1/2)(1/2)(1/2)}{(1)(1)} = \frac{1/2}{1} = \frac{1}{2}$, matching $a - b = \tfrac{3}{4} - \tfrac{1}{4} = \tfrac{1}{2}$. *Step 4: Limiting cases.* **$q \to 0$ (all informed):** $a \to \frac{2p}{2p} = 1$ and $b \to \frac{0}{(1-p) \cdot 2} = 0$. The spread goes to 1, the maximum possible. Every counterparty is informed, so the market maker must quote the widest possible spread to avoid being picked off. Trading effectively shuts down. **$q \to 1$ (all noise):** $a \to \frac{p \cdot 1}{0 + 1} = p$ and $b \to \frac{p}{p + 0} = p$. The spread collapses to zero. With no informed traders, a buy or sell carries no information about $V$, so both quotes equal the prior $p$. The market is perfectly liquid. **Answer:** The zero-profit quotes are $a(p, q) = \frac{p(2 - q)}{2p + q - 2pq}$ and $b(p, q) = \frac{pq}{pq + (1-p)(2-q)}$. The spread is $\frac{4p(1-p)(1-q)}{(2p+q-2pq)(2-2p-q+2pq)}$. For $p = q = \tfrac{1}{2}$ this gives $a = \tfrac{3}{4}$, $b = \tfrac{1}{4}$, spread $= \tfrac{1}{2}$. The spread ranges from 0 (pure noise) to 1 (pure informed), and is maximized in the prior at $p = 1/2$ for any fixed $q$. --- Half-Gaussian Integral [easy, Probability] --- https://quantvault.org/problem-428-half-gaussian-integral.html Q: Evaluate the integral: $$\int_{0}^{\infty} e^{-x^2/2} \, dx$$ You may use the fact that the full Gaussian integral is $\int_{-\infty}^{\infty} e^{-x^2/2} \, dx = \sqrt{2\pi}$. A: **How to Think About It:** The integrand $e^{-x^2/2}$ is the kernel of the standard normal distribution. The full integral over the real line equals $\sqrt{2\pi}$ -- this is the normalizing constant of the standard normal density. Since the integrand is even (symmetric about zero), the integral from 0 to infinity is exactly half the full integral. That is all there is to this problem. Alternatively, you can derive it from the classic Gaussian integral $\int_0^\infty e^{-t^2} dt = \sqrt{\pi}/2$ via a simple substitution. **Quick Estimate:** $\sqrt{\pi/2} = \sqrt{1.571} \approx 1.253$. A quick sanity check: the integrand starts at $e^0 = 1$ and decays to 0. Most of its mass is in $[0, 2]$, and the average value over that range is around 0.5-0.6. So the integral is roughly $0.55 \times 2 \approx 1.1$, which is consistent with 1.25. **Approach:** Use symmetry of the even integrand. **Formal Solution:** **Method 1 (Symmetry):** The integrand $f(x) = e^{-x^2/2}$ satisfies $f(-x) = f(x)$, so it is even. Therefore: $$\int_{-\infty}^{\infty} e^{-x^2/2} \, dx = 2 \int_{0}^{\infty} e^{-x^2/2} \, dx$$ Dividing both sides by 2: $$\int_{0}^{\infty} e^{-x^2/2} \, dx = \frac{1}{2} \sqrt{2\pi} = \sqrt{\frac{\pi}{2}}$$ **Method 2 (Substitution from the standard Gaussian integral):** It is known that $\int_0^\infty e^{-t^2} \, dt = \frac{\sqrt{\pi}}{2}$. Substitute $t = x/\sqrt{2}$, so $dt = dx/\sqrt{2}$ and $x = \sqrt{2}\, t$: $$\int_0^\infty e^{-x^2/2} \, dx = \int_0^\infty e^{-t^2} \cdot \sqrt{2} \, dt = \sqrt{2} \cdot \frac{\sqrt{\pi}}{2} = \frac{\sqrt{2\pi}}{2} = \sqrt{\frac{\pi}{2}}$$ **Answer:** $$\int_{0}^{\infty} e^{-x^2/2} \, dx = \sqrt{\frac{\pi}{2}} \approx 1.2533$$ --- Last Marble Standing [medium, Probability] --- https://quantvault.org/problem-444-last-marble-standing.html Q: An urn contains $r$ red marbles, $b$ blue marbles, and $g$ green marbles. You draw marbles one at a time, uniformly at random, without replacement. What is the probability that immediately after the last red marble is drawn, there is still at least one blue marble AND at least one green marble remaining in the urn? Derive a general formula, then evaluate it for $r = 10$, $b = 20$, $g = 30$. A: **How to Think About It:** The key move is to stop thinking about the draw sequence and instead think about the position of the last marble of each color in a random permutation. Drawing $r + b + g$ marbles without replacement is exactly the same as choosing a uniformly random ordering of all of them. You need the last red to come before the last blue and before the last green -- equivalently, you want red NOT to be the last color still standing among the relevant pairs. Inclusion-exclusion on the two bad events handles this cleanly. **Quick Estimate:** For $r = 10$, $b = 20$, $g = 30$: the last red is the last among all $r + b + g = 60$ marbles with probability $10/60 = 1/6$ -- that is the probability both other colors run out first, which is a lower bound on the bad events. The answer should be comfortably above $1/2$ because red is the smallest group. Rough bracket: probably between $0.55$ and $0.65$. **Approach:** Reformulate as a permutation problem, identify the two bad events, and apply inclusion-exclusion. **Formal Solution:** Model the process as a uniformly random permutation of all $r + b + g$ marbles. Define: - $\text{last}(C)$ = the position of the last marble of color $C$ in the permutation. We want the event: $\text{last}(R) < \text{last}(B)$ and $\text{last}(R) < \text{last}(G)$ -- i.e., after the last red is drawn, at least one blue and one green still remain. Define the two bad events: $$A: \text{last}(G) < \text{last}(R) \quad \text{(all greens drawn before the last red)}$$ $$B: \text{last}(B) < \text{last}(R) \quad \text{(all blues drawn before the last red)}$$ By inclusion-exclusion: $$P(\text{good}) = 1 - P(A \cup B) = 1 - P(A) - P(B) + P(A \cap B)$$ **Computing $P(A)$:** Among the $r + g$ marbles that are red or green, the last one is equally likely to be any of them by symmetry. So: $$P(A) = P(\text{last of } \{R, G\} \text{ is red}) = \frac{r}{r + g}$$ **Computing $P(B)$:** By the same symmetry argument on red and blue marbles: $$P(B) = \frac{r}{r + b}$$ **Computing $P(A \cap B)$:** Both bad events occur if and only if the overall last marble among all three colors is red -- i.e., red's last position exceeds both the last green and last blue. Among all $r + b + g$ marbles, the very last one is equally likely to be any marble, so: $$P(A \cap B) = \frac{r}{r + b + g}$$ **General formula:** $$\boxed{P(\text{good}) = 1 - \frac{r}{r+g} - \frac{r}{r+b} + \frac{r}{r+b+g}}$$ **Numerical case $r = 10$, $b = 20$, $g = 30$:** $$1 - \frac{10}{40} - \frac{10}{30} + \frac{10}{60} = 1 - \frac{1}{4} - \frac{1}{3} + \frac{1}{6} = \frac{12 - 3 - 4 + 2}{12} = \frac{7}{12}$$ **Answer:** $\dfrac{7}{12} \approx 0.583$ --- Last White Kitten with Three Black Remaining [medium, Probability] --- https://quantvault.org/problem-445-last-white-kitten-with-three-black-remaining.html Q: A box contains $15$ kittens: $8$ black and $7$ white. You draw kittens one at a time uniformly at random (without replacement) until the box is empty. What is the probability that when the last white kitten is drawn, exactly $3$ black kittens remain in the box? A: **How to Think About It:** Think about the full sequence of $15$ draws as a random permutation of $8$ B's and $7$ W's. The condition "the last white kitten is drawn with exactly $3$ black remaining" means: the last $3$ positions in the sequence are all B, position $12$ is W, and the first $11$ positions contain the remaining $6$ W's and $5$ B's. So this is a straightforward counting problem. **Quick Estimate:** The last white kitten is drawn at position $12$ (out of $15$). Roughly, the probability that position $12$ is the last W is moderate -- there are $15$ possible positions for the last W, and it needs to land at exactly position $12$. Quick ballpark: something on the order of $1/15$ to $1/10$, so maybe $5\%$-$10\%$. **Approach:** Count favorable permutations over total permutations using combinations. **Formal Solution:** The total number of distinguishable sequences of $8$ B's and $7$ W's is: $$\binom{15}{7} = 6435$$ For the favorable outcome, we need the sequence to end in $W B B B$ (positions $12, 13, 14, 15$), where position $12$ is the last W and positions $13$-$15$ are the last $3$ B's. The first $11$ positions must contain exactly $6$ W's (the remaining whites) and $5$ B's (the remaining blacks). The number of ways to arrange these is: $$\binom{11}{6} = 462$$ (Choose $6$ of the $11$ positions for W; the rest are B.) The probability is: $$P = \frac{\binom{11}{6}}{\binom{15}{7}} = \frac{462}{6435} = \frac{14}{195} \approx 0.0718$$ **Verification:** $462 / 6435 = 462 / 6435$. Simplify: $\gcd(462, 6435) = 33$, so $462/33 = 14$ and $6435/33 = 195$. Thus $P = 14/195$. **Answer:** $\dfrac{14}{195} \approx 7.2\%$. --- Lily Pad Doubling: Head Start Problem [easy, Brain Teaser] --- https://quantvault.org/problem-447-lily-pad-doubling-head-start-problem.html Q: A single lily pad floats in a pond. Each day, its area doubles until it covers the entire pond. Starting from one lily pad, the pond is fully covered on day 30. If you instead start with 8 lily pads on day 1, how many days does it take to cover the entire pond? A: **How to Think About It:** Doubling problems are all about logarithms and equivalence. The key question is: starting with 8 pads is the same as starting with 1 pad but at what day? Since the area doubles each day, 1 pad becomes 8 pads after 3 days ($2^3 = 8$). So starting with 8 pads today is equivalent to having started with 1 pad 3 days ago. **Quick Estimate:** Day 30 covers the full pond with 1 starting pad. Three days of doubling get you from 1 to 8. So 8 pads should finish 3 days earlier: day $30 - 3 = 27$. That's the answer -- no algebra needed once you see the equivalence. **Formal Solution:** Let the pond's total area be $A$. Starting from 1 lily pad with area $a_0$, after $n$ days the total area is $a_0 \cdot 2^n$. The pond is covered when: $$a_0 \cdot 2^n = A \implies 2^n = A / a_0$$ We are told this happens at $n = 30$, so $A / a_0 = 2^{30}$. Now start with 8 pads, each of area $a_0$. After $n$ days the total area is $8 a_0 \cdot 2^n = a_0 \cdot 2^{n+3}$. The pond is covered when: $$a_0 \cdot 2^{n+3} = A = a_0 \cdot 2^{30} \implies n + 3 = 30 \implies n = 27$$ **Answer:** 27 days. --- Lions and Gazelle: Backward Induction [easy, Game Theory] --- https://quantvault.org/problem-450-lions-and-gazelle-backward-induction.html Q: A magical savanna has an odd number $n$ of lions and one gazelle. The lions are hungry and want to eat the gazelle, but if a lion eats a gazelle (or another lion that has become a gazelle), it falls asleep and transforms into a gazelle itself -- making it vulnerable to being eaten by the remaining lions. Each lion is rational: it wants to eat if possible, but it values survival above all else. A lion will only eat if it can guarantee it won't be eaten afterward. How many gazelles are consumed when $n = 15$? A: **How to Think About It:** This is a classic backward induction puzzle. Don't try to reason about 15 lions directly -- start from the simplest case and build up. The key tension is that eating makes you vulnerable, so a lion will only eat if the resulting configuration is "safe" for it. The trick is figuring out which configurations are safe. **Quick Estimate:** The answer is going to depend on the parity of $n$. With 1 lion, it obviously eats. With 2 lions, eating is suicidal. The pattern should alternate, and since 15 is odd, we expect 1 gazelle consumed. **Approach:** Build the solution inductively from $n = 1$ upward. **Formal Solution:** - **$n = 1$:** One lion, one gazelle. The lion eats freely since no other lion can threaten it. Gazelles consumed: $1$. - **$n = 2$:** If lion A eats the gazelle, A becomes a gazelle and we are in the $n = 1$ scenario with lion B. Lion B will eat lion A. Since lion A values survival, it does not eat. Gazelles consumed: $0$. - **$n = 3$:** If lion A eats the gazelle, A becomes a gazelle and we are in the $n = 2$ scenario. We just showed that with 2 lions, neither eats. So lion A is safe after eating. Lion A eats. Gazelles consumed: $1$. - **$n = 4$:** If lion A eats, we reduce to $n = 3$. With 3 lions, one lion eats -- meaning lion A (now a gazelle) gets eaten. So lion A does not eat. Gazelles consumed: $0$. The pattern is clear: - **Odd $n$:** Exactly 1 gazelle is consumed. The first lion to eat knows the remaining even number of lions will be in a stalemate. - **Even $n$:** No gazelles are consumed. Eating would leave an odd number of lions, and the eater (now a gazelle) would be eaten. For $n = 15$ (odd), exactly one lion eats the gazelle, and then the remaining 14 lions are in a stalemate. **Answer:** With $n = 15$ lions, exactly $1$ gazelle is consumed. --- Marble Transfer: Conditional Probability [easy, Probability] --- https://quantvault.org/problem-452-marble-transfer-conditional-probability.html Q: You have two bins. Bin 1 contains $r_1 = 4$ red and $b_1 = 3$ blue marbles. Bin 2 contains $r_2 = 5$ red and $b_2 = 3$ blue marbles. You draw one marble uniformly at random from Bin 1 and drop it into Bin 2. Then you draw one marble uniformly at random from the now-enlarged Bin 2. What is the probability that the marble you draw from Bin 2 is blue? A: **How to Think About It:** The draw from Bin 2 depends on what you transferred -- if you moved a blue marble over, you boosted the blue count; if you moved a red, you did not. So the first thing to do is split on the color of the transferred marble. This is the Law of Total Probability in its most natural form: condition on the thing that creates the uncertainty, compute each branch, and weight by the branch probabilities. A quick gut check before calculating: Bin 1 is mostly red (4 red, 3 blue), so most of the time you are adding a red marble to Bin 2, which makes the blue draw less likely. Bin 2 starts at $3/8 = 0.375$ blue. The transfer nudges that up or down depending on what moves. Since red is more likely to transfer, the answer should be slightly below 0.375. Let us see. **Quick Estimate:** Probability of transferring blue from Bin 1: $3/7 \approx 0.43$. Probability of transferring red: $4/7 \approx 0.57$. - If blue transfers: Bin 2 has 4 blue, 5 red (9 total). $P(\text{blue draw}) = 4/9 \approx 0.44$. - If red transfers: Bin 2 has 3 blue, 6 red (9 total). $P(\text{blue draw}) = 3/9 = 0.33$. Weighted average: $(0.43)(0.44) + (0.57)(0.33) \approx 0.19 + 0.19 = 0.38$. So the answer is around $0.38$, which is close to the baseline $3/8 = 0.375$ -- makes sense since the transfer is a wash in expectation (it does not systematically bias toward blue or red in Bin 2). **Approach:** Apply the Law of Total Probability, conditioning on the color of the marble transferred from Bin 1. **Formal Solution:** Let $B_1$ be the event that the marble drawn from Bin 1 is blue, and $R_1$ the event it is red. Let $B_2$ be the event that the marble drawn from Bin 2 is blue. From Bin 1 (7 marbles total): $$P(B_1) = \frac{b_1}{r_1 + b_1} = \frac{3}{7}, \quad P(R_1) = \frac{r_1}{r_1 + b_1} = \frac{4}{7}$$ After the transfer, Bin 2 always has $r_2 + b_2 + 1 = 9$ marbles. - If a blue was transferred: Bin 2 has $b_2 + 1 = 4$ blue marbles. $$P(B_2 \mid B_1) = \frac{b_2 + 1}{r_2 + b_2 + 1} = \frac{4}{9}$$ - If a red was transferred: Bin 2 has $b_2 = 3$ blue marbles. $$P(B_2 \mid R_1) = \frac{b_2}{r_2 + b_2 + 1} = \frac{3}{9} = \frac{1}{3}$$ By the Law of Total Probability: $$P(B_2) = P(B_2 \mid B_1)P(B_1) + P(B_2 \mid R_1)P(R_1)$$ $$P(B_2) = \frac{4}{9} \cdot \frac{3}{7} + \frac{3}{9} \cdot \frac{4}{7} = \frac{12}{63} + \frac{12}{63} = \frac{24}{63} = \frac{8}{21}$$ The general formula is: $$P(B_2) = \frac{b_1(b_2 + 1) + r_1 b_2}{(b_1 + r_1)(b_2 + r_2 + 1)}$$ **Answer:** $P(B_2) = \dfrac{8}{21} \approx 0.381$ --- Matching Royals: Each Player Gets One of Each Rank [medium, Probability] --- https://quantvault.org/problem-456-matching-royals-each-player-gets-one-of-each-rank.html Q: A reduced deck of 16 cards consists of 4 ranks (Jack, Queen, King, Ace), each appearing in all 4 suits. The deck is dealt evenly to 4 players, so each player receives exactly 4 cards. What is the probability that every player receives exactly one card of each rank? Round your answer to the nearest ten-thousandth. A: **How to Think About It:** We are dealing 16 distinct cards into 4 hands of 4 cards each, and we want each hand to be a "rainbow" -- one card from each rank. Think of it rank by rank: the 4 Jacks go to 4 players, the 4 Queens go to 4 players, and so on. Each rank's 4 cards must be distributed one-per-player. The total number of deals is the multinomial, and the favorable outcomes are the product of permutations for each rank. **Quick Estimate:** There are 4 ranks, each with 4 cards to assign to 4 players. For the first rank, all $4! = 24$ assignments are equally likely (one card per player). For the second rank, given the first is "used up" in terms of rank slots but not in terms of card slots, we again have $4!$ ways. But the constraint interacts with the dealing -- let me just compute directly. **Approach:** Count favorable outcomes (one card of each rank per player) over total outcomes (arbitrary deals of 4 cards each). **Formal Solution:** **Total deals:** We distribute 16 distinct cards into 4 groups of 4. The number of ways is the multinomial coefficient: $$\binom{16}{4, 4, 4, 4} = \frac{16!}{(4!)^4}$$ **Favorable deals:** For each rank (4 cards, one per suit), we assign one card to each of the 4 players. That is a permutation of 4 items: $4!$ ways per rank. Since there are 4 ranks and the assignments are independent: $$\text{Favorable} = (4!)^4$$ **Probability:** $$P = \frac{(4!)^4}{\binom{16}{4,4,4,4}} = \frac{(4!)^4}{\frac{16!}{(4!)^4}} = \frac{(4!)^8}{16!}$$ Now compute numerically: - $4! = 24$ - $(4!)^8 = 24^8 = 110{,}075{,}314{,}176$ - $16! = 20{,}922{,}789{,}888{,}000$ $$P = \frac{110{,}075{,}314{,}176}{20{,}922{,}789{,}888{,}000} \approx 0.005260$$ Rounding to the nearest ten-thousandth: $P \approx 0.0053$. Let me verify: $24^8 = 24^2 \times 24^2 \times 24^2 \times 24^2 = 576^4$. $576^2 = 331{,}776$. $576^4 = 331{,}776^2 = 110{,}075{,}314{,}176$. Check. $16! = 20{,}922{,}789{,}888{,}000$. The ratio is $110{,}075{,}314{,}176 / 20{,}922{,}789{,}888{,}000 \approx 0.005260$. **Answer:** The probability that each player receives one card of each rank is: $$P = \frac{(4!)^8}{16!} \approx 0.0053$$ --- Minimal Market Spread Under Correlation Uncertainty [hard, Finance] --- https://quantvault.org/problem-459-minimal-market-spread-under-correlation-uncertainty.html Q: Two events $A$ and $B$ have known marginal probabilities $P(A) = p$ and $P(B) = q$. Define the indicator $Y = \mathbf{1}\{A \oplus B\}$, i.e., $Y = 1$ if exactly one of $A$ or $B$ occurs (exclusive or). The catch: you do not know the joint distribution. You only know that the correlation $\rho = \operatorname{Corr}(\mathbf{1}_A, \mathbf{1}_B)$ lies in a known interval $[\rho_L, \rho_U]$. 1. Derive tight (attainable) bounds on $E[Y]$ over all joint laws consistent with the given marginals and the constraint $\rho \in [\rho_L, \rho_U]$. 2. You must post a single symmetric market $(b, a)$ with mid $m$ and half-spread $s/2$, so $b = m - s/2$ and $a = m + s/2$. An informed trader knows the true $\rho \in [\rho_L, \rho_U]$ and will trade against you whenever your quote is off. Find the minimal spread $s$ (and the corresponding mid $m$) that guarantees nonnegative expected P&L against this worst-case informed trader. A: **How to Think About It:** This is a classic robust market-making problem. You are pricing a digital on an XOR event, but you do not know how the two underlying events are correlated -- you just have bounds on the correlation. Part (i) is really about Frechet bounds restricted by a correlation constraint, and part (ii) asks: how wide does your spread need to be to avoid getting picked off by someone who knows more than you? The answer to part (ii) falls out immediately once you have part (i) -- the spread has to cover the full range of fair values. **Quick Estimate:** Take $p = q = 0.5$ and $\rho \in [-0.5, 0.5]$. Then $\sigma_A = \sigma_B = 0.5$, so $c = 0.25$. The XOR base rate is $p + q - 2pq = 0.5$. The range of $E[Y]$ is $0.5 - 2(-0.5)(0.25) = 0.75$ down to $0.5 - 2(0.5)(0.25) = 0.25$. So the fair value swings between 0.25 and 0.75 depending on correlation, and the spread must be at least $0.75 - 0.25 = 0.50$. That is a very wide spread -- correlation uncertainty is expensive. **Approach:** Express $E[Y]$ as a linear function of $\rho$, then optimize over the constraint interval. **Formal Solution:** *Part (i): Bounds on $E[Y]$* Write $Y = \mathbf{1}_A + \mathbf{1}_B - 2\,\mathbf{1}_A \mathbf{1}_B$, so: $$E[Y] = p + q - 2\,P(A \cap B)$$ The correlation between the two indicators is: $$\rho = \frac{P(A \cap B) - pq}{\sqrt{p(1-p)\,q(1-q)}}$$ Let $c = \sqrt{p(1-p)\,q(1-q)}$ denote the product of marginal standard deviations. Then $P(A \cap B) = pq + \rho\, c$, and: $$E[Y] = (p + q - 2pq) - 2\rho\, c$$ This is **linear and decreasing** in $\rho$. Therefore over $\rho \in [\rho_L, \rho_U]$: $$E[Y]_{\max} = (p + q - 2pq) - 2\rho_L\, c \quad (\text{attained at } \rho = \rho_L)$$ $$E[Y]_{\min} = (p + q - 2pq) - 2\rho_U\, c \quad (\text{attained at } \rho = \rho_U)$$ These bounds are tight because for any $\rho$ in the Frechet-feasible range, there exists a $2 \times 2$ joint law with the given marginals achieving that correlation. *Part (ii): Minimal spread* The market maker posts bid $b = m - s/2$ and ask $a = m + s/2$. The informed trader knows the true fair value $V(\rho) = (p+q-2pq) - 2\rho\,c$ and acts as follows: - If $V(\rho) > a$: trader buys at $a$, MM loses $V(\rho) - a$ per contract. - If $V(\rho) < b$: trader sells at $b$, MM loses $b - V(\rho)$ per contract. - If $b \leq V(\rho) \leq a$: no trade (or zero expected loss). For the MM to have nonnegative expected P&L against all $\rho \in [\rho_L, \rho_U]$, we need: $$b \leq V(\rho) \leq a \quad \forall\, \rho \in [\rho_L, \rho_U]$$ That is, the bid-ask interval must contain the entire range $[E[Y]_{\min},\, E[Y]_{\max}]$. The minimal spread that achieves this is: $$s^{*} = E[Y]_{\max} - E[Y]_{\min} = 2(\rho_U - \rho_L)\,c = 2(\rho_U - \rho_L)\sqrt{p(1-p)\,q(1-q)}$$ with mid: $$m^{*} = \frac{E[Y]_{\max} + E[Y]_{\min}}{2} = (p + q - 2pq) - (\rho_L + \rho_U)\,c$$ **Answer:** The tight bounds on $E[Y]$ are: $$E[Y] \in \Big[(p+q-2pq) - 2\rho_U\,c,\;\; (p+q-2pq) - 2\rho_L\,c\Big]$$ where $c = \sqrt{p(1-p)\,q(1-q)}$. The minimal spread is: $$s^{*} = 2(\rho_U - \rho_L)\sqrt{p(1-p)\,q(1-q)}$$ with mid $m^{*} = (p+q-2pq) - (\rho_L + \rho_U)\,c$. --- Minimum Tests to Find the Broken Revision [easy, Combinatorics] --- https://quantvault.org/problem-460-minimum-tests-to-find-the-broken-revision.html Q: You have 100 sequential code revisions. At some point, a bug was introduced -- meaning revisions $1$ through $k-1$ are clean, and revisions $k$ through $100$ are broken. You can test any revision to determine whether it is clean or broken. What is the minimum number of tests needed to guarantee you find the exact revision $k$ that introduced the bug? A: **How to Think About It:** This is a classic binary search setup. Each test tells you whether the bug was introduced before or after a given revision, which splits the remaining candidates roughly in half. The question is really asking: how many times can you halve 100 before you get down to 1 candidate? **Quick Estimate:** $2^6 = 64$ and $2^7 = 128$. Since $64 < 100 < 128$, six tests are not enough to distinguish 100 candidates, but seven are. So the answer is 7. **Approach:** Use binary search. Each test on a midpoint revision partitions the remaining candidates into two halves. **Formal Solution:** Maintain a search interval $[\text{low}, \text{high}]$ initialized to $[1, 100]$. 1. Test the midpoint $\text{mid} = \lfloor (\text{low} + \text{high}) / 2 \rfloor$. 2. If $\text{mid}$ is broken, the bug was introduced at or before $\text{mid}$, so set $\text{high} = \text{mid}$. 3. If $\text{mid}$ is clean, the bug was introduced after $\text{mid}$, so set $\text{low} = \text{mid} + 1$. 4. Repeat until $\text{low} = \text{high}$. The number of tests needed is the number of halvings required to reduce 100 candidates to 1: $$\lceil \log_2(100) \rceil = \lceil 6.644 \rceil = 7$$ To verify, trace through the candidate count after each test: - Start: 100 candidates - After test 1: 50 - After test 2: 25 - After test 3: 13 - After test 4: 7 - After test 5: 4 - After test 6: 2 - After test 7: 1 (found!) **What if revision $1$ is known to be clean?** The statement allows $k = 1$ (every revision broken), so there are $100$ candidates for $k$. If you are also told revision $1$ is clean, the candidates shrink to $k \in \{2, \dots, 100\}$, which is $99$ values, and the count is $\lceil \log_2 99 \rceil$. Since $64 < 99 \le 128$, that is still $7$: the answer does not change. This is exactly the algorithm behind `git bisect`, which binary-searches through commits to find the one that introduced a bug. **Answer:** The minimum number of tests is $\lceil \log_2(100) \rceil = 7$. --- Monte Carlo Methods and Variance Reduction [medium, Probability] --- https://quantvault.org/problem-462-monte-carlo-methods-and-variance-reduction.html Q: Explain how the Monte Carlo method works for estimating expectations. Then describe and compare the main variance reduction techniques: antithetic variates, control variates, importance sampling, stratified sampling, and quasi-Monte Carlo. For each technique, explain the intuition for why it reduces variance and give a concrete example of when you would use it in practice. A: **How to Think About It:** Monte Carlo is the brute-force workhorse of quantitative finance. Whenever you cannot compute $E[f(X)]$ analytically -- which is most of the time for path-dependent derivatives, high-dimensional integrals, or complex portfolio simulations -- you simulate it. The method is dead simple: draw $N$ samples, compute $f$ for each, average the results. The catch is that the error decays as $O(1/\sqrt{N})$, meaning you need 100x more samples to get 10x more accuracy. Variance reduction is about being smarter with your samples so you get the same accuracy with fewer draws. **Key Insight:** All variance reduction techniques exploit some known structure in the problem to reduce the randomness in your estimate. The more you know about $f$ and $X$, the more variance you can squeeze out. **The Method:** **1. Basic Monte Carlo** To estimate $\theta = E[f(X)]$, draw $X_1, \ldots, X_N$ i.i.d. from the distribution of $X$ and compute: $$\hat{\theta} = \frac{1}{N} \sum_{i=1}^N f(X_i)$$ By the law of large numbers, $\hat{\theta} \to \theta$ as $N \to \infty$. By the CLT, the standard error is $\sigma_f / \sqrt{N}$ where $\sigma_f^2 = \text{Var}(f(X))$. **2. Antithetic Variates** Idea: instead of drawing $N$ independent samples, draw $N/2$ pairs $(U_i, 1 - U_i)$ (or more generally, pair each sample with its "mirror image"). If $f$ is monotone, the two evaluations in each pair are negatively correlated, so their average has lower variance than two independent draws. Estimator: $\hat{\theta} = \frac{1}{N} \sum_{i=1}^{N/2} [f(U_i) + f(1-U_i)]$. When to use: pricing options where the payoff is monotone in the underlying (e.g., vanilla calls). Pairing an up-path with a down-path is a natural antithetic construction. **3. Control Variates** Idea: find a random variable $g(X)$ whose expectation $E[g(X)]$ you know analytically, and which is correlated with $f(X)$. Subtract off the "error" in $g$ to reduce the error in $f$: $$\hat{\theta}_{CV} = \hat{\theta} - c(\bar{g} - E[g])$$ where $c = \text{Cov}(f, g) / \text{Var}(g)$ is the optimal coefficient. Variance reduction factor: $1 - \rho^2_{f,g}$. When to use: pricing an exotic option where a related vanilla option has a known formula. Use the vanilla payoff as the control variate. For example, pricing an Asian call using a European call as control. **4. Importance Sampling** Idea: sample from a different distribution $q(x)$ that puts more weight where $f(x)p(x)$ is large, and correct with likelihood ratios: $$\hat{\theta}_{IS} = \frac{1}{N} \sum_{i=1}^N f(X_i) \frac{p(X_i)}{q(X_i)}, \quad X_i \sim q$$ The optimal proposal is $q^{*}(x) \propto |f(x)| p(x)$, which gives zero variance (but requires knowing $\theta$, so it is not directly useful). In practice, you choose $q$ to shift mass toward the important region. When to use: estimating rare-event probabilities (e.g., probability of a 10-sigma move). Standard MC gives almost all zeros -- importance sampling shifts the distribution toward the tail. **5. Stratified Sampling** Idea: partition the sample space into strata $S_1, \ldots, S_K$, then sample proportionally from each stratum. This guarantees you cover the entire space evenly, unlike pure random sampling which can cluster. $$\hat{\theta}_{SS} = \sum_{k=1}^K P(S_k) \cdot \hat{\theta}_k$$ Variance reduction comes from removing the between-strata variance. The total variance is only the within-strata variance. When to use: high-dimensional integrals where certain regions contribute disproportionately. For example, stratifying by the first time step in a path simulation. **6. Quasi-Monte Carlo** Idea: replace pseudorandom numbers with low-discrepancy sequences (Sobol, Halton) that fill the space more evenly. The error improves from $O(1/\sqrt{N})$ to $O((\log N)^d / N)$ for $d$-dimensional integrals -- essentially $O(1/N)$ for moderate dimensions. When to use: pricing high-dimensional derivatives (basket options, mortgage-backed securities). Works best when the integrand is smooth and the effective dimension is moderate. **Practical Considerations:** - Antithetic variates: easiest to implement, modest improvement (typically 20-50% variance reduction for monotone payoffs). - Control variates: most widely used in practice. The key is finding a good control -- the closer to $|\rho| = 1$, the better. - Importance sampling: powerful but dangerous. A bad proposal can increase variance dramatically. Requires problem-specific tuning. - Stratified sampling: useful but harder in high dimensions (curse of dimensionality for the strata). - Quasi-MC: strong theoretical guarantees, but performance degrades in very high dimensions and with discontinuous integrands. **Answer:** Monte Carlo estimates $E[f(X)]$ by sample averaging with $O(1/\sqrt{N})$ convergence. The five main variance reduction techniques each exploit different structure: antithetic variates use symmetry, control variates use correlation with known quantities, importance sampling reweights toward important regions, stratified sampling ensures even coverage, and quasi-MC uses deterministic low-discrepancy sequences. --- Non-Adjacent Selection from 1 to 10 [medium, Probability] --- https://quantvault.org/problem-465-non-adjacent-selection-from-1-to-10.html Q: You pick 3 numbers uniformly at random from the set $\{1, 2, \ldots, 10\}$ (all $\binom{10}{3}$ subsets equally likely). The catch: the selection is only valid if no two chosen numbers are adjacent -- meaning no two differ by exactly 1. What is the probability that a random 3-element subset satisfies this non-adjacency condition? A: **How to Think About It:** This is a counting problem dressed up as probability. The denominator is easy: $\binom{10}{3} = 120$ equally likely subsets. The work is in the numerator -- how many 3-element subsets of $\{1, \ldots, 10\}$ have no two elements differing by 1? The key is recognizing that a non-adjacency constraint on an ordered set is equivalent to a simpler unconstrained counting problem via a classic substitution. Before going formal, sanity check the scale: there are 120 total subsets. Non-adjacency is a meaningful restriction -- you can't pick, say, $\{1, 2, 3\}$ or $\{5, 6, 9\}$ -- so the answer should be noticeably less than 1. A rough guess might be around half, so expect something in the $\frac{1}{2}$ to $\frac{2}{3}$ range. **Quick Estimate:** Count directly for small cases to build intuition. For $\{1, \ldots, 5\}$, choose 2 non-adjacent: valid pairs are $\{1,3\}, \{1,4\}, \{1,5\}, \{2,4\}, \{2,5\}, \{3,5\}$ -- that is 6 out of $\binom{5}{2} = 10$, giving $3/5$. The bijection argument (explained below) predicts $\binom{4}{2}/\binom{5}{2} = 6/10$. Checks out. Scaling to our problem, the bijection gives $\binom{8}{3}/\binom{10}{3} = 56/120 = 7/15 \approx 0.467$. Slightly less than half -- makes sense given the restriction bites harder with more elements. **Approach:** Use a variable substitution (stars and bars bijection) to reduce the constrained counting problem to an unconstrained one. **Formal Solution:** Let the three chosen numbers in increasing order be $a_1 < a_2 < a_3$. The non-adjacency constraint requires $a_2 \geq a_1 + 2$ and $a_3 \geq a_2 + 2$. Define new variables: $$b_1 = a_1, \quad b_2 = a_2 - 1, \quad b_3 = a_3 - 2.$$ Then $b_1 < b_2 < b_3$, and since $a_3 \leq 10$, we get $b_3 \leq 8$. So $(b_1, b_2, b_3)$ is an unconstrained 3-element subset of $\{1, 2, \ldots, 8\}$. The map is a bijection, so: $$\text{Number of valid subsets} = \binom{8}{3} = 56.$$ The total number of 3-element subsets is $\binom{10}{3} = 120$. $$P(\text{no two adjacent}) = \frac{56}{120} = \frac{7}{15}.$$ **Answer:** $\displaystyle\frac{7}{15} \approx 0.467$. --- Odd Die, Even Result After Subtraction [easy, Probability] --- https://quantvault.org/problem-467-odd-die-even-result-after-subtraction.html Q: A fair $n$-sided die, where $n$ is odd, has faces numbered $1$ through $n$. You roll the die, record the outcome, and subtract $1$. What is the probability that the result is even? Compute the answer for $n = 11$. A: **How to Think About It:** Subtracting 1 flips the parity of the outcome: even becomes odd, odd becomes even. So asking "what is the probability the result is even" is exactly the same as asking "what is the probability the die shows an odd number?" Now count: among $\{1, 2, \ldots, n\}$ with $n$ odd, there are $\frac{n+1}{2}$ odd numbers and $\frac{n-1}{2}$ even numbers -- the odd numbers outnumber the even numbers by exactly one, since $n$ itself is odd and goes into the odd bucket. **Quick Estimate:** For $n = 11$: faces are $\{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\}$. Odd faces: $\{1, 3, 5, 7, 9, 11\}$ -- that is 6 faces. Total: 11 faces. So probability $= 6/11 \approx 0.545$. Slightly more than half, as expected. **Formal Solution:** Let $X$ be the outcome of the die roll, $X \sim \text{Uniform}\{1, 2, \ldots, n\}$. We want $P(X - 1 \text{ is even}) = P(X \text{ is odd})$. Among $\{1, 2, \ldots, n\}$ with $n$ odd: - Odd values: $1, 3, 5, \ldots, n$ -- these are $\frac{n+1}{2}$ values. - Even values: $2, 4, 6, \ldots, n-1$ -- these are $\frac{n-1}{2}$ values. Since the die is fair: $$P(X \text{ is odd}) = \frac{(n+1)/2}{n} = \frac{n+1}{2n}$$ For $n = 11$: $$P = \frac{11+1}{2 \times 11} = \frac{12}{22} = \frac{6}{11}$$ **Answer:** $\dfrac{n+1}{2n}$, which equals $\dfrac{6}{11}$ for $n = 11$. --- Pattern Waiting Time [hard, Expectation] --- https://quantvault.org/problem-475-pattern-waiting-time.html Q: You toss a fair coin repeatedly and wait for a specific pattern to appear as a consecutive run. Start with the pattern $\text{HTH}$. 1. Model the process as a Markov chain where each state represents the length of the longest suffix of your flip history that matches a prefix of $\text{HTH}$. Write down the transition matrix, set up the first-step equations for the expected waiting time $E[T]$, and solve for $E[T]$. 2. Now generalize. For an arbitrary binary pattern $P = p_1 p_2 \cdots p_L$ with a fair coin, explain how to construct the corresponding automaton (with states $0, 1, \ldots, L$), and derive a closed-form formula for $E[T]$ in terms of the pattern's self-overlap structure. A: **How to Think About It:** This is a classic Markov chain problem. You track how much of your target pattern you have "built up" so far. Each coin flip either extends your current match by one character or knocks you back to some earlier state. The trick is figuring out the failure transitions -- when you fail to extend, you do not always go back to state 0, because the tail of what you have already seen might itself be a prefix of the target. This is the same failure-function logic as the KMP string matching algorithm. Once you have the chain, it is a standard first-step expected-value calculation. Before doing any algebra, you can guess: the pattern HTH has a self-overlap (it starts and ends with H), which creates a "false start" effect that inflates the waiting time compared to a non-overlapping pattern like HTT. **Quick Estimate:** A pattern of length 3 on a fair coin has a base waiting time of at least $2^3 = 8$ flips (that is the contribution from the full pattern match). The overlap where the last character H matches the first character H adds $2^1 = 2$ more, giving roughly $8 + 2 = 10$. Compare this to HTT, which has no self-overlap beyond the trivial full match, so $E[T_{\text{HTT}}] = 8$. The overlap penalty for HTH is modest but real. **Approach:** Build the Markov chain for HTH, write first-step equations, and solve. **Formal Solution:** *Part (i): The HTH chain* The states are $\{0, 1, 2, 3\}$, where state $k$ means the last $k$ flips match the first $k$ characters of HTH, and state 3 is absorbing (pattern found). Transitions (each outcome has probability $1/2$): - **State 0** (no match): Flip H $\to$ state 1 (matched "H"). Flip T $\to$ state 0. - **State 1** (matched "H"): Flip T $\to$ state 2 (matched "HT"). Flip H $\to$ state 1 (the new H is itself a valid prefix). - **State 2** (matched "HT"): Flip H $\to$ state 3 (matched "HTH" -- done!). Flip T $\to$ state 0 ("HTT" has no prefix overlap with HTH). - **State 3**: Absorbing. Let $E_i$ be the expected number of additional flips from state $i$ to reach state 3. $$E_0 = 1 + \frac{1}{2} E_1 + \frac{1}{2} E_0$$ $$E_1 = 1 + \frac{1}{2} E_1 + \frac{1}{2} E_2$$ $$E_2 = 1 + \frac{1}{2}(0) + \frac{1}{2} E_0$$ From the first equation: $\frac{1}{2} E_0 = 1 + \frac{1}{2} E_1$, so $E_0 = 2 + E_1$. From the second: $\frac{1}{2} E_1 = 1 + \frac{1}{2} E_2$, so $E_1 = 2 + E_2$. From the third: $E_2 = 1 + \frac{1}{2} E_0$. Substituting backward: $$E_1 = 2 + 1 + \frac{1}{2} E_0 = 3 + \frac{1}{2} E_0$$ $$E_0 = 2 + 3 + \frac{1}{2} E_0 = 5 + \frac{1}{2} E_0$$ $$\frac{1}{2} E_0 = 5 \implies E_0 = 10$$ So $E_1 = 8$ and $E_2 = 6$. $$\boxed{E[T] = E_0 = 10}$$ *Part (ii): General binary pattern* For an arbitrary pattern $P = p_1 p_2 \cdots p_L$ on a fair coin, construct the automaton exactly as above: state $k$ means the last $k$ flips match $p_1 \cdots p_k$. The failure transitions use the KMP failure function -- when you are in state $k$ and the next flip does not match $p_{k+1}$, you fall back to the longest proper suffix of $p_1 \cdots p_k$ (plus the new flip) that is also a prefix of $P$. The closed-form result is the **Conway leading number** formula. Define the self-overlap indicator: for each $k \in \{1, 2, \ldots, L\}$, let $c_k = 1$ if the last $k$ characters of $P$ equal the first $k$ characters of $P$, and $c_k = 0$ otherwise. (Note $c_L = 1$ always, since the whole pattern trivially matches itself.) Then: $$E[T] = \sum_{k=1}^{L} c_k \cdot 2^k$$ For a biased coin with $P(H) = p$ and $P(T) = 1 - p$, the contribution of each overlapping position $k$ is $\prod_{i=1}^{k} \frac{1}{q_i}$, where $q_i$ is the probability of the $i$-th character in the pattern. **Verification for HTH:** The self-overlaps are at $k = 1$ (suffix "H" = prefix "H") and $k = 3$ (trivial). Position $k = 2$: suffix "TH" $\neq$ prefix "HT", so $c_2 = 0$. Therefore $E[T] = 2^1 + 2^3 = 2 + 8 = 10$. This matches. **Comparison examples:** - HTT: only $c_3 = 1$, so $E[T] = 2^3 = 8$. - HH: $c_1 = 1$ ("H" = "H") and $c_2 = 1$, so $E[T] = 2 + 4 = 6$. - THH: only $c_3 = 1$, so $E[T] = 8$. **Answer:** For the pattern HTH on a fair coin, $E[T] = 10$. In general, $E[T] = \sum_{k=1}^{L} c_k \cdot 2^k$, where $c_k = 1$ if the pattern has a self-overlap of length $k$ (its last $k$ characters equal its first $k$), and $c_k = 0$ otherwise. The automaton is constructed using the KMP failure function: upon a mismatch at state $k$, transition to the longest proper prefix of $p_1 \cdots p_k$ (appended with the new flip) that is also a prefix of $P$. --- Pirate Gold Division: Extending the Pattern [hard, Game Theory] --- https://quantvault.org/problem-478-pirate-gold-division-extending-the-pattern.html Q: Consider the classic pirate gold division problem. There are $n$ pirates ranked by seniority (Pirate $n$ is the most senior). They must divide 100 gold coins. The rules are: - The most senior surviving pirate proposes a division of the coins. - All pirates (including the proposer) vote. If at least half vote in favor, the proposal passes. - If the proposal fails, the proposer is thrown overboard, and the next most senior pirate proposes. - Pirates are perfectly rational, greedy (prefer more gold), and above all else prefer survival. Work through the optimal proposals for $n = 6$ and $n = 7$ pirates using backward induction from the known cases ($n = 1$ through $5$). Then identify the general pattern: how does the proposer's share and the set of bribed pirates change as $n$ increases? Finally, what is the maximum number of pirates that can play this game with 100 gold coins such that the most senior pirate still survives? A: **How to Think About It:** This is one of the great backward induction puzzles. The key insight is that every pirate can figure out exactly what happens if they get thrown overboard, so the proposer only needs to offer the cheapest possible bribes to secure a majority. A pirate who would receive 0 coins in the next round will happily vote yes for just 1 coin. The proposer exploits this ruthlessly -- buying the cheapest votes available. **Quick Estimate:** The proposer needs $\lceil n/2 \rceil$ votes total (including their own), so they must buy $\lceil n/2 \rceil - 1$ votes at 1 coin each. For $n = 6$, that is 2 bought votes costing 2 coins, leaving 98. For $n = 7$, that is 3 bought votes costing 3 coins, leaving 97. The proposer's share drops by 1 coin for every 2 additional pirates. The bribes eventually consume the whole treasure: at $n = 200$ the proposer must buy 99 votes and keeps just 1 coin, so $n = 200$ is the last crew size at which proposing is still profitable. **Approach:** Build up from the base cases using backward induction, tracking who gets 0 at each stage (those are the cheaply bribable voters). **Formal Solution:** **Base cases (recap):** - $n = 1$: Pirate 1 takes all 100 coins. - $n = 2$: Pirate 2 takes all 100 coins (votes for themselves, that is 1 out of 2 = 50%, proposal passes). Pirate 1 gets 0. - $n = 3$: Pirate 3 needs 2 votes. Pirate 1 got 0 in the $n = 2$ case, so offering Pirate 1 just 1 coin secures their vote. Distribution: Pirate 3 gets 99, Pirate 1 gets 1, Pirate 2 gets 0. - $n = 4$: Pirate 4 needs 2 votes. Pirate 2 got 0 in the $n = 3$ case. Distribution: Pirate 4 gets 99, Pirate 2 gets 1, Pirates 1 and 3 get 0. - $n = 5$: Pirate 5 needs 3 votes. Pirates 1 and 3 got 0 in the $n = 4$ case. Distribution: Pirate 5 gets 98, Pirates 1 and 3 each get 1, Pirates 2 and 4 get 0. **$n = 6$:** Pirate 6 needs $\lceil 6/2 \rceil = 3$ votes (including their own), so they must buy 2 votes. In the $n = 5$ case, Pirates 2 and 4 received 0 coins, so they are the cheap votes. Pirate 6 offers them 1 coin each. Distribution: Pirate 6 gets 98, Pirates 2 and 4 each get 1, Pirates 1, 3, and 5 get 0. **$n = 7$:** Pirate 7 needs $\lceil 7/2 \rceil = 4$ votes, so they must buy 3 votes. In the $n = 6$ case, Pirates 1, 3, and 5 received 0 coins. Pirate 7 offers each of them 1 coin. Distribution: Pirate 7 gets 97, Pirates 1, 3, and 5 each get 1, Pirates 2, 4, and 6 get 0. **The pattern:** - The proposer (Pirate $n$) keeps $100 - (\lceil n/2 \rceil - 1)$ coins (as long as this is nonnegative). - They bribe exactly the $\lceil n/2 \rceil - 1$ pirates who received 0 in the $(n-1)$ case, giving each 1 coin. - The bribed pirates alternate in parity: for even $n$, the proposer bribes even-numbered pirates; for odd $n$, the proposer bribes odd-numbered pirates. - The proposer's share decreases by 1 coin for every 2 additional pirates. **Maximum number of pirates:** The proposer needs $\lceil n/2 \rceil$ votes (including their own) and must buy $\lceil n/2 \rceil - 1$ of them at 1 coin each, keeping $100 - (\lceil n/2 \rceil - 1)$ coins. Tracking this as $n$ grows: - At $n = 200$: they need 100 votes, buy 99, and keep $100 - 99 = 1$ coin. This is the largest crew for which the most senior pirate still ends up with any gold. - At $n = 201$: they need 101 votes, spend all 100 coins on bribes, and keep 0. They still vote for their own proposal (survival beats gold), so it passes 101-to-100 -- they survive but gain nothing. - At $n = 202$: they need 101 votes = their own + 100 bought. In the 201-pirate case, 101 pirates end up with 0 coins, so buying 100 of them for 1 coin each secures exactly half the votes -- they survive, again with 0 coins. - At $n = 203$: they need 102 votes but can muster at most their own + 100 bought = 101 -- they are thrown overboard. - Beyond that, survival becomes sporadic: pirates who are doomed in the continuation vote yes for free, so Pirate 204 survives (Pirate 203's free vote + their own + 100 bribes), then 205--207 die, 208 survives, and in general the proposer survives exactly at crew sizes $n = 200 + 2^k$ (204, 208, 216, 232, ...). Bare survival therefore has no finite maximum. **Answer:** For bare survival there is no finite maximum. Up to $n = 200$ the most senior pirate survives and keeps gold; at $n = 200$ he bribes 99 pirates with 1 coin each and keeps 1 coin, so $200$ is the largest crew in which he still comes out ahead. At $n = 201$ and $n = 202$ he survives with 0 coins, at $n = 203$ he dies, and beyond that he survives only at crew sizes $n = 200 + 2^k$ (204, 208, 216, ...), propped up by free votes from doomed pirates. The general pattern (while bribes are affordable): Pirate $n$ keeps $100 - \lceil n/2 \rceil + 1$ coins and bribes $\lceil n/2 \rceil - 1$ alternating-parity pirates with 1 coin each. --- Posterior Fair Value from Noisy Signal [medium, Probability] --- https://quantvault.org/problem-481-posterior-fair-value-from-noisy-signal.html Q: A latent "fair value" $V$ is drawn from $V \sim N(0, \sigma_V^2)$ with $\sigma_V = 1$. You observe a noisy signal $S = V + \epsilon$, where $\epsilon \sim N(0, \sigma_\epsilon^2)$ with $\sigma_\epsilon = 2$, independent of $V$. Given an observation $S = 1.5$: 1. Compute the posterior distribution $V \mid S = 1.5$ (find the posterior mean and variance). 2. Compute $P(V > 0 \mid S = 1.5)$. A: **How to Think About It:** This is the workhorse signal-extraction problem in quant finance. You have a noisy observation of some true value and you want to update your belief about that value. The prior says $V$ is centered at 0, and you see $S = 1.5$. Your posterior for $V$ should shift toward 1.5, but not all the way -- because the signal is noisy ($\sigma_\epsilon = 2$ is twice $\sigma_V = 1$), you should weight the prior heavily. The posterior mean will be a precision-weighted average of the prior mean and the signal. Since $V > 0$ lines up with the direction the signal is pulling you, $P(V > 0 \mid S)$ should be comfortably above 0.5, but not enormous given the noise. **Quick Estimate:** The signal-to-noise ratio here is $\sigma_V^2 / \sigma_\epsilon^2 = 1/4$, which is low. So the posterior mean should be pulled only modestly toward $S = 1.5$. Rough guess: posterior mean $\approx 1.5 \times (1/5) = 0.3$. Posterior variance should be less than the prior variance of 1 -- maybe around $0.8$, giving posterior std $\approx 0.9$. Then $P(V > 0) \approx \Phi(0.3 / 0.9) \approx \Phi(0.33) \approx 0.63$. Let's verify. **Approach:** Use the standard conjugate normal-normal update. **Formal Solution:** The joint distribution of $(V, S)$ is bivariate normal. We have: - $V \sim N(0, 1)$ - $S = V + \epsilon$ with $\epsilon \sim N(0, 4)$, so $S \sim N(0, 1 + 4) = N(0, 5)$ - $\text{Cov}(V, S) = \text{Cov}(V, V + \epsilon) = \text{Var}(V) = 1$ By the standard conditional normal formula: $$E[V \mid S] = \mu_V + \frac{\text{Cov}(V,S)}{\text{Var}(S)}(S - \mu_S) = 0 + \frac{1}{5}(S - 0) = \frac{S}{5}$$ $$\text{Var}(V \mid S) = \text{Var}(V) - \frac{\text{Cov}(V,S)^2}{\text{Var}(S)} = 1 - \frac{1}{5} = \frac{4}{5}$$ Equivalently, using precision (inverse variance) weighting: the prior precision is $\tau_V = 1/\sigma_V^2 = 1$ and the signal precision is $\tau_\epsilon = 1/\sigma_\epsilon^2 = 1/4$. The posterior precision is $\tau_{\text{post}} = \tau_V + \tau_\epsilon = 1 + 1/4 = 5/4$, giving posterior variance $\sigma_{\text{post}}^2 = 4/5$. The posterior mean is: $$\mu_{\text{post}} = \frac{\tau_V \cdot 0 + \tau_\epsilon \cdot S}{\tau_{\text{post}}} = \frac{(1/4) \cdot 1.5}{5/4} = \frac{1.5}{5} = 0.3$$ So: $$V \mid S = 1.5 \sim N\!\left(0.3, \, \frac{4}{5}\right)$$ For Part 2, we need $P(V > 0 \mid S = 1.5)$. Standardizing: $$P(V > 0 \mid S = 1.5) = P\!\left(Z > \frac{0 - 0.3}{\sqrt{4/5}}\right) = P\!\left(Z > \frac{-0.3}{0.8944}\right) = P(Z > -0.3354)$$ $$= \Phi(0.3354) \approx 0.6313$$ **Answer:** 1. $V \mid S = 1.5 \sim N(0.3, \; 4/5)$. The posterior mean is $\mu_{\text{post}} = 0.3$ and the posterior variance is $\sigma_{\text{post}}^2 = 4/5 = 0.8$. 2. $P(V > 0 \mid S = 1.5) = \Phi(0.3 / \sqrt{0.8}) \approx 0.631$. --- Posterior Probability of Gender Given Infection [easy, Probability] --- https://quantvault.org/problem-483-posterior-probability-of-gender-given-infection.html Q: A population is split evenly between men and women. A disease infects 3% of men but only 1% of women. You randomly select a person and discover they are infected. What is the probability that the infected person is male? Generalize: if the population has fraction $q$ male, men are infected at rate $p_M$, and women at rate $p_F$, derive $P(\text{Male} \mid \text{Infected})$. A: **How to Think About It:** Before doing any math, think about what your gut says. Men get infected at 3x the rate of women, and the population is 50-50. So among all infected people, men should be over-represented -- roughly 3 out of every 4 infected people should be male. That is your quick estimate: about 75%. Now let's confirm. **Quick Estimate:** In a population of 10,000 (5,000 men, 5,000 women), about $5{,}000 \times 0.03 = 150$ men are infected and $5{,}000 \times 0.01 = 50$ women are infected. Total infected: 200. Fraction male: $150/200 = 0.75$. Done -- 75%. **Approach:** Bayes' theorem with the law of total probability. **Formal Solution:** Define: - $P(M) = q$, $P(F) = 1 - q$ - $P(I \mid M) = p_M$, $P(I \mid F) = p_F$ By the law of total probability: $$P(I) = p_M \cdot q + p_F \cdot (1 - q)$$ By Bayes' theorem: $$P(M \mid I) = \frac{P(I \mid M) \cdot P(M)}{P(I)} = \frac{p_M \cdot q}{p_M \cdot q + p_F \cdot (1 - q)}$$ Plugging in $q = 0.5$, $p_M = 0.03$, $p_F = 0.01$: $$P(M \mid I) = \frac{0.03 \times 0.5}{0.03 \times 0.5 + 0.01 \times 0.5} = \frac{0.015}{0.02} = 0.75$$ **Answer:** $P(\text{Male} \mid \text{Infected}) = 0.75$. In general, $P(M \mid I) = \dfrac{p_M \cdot q}{p_M \cdot q + p_F(1 - q)}$. --- Prime Picker Expected Payoff [easy, Expectation] --- https://quantvault.org/problem-486-prime-picker-expected-payoff.html Q: An integer $n$ is chosen uniformly at random from $\{2, 3, 4, 5, 6, 7, 8, 9, 10\}$. - If $n$ is prime, you receive $\$n$. - If $n$ is composite, you pay $\$n/2$. What is the fair value of this game? A: **How to Think About It:** This is a straightforward expected value calculation. List all outcomes, compute the payoff for each, weight by the uniform probability $1/9$, and add up. The only subtlety is remembering which numbers are prime: the primes in $\{2, \ldots, 10\}$ are $2, 3, 5, 7$. The composites are $4, 6, 8, 9, 10$. Quick gut check: 4 primes paying their face value versus 5 composites costing half their face value, so the composites might outweigh since there are more of them. **Quick Estimate:** Sum of prime payoffs: $2 + 3 + 5 + 7 = 17$. Sum of composite penalties: $4/2 + 6/2 + 8/2 + 9/2 + 10/2 = 2 + 3 + 4 + 4.5 + 5 = 18.5$. Net: $17 - 18.5 = -1.5$. Divide by 9 outcomes: $-1.5/9 \approx -0.167$. So the game costs you about 17 cents on average. **Approach:** Direct expected value computation. **Formal Solution:** Each of the 9 integers is equally likely with probability $1/9$. The payoff function is: | $n$ | Type | Payoff | |-----|------|--------| | 2 | Prime | $+2$ | | 3 | Prime | $+3$ | | 4 | Composite | $-2$ | | 5 | Prime | $+5$ | | 6 | Composite | $-3$ | | 7 | Prime | $+7$ | | 8 | Composite | $-4$ | | 9 | Composite | $-4.5$ | | 10 | Composite | $-5$ | $$E[\text{payoff}] = \frac{1}{9}(2 + 3 - 2 + 5 - 3 + 7 - 4 - 4.5 - 5)$$ $$= \frac{-1.5}{9} = -\frac{1}{6}$$ **Answer:** The fair value is $-\$1/6 \approx -\$0.167$. The game is slightly unfavorable: on average you lose about 17 cents per play. --- Probability a Random Subset Is Contained in Another [medium, Probability] --- https://quantvault.org/problem-488-probability-a-random-subset-is-contained-in-another.html Q: Two subsets $A$ and $B$ are chosen independently and uniformly at random from all subsets of a set $X$ with $|X| = 5$. What is the probability that $A \subseteq B$? Generalize to $|X| = n$. A: **How to Think About It:** The trick is to think element by element. For each element $x \in X$, there are four equally likely possibilities for whether $x$ is in $A$ and whether $x$ is in $B$. The constraint $A \subseteq B$ means "if $x \in A$ then $x \in B$" -- i.e., the case $(x \in A, x \notin B)$ is forbidden. So 3 out of 4 possibilities are valid for each element, and since the elements are independent, you just raise $3/4$ to the power $n$. **Quick Estimate:** For $n = 5$: $(3/4)^5 = 243/1024 \approx 0.237$. This makes sense -- it is a bit less than $1/4$, which is reasonable since requiring one random set to contain another is a fairly strong condition but not extreme. **Approach:** Use element-wise independence to decompose the joint event. **Formal Solution:** The total number of ordered pairs $(A, B)$ of subsets of $X$ is $2^n \times 2^n = 4^n$, since each subset is chosen independently. For $A \subseteq B$ to hold, we need: for every $x \in X$, if $x \in A$ then $x \in B$. Consider any single element $x$. Independently, $x$ is in $A$ with probability $1/2$ and in $B$ with probability $1/2$. The four equally likely membership patterns are: | $x \in A?$ | $x \in B?$ | Valid for $A \subseteq B$? | |---|---|---| | No | No | Yes | | No | Yes | Yes | | Yes | No | No | | Yes | Yes | Yes | So for each element, the probability of a valid pattern is $3/4$. Since the membership decisions for different elements are independent (both $A$ and $B$ are chosen uniformly), the events across elements are independent. Therefore: $$P(A \subseteq B) = \left(\frac{3}{4}\right)^n$$ For $n = 5$: $$P(A \subseteq B) = \left(\frac{3}{4}\right)^5 = \frac{3^5}{4^5} = \frac{243}{1024}$$ **Answer:** $P(A \subseteq B) = (3/4)^n$. For $n = 5$, this equals $243/1024 \approx 0.237$. --- Probability the Last Die Roll Is Two When Summing Past 100 [hard, Probability] --- https://quantvault.org/problem-496-probability-the-last-die-roll-is-two-when-summing-past-100.html Q: You throw a fair six-sided die repeatedly and keep a running total. The moment the total exceeds $100$, you stop. What is the probability that the final throw -- the one that pushed the total past $100$ -- was a $2$? A: **How to Think About It:** The total is climbing by $1$-$6$ each throw, so just before the crossing throw the total sits at one of the values $95, 96, \ldots, 100$. For the last throw to be a $2$, you need the total to be at $99$ or $100$ right before that throw (because only then does adding $2$ push you past $100$). So the question reduces to: how likely is the random walk to visit each of those positions? For large thresholds the renewal theorem tells us that each position is visited with roughly the same probability $1/\mu$, which makes the answer proportional to $k$. **Quick Estimate:** The mean of a fair die is $\mu = 3.5$, so the renewal theorem says each integer is visited with probability approximately $1/3.5 = 2/7 \approx 0.286$. For the last throw to be $2$, we need the running total to be $99$ or $100$ just before throwing. Each of those two states is visited with probability about $2/7$, and from each the probability of rolling a $2$ is $1/6$. So $P \approx 2 \times \frac{2}{7} \times \frac{1}{6} = \frac{4}{42} = \frac{2}{21} \approx 0.095$. **Approach:** Use the Key Renewal Theorem for a discrete random walk with step distribution $\{1,2,3,4,5,6\}$, each with probability $1/6$. **Formal Solution:** Let $S_n = X_1 + X_2 + \cdots + X_n$ where each $X_i$ is a fair die roll. We stop at the first $n$ with $S_n > 100$. The last throw is $X_n$. Define $p_j = P(S_m = j \text{ for some } m \geq 0)$ as the probability the random walk ever visits position $j$. By the Key Renewal Theorem, since the step distribution has $\gcd$ of its support equal to $1$ (aperiodic) and finite mean $\mu = 7/2$: $$p_j \to \frac{1}{\mu} = \frac{2}{7} \quad \text{as } j \to \infty$$ For the last throw to equal $k$, we need $S_{n-1} \leq 100$ and $S_{n-1} + k > 100$, which means $S_{n-1} \in \{101 - k, \, 102 - k, \, \ldots, \, 100\}$. That is $k$ possible positions. The probability is: $$P(\text{last throw} = k) = \sum_{j=101-k}^{100} p_j \cdot \frac{1}{6}$$ For $N = 100$ (large enough for the renewal approximation to be excellent), each $p_j \approx 2/7$, so: $$P(\text{last throw} = k) \approx k \cdot \frac{2}{7} \cdot \frac{1}{6} = \frac{k}{21}$$ Sanity check: $\sum_{k=1}^{6} \frac{k}{21} = \frac{1+2+3+4+5+6}{21} = \frac{21}{21} = 1$. The probabilities sum to $1$. For $k = 2$: $$P(\text{last throw} = 2) = \frac{2}{21}$$ Note that larger die values are more likely to be the crossing throw, which makes sense: a $6$ can push you over from any of six positions ($95$-$100$), while a $1$ can only do so from position $100$ itself. **Answer:** $P(\text{last throw} = 2) = \dfrac{2}{21} \approx 0.0952$. --- Race to Three Heads [hard, Probability] --- https://quantvault.org/problem-498-race-to-three-heads.html Q: Two players take turns flipping a fair coin. The first player to accumulate 3 consecutive heads on their own flips wins. Players alternate turns, flipping once per turn. You go first and flip heads on your very first flip. What is the probability that you win the game? Clarifications: - Each player's "streak" only counts consecutive heads on their own turns. Your opponent's flips do not break your streak, and vice versa. - If you flip tails, your streak resets to 0. If you flip heads, your streak increments by 1. - The game ends the moment either player reaches a streak of 3 heads. A: **How to Think About It:** The state is tiny: (your streak, their streak, whose turn), with streaks in $\{0,1,2\}$. Two observations turn this from an 18-equation grind into something you can reason through. First, **role-swap symmetry**: your winning probability in state $(a, b)$ with *their* turn equals $1$ minus *their* winning probability in $(b, a)$ with the mover's turn — so half the unknowns are mirrors of the other half. Second, and this is the interview move: you don't need the full system to get the number. Expand just **one round** (their flip, then yours) from the starting position — every branch lands in a state you can price roughly, and one branch lands in (approximately) the *mirror of the start*, which puts $P$ on both sides of the equation. Solving that little self-referential equation gives the answer to two decimal places. **Quick Estimate:** Let $P$ = your win probability from the start (your streak 1, opponent about to flip). Play one round — opponent flips, then you flip. No one can finish within this round, so the four equally likely branches ($\tfrac14$ each) are: - **They T, you H** $\to$ you sit at streak 2. Estimate it with its own one-liner: your next flip converts with probability $\tfrac12$ — instant win. Miss ($\tfrac12$) and your streak resets, leaving a roughly even race in which they move next, worth a bit under a coin flip to you ($\approx 0.45$). So $\tfrac12 \cdot 1 + \tfrac12 \cdot 0.45 \approx 0.72$ — call it $0.7$. - **They T, you T** $\to$ fresh symmetric game, but *they* move first. How big is a mover's edge from scratch? Both of you need an uninterrupted $HHH$ ($\tfrac18$ per three-turn window); moving first just means they get each window one flip before you do — one extra $\tfrac18$-shot's worth of head start, a couple of points, not more. Mover $\approx 0.52$, so you $\approx 0.48$. - **They H, you H** $\to$ your streak 2 vs. their 1. Same as the $0.7$ case, except their threat is live: from streak 1 they finish with $HH$ on their next two flips ($\tfrac14$) instead of needing the full $\tfrac18$ triple. A doubled threat that still usually misses shaves a couple of points: call it $0.68$. - **They H, you T** $\to$ they hold streak 1, you hold 0: the mirror of your starting position, so your probability is about $1 - P$. Now solve the one-line recursion: $$P \approx \tfrac14\big(0.7 + 0.48 + 0.68\big) + \tfrac14(1 - P) \;\Rightarrow\; 5P \approx 2.86 \;\Rightarrow\; P \approx 0.572.$$ That is the whole trick — and note that every input was itself a tiny estimate of the same kind: convert-or-reset one-liners for the streak branches, a first-mover-edge argument for the fresh race, symmetry for the hard branch. Heuristics all the way down, each one checkable in your head. (The exact answer turns out to be $0.5706$ — the mental version is off by $0.002$.) **Approach:** For the exact number, set up the mover-perspective values, collapse the 18 states to 9 via the role-swap symmetry, and back-substitute. **Formal Solution:** **Part 1 — dynamics.** Let $M(a,b)$ = P(you win $\mid$ streaks $(a,b)$, your turn) and $O(a,b)$ = P(you win $\mid$ streaks $(a,b)$, their turn). Flips are fair, streaks reset on tails, and hitting 3 ends the game: $$M(2,b) = \tfrac12 \cdot 1 + \tfrac12\,O(0,b), \qquad M(a,b) = \tfrac12\,O(a{+}1,b) + \tfrac12\,O(0,b) \;\; (a \le 1),$$ $$O(a,2) = \tfrac12 \cdot 0 + \tfrac12\,M(a,0), \qquad O(a,b) = \tfrac12\,M(a,b{+}1) + \tfrac12\,M(a,0) \;\; (b \le 1).$$ **Part 2 — symmetry collapse.** Swapping roles swaps the streaks and the mover, so $$O(a,b) = 1 - M(b,a),$$ which eliminates all nine $O$'s. Substituting into the $M$ equations leaves a $9\times9$ linear system in $M(a,b)$ alone, e.g. $M(2,b) = 1 - \tfrac12 M(b,0)$ and $M(a,b) = 1 - \tfrac12\big[M(b,a{+}1) + M(b,0)\big]$ for $a \le 1$. **Part 3 — solve.** Back-substitution clears to denominator $913$. The full table (all values $/913$): | $M(a,b)$ | $b=0$ | $b=1$ | $b=2$ | |---|---|---|---| | $a=0$ | $478$ | $392$ | $256$ | | $a=1$ | $546$ | $496$ | $288$ | | $a=2$ | $674$ | $640$ | $576$ | Every equation can be spot-checked by inspection — for instance $M(2,0) = 1 - \tfrac12 M(0,0) = 1 - \tfrac{239}{913} = \tfrac{674}{913}$ ✓, and $M(0,0) = 1 - \tfrac12[M(0,1) + M(0,0)] \Rightarrow \tfrac32 M(0,0) = 1 - \tfrac{196}{913} = \tfrac{717}{913} \Rightarrow M(0,0) = \tfrac{478}{913}$ ✓. The quantity we want is the start state — your streak 1, *their* flip: $$O(1,0) = 1 - M(0,1) = 1 - \frac{392}{913} = \boxed{\dfrac{521}{913} \approx 0.5706}$$ **Part 4 — sanity checks.** The mental recursion's inputs were honest: the table gives $O(2,0) = 1 - M(0,2) = \tfrac{657}{913} \approx 0.72$ (we guessed $0.7$), fresh-game mover edge $M(0,0) \approx 0.524$ (we guessed $0.52$), $O(2,1) \approx 0.685$ (we guessed $0.68$), and the "mirror" branch $O(0,1) = 1 - M(1,0) \approx 0.402$ vs. our $1 - P \approx 0.43$ — the roughest input, and still only $0.03$ off, which the $\tfrac14$ weight shrinks to under a point of error. A 2M-game Monte Carlo gives $0.5707 \pm 0.0004$. **Answer:** $P = \dfrac{521}{913} \approx 0.571$. Your edge compounds two effects: the 1-head head start (worth most of it) and favorable turn parity. And the reusable interview skill: expand one round, price the easy branches coarsely, use symmetry to write the hard branch as $1-P$, and solve the little recursion — it gets you to $0.57$ in under a minute without touching the $9\times9$ system. --- Rainbow Train Rearrangement [medium, Probability] --- https://quantvault.org/problem-499-rainbow-train-rearrangement.html Q: A train has three cars: Red, Green, and Blue. The conductor can perform one operation as many times as they like: disconnect all cars behind the front car and move them to the front, cycling the rear car to the leading position. More precisely, at each step the conductor takes the rearmost car and places it at the front of the train. The cars start in a uniformly random order. What is the probability that the conductor can rearrange the train into rainbow order (Red, then Green, then Blue)? A: **How to Think About It:** First name the operation: taking the rear car to the front is a **cyclic rotation** of the three cars. So the conductor's *entire* toolkit is rotate-0, rotate-1, rotate-2 times — after three rotations you're back to start. The heuristic is **orbit counting**: the reachable arrangements from any start form one rotation orbit of size $3$, and the target is reachable iff the start lies in the target's orbit. The naive trap is to assume any of the $3!=6$ orders can be reached — but rotation cannot change the *cyclic order* of the colors, only where you cut the cycle. **Quick Estimate:** No heavy math needed — count the orbit in your head. Rotation preserves cyclic order, and the $6$ permutations of three distinct items split into exactly two cyclic classes: the clockwise cycle $R\to G\to B$ and its mirror $R\to B\to G$. Rainbow order sits in one class of size $3$; a uniformly random start lands in that class with probability $\tfrac{3}{6}=\tfrac12$. That's the answer. **Approach:** Count starting permutations in the target's rotation orbit, divide by $3!$. **Formal Solution:** Label rainbow order $R=(1,2,3)$ (Red, Green, Blue). Applying the rotation (rear to front) repeatedly: $$(1,2,3)\;\xrightarrow{\text{rot}}\;(3,1,2)\;\xrightarrow{\text{rot}}\;(2,3,1)\;\xrightarrow{\text{rot}}\;(1,2,3).$$ So the target's orbit is $$\text{Orbit}_1 = \{(1,2,3),\,(3,1,2),\,(2,3,1)\}.$$ The other three permutations form a disjoint orbit that never reaches the target: $$\text{Orbit}_2 = \{(1,3,2),\,(2,1,3),\,(3,2,1)\},\qquad (1,3,2)\to(2,1,3)\to(3,2,1)\to(1,3,2).$$ Each orbit has size $3$ because $\mathbb{Z}_3$ acts freely on the $3$ distinct cars. The conductor reaches rainbow order iff the start is in $\text{Orbit}_1$. With a uniform start: $$P = \frac{|\text{Orbit}_1|}{3!} = \frac{3}{6} = \boxed{\tfrac12}.$$ **Answer:** $\dfrac{1}{2}$. --- Random Walk on a Table -- Expected Steps to Fall Off [medium, Expectation] --- https://quantvault.org/problem-503-random-walk-on-a-table-expected-steps-to-fall-off.html Q: A robot is placed on a one-dimensional table of length $L$ cm, at a distance $x$ cm from the left edge (so it is $L - x$ cm from the right edge). At each step, the robot moves 1 cm to the left with probability $p$ and 1 cm to the right with probability $q = 1 - p$. The robot falls off when it reaches either edge. 1. Find the expected number of steps $E_x$ until the robot falls off, as a function of $x$, $L$, $p$, and $q$. 2. Simplify for the symmetric case $p = q = 1/2$. 3. Verify your formula with a sanity check: what happens at $x = 0$, $x = L$, and $x = L/2$ in the symmetric case? A: **How to Think About It:** This is the gambler's ruin problem in disguise. The robot's position is the gambler's fortune, the table edges are the absorbing barriers at 0 and $L$, and we want the expected duration of the game rather than the ruin probability. The recurrence is straightforward: from position $i$, you spend one step and then you are at $i-1$ or $i+1$ with probabilities $p$ and $q$. Boundary conditions: $E_0 = E_L = 0$ (you have already fallen off). The recurrence is a second-order linear difference equation with constant coefficients, so you can solve it by standard techniques. **Quick Estimate:** For the symmetric case with $L = 100$ and $x = 30$: the formula (which we will derive) gives $E_{30} = 30 \times (100 - 30) = 2100$ steps. Intuitively, the expected duration scales quadratically in $L$ for a symmetric random walk -- doubling the table length quadruples the expected time. The maximum is at the center: $E_{L/2} = L^2/4$. **Approach:** Set up the recurrence $E_i = 1 + p E_{i-1} + q E_{i+1}$ with $E_0 = E_L = 0$ and solve as a linear difference equation. **Formal Solution:** *The Recurrence:* For $1 \leq i \leq L-1$: $$E_i = 1 + p E_{i-1} + q E_{i+1}, \quad E_0 = E_L = 0$$ Rearranging: $q E_{i+1} - E_i + p E_{i-1} = -1$. *Case 1: Symmetric walk ($p = q = 1/2$):* The recurrence becomes: $$\frac{1}{2} E_{i+1} - E_i + \frac{1}{2} E_{i-1} = -1$$ or equivalently $E_{i+1} - 2E_i + E_{i-1} = -2$. Let $D_i = E_{i+1} - E_i$. Then $D_i - D_{i-1} = -2$, so $D_i = D_0 - 2i$. Summing: $E_i = E_0 + \sum_{j=0}^{i-1} D_j = \sum_{j=0}^{i-1}(D_0 - 2j) = i D_0 - i(i-1)$. Using $E_L = 0$: $L D_0 - L(L-1) = 0$, so $D_0 = L - 1$. Therefore: $$E_i = i(L-1) - i(i-1) = i(L - i)$$ *Sanity checks:* $E_0 = 0$ and $E_L = 0$ (good). $E_{L/2} = L^2/4$ (maximum at center). $E_1 = L - 1$ and $E_{L-1} = L - 1$ (symmetric). *Case 2: Biased walk ($p \neq q$):* The homogeneous equation $q E_{i+1} - E_i + p E_{i-1} = 0$ has characteristic equation $q r^2 - r + p = 0$, giving roots $r = 1$ and $r = p/q$. The general solution to the inhomogeneous equation is: $$E_i = A + B \left(\frac{p}{q}\right)^i + \frac{i}{q - p}$$ (The particular solution $i/(q-p)$ works when $p \neq q$.) Applying $E_0 = 0$: $A + B = 0$, so $B = -A$. Applying $E_L = 0$: $A - A(p/q)^L + L/(q-p) = 0$, giving: $$A = \frac{L}{(q-p)(1 - (p/q)^L)}$$ Therefore: $$E_i = \frac{L}{(q-p)} \cdot \frac{1 - (p/q)^i}{1 - (p/q)^L} - \frac{i}{q-p}$$ which can also be written as: $$E_i = \frac{1}{q - p}\left(\frac{L(1 - (p/q)^i)}{1 - (p/q)^L} - i\right)$$ **Answer:** For a symmetric walk ($p = q = 1/2$), the expected number of steps to fall off from position $x$ is $E_x = x(L - x)$. For a biased walk ($p \neq q$), the formula is $E_x = \frac{1}{q-p}\left(\frac{L(1-(p/q)^x)}{1-(p/q)^L} - x\right)$. --- Scotty's Car Between Vacant Spots [medium, Probability] --- https://quantvault.org/problem-508-scotty-s-car-between-vacant-spots.html Q: Scotty parks his car in a row of $N = 15$ spots. He never parks at either endpoint -- his car is always somewhere in the interior. He then leaves, and when he returns, he finds that $k = 6$ total cars are in the lot (including his own), with the other $5$ having parked uniformly at random among the remaining open spots (at most one car per spot). What is the probability that Scotty's car has an empty spot on both its left and its right? Round to the nearest thousandth. A: **How to Think About It:** The key move here is to recognize that Scotty's specific interior position doesn't matter. Any interior spot has exactly two neighbors. So the question reduces entirely to: of all the ways the other $5$ cars can fill the remaining $14$ spots, in how many arrangements do both of Scotty's neighbors stay empty? Once you see that framing, it's a clean combinatorics ratio -- favorable arrangements over total arrangements. **Quick Estimate:** There are $14$ spots for the other $5$ cars. Scotty's car occupies $1$ interior spot, so there are $2$ "blocked" spots (his immediate neighbors) and $12$ free spots. Rough sanity check: each of the $5$ other cars independently avoids the $2$ blocked spots with probability $12/14 \approx 0.857$. If cars were placed independently (they are not -- no replacement -- but this is our ballpark), the probability would be roughly $0.857^5 \approx 0.46$. The actual answer will be somewhat lower because placing $5$ cars without replacement concentrates more mass, but 0.40-0.45 is a reasonable bracket to check against. **Approach:** Straightforward combinatorial counting. Total arrangements vs. favorable arrangements, both as combinations. **Formal Solution:** Since Scotty is in an interior spot, the specific position is irrelevant -- any interior spot has exactly $2$ adjacent spots. There are $N - 1 = 14$ remaining spots for the other $k - 1 = 5$ cars. *Total arrangements:* The $5$ other cars choose $5$ spots from the $14$ available: $$\binom{14}{5}$$ *Favorable arrangements:* Both spots adjacent to Scotty's car must be empty, so the $5$ other cars must park in the $N - 3 = 12$ spots that are not Scotty's spot and not either neighbor: $$\binom{12}{5}$$ *Probability:* $$P = \frac{\binom{N-3}{k-1}}{\binom{N-1}{k-1}} = \frac{\binom{12}{5}}{\binom{14}{5}}$$ Computing: $$\binom{12}{5} = \frac{12!}{5! \cdot 7!} = 792, \qquad \binom{14}{5} = \frac{14!}{5! \cdot 9!} = 2002$$ $$P = \frac{792}{2002} = \frac{36}{91} \approx 0.396$$ **Answer:** $\dfrac{\binom{12}{5}}{\binom{14}{5}} = \dfrac{792}{2002} \approx \boxed{0.396}$ --- Stationary Distribution of a Candy Conveyor Belt Markov Chain [easy, Probability] --- https://quantvault.org/problem-515-stationary-distribution-of-a-candy-conveyor-belt-markov-chai.html Q: A conveyor belt dispenses two types of candy: chocolate (C) and jelly bean (J). The sequence is generated by a two-state Markov chain with the following transition probabilities: $$P(J | C) = \frac{4}{5}, \quad P(C | C) = \frac{1}{5}$$ $$P(C | J) = \frac{2}{3}, \quad P(J | J) = \frac{1}{3}$$ Assuming the chain has been running long enough to reach stationarity, what fraction of candies are jelly beans? A: **How to Think About It:** In steady state, the rate of flow from C to J must equal the rate of flow from J to C -- otherwise the proportion of each type would drift over time. This balance condition is called the "flow balance" or "detailed balance" equation and it gives a very fast route to the answer. Before computing: chocolates transition to jelly beans with probability 4/5 (high rate leaving C), while jelly beans transition to chocolates with probability 2/3 (moderate rate leaving J). Since chocolates leave C quickly, C should be rare in steady state. Rough guess: maybe 30% chocolate, 70% jelly bean. **Quick Estimate:** Let $\pi_J$ be the steady-state fraction of jelly beans. The flow out of the C state equals $\pi_C \times P(J|C) = \pi_C \times 4/5$, and the flow into the C state from J equals $\pi_J \times P(C|J) = \pi_J \times 2/3$. Setting these equal and using $\pi_C + \pi_J = 1$ should give us $\pi_J$ directly. **Approach:** Solve the stationary distribution equations $\pi = \pi P$ (or equivalently, flow balance). **Formal Solution:** The transition matrix is: $$P = \begin{pmatrix} 1/5 & 4/5 \\ 2/3 & 1/3 \end{pmatrix}$$ where rows are current state (C, J) and columns are next state (C, J). The stationary distribution $\pi = (\pi_C, \pi_J)$ satisfies $\pi P = \pi$ with $\pi_C + \pi_J = 1$. **Flow balance** (equivalent and faster for two states): the rate of C-to-J transitions equals the rate of J-to-C transitions: $$\pi_C \cdot P(J|C) = \pi_J \cdot P(C|J)$$ $$\pi_C \cdot \frac{4}{5} = \pi_J \cdot \frac{2}{3}$$ Substituting $\pi_C = 1 - \pi_J$: $$(1 - \pi_J) \cdot \frac{4}{5} = \pi_J \cdot \frac{2}{3}$$ $$\frac{4}{5} - \frac{4}{5}\pi_J = \frac{2}{3}\pi_J$$ $$\frac{4}{5} = \pi_J \left(\frac{2}{3} + \frac{4}{5}\right) = \pi_J \cdot \frac{10 + 12}{15} = \pi_J \cdot \frac{22}{15}$$ $$\pi_J = \frac{4}{5} \cdot \frac{15}{22} = \frac{60}{110} = \frac{6}{11}$$ And $\pi_C = 1 - 6/11 = 5/11$. **Verification:** Check $\pi P = \pi$: $$\pi_C^{\text{new}} = \pi_C \cdot \frac{1}{5} + \pi_J \cdot \frac{2}{3} = \frac{5}{11} \cdot \frac{1}{5} + \frac{6}{11} \cdot \frac{2}{3} = \frac{1}{11} + \frac{4}{11} = \frac{5}{11} \checkmark$$ $$\boxed{\pi_J = \frac{6}{11} \approx 0.545}$$ About 54.5% of candies are jelly beans in the long run. **Answer:** The stationary fraction of jelly beans is $6/11 \approx 54.5\%$. --- The Birthday Problem: Shared Birthdays in a Room [easy, Probability] --- https://quantvault.org/problem-518-the-birthday-problem-shared-birthdays-in-a-room.html Q: You are at a party and wondering about birthday coincidences. **Part 1:** There are $N \leq 365$ people in the room. What is the probability that at least two of them share the same birthday? Assume birthdays are uniformly distributed across 365 days and independent. **Part 2:** Now focus on yourself. Given $N$ people in the room (including you), what is the probability that at least one other person shares your birthday? A: **How to Think About It:** Both parts are easiest via the complement -- compute the probability of no match and subtract from 1. The twist is that Part 1 and Part 2 are measuring very different things. Part 1 is asking about any collision among $N$ people, which is a much higher-probability event because there are $\binom{N}{2}$ pairs that could match. Part 2 fixes your birthday and asks only whether the remaining $N-1$ people avoid it. If you see $N = 23$, the answer to Part 1 is already above 50%, but the answer to Part 2 is only around 6%. Mixing these up is the most common mistake. **Quick Estimate:** For Part 1 with $N = 23$: there are $\binom{23}{2} = 253$ pairs. Each pair matches with probability $1/365 \approx 0.0027$. Treating pairs as independent (rough), the expected number of matches is $253/365 \approx 0.69$. By Poisson approximation, $P(\text{at least one match}) \approx 1 - e^{-0.69} \approx 1 - 0.50 = 0.50$. So around $N = 23$ the crossover hits 50% -- that is the famous birthday paradox threshold. For Part 2 with $N = 23$: the other 22 people each independently avoid your birthday with probability $364/365$. So $P(\text{no match}) = (364/365)^{22} \approx e^{-22/365} \approx e^{-0.060} \approx 0.942$. Thus $P(\text{at least one match}) \approx 1 - 0.942 = 0.058$, only about 6%. **Approach:** Complement rule. Compute $P(\text{no collision})$ in closed form for each part. **Formal Solution:** **Part 1:** We want $P(\text{at least two people share a birthday})$. Work via the complement: compute $P(\text{all } N \text{ birthdays are distinct})$. Assign birthdays sequentially. Person 1 can have any of 365 days. Person 2 must avoid person 1: $364/365$ choices. Person $k$ must avoid the $k-1$ previous birthdays: $(365-k+1)/365$ choices. So: $$P(\text{all distinct}) = \frac{365}{365} \cdot \frac{364}{365} \cdot \frac{363}{365} \cdots \frac{365-N+1}{365} = \frac{365!}{(365-N)! \cdot 365^N}$$ Therefore: $$P(\text{at least one shared birthday}) = 1 - \frac{365!}{(365-N)! \cdot 365^N}$$ This can also be written using the falling factorial notation: $$= 1 - \frac{365^{\underline{N}}}{365^N}$$ where $365^{\underline{N}} = 365 \cdot 364 \cdots (365-N+1)$ is the falling factorial. **Part 2:** Now your birthday is fixed. You want to know if any of the other $N-1$ people share it. Each of them independently has your birthday with probability $1/365$, so each avoids it with probability $364/365$. $$P(\text{no one else has your birthday}) = \left(\frac{364}{365}\right)^{N-1}$$ $$P(\text{at least one person shares your birthday}) = 1 - \left(\frac{364}{365}\right)^{N-1}$$ **Answer:** - Part 1: $\displaystyle 1 - \frac{365!}{(365-N)!\cdot 365^N}$ - Part 2: $\displaystyle 1 - \left(\frac{364}{365}\right)^{N-1}$ At $N = 23$: Part 1 $\approx 50.7\%$, Part 2 $\approx 6.1\%$. To get Part 2 above 50% you need $N \geq 254$. --- The Other Ball in the Box [easy, Probability] --- https://quantvault.org/problem-519-the-other-ball-in-the-box.html Q: There are three boxes, each containing two balls: - One box has two blue balls (BB). - One box has one blue and one red ball (BR). - One box has two red balls (RR). You pick a box at random and draw one ball from it -- it comes out blue. What is the probability that the other ball still in that box is also blue? A: **How to Think About It:** Your first instinct might be: "I drew from one of two boxes that had any blue balls -- BB or BR -- so it's 50/50." That's wrong, and it's wrong in a way that comes up constantly in conditional probability puzzles. The issue is that BB was more likely to produce the blue ball you drew than BR was. You need to weight by how easy it was for each box to give you blue. **Quick Estimate:** There are 6 balls in total across all three boxes: 3 blue and 3 red. Each individual ball was equally likely to be the one you drew. Given that you drew a blue ball, you drew one of the 3 blue balls uniformly. Two of those 3 blue balls live in the BB box -- so there's a 2/3 chance the box you picked is BB, meaning the other ball is also blue. The answer is $\frac{2}{3}$, not $\frac{1}{2}$. **Approach:** Confirm with Bayes' theorem to make it airtight. **Formal Solution:** Label the boxes: BB (two blue), BR (one blue, one red), RR (two red). Each box is equally likely a priori: $P(\text{BB}) = P(\text{BR}) = P(\text{RR}) = \frac{1}{3}$. The likelihoods of drawing a blue ball from each box are: $$P(\text{blue} \mid \text{BB}) = 1, \quad P(\text{blue} \mid \text{BR}) = \frac{1}{2}, \quad P(\text{blue} \mid \text{RR}) = 0.$$ Marginal probability of drawing blue: $$P(\text{blue}) = 1 \cdot \frac{1}{3} + \frac{1}{2} \cdot \frac{1}{3} + 0 \cdot \frac{1}{3} = \frac{1}{3} + \frac{1}{6} = \frac{1}{2}.$$ Posterior probability the box is BB given we drew blue: $$P(\text{BB} \mid \text{blue}) = \frac{P(\text{blue} \mid \text{BB}) \cdot P(\text{BB})}{P(\text{blue})} = \frac{1 \cdot \frac{1}{3}}{\frac{1}{2}} = \frac{2}{3}.$$ If the box is BB, the remaining ball is blue with certainty. If the box is BR (posterior probability $\frac{1}{3}$), the remaining ball is red. So: $$P(\text{other ball is blue} \mid \text{drew blue}) = \frac{2}{3}.$$ **Answer:** $\dfrac{2}{3}$. --- Three-Way Duel With Rational Players [hard, Game Theory] --- https://quantvault.org/problem-522-three-way-duel-with-rational-players.html Q: Alice, Bob, and Chad are in a three-way duel. They take turns shooting in the fixed order Alice, Bob, Chad, repeating until only one person remains. Their guns have the following hit probabilities: - Alice: $1/3$ - Bob: $2/3$ - Chad: $1$ (never misses) If a shot misses the target, it hits no one. Each player plays optimally to maximize their own probability of survival. On each turn, a player may choose to shoot at either opponent -- or choose not to shoot at all. Under optimal play from all three players, what is the probability that Alice is the last one standing? A: **How to Think About It:** In a three-way duel, the strongest player is the biggest threat to everyone. Chad never misses, so both Alice and Bob want Chad dead first. But here's the subtle part: Alice, being the weakest shooter, actually benefits from doing nothing in the first round. Why? If she shoots at Chad and hits (probability $1/3$), she's now in a two-player duel against Bob, who shoots with $2/3$ accuracy -- a tough spot. If she shoots at Bob and hits, she faces Chad, who never misses -- even worse. Her best move is to deliberately miss (or not shoot) and let Bob and Chad fight it out. **Quick Estimate:** Bob will shoot at Chad (the bigger threat). He hits with probability $2/3$. If Bob hits, Chad dies and Alice faces Bob with Alice shooting first -- decent odds. If Bob misses (probability $1/3$), Chad shoots Bob dead (he never misses). Then Alice faces Chad. Rough estimate: $P(\text{Alice wins}) \approx (2/3) \times (\text{Alice vs Bob}) + (1/3) \times (\text{Alice vs Chad})$. Alice vs Chad: she must hit first shot ($1/3$). Alice vs Bob: maybe around $3/7$. So roughly $(2/3)(3/7) + (1/3)(1/3) = 6/21 + 7/63 \approx 0.40$. **Approach:** Analyze Alice's optimal first move, then reduce to two-player duels. **Formal Solution:** **Step 1: Alice's optimal first-round strategy.** Every player prefers facing a weaker opponent. Chad ($p = 1$) is the strongest, so: - Bob will shoot at Chad (eliminating the bigger threat). - Chad (if alive) will shoot at Bob (the bigger threat to Chad, since Bob is more accurate than Alice). - Alice should deliberately not shoot. Shooting at Chad risks killing him and leaving her against Bob. Shooting at Bob risks leaving her against Chad. Doing nothing lets Bob and Chad eliminate each other. **Step 2: What happens in the first round.** Alice passes. Bob shoots at Chad: - With probability $2/3$: Bob hits, Chad dies. Survivors: Alice and Bob, with Alice's turn next. - With probability $1/3$: Bob misses. Chad then shoots Bob dead (probability 1). Survivors: Alice and Chad, with Alice's turn next. **Step 3: Alice vs Bob (Alice shoots first).** This is a turn-based duel where Alice hits with probability $p = 1/3$ and Bob hits with $q = 2/3$, with Alice going first. Let $a$ be Alice's win probability. On Alice's turn: - She hits Bob with probability $1/3$ -- she wins. - She misses with probability $2/3$. Then Bob shoots: - He hits Alice with probability $2/3$ -- Bob wins. - He misses with probability $1/3$ -- back to the start. $$a = \frac{1}{3} + \frac{2}{3} \cdot \frac{1}{3} \cdot a$$ Solving: $a = \frac{1}{3} + \frac{2}{9}a$, so $\frac{7}{9}a = \frac{1}{3}$, giving $a = \frac{3}{7}$. **Step 4: Alice vs Chad (Alice shoots first).** Chad never misses, so if Alice doesn't kill Chad on her first shot, Chad kills her on his turn. Alice's win probability is simply $1/3$. **Step 5: Combine by total probability.** $$P(\text{Alice wins}) = \frac{2}{3} \cdot \frac{3}{7} + \frac{1}{3} \cdot \frac{1}{3} = \frac{6}{21} + \frac{1}{9} = \frac{18}{63} + \frac{7}{63} = \frac{25}{63}$$ **Answer:** Under optimal play, Alice's probability of being the last one standing is $\dfrac{25}{63} \approx 0.397$. --- Tic-Tac-Toe by Random Ball Placement [medium, Probability] --- https://quantvault.org/problem-523-tic-tac-toe-by-random-ball-placement.html Q: You have a $3 \times 3$ grid. You randomly drop three (distinguishable) balls into the grid, where each ball independently lands in one of the 9 squares with equal probability. The only restriction is that all three balls cannot land in the same square. What is the probability that the three balls form a tic-tac-toe -- that is, they occupy three squares in a row, column, or diagonal? A: **How to Think About It:** This is a counting problem with a small twist: we are sampling with replacement (balls can share squares) but excluding the degenerate case where all three pile into the same cell. The grid has exactly 8 winning lines (3 rows, 3 columns, 2 diagonals), and for each line the balls must land in all three distinct squares. The numerator is easy to count; the denominator is just the full sample space minus the excluded arrangements. **Quick Estimate:** There are $9^3 = 729$ unrestricted placements and only 9 degenerate ones, so the valid sample space is 720. Each winning line requires one ball per square, giving $3! = 6$ arrangements per line. With 8 lines that is $48$ winners. A quick ratio: $48/720 \approx 0.067$. So roughly a 1-in-15 chance -- pretty rare, which makes sense because three random balls have a lot of room to scatter. **Approach:** Straight combinatorial counting -- enumerate the sample space and favorable outcomes, then divide. **Formal Solution:** 1. **Sample space.** Each of the 3 distinguishable balls is placed independently and uniformly at random into one of 9 squares. The total number of unrestricted placements is $9^3 = 729$. We subtract the 9 outcomes where all three balls land in the same square: $$|\Omega| = 729 - 9 = 720$$ 2. **Winning lines.** A tic-tac-toe requires the three balls to occupy a complete row, column, or diagonal. The 8 winning lines are: - 3 rows - 3 columns - 2 diagonals 3. **Favorable outcomes per line.** For a given line of 3 squares, we need each ball in a different square of that line. The number of ways to assign 3 distinguishable balls to 3 distinct squares is $3! = 6$. 4. **No double-counting.** No two distinct winning lines share all 3 squares, so no arrangement is counted twice. 5. **Total favorable outcomes:** $$|W| = 8 \times 6 = 48$$ 6. **Probability:** $$P(\text{tic-tac-toe}) = \frac{48}{720} = \frac{1}{15}$$ **Answer:** The probability of tic-tac-toe is $\dfrac{1}{15} \approx 0.0667$. --- Trade-by-Trade Realized P&L Engine With FIFO Matching [hard, Coding] --- https://quantvault.org/problem-524-trade-by-trade-realized-p-l-engine-with-fifo-matching.html Q: You are building a real-time P&L engine for a trading desk. A stream of $m$ timestamped trades arrives, each represented as a pair $(p_j, q_j)$ where $p_j$ is the execution price and $q_j$ is the signed quantity ($q_j > 0$ for buys, $q_j < 0$ for sells). Your engine must process the stream in a single pass using $O(m)$ time and $O(1)$ extra space (beyond the inventory queue), and return three things: 1. **Realized P&L** -- computed using FIFO (first-in, first-out) inventory matching. When a trade offsets an existing position, match it against the oldest inventory lot first. 2. **Ending inventory and average cost** -- the remaining open position after all trades, along with its weighted-average cost basis. 3. **Maximum drawdown of cumulative realized P&L** -- the largest peak-to-trough decline in the running realized P&L over the trade sequence. Your implementation must handle the following edge cases: - **Partial fills**: a single trade may only partially offset the oldest lot, leaving a residual. - **Zero-quantity records**: trades with $q_j = 0$ (no-ops that should be skipped). - **Fees**: each trade may carry an optional fee that reduces realized P&L. - **NaN prices or quantities**: invalid records that should be skipped with a warning. **Constraints:** - $1 \le m \le 10^6$ - Prices and quantities are floating-point numbers. - Fees are non-negative floats (default 0). **Example:** Input trades: `[(100, +10), (102, +5), (105, -8), (103, -4), (101, +3)]` Using FIFO matching: - Sell 8 at 105: matched against the first lot (bought 10 at 100). Realized P&L on 8 units = $8 \times (105 - 100) = +40$. Remaining from lot 1: 2 units at 100. - Sell 4 at 103: matched against remaining 2 from lot 1 at 100, then 2 from lot 2 at 102. Realized P&L = $2 \times (103 - 100) + 2 \times (103 - 102) = 6 + 2 = +8$. - Final realized P&L: $+48$. - Ending inventory: 3 units from lot 2 at 102, plus 3 units from the new buy at 101. Total 6 long, average cost $(3 \times 102 + 3 \times 101)/6 = 101.5$. - Cumulative realized P&L series: $[0, 0, +40, +48, +48]$. Peak = 48, no drawdown occurs, so max drawdown = 0. A: **How to Think About It:** This is a bread-and-butter systems problem on any trading desk. Every desk has a P&L engine, and the FIFO matching logic is how most firms (and tax authorities) track cost basis. The core data structure is a deque (double-ended queue) of inventory lots. When a new trade comes in on the same side as the current position, you just append a new lot. When it comes in on the opposite side, you peel off the oldest lots first (FIFO), booking realized P&L on each matched portion. The max drawdown is a simple running-max tracker on the cumulative realized P&L -- no need for anything fancy. The $O(1)$ extra space claim needs a caveat: the inventory deque itself can grow up to $O(m)$ in the worst case (if every trade is on the same side). The problem means $O(1)$ space *beyond* the inventory, which is standard phrasing for this type of question. **Algorithm:** 1. Maintain a deque of lots, where each lot is `(price, remaining_qty)`. 2. For each incoming trade $(p_j, q_j)$: - Skip if $q_j = 0$ or if price/quantity is NaN. - If the trade is on the *same side* as the current inventory (or inventory is empty), push a new lot onto the back of the deque. - If the trade is on the *opposite side*, match against the front of the deque: - Take `match_qty = min(|q_j|, front.remaining_qty)`. - Book realized P&L: `match_qty * (p_j - front.price)` if closing a long, or `match_qty * (front.price - p_j)` if closing a short. - Subtract any fee pro-rated to the matched portion. - Reduce `front.remaining_qty` by `match_qty`. If it hits zero, pop the front. - Reduce the remaining trade quantity by `match_qty`. If there is still quantity left, continue matching the next lot. - If you exhaust the entire inventory and still have leftover trade quantity, the position has flipped sides -- push the residual as a new lot. 3. Track cumulative realized P&L. After each trade, update `peak = max(peak, cumulative_rpnl)` and `max_drawdown = max(max_drawdown, peak - cumulative_rpnl)`. 4. At the end, compute average cost from the remaining lots. **Code:** ```python from collections import deque from math import isnan from typing import List, Tuple, Optional def pnl_engine( trades: List[Tuple[float, float, float]], # (price, signed_qty, fee) ) -> dict: """ Single-pass FIFO P&L engine. Each trade is (price, signed_quantity, fee). fee defaults to 0. Returns realized_pnl, ending_position, avg_cost, max_drawdown. """ inventory: deque = deque() # each element: [price, remaining_qty (signed)] realized_pnl = 0.0 peak_pnl = 0.0 max_dd = 0.0 def same_side(a: float, b: float) -> bool: return (a > 0 and b > 0) or (a 2 else 0.0 # -- skip invalid records -- if isnan(price) or isnan(qty): continue if qty == 0.0: continue if isnan(fee): fee = 0.0 remaining = qty trade_rpnl = 0.0 # -- match against opposite-side inventory -- while remaining != 0.0 and inventory: front = inventory[0] if same_side(front[1], remaining): break # same side, no offset match_qty = min(abs(remaining), abs(front[1])) # P&L: if we were long (front[1]>0) and selling, gain = sell - cost # if we were short (front[1] 0: trade_rpnl += match_qty * (price - front[0]) else: trade_rpnl += match_qty * (front[0] - price) # reduce inventory lot if abs(front[1]) 0: front[1] -= match_qty else: front[1] += match_qty # reduce remaining trade qty if remaining > 0: remaining -= match_qty else: remaining += match_qty # -- any residual becomes a new inventory lot -- if abs(remaining) > 1e-12: inventory.append([price, remaining]) # -- apply fee -- trade_rpnl -= fee # -- update running P&L and drawdown -- realized_pnl += trade_rpnl if realized_pnl > peak_pnl: peak_pnl = realized_pnl dd = peak_pnl - realized_pnl if dd > max_dd: max_dd = dd # -- compute ending position and average cost -- total_pos = 0.0 cost_sum = 0.0 for lot in inventory: total_pos += lot[1] cost_sum += lot[0] * abs(lot[1]) avg_cost = cost_sum / abs(total_pos) if abs(total_pos) > 1e-12 else 0.0 return { "realized_pnl": round(realized_pnl, 6), "ending_position": round(total_pos, 6), "average_cost": round(avg_cost, 6), "max_drawdown": round(max_dd, 6), } ``` **Complexity:** - **Time**: $O(m)$ amortized. Each unit of quantity enters the deque at most once and leaves at most once, so the total work across all matching operations is bounded by the total quantity traded, which is $O(m)$ in terms of the number of trades. - **Space**: $O(k)$ for the inventory deque where $k$ is the number of open lots, plus $O(1)$ for the running P&L and drawdown trackers. In the worst case $k = m$ (all trades on the same side), but this is inherent to the problem -- you must store the inventory. **Answer:** The key idea is to maintain a FIFO deque of inventory lots, matching incoming offsetting trades against the oldest lots first. Realized P&L, ending position, and max drawdown are all tracked in a single pass. Time complexity is $O(m)$ amortized and extra space beyond the inventory is $O(1)$. --- Two Marks in the Same Region [easy, Probability] --- https://quantvault.org/problem-527-two-marks-in-the-same-region.html Q: A stick of length 1 is divided into $n$ equal regions, each of length $1/n$. Two marks are placed independently and uniformly at random along the stick. Find the probability that both marks fall in the same region when $n = 5$. A: **How to Think About It:** The first mark lands somewhere. Since all regions have equal length, the region it lands in does not matter -- by symmetry, it is equivalent to asking: given the first mark is in some region, what is the probability the second mark also falls there? The second mark is uniform on $[0, 1]$, and each region has length $1/n$, so this is just $1/n$. **Quick Estimate:** With $n = 5$ equal regions of length $1/5$ each, the second mark has a $1/5 = 0.2$ chance of landing in the same region as the first. Sanity check: if $n = 1$ (one region covering the whole stick), probability is 1. If $n = 100$, probability is $1/100$. Both make sense. **Approach:** Use symmetry to reduce to a single unconditional probability. **Formal Solution:** Let $X$ and $Y$ be the positions of the two marks, independently uniform on $[0, 1]$. The stick is divided into $n = 5$ regions: $[0, 1/5)$, $[1/5, 2/5)$, $[2/5, 3/5)$, $[3/5, 4/5)$, $[4/5, 1]$. **Method 1 (symmetry):** The first mark $X$ falls in exactly one region. Since all regions have equal length $1/n$, by symmetry we can condition on $X$ being in, say, region $[0, 1/5)$. The probability that $Y$ also falls in this region is: $$P(Y \in [0, 1/5)) = \frac{1/5}{1} = \frac{1}{5}$$ By symmetry of the regions, this probability is the same regardless of which region $X$ landed in. Therefore: $$P(X, Y \text{ in same region}) = \frac{1}{5}$$ **Method 2 (direct computation):** Each mark independently falls in each of the 5 regions with probability $1/5$. The probability both fall in region $k$ is $(1/5)^2 = 1/25$. Summing over all 5 regions: $$P(\text{same region}) = 5 \times \frac{1}{25} = \frac{5}{25} = \frac{1}{5}$$ **General formula:** For $n$ equal regions, $P(\text{same region}) = 1/n$. **Answer:** $P = \dfrac{1}{5} = 0.2$. --- Uniform Point in the Unit Disk Without Rejection [medium, Random Variables] --- https://quantvault.org/problem-531-uniform-point-in-the-unit-disk-without-rejection.html Q: You have access to an i.i.d. source of uniform random variables $U_1, U_2 \sim \text{Unif}(0,1)$. Construct a function $(X, Y) = f(U_1, U_2)$ such that $(X, Y)$ is uniformly distributed over the unit disk $\{(x, y) \in \mathbb{R}^2 : x^2 + y^2 \le 1\}$, without using rejection sampling. Prove that your construction produces a uniform distribution by deriving the joint density (or using a measure-preserving argument). Explicitly state any coordinate transformations you use. A: **How to Think About It:** The naive approach -- generate $(U_1, U_2)$ uniformly on the unit square and reject points outside the disk -- wastes about $1 - \pi/4 \approx 21.5\%$ of samples. The question asks us to avoid this. The key idea is to work in polar coordinates $(R, \Theta)$ and figure out the right marginal distributions so that the resulting $(X, Y)$ is uniform on the disk. A uniform point on the disk has density $f(x,y) = 1/\pi$ for $x^2 + y^2 \le 1$. In polar coordinates, the area element is $r \, dr \, d\theta$, so the joint density of $(R, \Theta)$ is $f(r, \theta) = r/\pi$ for $r \in [0,1]$, $\theta \in [0, 2\pi)$. Separating: $\Theta \sim \text{Unif}(0, 2\pi)$ and $R$ has density $f_R(r) = 2r$ for $r \in [0,1]$. The CDF of $R$ is $F_R(r) = r^2$, so $R = \sqrt{U}$ by the inverse CDF method. **Approach:** Use inverse CDF sampling for the radius and a linear rescaling for the angle, both via uniform inputs. **Formal Solution:** 1. **Set up polar coordinates.** We want $(X, Y)$ uniform on the unit disk. In polar coordinates, $X = R\cos\Theta$, $Y = R\sin\Theta$. The uniform density on the disk is: $$f_{X,Y}(x,y) = \frac{1}{\pi}, \quad x^2 + y^2 \le 1$$ 2. **Convert to polar.** The Jacobian of the transformation $(r, \theta) \mapsto (r\cos\theta, r\sin\theta)$ is $r$. So the joint density in polar coordinates is: $$f_{R,\Theta}(r,\theta) = \frac{1}{\pi} \cdot r = \frac{r}{\pi}, \quad r \in [0,1], \; \theta \in [0, 2\pi)$$ 3. **Factor the joint density.** Since $f_{R,\Theta}(r,\theta) = (2r) \cdot \frac{1}{2\pi}$, we see that $R$ and $\Theta$ are independent with: - $\Theta \sim \text{Unif}(0, 2\pi)$, so set $\Theta = 2\pi U_1$ - $R$ has PDF $f_R(r) = 2r$ on $[0,1]$, giving CDF $F_R(r) = r^2$ 4. **Inverse CDF for $R$.** Setting $F_R(R) = U_2$ gives $R^2 = U_2$, so $R = \sqrt{U_2}$. 5. **Final construction:** $$X = \sqrt{U_2}\cos(2\pi U_1), \quad Y = \sqrt{U_2}\sin(2\pi U_1)$$ 6. **Verification.** The map $(U_1, U_2) \mapsto (\Theta, R^2) = (2\pi U_1, U_2)$ is measure-preserving (linear rescaling of Lebesgue measure). Since $R = \sqrt{U_2}$ and $\Theta = 2\pi U_1$ are the correct marginals and they are independent (because $U_1, U_2$ are independent), the joint density is: $$f_{R,\Theta}(r,\theta) = f_R(r) \cdot f_\Theta(\theta) = 2r \cdot \frac{1}{2\pi} = \frac{r}{\pi}$$ which corresponds to uniform density $1/\pi$ on the disk after the polar-to-Cartesian Jacobian factor. **Answer:** Set $X = \sqrt{U_2}\cos(2\pi U_1)$ and $Y = \sqrt{U_2}\sin(2\pi U_1)$. The critical insight is that the radius needs CDF $F_R(r) = r^2$ (not uniform), because the area element in polar coordinates includes a factor of $r$. Taking $R = \sqrt{U}$ corrects for this. --- Uniform Sum Distribution with Non-Fair Dice [hard, Probability] --- https://quantvault.org/problem-532-uniform-sum-distribution-with-non-fair-dice.html Q: You have two 6-sided dice, each with sides labeled 1 through 6. The dice need not be fair -- each die can have any probability distribution over its six faces, as long as the probabilities are non-negative and sum to 1. Can you choose two such dice, both non-fair, so that the sum of the two dice is uniformly distributed over $\{2, 3, \ldots, 12\}$? That is, can we have $P(\text{sum} = k) = \frac{1}{11}$ for every $k \in \{2, \ldots, 12\}$? Prove your answer. A: **How to Think About It:** Before writing anything, ask: what constraints does "uniform sum" actually impose? The sum equals 2 only if both dice show 1, and the sum equals 12 only if both dice show 6. So those two extreme events each have probability exactly $\frac{1}{11}$. That pins down $p_1 q_1$ and $p_6 q_6$. Now ask: what about the sum equaling 7? That can happen many ways -- it is the most flexible outcome. But here is the trap: two of those ways, rolling (1,6) and (6,1), already have their individual pieces constrained by the above. You can write an inequality between them and the uniform probability, and that inequality forces a contradiction. The punchline is an AM-GM style argument: for any positive real $x$, we have $x + 1/x \geq 2$, and the uniform sum requirement needs $x + 1/x \leq 1$, which is impossible. **Approach:** Assume for contradiction that such distributions exist with $P(\text{sum} = k) = \frac{1}{11}$ for all $k \in \{2, \ldots, 12\}$. We derive the contradiction by combining the constraints from the extreme sums with the constraint from the sum equal to 7. **Formal Solution:** Let $p_i = P(\text{die 1 shows } i)$ and $q_j = P(\text{die 2 shows } j)$ for $i, j \in \{1, \ldots, 6\}$. Assume the sum is uniform, so $P(\text{sum} = k) = \frac{1}{11}$ for each $k \in \{2, \ldots, 12\}$. **Step 1: Pin down the extreme products.** The sum equals 2 if and only if die 1 shows 1 and die 2 shows 1 (the only way to make 2 with faces in $\{1, \ldots, 6\}$). Similarly, the sum equals 12 if and only if both dice show 6. So: $$p_1 q_1 = \frac{1}{11} \qquad \text{and} \qquad p_6 q_6 = \frac{1}{11}$$ In particular, all four quantities $p_1, q_1, p_6, q_6$ are strictly positive. **Step 2: Bound the sum-7 probability.** The event $\{\text{sum} = 7\}$ can be decomposed as the union of mutually exclusive events $\{(i, j) : i + j = 7\}$. The pairs $(1,6)$ and $(6,1)$ are among them, so: $$P(\text{sum} = 7) \geq p_1 q_6 + p_6 q_1$$ By the uniform assumption, $P(\text{sum} = 7) = \frac{1}{11}$, giving: $$p_1 q_6 + p_6 q_1 \leq \frac{1}{11}$$ **Step 3: Substitute using Step 1.** From Step 1: $q_1 = \frac{1}{11 p_1}$ and $q_6 = \frac{1}{11 p_6}$. Substitute: $$p_1 \cdot \frac{1}{11 p_6} + p_6 \cdot \frac{1}{11 p_1} \leq \frac{1}{11}$$ Multiply both sides by 11: $$\frac{p_1}{p_6} + \frac{p_6}{p_1} \leq 1$$ **Step 4: Apply AM-GM to get a contradiction.** Let $x = \frac{p_1}{p_6} > 0$. The inequality becomes $x + \frac{1}{x} \leq 1$. But by AM-GM (or because $(\sqrt{x} - 1/\sqrt{x})^2 \geq 0$ implies $x + 1/x \geq 2$), we have $x + \frac{1}{x} \geq 2$ for all $x > 0$. This contradicts $x + \frac{1}{x} \leq 1$. **Answer:** No such dice exist. The assumption of a uniform sum forces $\frac{p_1}{p_6} + \frac{p_6}{p_1} \leq 1$, which contradicts $x + \frac{1}{x} \geq 2$ for all positive $x$. --- Unreliable Witness and Bayes' Theorem [easy, Probability] --- https://quantvault.org/problem-534-unreliable-witness-and-bayes-theorem.html Q: You have a pet cat that sometimes sneaks out of the house while you're at work. Every day, you call your neighbor to ask whether the cat left. Your neighbor always gives a definite yes or no -- but sometimes they lie (saying the cat left when it didn't, or saying it stayed when it actually left). The probability that your cat actually leaves on any given day is $P(C) = 0.001$. The probability that your neighbor lies is $P(L) = 0.1$, independent of whether the cat actually left. Your neighbor tells you the cat left. What is the probability that the cat actually left the house? A: **How to Think About It:** This is a classic base-rate problem -- the textbook example of why naive intuition about conditional probability fails. Your neighbor says the cat left. Most people's gut reaction is "well, the neighbor is right 90% of the time, so maybe 80-90% chance the cat left." That instinct is completely wrong here, and recognizing why is the whole point of the problem. The base rate of the cat leaving is tiny ($0.1\%$), so even a fairly reliable witness generates far more false positives than true positives. **Quick Estimate:** Think about 10,000 days. The cat leaves on about $10$ of them. The neighbor lies on about $1{,}000$ days (10% of 10,000). On the 10 days the cat actually leaves, the neighbor correctly reports it about $9$ times. On the $9{,}990$ days the cat stays home, the neighbor falsely claims it left about $999$ times. So when the neighbor says "yes," there are roughly $9$ true alarms vs. $999$ false alarms. That gives $P(\text{cat left} \mid \text{neighbor says yes}) \approx 9 / (9 + 999) \approx 0.009$, or just under $1\%$. **Approach:** Apply Bayes' theorem directly with the law of total probability to get the exact answer. **Formal Solution:** Define the events: - $C$ = cat left the house - $N$ = neighbor says the cat left We need $P(C \mid N)$. By Bayes' theorem: $$P(C \mid N) = \frac{P(N \mid C) \cdot P(C)}{P(N)}$$ The likelihood $P(N \mid C)$ is the probability the neighbor says yes when the cat actually left -- i.e., the neighbor tells the truth: $$P(N \mid C) = 1 - P(L) = 0.9$$ The false-positive rate $P(N \mid \neg C)$ is the probability the neighbor says yes when the cat stayed -- i.e., the neighbor lies: $$P(N \mid \neg C) = P(L) = 0.1$$ By the law of total probability: $$P(N) = P(N \mid C) \cdot P(C) + P(N \mid \neg C) \cdot P(\neg C) = 0.9 \times 0.001 + 0.1 \times 0.999 = 0.0009 + 0.0999 = 0.1008$$ Plugging in: $$P(C \mid N) = \frac{0.9 \times 0.001}{0.1008} = \frac{0.0009}{0.1008} \approx 0.00893$$ **Answer:** $P(C \mid N) \approx 0.0089$, or about $0.89\%$. Despite the neighbor's report, there is less than a $1\%$ chance your cat actually left. --- Add Two Numbers (Linked List) [easy, Coding] --- https://quantvault.org/problem-546-add-two-numbers-linked-list.html Q: You are given two non-empty lists representing two non-negative integers. The digits are stored in **reverse order** (least-significant digit first), and each element is a single digit (0-9). Add the two numbers and return the sum as a digit list, also in reverse order. Implement the function: ```python def add_two_numbers(l1, l2): ... ``` where `l1` and `l2` are lists of ints, and the return value is a list of ints (least-significant-first). You may assume the two numbers do not contain any leading zeros, except the number 0 itself. **Constraints:** - The number of digits in each list is in the range $[1, 100]$. - $0 \leq \text{digit} \leq 9$ - The lists represent numbers without leading zeros. **Example** `add_two_numbers([2, 4, 3], [5, 6, 4]) -> [7, 0, 8]` The lists represent $342$ and $465$ (read least-significant-first), and $342 + 465 = 807$, whose digits in reverse order are $[7, 0, 8]$. A: **How to Think About It:** This is elementary-school addition, digit by digit, starting from the ones place -- which is exactly how the lists are ordered. You walk both lists in lockstep, add corresponding digits plus any carry from the previous step, and append one digit of the result each step. The only subtlety is handling lists of different lengths and a final carry that extends the result by one digit. **Algorithm:** Maintain a `carry` variable initialized to 0. At each step, sum the current digits from both lists (treating exhausted lists as contributing 0) plus the carry. The new digit is the sum modulo 10, the new carry is the sum divided by 10. Continue until both lists are exhausted **and** the carry is 0. The function takes two digit lists and returns a digit list (both least-significant-first) -- it does not use linked-list nodes. **Code:** ```python def add_two_numbers(l1, l2): # l1, l2 are non-empty lists of single digits (0-9) representing # non-negative integers stored in REVERSE order (least significant first). # Return their sum as a digit list, also least-significant-first. result = [] carry = 0 i = 0 n = max(len(l1), len(l2)) while i < n or carry: d1 = l1[i] if i < len(l1) else 0 d2 = l2[i] if i < len(l2) else 0 total = d1 + d2 + carry result.append(total % 10) carry = total // 10 i += 1 return result ``` **Complexity:** Time $O(\max(m, n))$ where $m$ and $n$ are the lengths of the two lists. Space $O(\max(m, n))$ for the result list. **Optimization notes:** - **Reduce branching:** Reading both values with a default of 0 when an index is out of range (as done here) gives a uniform loop body and reduces branch misprediction versus separate length checks. - **Stack vs. heap allocation:** In a lower-level language, a pre-allocated array or arena allocator instead of per-node allocation cuts overhead significantly. **Answer:** Walk both lists simultaneously with carry propagation, appending `total % 10` and carrying `total // 10`. Time $O(\max(m, n))$, space $O(\max(m, n))$. --- Bayesian Posterior with Two Binary Signals [easy, Probability] --- https://quantvault.org/problem-552-bayesian-posterior-with-two-binary-signals.html Q: A binary event $E \in \{0, 1\}$ has prior $P(E = 1) = 0.40$. You receive two independent analyst signals $S_1, S_2 \in \{0, 1\}$, each with the same accuracy: $$P(S_i = 1 \mid E = 1) = 0.70, \quad P(S_i = 1 \mid E = 0) = 0.30$$ The signals are conditionally independent given $E$. You observe $(S_1, S_2) = (1, 0)$. Compute the posterior probability $P(E = 1 \mid S_1 = 1, S_2 = 0)$. A: **How to Think About It:** You have a prior belief that the event happens with probability 0.40, and you get two analyst signals with 70% accuracy. One signal says "yes" ($S_1 = 1$) and one says "no" ($S_2 = 0$). Since the signals disagree, they partially cancel out, and the posterior should not move dramatically from the prior. But they are symmetric in accuracy, so the "yes" signal and "no" signal have equal information content -- intuitively, the posterior should stay near the prior. The slight asymmetry comes from the prior itself. **Quick Estimate:** With symmetric signal accuracy (70/30 split), one "yes" and one "no" should roughly cancel. The likelihood ratio for $(1, 0)$ under $E=1$ vs. $E=0$ is $\frac{0.7 \times 0.3}{0.3 \times 0.7} = 1$. So the posterior should equal the prior! Let's verify. **Approach:** Direct application of Bayes' theorem with conditionally independent signals. **Formal Solution:** By Bayes' theorem: $$P(E = 1 \mid S_1 = 1, S_2 = 0) = \frac{P(S_1 = 1, S_2 = 0 \mid E = 1) \cdot P(E = 1)}{P(S_1 = 1, S_2 = 0)}$$ **Numerator** (likelihood $\times$ prior for $E = 1$): By conditional independence: $$P(S_1 = 1, S_2 = 0 \mid E = 1) = P(S_1 = 1 \mid E = 1) \cdot P(S_2 = 0 \mid E = 1) = 0.70 \times 0.30 = 0.21$$ $$\text{Numerator} = 0.21 \times 0.40 = 0.084$$ **Denominator** (total probability of observed signals): $$P(S_1 = 1, S_2 = 0 \mid E = 0) = P(S_1 = 1 \mid E = 0) \cdot P(S_2 = 0 \mid E = 0) = 0.30 \times 0.70 = 0.21$$ $$P(S_1 = 1, S_2 = 0) = 0.21 \times 0.40 + 0.21 \times 0.60 = 0.21 \times 1.00 = 0.21$$ **Posterior:** $$P(E = 1 \mid S_1 = 1, S_2 = 0) = \frac{0.084}{0.21} = 0.40$$ **Answer:** $P(E = 1 \mid S_1 = 1, S_2 = 0) = 0.40$ The posterior equals the prior. This is not a coincidence -- when one signal agrees and one disagrees, and both signals have symmetric accuracy ($P(S_i = 1 \mid E = 1) = P(S_i = 0 \mid E = 0) = 0.70$), the likelihood ratio is exactly 1, so the data provides no net information. **Verification via likelihood ratio:** The likelihood ratio for each signal is $\Lambda_i = P(S_i \mid E=1) / P(S_i \mid E=0)$. For $S_1 = 1$: $\Lambda_1 = 0.7/0.3 = 7/3$. For $S_2 = 0$: $\Lambda_2 = 0.3/0.7 = 3/7$. Combined: $\Lambda = \Lambda_1 \cdot \Lambda_2 = (7/3)(3/7) = 1$. A likelihood ratio of 1 means no update -- the posterior odds equal the prior odds. --- Chebyshev Bound for Bivariate Normal Difference [medium, Probability] --- https://quantvault.org/problem-560-chebyshev-bound-for-bivariate-normal-difference.html Q: Let $(X, Y) \sim \text{BVN}(0, 0, \sigma^2, \sigma^2, \rho)$ -- a bivariate normal with zero means, equal marginal variances $\sigma^2$, and correlation $\rho$. Using Chebyshev's Inequality, find an upper bound on $P(|X - Y| \geq \sigma)$ when $\sigma^2 = 4$ and $\rho = 3/4$. A: **How to Think About It:** Chebyshev's Inequality says that for any random variable $Z$ with finite mean and variance, $P(|Z - \mu_Z| \geq k) \leq \text{Var}(Z)/k^2$. To apply it to $|X - Y| \geq \sigma$, we need two things: (1) confirm that $X - Y$ is centered (mean zero), and (2) compute $\text{Var}(X - Y)$. The correlation $\rho$ enters through the covariance term in the variance of a difference, which is the main calculation here. **Quick Estimate:** At $\rho = 3/4$, the two variables are highly correlated -- $X - Y$ has much less variance than if $X$ and $Y$ were independent. With independent normals, $\text{Var}(X - Y) = 2\sigma^2$, giving a Chebyshev bound of 2. But with $\rho = 3/4$, the variance shrinks to $2\sigma^2(1 - 3/4) = \sigma^2/2$, so the bound should be $1/2$. That is below 1, so it is an informative (non-trivial) bound. **Formal Solution:** First, check centering: $E[X - Y] = E[X] - E[Y] = 0 - 0 = 0$, so $X - Y$ has mean zero. Chebyshev's Inequality applies directly: $$P(|X - Y| \geq \sigma) \leq \frac{\text{Var}(X - Y)}{\sigma^2}$$ Compute $\text{Var}(X - Y)$ using the variance of a linear combination: $$\text{Var}(X - Y) = \text{Var}(X) + \text{Var}(Y) - 2\,\text{Cov}(X, Y)$$ Since $\text{Cov}(X, Y) = \rho \sigma^2$: $$\text{Var}(X - Y) = \sigma^2 + \sigma^2 - 2\rho\sigma^2 = 2\sigma^2(1 - \rho)$$ Substituting into Chebyshev: $$P(|X - Y| \geq \sigma) \leq \frac{2\sigma^2(1 - \rho)}{\sigma^2} = 2(1 - \rho)$$ With $\rho = 3/4$: $$P(|X - Y| \geq \sigma) \leq 2\left(1 - \frac{3}{4}\right) = 2 \times \frac{1}{4} = \frac{1}{2}$$ **Answer:** $P(|X - Y| \geq \sigma) \leq \dfrac{1}{2}$. The general formula is $2(1 - \rho)$, which decreases toward 0 as $\rho \to 1$ (perfectly correlated variables have $X = Y$ almost surely, so their difference is zero). --- Counting Friend Circles [medium, Coding] --- https://quantvault.org/problem-569-counting-friend-circles.html Q: You are given an $n \times n$ adjacency matrix $M$ where $M[i][j] = 1$ means person $i$ and person $j$ are friends. Friendship is symmetric ($M[i][j] = M[j][i]$) and reflexive ($M[i][i] = 1$). A "friend circle" is a group of people who are all connected directly or transitively -- if A is friends with B, and B is friends with C, then A, B, and C are all in the same circle even if A and C are not directly friends. Implement: ```python def find_circle_num(M): ... ``` `M` is an $n \times n$ symmetric 0/1 matrix. **Return** the total number of friend circles (connected components) as an integer. **Constraints:** - $1 \le n \le 1000$ - $M[i][j] \in \{0, 1\}$ - $M[i][j] = M[j][i]$ for all $i, j$ - $M[i][i] = 1$ for all $i$ **Example 1:** Input: ``` M = [[1, 1, 0], [1, 1, 0], [0, 0, 1]] ``` Output: `2` Explanation: Persons 0 and 1 are friends (same circle). Person 2 is alone. Two circles total. **Example 2:** Input: ``` M = [[1, 1, 0], [1, 1, 1], [0, 1, 1]] ``` Output: `1` Explanation: Person 0 is friends with 1, and 1 is friends with 2, so all three are transitively connected. One circle. **Example** `find_circle_num([[1, 1, 0], [1, 1, 1], [0, 1, 1]]) -> 1` All three people are transitively connected through person 1, forming a single friend circle. A: **How to Think About It:** This is the classic "number of connected components" problem, dressed up with a social-network story. The adjacency matrix is a graph where each person is a node and each friendship is an undirected edge. Friend circles are exactly the connected components. Two standard ways to count them: (1) run DFS/BFS from each unvisited node, incrementing a counter each time; or (2) use a Union-Find (disjoint set) structure to merge connected nodes and count the remaining distinct roots. **Contract:** The entry function is `find_circle_num(M)`. It takes the $n \times n$ symmetric 0/1 matrix and must **return** the integer count of connected components. (Note: it must be named exactly `find_circle_num` -- a helper with a different name will not be called by the grader.) **Algorithm (Union-Find):** Initialize each person as their own parent. For each pair $(i, j)$ with $i < j$ and $M[i][j] = 1$, union their sets. At the end, count the number of distinct roots. **Solution (Union-Find, the entry function):** ```python def find_circle_num(M): # M is an n x n symmetric 0/1 adjacency matrix (M[i][i] == 1). # Return the number of connected components (friend circles). n = len(M) parent = list(range(n)) def find(x): while parent[x] != x: parent[x] = parent[parent[x]] x = parent[x] return x def union(a, b): ra, rb = find(a), find(b) if ra != rb: parent[ra] = rb for i in range(n): for j in range(i + 1, n): if M[i][j] == 1: union(i, j) return len({find(i) for i in range(n)}) ``` **Alternative (DFS):** ```python def find_circle_num_dfs(M): n = len(M) visited = [False] * n count = 0 def dfs(i): visited[i] = True for j in range(n): if M[i][j] == 1 and not visited[j]: dfs(j) for i in range(n): if not visited[i]: dfs(i) count += 1 return count ``` **Complexity:** - **DFS/BFS:** Time $O(n^2)$ -- every matrix entry is examined once across all traversals. Space $O(n)$ for the visited array (plus $O(n)$ recursion stack in the worst case). - **Union-Find:** Time $O(n^2 \cdot \alpha(n))$, effectively $O(n^2)$ since the inverse Ackermann $\alpha(n) \le 5$ for any practical $n$. Space $O(n)$ for the parent array. Both are $O(n^2)$ overall, which is optimal since you must read the entire matrix. DFS is simpler; Union-Find shines when edges arrive incrementally. --- Dice Payoff: Product of Two vs. Square of One [easy, Expectation] --- https://quantvault.org/problem-572-dice-payoff-product-of-two-vs-square-of-one.html Q: You can play one of two games: - **Game 1:** Roll two fair six-sided dice. Your payoff is the product of the two outcomes. - **Game 2:** Roll one fair six-sided die. Your payoff is the square of the outcome. (a) Without any calculation, argue intuitively which game has the higher expected payoff. (b) Verify your answer with explicit calculations. A: **How to Think About It:** Both games square a die roll in some sense, but Game 1 multiplies two independent rolls while Game 2 squares a single roll. The key question: is $E[D_1 \cdot D_2]$ greater or less than $E[D^2]$? Since $D_1$ and $D_2$ are independent and identically distributed, $E[D_1 D_2] = E[D_1] E[D_2] = (E[D])^2$. And $E[D^2] \geq (E[D])^2$ by Jensen's inequality (squaring is convex). So Game 2 wins. **Quick Estimate:** $E[D] = 3.5$. Game 1 pays approximately $3.5^2 = 12.25$. Game 2 will pay something above that -- the difference is $\text{Var}(D)$, which we expect to be around 2-3. **(a) Intuitive argument:** Game 1 produces the product of two independent dice. By independence: $E[D_1 D_2] = E[D_1] \cdot E[D_2] = (E[D])^2$. Game 2 squares a single die: $E[D^2]$. By Jensen's inequality, since $f(x) = x^2$ is convex: $E[D^2] \geq (E[D])^2$, with equality only if $D$ is constant. Since a die is not constant, $E[D^2] > (E[D])^2 = E[D_1 D_2]$. Game 2 has the higher expected payoff. **(b) Calculation:** For a fair six-sided die, $D$ is uniform on $\{1, 2, 3, 4, 5, 6\}$. $$E[D] = \frac{1+2+3+4+5+6}{6} = \frac{21}{6} = 3.5$$ $$E[D^2] = \frac{1^2+2^2+3^2+4^2+5^2+6^2}{6} = \frac{1+4+9+16+25+36}{6} = \frac{91}{6} \approx 15.17$$ $$E[D_1 D_2] = (E[D])^2 = 3.5^2 = 12.25$$ $$\text{Difference} = E[D^2] - (E[D])^2 = \frac{91}{6} - \frac{49}{4} = \frac{182}{12} - \frac{147}{12} = \frac{35}{12} \approx 2.92$$ This difference is exactly $\text{Var}(D)$, confirming the Jensen argument. **Answer:** Game 2 has the higher expected payoff: $91/6 \approx 15.17$ vs. $12.25$. The gap is exactly $\text{Var}(D) = 35/12 \approx 2.92$. --- Expected Chord Length Through a Disk (Random Radius Method) [medium, Expectation] --- https://quantvault.org/problem-577-expected-chord-length-through-a-disk-random-radius-method.html Q: A chord is drawn through a unit disk as follows: pick a point uniformly at random inside the disk, then draw the chord that passes through that point and is perpendicular to the radius at that point. What is the expected length of the chord? Note: this is one of the three classical sampling methods in Bertrand's paradox -- each method gives a different answer. Be explicit about which method you are using. A: **How to Think About It:** A chord perpendicular to the radius at distance $d$ from the center has length $2\sqrt{1-d^2}$ (Pythagoras on the right triangle: center, foot of the perpendicular, endpoint). So the whole problem reduces to *what is the distribution of $d$* for a uniform point in the disk. The heuristic that makes this trivial in your head is a **change of variable to the quantity that is actually uniform**: for a uniform point, $d^2$ is Uniform$(0,1)$ (the fraction of area inside radius $d$ is $d^2$). Naively averaging over $d$ tempts you to plug in a 'typical' $d$ and stop — a Jensen trap, since $2\sqrt{1-d^2}$ is concave, so $E[\text{length}] < \text{length}(E[d])$. **Quick Estimate:** Let $u = 1 - d^2$. Since $d^2$ is Uniform$(0,1)$, so is $u$, and the chord length is exactly $2\sqrt{u}$. Now the estimate is a one-liner mental integral: the average of $\sqrt{u}$ over $u \in (0,1)$ is $\int_0^1 u^{1/2}\,du = \tfrac{2}{3}$ (each intermediate: power rule bumps the exponent to $3/2$ and divides, $\tfrac{1}{3/2} = \tfrac23$). Hence $E[\text{length}] \approx 2 \cdot \tfrac23 = \tfrac43 \approx 1.33$. Sanity bracket: chords range from $2$ (center) down to $0$ (edge), and the area density $2d$ pushes the point toward the rim, so the mean chord should sit well below $1$ in $d$ but the length still averages above $1$ — $1.33$ fits, and the Jensen bound $2\sqrt{1-E[d^2]} = 2\sqrt{1/2} \approx 1.41$ correctly sits *above* it. **Approach:** Find the PDF of $d$, then compute $E[2\sqrt{1-d^2}]$ — or use the $u = 1-d^2$ shortcut directly. **Formal Solution:** **(a) Distribution of $d$.** For a uniform point in the unit disk, $P(D \le d) = \dfrac{\pi d^2}{\pi} = d^2$ on $[0,1]$, so the PDF is $$f_D(d) = 2d, \qquad 0 \le d \le 1.$$ The factor $2d$ (the circumference-of-ring Jacobian) is why the point favors large $d$. **(b) Chord length.** From the right triangle, $$\ell(d) = 2\sqrt{1 - d^2}.$$ **(c) Expected length.** $$E[\ell] = \int_0^1 2\sqrt{1-d^2}\cdot 2d\,dd = 4\int_0^1 d\sqrt{1-d^2}\,dd.$$ Substitute $u = 1 - d^2,\ du = -2d\,dd$: $$E[\ell] = 4\int_1^0 \sqrt{u}\left(-\tfrac{du}{2}\right) = 2\int_0^1 u^{1/2}\,du = 2\cdot\frac{u^{3/2}}{3/2}\Big|_0^1 = 2\cdot\frac23.$$ $$\boxed{E[\ell] = \frac{4}{3} \approx 1.333}$$ This matches the mental route: the substitution is exactly the statement that $u = 1-d^2 = 1-D^2$ is Uniform$(0,1)$, so $E[\ell] = 2\,E[\sqrt{u}] = 2\cdot\tfrac23$. **Context — Bertrand's paradox.** This $4/3$ is the **random-radius method**. The other two classical samplings differ: random angle on a diameter gives $E[\ell] = \pi/2 \approx 1.571$; two independent uniform endpoints on the circle give $E[\ell] = 4/\pi \approx 1.273$. 'Random chord' is under-specified — the sampling mechanism must be stated. **Answer:** $E[\ell] = \dfrac{4}{3} \approx 1.33$ (random-radius method). --- Expected Distinct Cards from Packs [easy, Expectation] --- https://quantvault.org/problem-578-expected-distinct-cards-from-packs.html Q: James buys 6 packs of football cards. Each pack contains exactly one card, chosen uniformly at random from a set of 10 distinct cards (with replacement across packs). What is the expected number of distinct cards James ends up with? A: **How to Think About It:** This is a classic coupon-collector-style question, but we are not collecting all coupons -- we just want the expected number of distinct types in a fixed number of draws. The right tool is indicator random variables. For each of the 10 card types, ask: what is the probability that card $i$ shows up at least once in 6 draws? Then use linearity of expectation to sum those probabilities. The complement (never seeing a given card) is much easier to compute. **Quick Estimate:** Each specific card has a $\frac{1}{10}$ chance of appearing in any given pack, so it has a $\frac{9}{10}$ chance of missing. With 6 independent packs, the probability of never seeing card $i$ is $(0.9)^6 \approx 0.531$. So each card appears with probability about $1 - 0.531 = 0.469$. With 10 cards, the expected distinct count is roughly $10 \times 0.469 \approx 4.69$. **Approach:** Formalize via indicator variables and linearity of expectation. **Formal Solution:** Define indicator variables $X_i$ for $i = 1, 2, \ldots, 10$: $$X_i = \begin{cases} 1, & \text{if card } i \text{ appears in at least one pack,} \\ 0, & \text{otherwise.} \end{cases}$$ The total number of distinct cards is $X = X_1 + X_2 + \cdots + X_{10}$. By linearity of expectation: $$\mathbb{E}[X] = \sum_{i=1}^{10} \mathbb{E}[X_i] = \sum_{i=1}^{10} P(\text{card } i \text{ appears at least once}).$$ The probability that card $i$ never appears in any of the 6 packs is $\left(\frac{9}{10}\right)^6$, so: $$\mathbb{E}[X_i] = 1 - \left(\frac{9}{10}\right)^6 = 1 - 0.531441 = 0.468559.$$ Therefore: $$\mathbb{E}[X] = 10 \times \left(1 - \left(\frac{9}{10}\right)^6\right) = 10 \times 0.468559 \approx 4.686.$$ **Answer:** $\mathbb{E}[X] = 10\left(1 - \left(\frac{9}{10}\right)^6\right) \approx 4.69$ distinct cards. --- Expected Payoff of a Capped Coin-Flip Game [medium, Expectation] --- https://quantvault.org/problem-583-expected-payoff-of-a-capped-coin-flip-game.html Q: Samuel flips a fair coin repeatedly until he gets tails. Let $n$ be the total number of flips (including the final tails). His payoff is $\min(64, 2^n)$ dollars. What is Samuel's expected payoff? A: **How to Think About It:** The payoff doubles with each flip ($2, 4, 8, \ldots$) but is capped at $64 = 2^6$. So for the first 6 flips, the payoff grows exponentially, and after that it is flat at 64. The key structural observation: for flips $n = 1$ through $6$, each outcome contributes exactly the same amount to the expected value, because the probability $(1/2)^n$ exactly cancels the payoff $2^n$. Once the cap kicks in, you are just collecting $64$ times a small tail probability. This is a truncated version of the St. Petersburg paradox -- the cap is what makes the expectation finite and manageable. **Quick Estimate:** Each of the first 6 flips contributes $(1/2)^n \times 2^n = 1$ dollar. That gives 6 dollars. The probability of reaching flip 7 or beyond is $(1/2)^6 = 1/64$, and the payoff is capped at 64, so that tail contributes $64 \times (1/64) = 1$ dollar. Quick answer: $6 + 1 = 7$. Done. **Approach:** Split the expectation at the cap boundary $n = 6$ and sum separately. **Formal Solution:** The number of flips $n$ follows a geometric distribution: $P(n = i) = (1/2)^i$ for $i = 1, 2, 3, \ldots$ The expected payoff is: $$E[\text{Payoff}] = \sum_{i=1}^{\infty} P(n = i) \cdot \min(64, 2^i)$$ Split at $i = 6$, since $2^i \leq 64$ for $i \leq 6$ and $2^i > 64$ for $i \geq 7$: $$E[\text{Payoff}] = \underbrace{\sum_{i=1}^{6} \frac{1}{2^i} \cdot 2^i}_{\text{uncapped region}} + \underbrace{\sum_{i=7}^{\infty} \frac{1}{2^i} \cdot 64}_{\text{capped region}}$$ The first sum: $$\sum_{i=1}^{6} \frac{2^i}{2^i} = \sum_{i=1}^{6} 1 = 6$$ The second sum: $$64 \sum_{i=7}^{\infty} \frac{1}{2^i} = 64 \cdot \frac{1/2^7}{1 - 1/2} = 64 \cdot \frac{1}{64} = 1$$ **Answer:** The expected payoff is $\boxed{7}$ dollars. --- Expected Payoff of a Recursive Die Game [easy, Expectation] --- https://quantvault.org/problem-584-expected-payoff-of-a-recursive-die-game.html Q: You roll a fair six-sided die repeatedly. On each roll: - If the outcome is 4, 5, or 6, you get paid that amount (in dollars) and **continue** rolling. - If the outcome is 1, 2, or 3, you get paid that amount and the game **stops**. What is the expected total payoff? A: **How to Think About It:** Each roll pays you its face value regardless of whether the game continues. The game continues (with probability 1/2) or stops (with probability 1/2) depending on whether you roll high or low. This is a geometric-type recursion: the expected payoff equals one roll's payment plus (with probability 1/2) the expected payoff of the entire game again. **Quick Estimate:** The expected number of rolls is geometric with parameter $1/2$, giving $E[\text{rolls}] = 2$. The average payment per roll is $(1+2+3+4+5+6)/6 = 3.5$. So the expected total is roughly $2 \times 3.5 = 7$. We will confirm this is exact. **Formal Solution:** Let $E$ denote the expected total payoff. On each roll, two things can happen: 1. **High roll (4, 5, or 6)** -- probability $1/2$, expected payment $(4+5+6)/3 = 5$, game continues. 2. **Low roll (1, 2, or 3)** -- probability $1/2$, expected payment $(1+2+3)/3 = 2$, game stops. By the law of total expectation: $$E = \frac{1}{2}(5 + E) + \frac{1}{2}(2)$$ $$E = \frac{5}{2} + \frac{E}{2} + 1$$ $$E - \frac{E}{2} = \frac{7}{2}$$ $$\frac{E}{2} = \frac{7}{2}$$ $$E = 7$$ **Verification via Series Decomposition:** The game reaches roll $n$ if the first $n-1$ rolls were all high, which occurs with probability $(1/2)^{n-1}$. Conditional on roll $n$ occurring, that roll is high with probability $1/2$ (paying 5 in expectation) and low with probability $1/2$ (paying 2 in expectation), so the expected payment on roll $n$ given it happens is $(1/2)(5) + (1/2)(2) = 3.5$. Summing over all possible rolls: $$E = \sum_{n=1}^{\infty} 3.5 \cdot \left(\frac{1}{2}\right)^{n-1} = 3.5 \cdot \frac{1}{1 - 1/2} = 7 \quad \checkmark$$ Note that the payment amount and the continuation decision are correlated -- high rolls both pay more and continue the game. This means Wald's identity does not apply in its naive form here, but the recursive approach and the conditional series both handle this correctly. **Answer:** The expected total payoff is $\boxed{7}$. --- Expected Rolls to See an Odd Number [easy, Expectation] --- https://quantvault.org/problem-585-expected-rolls-to-see-an-odd-number.html Q: You roll a fair six-sided die repeatedly until you see an odd number. What is the expected number of rolls? A: **How to Think About It:** This is a classic "how many tries until your first success" problem. Each roll is independent, and exactly half the faces (1, 3, 5) are odd. So every roll is like a coin flip -- you either get an odd number (success) or you don't. The expected number of trials until the first success in a sequence of independent Bernoulli trials is $1/p$. With $p = 1/2$, you should expect about 2 rolls. **Quick Estimate:** Half the faces are odd, so on average you succeed every other roll. Gut answer: 2. **Approach:** Model the number of rolls as a geometric random variable and compute its expectation directly. **Formal Solution:** Let $X$ be the number of rolls until the first odd number appears. Each roll shows an odd number with probability $p = 3/6 = 1/2$, and rolls are independent. So $X \sim \text{Geom}(1/2)$, meaning: $$P(X = k) = (1 - p)^{k-1} \, p = \left(\frac{1}{2}\right)^k, \quad k = 1, 2, 3, \ldots$$ The expected value of a geometric random variable is: $$E[X] = \frac{1}{p} = \frac{1}{1/2} = 2$$ Alternatively, by first-step analysis: on the first roll, with probability $1/2$ you see an odd number (done in 1 roll), and with probability $1/2$ you see an even number and start over. So: $$E[X] = \frac{1}{2}(1) + \frac{1}{2}(1 + E[X])$$ Solving: $E[X] = 1 + \frac{1}{2} E[X]$, which gives $\frac{1}{2} E[X] = 1$, so $E[X] = 2$. **Answer:** The expected number of rolls is $E[X] = 2$. --- Expected Sum Until Rolling a 1 [medium, Expectation] --- https://quantvault.org/problem-587-expected-sum-until-rolling-a-1.html Q: You roll a fair six-sided die repeatedly and keep a running total of the results. The moment you roll a $1$, you stop -- and that $1$ is **not** added to your sum. What is the expected value of your final sum? A: **How to Think About It:** This is a classic "geometric stopping" problem. Before doing any algebra, think about it in two pieces: (1) how many "good" rolls (non-1) do you get before a 1 kills you, and (2) what does each good roll contribute on average? The stopping condition is memoryless -- every time you pick up the die, there is a $1/6$ chance the game ends. So the number of contributing rolls is geometric. Once you see that decomposition, the answer is immediate via Wald's identity. **Quick Estimate:** Each roll has a $1/6$ chance of being a 1 (game over) and a $5/6$ chance of continuing. The expected number of contributing rolls before the first 1 is $E[N] = 5$ (geometric with success probability $1/6$, counting failures before the first success). Each contributing roll is equally likely to be 2, 3, 4, 5, or 6, so its expected value is $(2+3+4+5+6)/5 = 4$. Quick multiplication: $5 \times 4 = 20$. That should be the answer. **Approach:** We can confirm this with a self-referencing equation for $E[S]$. **Formal Solution:** Let $S$ be the total sum. Condition on the first roll: - With probability $1/6$: roll a 1, stop, contribute $0$. - With probability $5/6$: roll some $k \in \{2,3,4,5,6\}$, add $k$ to the sum, and the remaining game looks identical to the original (by memorylessness). The expected value of a roll given that it is not a 1: $$E[X \mid X \neq 1] = \frac{2+3+4+5+6}{5} = 4$$ So the recursion is: $$E[S] = \frac{1}{6} \cdot 0 + \frac{5}{6}\bigl(4 + E[S]\bigr)$$ Expanding: $$E[S] = \frac{20}{6} + \frac{5}{6}\,E[S]$$ Subtract $\frac{5}{6}\,E[S]$ from both sides: $$\frac{1}{6}\,E[S] = \frac{20}{6}$$ $$E[S] = 20$$ **Wald's Identity (alternative):** The number of contributing rolls $N \sim \text{Geometric}(1/6)$ with $E[N] = 5$. Since each contributing roll is independent and identically distributed with mean 4, Wald's identity gives: $$E[S] = E[N] \cdot E[X \mid X \neq 1] = 5 \times 4 = 20$$ **Answer:** $E[S] = 20$. --- Gaussian Half-Space Probability [hard, Probability] --- https://quantvault.org/problem-595-gaussian-half-space-probability.html Q: Let $X, Y \sim N(0, 1)$ be jointly normal with correlation $\rho \in (-1, 1)$. For constants $k \in \mathbb{R}$ and $b \in \mathbb{R}$: 1. Compute $P(Y > kX + b)$ in closed form. 2. Compute $P(Y > kX + b \mid X > x_0)$ in closed form by conditioning on $X$ and using the conditional normal distribution. A: **How to Think About It:** The event $\{Y > kX + b\}$ is a half-space in the $(X, Y)$ plane. Since $(X, Y)$ is bivariate normal, any linear function of $(X, Y)$ is univariate normal. So $P(Y > kX + b) = P(Y - kX > b)$, and $Y - kX$ is normal -- we just need its mean and variance. For part 2, conditioning on $X > x_0$ means we first condition on a specific $X = x$, use the conditional distribution $Y \mid X = x$, and then integrate over $x > x_0$. **Quick Estimate:** Take $\rho = 0$, $k = 1$, $b = 0$. Then $Y - X \sim N(0, 2)$, so $P(Y > X) = P(Y - X > 0) = 1/2$ by symmetry. That checks out. If $\rho = 0.5$, $k = 1$, $b = 0$: $\text{Var}(Y - X) = 1 + 1 - 2(0.5)(1) = 1$, so $Y - X \sim N(0, 1)$ and $P(Y > X) = 1/2$ again. If $b = 1$: $P(Y - X > 1) = 1 - \Phi(1) \approx 0.159$. **Formal Solution:** **Part 1: $P(Y > kX + b)$** Define $W = Y - kX$. Since $X$ and $Y$ are jointly normal, $W$ is normal with: $$E[W] = E[Y] - kE[X] = 0$$ $$\text{Var}(W) = \text{Var}(Y) + k^2\text{Var}(X) - 2k\text{Cov}(X, Y) = 1 + k^2 - 2k\rho$$ So $W \sim N(0, 1 + k^2 - 2k\rho)$. Let $\sigma_W = \sqrt{1 + k^2 - 2k\rho}$. $$P(Y > kX + b) = P(W > b) = 1 - \Phi\!\left(\frac{b}{\sigma_W}\right) = \Phi\!\left(\frac{-b}{\sqrt{1 + k^2 - 2k\rho}}\right)$$ **Part 2: $P(Y > kX + b \mid X > x_0)$** The conditional distribution of $Y$ given $X = x$ is: $$Y \mid X = x \sim N\!\left(\rho x, \, 1 - \rho^2\right)$$ So for a fixed $x$: $$P(Y > kx + b \mid X = x) = P\!\left(\frac{Y - \rho x}{\sqrt{1 - \rho^2}} > \frac{kx + b - \rho x}{\sqrt{1 - \rho^2}}\right) = \Phi\!\left(\frac{\rho x - kx - b}{\sqrt{1 - \rho^2}}\right)$$ $$= \Phi\!\left(\frac{(\rho - k)x - b}{\sqrt{1 - \rho^2}}\right)$$ Now integrate over $X > x_0$ using the law of total probability: $$P(Y > kX + b \mid X > x_0) = \frac{P(Y > kX + b, \, X > x_0)}{P(X > x_0)}$$ The denominator is $P(X > x_0) = \Phi(-x_0)$. The numerator is: $$P(Y > kX + b, \, X > x_0) = \int_{x_0}^{\infty} \Phi\!\left(\frac{(\rho - k)x - b}{\sqrt{1 - \rho^2}}\right) \phi(x) \, dx$$ where $\phi(x)$ is the standard normal PDF. This integral can be evaluated in closed form using the identity for $\int \Phi(\alpha + \beta x)\phi(x)\,dx$. Specifically, for $\alpha, \beta \in \mathbb{R}$: $$\int_{x_0}^{\infty} \Phi(\alpha + \beta x) \phi(x) \, dx = \Phi_2\!\left(-x_0, \, \alpha + \beta \cdot 0, \, \frac{\beta}{\sqrt{1 + \beta^2}}\right) \text{ ... (via bivariate normal CDF)}$$ More precisely, using the standard result: for jointly normal $(X, Z)$ where $Z = \alpha + \beta X + \sqrt{1}\cdot\epsilon$ with $\epsilon$ independent standard normal, the joint event $\{X > x_0, Z > 0\}$ is a bivariate normal probability. In our case, set $\alpha = -b/\sqrt{1-\rho^2}$ and $\beta = (\rho - k)/\sqrt{1-\rho^2}$. Then: $$P(Y > kX + b, \, X > x_0) = \Phi_2\!\left(-x_0, \, \frac{-b}{\sqrt{1 + k^2 - 2k\rho}}, \, \frac{\rho - k}{\sqrt{1 + k^2 - 2k\rho}}\right)$$ where $\Phi_2(a, b, r)$ is the standard bivariate normal CDF $P(X_1 > -a, X_2 > -b)$ with correlation $r$. So the final answer is: $$P(Y > kX + b \mid X > x_0) = \frac{\Phi_2\!\left(-x_0, \, \frac{-b}{\sqrt{1+k^2-2k\rho}}, \, \frac{\rho-k}{\sqrt{1+k^2-2k\rho}}\right)}{\Phi(-x_0)}$$ **Answer:** 1. $P(Y > kX + b) = \Phi\!\left(\dfrac{-b}{\sqrt{1 + k^2 - 2k\rho}}\right)$ 2. $P(Y > kX + b \mid X > x_0) = \dfrac{\Phi_2\!\left(-x_0, \, \frac{-b}{\sqrt{1+k^2-2k\rho}}, \, \frac{\rho-k}{\sqrt{1+k^2-2k\rho}}\right)}{\Phi(-x_0)}$ where $\Phi_2(a, b, r)$ denotes the bivariate normal CDF with correlation $r$. --- Lucky Lottery Probability [easy, Probability] --- https://quantvault.org/problem-600-lucky-lottery-probability.html Q: A lottery draws 6 balls at random (without replacement) from an urn containing 70 balls labeled 1 through 70. Your ticket also has 6 distinct numbers, chosen uniformly at random from 1 to 70. You win if at least 5 of your 6 numbers match the 6 drawn balls (order does not matter). 1. Find the probability $p$ of winning. 2. Report $p \times 10^6$, rounded to the nearest hundredth. A: **How to Think About It:** This is a sampling-without-replacement problem. You have 70 balls, 6 of which match your ticket ('successes') and 64 of which do not ('failures'). The lottery draws 6 balls. The number of matches $N$ follows a Hypergeometric distribution -- the natural model whenever you sample without replacement from two groups. **Quick Estimate:** The number of ways to pick 6 from 70 is $\binom{70}{6} \approx 131$ million. The '5 match' outcome requires choosing 5 from your 6 winning numbers and 1 from the 64 non-winners: $\binom{6}{5}\binom{64}{1} = 6 \times 64 = 384$ ways. The '6 match' outcome has $\binom{6}{6}\binom{64}{0} = 1$ way. Combined: 385 ways out of roughly 131 million -- a probability around $3 \times 10^{-6}$. So $p \times 10^6 \approx 3$. **Formal Solution:** Let $N$ = number of ticket numbers that match the drawn balls. Since we draw without replacement from a population of 6 'matching' and 64 'non-matching' balls: $$N \sim \text{Hypergeometric}(\text{population}=70,\; \text{successes}=6,\; \text{draws}=6)$$ The PMF is: $$P(N = k) = \frac{\binom{6}{k}\binom{64}{6-k}}{\binom{70}{6}}$$ **Win condition:** $N \geq 5$, i.e., $N = 5$ or $N = 6$. $$P(N = 5) = \frac{\binom{6}{5}\binom{64}{1}}{\binom{70}{6}} = \frac{6 \times 64}{\binom{70}{6}} = \frac{384}{\binom{70}{6}}$$ $$P(N = 6) = \frac{\binom{6}{6}\binom{64}{0}}{\binom{70}{6}} = \frac{1}{\binom{70}{6}}$$ Now compute $\binom{70}{6}$: $$\binom{70}{6} = \frac{70 \times 69 \times 68 \times 67 \times 66 \times 65}{720} = 131,\!115,\!985$$ So: $$p = P(N \geq 5) = \frac{385}{131,\!115,\!985} \approx 2.9363 \times 10^{-6}$$ $$p \times 10^6 \approx 2.94$$ **Answer:** $p \times 10^6 \approx \mathbf{2.94}$. --- Maximum Fair Penalty in a Dice Payout Game [medium, Expectation] --- https://quantvault.org/problem-605-maximum-fair-penalty-in-a-dice-payout-game.html Q: You roll two fair $n$-sided dice (faces $1$ through $n$). Let $A$ and $B$ be disjoint subsets of the $n^2$ possible outcomes. - If the roll lands in $A$, you receive $\$p$. - If the roll lands in $B$, you pay $\$q$. - If the roll lands in neither $A$ nor $B$, you re-roll at no cost, and keep re-rolling until the outcome falls in $A$ or $B$. Given that $p$ is fixed, find the maximum value of $q$ such that your expected payout is non-negative. Evaluate for the specific case $p = 100$, $n = 10$, $|A| = 25$, and $|B| = 50$. A: **How to Think About It:** The re-roll mechanism is the key simplification here. Because you keep rolling until you land in $A \cup B$, everything outside that set is irrelevant -- it is as if those outcomes do not exist. The game effectively reduces to: draw uniformly from $A \cup B$, win $p$ if you land in $A$, lose $q$ if you land in $B$. The probability of each event is just the fraction of $A \cup B$ that it occupies. This is a bread-and-butter conditional probability observation. **Quick Estimate:** With $|A| = 25$ and $|B| = 50$, you land in $A$ one-third of the time and in $B$ two-thirds of the time (conditional on being in $A \cup B$). So the expected payout is roughly $(1/3)(100) - (2/3)(q)$. For breakeven, $(1/3)(100) = (2/3)(q)$, giving $q = 50$. Done -- that is the answer, and you can get it in 15 seconds. **Approach:** Condition on the outcome being in $A \cup B$ and set the expected payout to zero to find the breakeven $q$. **Formal Solution:** Since $A$ and $B$ are disjoint, $|A \cup B| = |A| + |B|$. The re-roll rule means we only care about outcomes in $A \cup B$. Given that the dice roll lands in $A \cup B$, each outcome in that set is equally likely (the dice are fair), so: $$P(\text{outcome} \in A \mid \text{outcome} \in A \cup B) = \frac{|A|}{|A| + |B|}$$ $$P(\text{outcome} \in B \mid \text{outcome} \in A \cup B) = \frac{|B|}{|A| + |B|}$$ The expected payout is: $$E[\text{payout}] = \frac{|A|}{|A| + |B|} \cdot p - \frac{|B|}{|A| + |B|} \cdot q$$ Setting $E[\text{payout}] \geq 0$: $$\frac{|A| \cdot p}{|A| + |B|} \geq \frac{|B| \cdot q}{|A| + |B|}$$ $$q \leq p \cdot \frac{|A|}{|B|}$$ Plugging in $p = 100$, $|A| = 25$, $|B| = 50$: $$q \leq 100 \cdot \frac{25}{50} = 50$$ **Answer:** The maximum value of $q$ for non-negative expected payout is $q = p \cdot \frac{|A|}{|B|}$. For the given parameters, $q = 50$. --- Mean and Variance of a Mixture of Normals [medium, Expectation] --- https://quantvault.org/problem-607-mean-and-variance-of-a-mixture-of-normals.html Q: A random variable $X$ follows a two-component normal mixture: with probability $w$ it is drawn from $N(\mu_1, \sigma_1^2)$, and with probability $1-w$ it is drawn from $N(\mu_2, \sigma_2^2)$. 1. Compute $E[X]$. 2. Compute $\text{Var}(X)$. Your answer should make clear why a mixture can have higher variance than either component alone. A: **How to Think About It:** Introduce a latent component indicator $C$ where $P(C=1) = w$ and $P(C=2) = 1-w$. Given $C$, $X$ is just a normal. The expectation is easy by linearity. The variance requires the law of total variance, which neatly splits into two interpretable pieces: the average within-component variance, and the variance of the component means (the between-component spread). **Quick Estimate:** To see why the variance can be large, take $\mu_1 = -10$, $\mu_2 = +10$, $\sigma_1^2 = \sigma_2^2 = 1$, $w = 0.5$. Each component has variance $1$, but the mixture has mean $0$ and variance $\sigma^2_{\text{avg}} + w(1-w)(\Delta\mu)^2 = 1 + 0.25\cdot(20)^2 = 1 + 100 = 101$. The separation between the means (here $\Delta\mu = 20$, so the squared gap is $400$) contributes far more than the within-component spreads. This is the intuition behind fat tails in financial return distributions -- a low-volatility regime mixed with a high-volatility regime produces heavy tails. **Approach:** Condition on the component indicator $C$. Part 1 uses the law of total expectation; Part 2 uses the law of total variance, $\text{Var}(X) = E[\text{Var}(X|C)] + \text{Var}(E[X|C])$, where the first term is the average within-component variance and the second is the spread of the component means. **Formal Solution:** **Part 1: Expectation.** By the law of total expectation: $$E[X] = E[E[X|C]] = w \cdot E[X|C=1] + (1-w) \cdot E[X|C=2] = w\mu_1 + (1-w)\mu_2$$ **Part 2: Variance.** By the law of total variance: $$\text{Var}(X) = E[\text{Var}(X|C)] + \text{Var}(E[X|C])$$ The first term is the expected within-component variance: $$E[\text{Var}(X|C)] = w\sigma_1^2 + (1-w)\sigma_2^2$$ For the second term, $E[X|C]$ takes value $\mu_1$ with probability $w$ and $\mu_2$ with probability $1-w$. Its variance is: $$\text{Var}(E[X|C]) = w\mu_1^2 + (1-w)\mu_2^2 - (w\mu_1 + (1-w)\mu_2)^2 = w(1-w)(\mu_1 - \mu_2)^2$$ Combining: $$\text{Var}(X) = w\sigma_1^2 + (1-w)\sigma_2^2 + w(1-w)(\mu_1-\mu_2)^2$$ **Answer:** $$E[X] = w\mu_1 + (1-w)\mu_2$$ $$\text{Var}(X) = w\sigma_1^2 + (1-w)\sigma_2^2 + w(1-w)(\mu_1-\mu_2)^2$$ The last term shows that even if both components have zero variance, the mixture has variance $w(1-w)(\mu_1-\mu_2)^2 > 0$ whenever the means differ. --- Minimum Number of Rooms for Interval Partitioning [medium, Coding] --- https://quantvault.org/problem-608-minimum-number-of-rooms-for-interval-partitioning.html Q: You are given $n$ intervals, each represented as $[s_i, e_i)$ (start inclusive, end exclusive). These represent meetings or events that each need a dedicated room. Two intervals can share a room only if they do not overlap. Find the minimum number of rooms required so that every interval is assigned to a room with no overlaps. Additionally, output an explicit assignment of each interval to a room, and prove that your assignment is optimal. **Constraints:** - $1 \leq n \leq 10^5$ - $0 \leq s_i < e_i \leq 10^9$ - Intervals are half-open: $[s_i, e_i)$ means an interval ending at time $t$ does not conflict with one starting at time $t$ **Example 1:** - Input: `intervals = [[0, 30), [5, 10), [15, 20)]` - Output: `2` - Explanation: `[5, 10)` and `[15, 20)` can share one room since they do not overlap. `[0, 30)` needs its own room. Total: 2 rooms. **Example 2:** - Input: `intervals = [[1, 5), [2, 6), [3, 7), [4, 8)]` - Output: `4` - Explanation: At time 4, all four intervals are active simultaneously, so 4 rooms are required. **Example 3:** - Input: `intervals = [[1, 3), [3, 5), [5, 7)]` - Output: `1` - Explanation: No two intervals overlap (they are end-to-start adjacent), so one room suffices. A: **How to Think About It:** This is the classic interval partitioning problem, also known as "meeting rooms II." The brute force approach -- try all possible assignments of intervals to rooms -- is exponential and clearly impractical. The key observation is that the minimum number of rooms equals the maximum number of intervals that overlap at any single point in time. If $k$ meetings are all happening simultaneously, you obviously need at least $k$ rooms. The beautiful part is that a simple greedy algorithm always achieves this lower bound exactly. **Algorithm:** Sort all intervals by start time. Maintain a min-heap (priority queue) that tracks the end times of the rooms currently in use. For each new interval, check if its start time is at least as large as the smallest end time in the heap. If so, the earliest-finishing room is free -- reuse it by replacing its end time. If not, you need a new room -- push the new end time onto the heap. The heap size at the end is the answer, and by tracking which room each interval is assigned to, you get the explicit assignment. To prove optimality: at any moment, the heap size equals the number of intervals currently active. The maximum heap size over the algorithm's run equals the maximum overlap depth. Since any valid assignment needs at least that many rooms, the greedy is optimal. **Code:** ```python import heapq def min_rooms(intervals): if not intervals: return 0, [] # Sort by start time, break ties by end time indexed = sorted(enumerate(intervals), key=lambda x: (x[1][0], x[1][1])) # Min-heap of (end_time, room_id) heap = [] assignment = [0] * len(intervals) next_room = 0 for orig_idx, (start, end) in indexed: if heap and heap[0][0] <= start: # Reuse the room that frees up earliest _, room_id = heapq.heappop(heap) heapq.heappush(heap, (end, room_id)) assignment[orig_idx] = room_id else: # Allocate a new room heapq.heappush(heap, (end, next_room)) assignment[orig_idx] = next_room next_room += 1 return len(heap), assignment ``` Alternative sweep-line approach (just counting rooms, no assignment): ```python def min_rooms_sweep(intervals): events = [] for start, end in intervals: events.append((start, 1)) # meeting starts events.append((end, -1)) # meeting ends events.sort() # ties: ends before starts (since -1 < 1) max_rooms = 0 current = 0 for _, delta in events: current += delta max_rooms = max(max_rooms, current) return max_rooms ``` **Complexity:** - **Time:** $O(n \log n)$ -- dominated by the sort. Each heap operation is $O(\log n)$ and there are $n$ of them. - **Space:** $O(n)$ -- the heap holds at most $n$ entries, and we store the assignment array. **Optimality Proof:** Let $d$ be the maximum depth of overlap (the most intervals active at any single time point). Clearly, any valid assignment requires $\geq d$ rooms. The greedy algorithm opens a new room only when the current interval overlaps with all existing rooms -- which means $d$ intervals are simultaneously active. Therefore the greedy uses exactly $d$ rooms, matching the lower bound. **Answer:** The minimum number of rooms equals the maximum overlap depth. A greedy algorithm sorting by start time and using a min-heap of end times solves this in $O(n \log n)$ time and $O(n)$ space, producing both the count and an explicit optimal assignment. --- Monte Carlo Estimation of Pi and e [medium, Probability] --- https://quantvault.org/problem-612-monte-carlo-estimation-of-pi-and-e.html Q: Describe how you would use Monte Carlo simulation to estimate: 1. The value of $\pi$ 2. The value of $e$ For each, explain the underlying mathematical principle, give a concrete algorithm, and discuss the convergence rate. **Example** ``` estimate_pi(10000, 1) ≈ 3.14159 ``` The result is a Monte-Carlo estimate that converges to 3.14159 as the sample count grows (deterministic for a fixed seed). A: **How to Think About It:** Monte Carlo is all about finding a random experiment whose expected value equals the constant you want, then averaging many independent copies. For $\pi$, the classic trick is that the area of a quarter circle inscribed in the unit square is $\pi/4$ -- throw darts at the unit square and the fraction landing inside the circle converges to $\pi/4$. For $e$, you need a different experiment: it turns out the expected number of independent $U(0,1)$ draws needed for the running sum to first exceed $1$ is exactly $e$. **Quick Estimate:** With $N = 10{,}000$ samples, the $\pi$ estimator $\hat\pi = 4H/N$ has standard error $4\sqrt{\tfrac{(\pi/4)(1-\pi/4)}{N}} = \sqrt{\tfrac{\pi(4-\pi)}{N}} \approx \sqrt{2.70/10000} \approx 0.016$, so about $2$ decimal places. For $e$, the stopping count $N$ has variance $\operatorname{Var}(N) = 3e - e^2 \approx 0.765$ (derived below), so with $M = 10{,}000$ trials the standard error is $\sqrt{(3e-e^2)/M} \approx \sqrt{0.765/10000} \approx 0.0088$. **Approach:** For each constant, identify an unbiased random observable (an indicator for $\pi$, a stopping count for $e$), give the sampling algorithm, justify it via the law of large numbers, and read off the $O(1/\sqrt{N})$ Monte Carlo convergence rate from the CLT. **Formal Solution:** **Part (a) -- Estimating $\pi$.** Consider the unit square $[0,1]^2$. The quarter disk $\{(x,y): x^2 + y^2 \le 1\}$ has area $\pi/4$. Algorithm: 1. Sample $N$ points $(x_i, y_i)$ uniformly from $[0,1]^2$. 2. Count hits $H = \sum_{i=1}^{N} \mathbf{1}[x_i^2 + y_i^2 \le 1]$. 3. Estimate $\hat{\pi} = 4H/N$. Why it works: $E\!\left[\mathbf{1}[x^2+y^2\le 1]\right] = \pi/4$, so by the law of large numbers $H/N \to \pi/4$ and $\hat\pi \to \pi$. Since $H \sim \text{Bin}(N, \pi/4)$, $\operatorname{Var}(\hat\pi) = 16\cdot\tfrac{(\pi/4)(1-\pi/4)}{N} = \tfrac{\pi(4-\pi)}{N}$. **Part (b) -- Estimating $e$.** Let $U_1, U_2, \dots \sim U(0,1)$ be i.i.d. and define the stopping count $$N = \min\Bigl\{k : U_1 + U_2 + \cdots + U_k > 1\Bigr\}.$$ Algorithm: 1. Repeat $M$ independent trials. 2. In each trial, draw $U(0,1)$ values and accumulate the sum until it first exceeds $1$; record the count $N_j$. 3. Estimate $\hat{e} = \frac{1}{M}\sum_{j=1}^{M} N_j$. Why it works: the event $\{N > n\}$ means the first $n$ uniforms sum to at most $1$, i.e. $(U_1,\dots,U_n)$ lies in the $n$-simplex $\{u_i \ge 0,\ \sum u_i \le 1\}$, whose volume is $1/n!$. So $P(N > n) = 1/n!$ for $n \ge 0$, and by the tail-sum formula for a nonnegative integer random variable, $$E[N] = \sum_{n=0}^{\infty} P(N > n) = \sum_{n=0}^{\infty} \frac{1}{n!} = e.$$ *Variance of the stopping count.* Use $E[N(N-1)] = \sum_{n\ge 0} 2n\,P(N>n)$ (or the pmf $P(N=n) = \tfrac{1}{(n-1)!} - \tfrac{1}{n!}$ for $n\ge 1$). The second factorial moment is $$E[N(N-1)] = \sum_{n=0}^{\infty} 2n\cdot\frac{1}{n!} = 2\sum_{n=1}^{\infty}\frac{1}{(n-1)!} = 2e,$$ so $E[N^2] = E[N(N-1)] + E[N] = 2e + e = 3e$. Therefore $$\operatorname{Var}(N) = E[N^2] - (E[N])^2 = 3e - e^2 \approx 3(2.71828) - (2.71828)^2 \approx 0.7658.$$ Hence the Monte Carlo standard error of $\hat e$ over $M$ trials is $\sqrt{(3e - e^2)/M}$. **Code:** ```python import random def estimate_pi(N=1_000_000): hits = sum(1 for _ in range(N) if random.random()**2 + random.random()**2 n) = 1/n!$ gives $E[N] = \sum 1/n! = e$; the stopping count has $\operatorname{Var}(N) = 3e - e^2 \approx 0.765$, so the standard error over $M$ trials is $\sqrt{(3e-e^2)/M}$. Both estimators converge at the Monte Carlo rate $O(1/\sqrt{N})$. --- Probability of More Heads Than Tails With Odd Flips [easy, Probability] --- https://quantvault.org/problem-632-probability-of-more-heads-than-tails-with-odd-flips.html Q: You flip a fair coin $2n + 1$ times (an odd number). What is the probability of getting more heads than tails? A: **How to Think About It:** With an odd number of flips, you can never tie -- the number of heads and tails must differ by at least 1. So the only two outcomes are "more heads" and "more tails." Since the coin is fair, by symmetry these two events have the same probability. Two equally likely events that partition the entire sample space each have probability $1/2$. That is the whole argument -- no computation needed. **Quick Estimate:** Take $n = 1$ (3 flips). Outcomes: HHH, HHT, HTH, THH, HTT, THT, TTH, TTT. More heads (at least 2): HHH, HHT, HTH, THH -- that is 4 out of 8 = $1/2$. Checks out. **Approach:** Symmetry argument on the binomial distribution. **Formal Solution:** Let $H$ denote the number of heads in $2n+1$ flips. We have $H \sim \text{Binomial}(2n+1, 1/2)$. "More heads than tails" means $H \geq n+1$ (since there are $2n+1$ flips total, more heads requires at least $n+1$). "More tails than heads" means $H \leq n$. Since $2n+1$ is odd, $H$ cannot equal $(2n+1)/2$, so ties are impossible. Therefore: $$P(H \geq n+1) + P(H \leq n) = 1$$ By symmetry of the fair coin, $P(H = k) = \binom{2n+1}{k}(1/2)^{2n+1} = \binom{2n+1}{2n+1-k}(1/2)^{2n+1} = P(H = 2n+1-k)$. This maps $H \geq n+1$ bijectively to $H \leq n$, so: $$P(H \geq n+1) = P(H \leq n)$$ Combining with the partition: $$P(H \geq n+1) = \frac{1}{2}$$ **Answer:** The probability of getting more heads than tails in $2n+1$ fair coin flips is exactly $\dfrac{1}{2}$. --- Probability of Truth Given Corroboration [medium, Probability] --- https://quantvault.org/problem-637-probability-of-truth-given-corroboration.html Q: Alice and Bob are witnesses to a crime. When asked questions, Alice tells the truth with probability $p_A$ and lies with probability $1 - p_A$. Bob tells the truth with probability $p_B$ and lies with probability $1 - p_B$. Their truthfulness is independent. The detective asks Alice a yes/no question. She gives an answer, and Bob then states that Alice's answer is truthful. Given that $p_A = 1/3$ and $p_B = 3/5$, find the probability that Alice actually told the truth. A: **How to Think About It:** Bob says "Alice told the truth." But Bob might be lying about that. There are two scenarios consistent with Bob's statement: either Alice really did tell the truth AND Bob is honestly reporting that, or Alice lied AND Bob is also lying (falsely claiming she told the truth). We need the conditional probability that we are in the first scenario. **Quick Estimate:** With $p_A = 1/3$ and $p_B = 3/5$: both tell truth has probability $(1/3)(3/5) = 1/5$. Both lie has probability $(2/3)(2/5) = 4/15$. The confirmation event has total probability $1/5 + 4/15 = 3/15 + 4/15 = 7/15$. So the answer is $(1/5)/(7/15) = (3/15)/(7/15) = 3/7 \approx 0.429$. **Approach:** Apply Bayes' theorem with the two scenarios that produce Bob's confirmation. **Formal Solution:** Let $T$ = "Alice told the truth" and $E$ = "Bob confirms Alice's answer is truthful." Bob confirms Alice if either: - Alice told the truth and Bob tells the truth: probability $p_A \cdot p_B$ - Alice lied and Bob lies (falsely says she was truthful): probability $(1 - p_A)(1 - p_B)$ By Bayes' theorem: $$P(T \mid E) = \frac{P(E \mid T) P(T)}{P(E)} = \frac{p_A p_B}{p_A p_B + (1 - p_A)(1 - p_B)}$$ Plugging in $p_A = 1/3$, $p_B = 3/5$: $$P(T \mid E) = \frac{\frac{1}{3} \cdot \frac{3}{5}}{\frac{1}{3} \cdot \frac{3}{5} + \frac{2}{3} \cdot \frac{2}{5}} = \frac{\frac{3}{15}}{\frac{3}{15} + \frac{4}{15}} = \frac{3}{7}$$ **Answer:** $P(\text{Alice told truth} \mid \text{Bob confirms}) = \dfrac{3}{7} \approx 0.4286$. --- Sample Mean of a Product Sequence [medium, Probability] --- https://quantvault.org/problem-645-sample-mean-of-a-product-sequence.html Q: You have two independent sequences of IID random variables: $X_1, X_2, \ldots$ with mean $\mu_X$ and variance $\sigma_X^2$, and $Y_1, Y_2, \ldots$ with mean $\mu_Y$ and variance $\sigma_Y^2$. The two sequences are independent of each other. Define $Z_i = X_i Y_i$. Does the sample mean $$\frac{Z_1 + Z_2 + \cdots + Z_n}{n}$$ converge to a limiting distribution as $n \to \infty$? If it does, state the sum of the mean and variance of that limiting distribution. If it does not converge, answer $-100$. Use $\mu_X = 5$, $\mu_Y = 6$, $\sigma_X^2 = 25$, $\sigma_Y^2 = 36$. A: **How to Think About It:** The first question an interviewer is probing here is: do you know when to reach for the LLN versus the CLT? The sample mean of $n$ IID terms -- that is the LLN setup, not CLT. The CLT applies when you standardize the sum; here we are just dividing by $n$, so the sum concentrates on its expectation. Before worrying about the specific values of $\mu$ and $\sigma^2$, check: are the $Z_i$ IID with finite mean? Yes -- products of independent IID sequences are themselves IID, and finite moments of $X$ and $Y$ give finite moments of $XY$. So by the LLN, the sample mean converges to $E[Z_1]$. That is a constant -- no distribution, just a point mass. The "variance of the limiting distribution" is zero. **Quick Estimate:** Before the formal work, sanity-check the answer. With $\mu_X = 5$ and $\mu_Y = 6$, and since $X_i$ and $Y_i$ are independent, we expect $E[X_i Y_i] = E[X_i] \cdot E[Y_i] = 5 \times 6 = 30$. So the sample mean should converge to $30$. The limiting "distribution" is a point mass at $30$, with mean $30$ and variance $0$. The answer is $30 + 0 = 30$. **Approach:** Formally verify that $Z_i$ is IID with finite mean, apply the Weak Law of Large Numbers, and compute $E[Z_1]$ using independence. **Formal Solution:** First, check the conditions for the LLN: - Each $Z_i = X_i Y_i$ is a measurable function of $(X_i, Y_i)$. Since the $(X_i, Y_i)$ pairs are independent across $i$ (by independence of the two sequences) and identically distributed, the $Z_i$ are IID. - $E[|Z_1|] = E[|X_1 Y_1|] = E[|X_1|] \cdot E[|Y_1|]$ by independence. Finite variance implies finite mean, so both factors are finite. Thus $E[|Z_1|] < \infty$. By the Weak Law of Large Numbers: $$\frac{Z_1 + Z_2 + \cdots + Z_n}{n} \xrightarrow{p} E[Z_1]$$ Now compute $E[Z_1]$: $$E[Z_1] = E[X_1 Y_1] = E[X_1] \cdot E[Y_1] = \mu_X \cdot \mu_Y$$ where the factorization uses independence of $X_1$ and $Y_1$. The limiting "distribution" is a point mass at $\mu_X \mu_Y = 5 \times 6 = 30$. Its mean is $30$ and its variance is $0$. Why the variance is $0$ and not something like $\sigma^2/n$: at any finite $n$, $\text{Var}\big(\tfrac{1}{n}\sum_i Z_i\big) = \text{Var}(Z_1)/n$, which does shrink like $1/n$. Here $\text{Var}(Z_1) = E[X^2]\,E[Y^2] - (\mu_X\mu_Y)^2 = (25+25)(36+36) - 30^2 = 2700$, so the finite-$n$ variance is $2700/n$. The question asks about the limit as $n \to \infty$, where $2700/n \to 0$, so the answer is a single number with no $n$ in it. $$\text{Mean} + \text{Variance} = 30 + 0 = 30$$ **Answer:** $\boxed{30}$ --- Snake Weight System of Equations [easy, Brain Teaser] --- https://quantvault.org/problem-650-snake-weight-system-of-equations.html Q: A man catches a rattlesnake. The head weighs 12 ounces. The tail weighs half the weight of the body. The body weighs 1.5 times the combined weight of the head and tail. How much does the entire snake weigh in ounces? A: **How to Think About It:** Three parts — head, body, tail. The head is pinned directly (12 oz); the body and tail are each defined *relative* to the others, so this is a small linear system. The heuristic is **substitution**: express the two unknowns in terms of one, collapse to a single equation, solve. The trap is trying to guess-and-add without noticing that the body is defined against the *head+tail combined*, not against the whole snake — read the dependency chain before writing anything. **Quick Estimate:** Do the fixed-point in your head in two moves. The body is $1.5\times(\text{head}+\text{tail})$, and the tail is half the body — so the tail feeds back into the body's own definition. Ignore the tail for a first pass: body $\approx 1.5\times 12 = 18$. Now the tail is about half of that, $\approx 9$, which bumps the head+tail combined to $\approx 21$, so body $\approx 1.5\times 21 \approx 31$; tail climbs to $\approx 16$, combined $\approx 28$, body $\approx 1.5\times 28 \approx 42$... the iteration is still climbing, which tells you the naive $18$ was a big underestimate. Close it exactly: the tail is $0.75$ of the *body* worth of feedback, so $b = 18 + 0.75b \Rightarrow 0.25b = 18 \Rightarrow b = 72$. Then tail $= 36$, total $= 12 + 72 + 36 = 120$ oz. The lesson of the estimate: the self-referential tail term roughly *quadruples* the body from the $18$ first guess. **Approach:** Set up two equations, substitute the tail into the body. **Formal Solution:** Let $b$ = body weight, $t$ = tail weight (ounces); head $= 12$. $$t = \tfrac{1}{2}b, \qquad b = 1.5\,(12 + t).$$ Substitute the first into the second: $$b = 1.5\left(12 + \tfrac{b}{2}\right) = 18 + 0.75\,b \;\Rightarrow\; 0.25\,b = 18 \;\Rightarrow\; \boxed{b = 72}.$$ Then $t = 72/2 = 36$, and $$\text{total} = 12 + 72 + 36 = \boxed{120 \text{ oz}}.$$ **Answer:** The snake weighs **120 ounces**. --- The Fly Between Two Cars [easy, Expectation] --- https://quantvault.org/problem-656-the-fly-between-two-cars.html Q: A one-lane road is $300$ miles long. Two cars start at opposite ends and drive toward each other at constant speeds: one at $90$ mph and the other at $45$ mph. A fly starts on the slower car and flies toward the faster car at $135$ mph. The moment it reaches the faster car, it immediately turns around and flies back to the slower car at the same speed. This continues until the two cars collide. How many miles does the fly travel in total? A: **How to Think About It:** Before you start summing up the fly's individual back-and-forth trips, pause and think about the big picture. The fly is in the air continuously from the moment the cars start until the moment they collide. It never stops. So the total distance the fly travels is just: speed of fly $\times$ total time. The only question is: how long until the cars meet? **Quick Estimate:** The two cars close the $300$-mile gap at a combined speed of $90 + 45 = 135$ mph. So they meet after $300 / 135 = 20/9 \approx 2.22$ hours. The fly moves at $135$ mph the entire time, so it covers $135 \times (300/135) = 300$ miles. Done. **Approach:** That is the "trick" solution -- it avoids tracking the fly's trajectory entirely. But a classic follow-up is: can you also solve it by summing the geometric series of individual trips? Let's do that. **Formal Solution (Geometric Series Method):** Label the slow car as $S$ (starting at mile $0$, speed $45$ mph) and the fast car as $F$ (starting at mile $300$, speed $90$ mph). The fly starts on $S$. **Trip 1 (fly $\to$ $F$):** The fly starts on $S$ and flies toward $F$. The closing speed between the fly and $F$ is $135 + 90 = 225$ mph across $300$ miles. Time to meet: $t_1 = 300/225 = 4/3$ hours. The fly covers $135 \times 4/3 = 180$ miles. At that moment, the gap between $S$ and $F$ is $300 - (45 + 90) \times 4/3 = 300 - 180 = 120$ miles. **Trip 2 (fly $\to$ $S$):** Now the fly heads back toward $S$. Closing speed between fly and $S$ is $135 + 45 = 180$ mph across $120$ miles. Time: $120/180 = 2/3$ hours. Fly travels $135 \times 2/3 = 90$ miles. The gap shrinks by the combined car speed times time: $(90 + 45) \times 2/3 = 90$, so the new gap is $120 - 90 = 30$ miles. After one full round trip (out and back), the fly has traveled $180 + 90 = 270$ miles, and the gap between the cars has gone from $300$ to $30$ miles -- a factor of $1/10$. **The pattern:** Each full round trip shrinks the gap by a factor of $1/10$, and the fly's distance on each round trip also scales by $1/10$. So the total distance is: $$D = 270 \sum_{i=0}^{\infty} \left(\frac{1}{10}\right)^i = 270 \times \frac{1}{1 - 1/10} = 270 \times \frac{10}{9} = 300$$ Both methods agree. **Answer:** The fly travels $\mathbf{300}$ miles. The key insight is that the fly's speed equals the cars' combined closing speed, so the total distance equals the initial gap. But even without noticing that coincidence, summing the geometric series of round trips gives the same result. --- Tyre Rotation for Maximum Distance [medium, Brain Teaser] --- https://quantvault.org/problem-661-tyre-rotation-for-maximum-distance.html Q: A car has two front tyres and two rear tyres. Front tyres wear out after $x$ km, and rear tyres wear out after $y$ km (with $x \neq y$ in general). You can swap the front and rear tyres at any point during the journey. What is the maximum total distance the car can travel before any tyre is completely worn out? At what point should you perform the swap? A: **How to Think About It:** Each tyre has a wear budget. A front tyre consumes $1/x$ of its life per km, and a rear tyre consumes $1/y$ per km. If you swap at some point $s$, each tyre spends some distance in front and some in rear. The goal is to choose $s$ so that all four tyres reach exactly 100% wear at the same time -- that way no tyre goes to waste and no tyre blows out early. **Quick Estimate:** Say $x = 40{,}000$ km (front) and $y = 80{,}000$ km (rear). Harmonic mean: $2 \times 40{,}000 \times 80{,}000 / (40{,}000 + 80{,}000) = 6.4 \times 10^9 / 120{,}000 \approx 53{,}333$ km. So the answer is between the two limits ($40k$ and $80k$), closer to the smaller one. Swap at $53{,}333/2 \approx 26{,}667$ km. **Approach:** Set up a wear equation per tyre and solve for the total distance $d$ that exhausts all tyres simultaneously. **Formal Solution:** Let $d$ be the total distance and $s$ be the distance at which we swap. Each tyre spends $s$ km in one position and $d - s$ km in the other. For a tyre that starts in front and moves to rear, its total wear is: $$\frac{s}{x} + \frac{d - s}{y} = 1$$ For a tyre that starts in rear and moves to front: $$\frac{s}{y} + \frac{d - s}{x} = 1$$ By symmetry, both equations give the same constraint. Adding them: $$\frac{s}{x} + \frac{d-s}{y} + \frac{s}{y} + \frac{d-s}{x} = 2$$ $$d\left(\frac{1}{x} + \frac{1}{y}\right) = 2$$ $$d = \frac{2xy}{x + y}$$ The optimal swap point is $s = d/2 = \frac{xy}{x+y}$ km. **Answer:** $$d = \frac{2xy}{x + y}$$ Swap at $s = \frac{xy}{x + y}$ km. The maximum distance is the harmonic mean of $x$ and $y$. --- 24 Game Solver [medium, Coding] --- https://quantvault.org/problem-666-24-game-solver.html Q: Given four numbers (integers or fractions), determine whether you can combine them using the four basic operations ($+$, $-$, $\times$, $\div$) and parentheses to obtain exactly $24$. Each number must be used exactly once. For example, given $[1, 2, 3, 4]$, one valid expression is $1 \times 2 \times 3 \times 4 = 24$. Write a function that takes a list of four numbers and returns `True` if 24 can be made, `False` otherwise. **Constraints:** - Input is a list of exactly 4 positive numbers - You may use $+$, $-$, $\times$, $\div$ (real-valued division) - Parentheses can be placed anywhere - Each number is used exactly once - Floating-point division is inexact: in Python `8/(3-8/3)` evaluates to `23.99999999999999`, so compare the result to $24$ with a tolerance (e.g. `abs(x - 24) < 1e-9`) or use exact `fractions.Fraction` arithmetic. A check like `x == 24` fails on cases such as $[3, 3, 8, 8]$. **Examples:** - Input: `[1, 2, 3, 4]` -- Output: `True` (e.g., $1 \times 2 \times 3 \times 4$) - Input: `[8, 3, 8, 3]` -- Output: `True` (e.g., $8 / (3 - 8/3) = 24$) - Input: `[1, 1, 1, 1]` -- Output: `False` A: **How to Think About It:** With only 4 numbers, the total search space is small enough for brute force. At each step you pick two numbers, combine them with one of 4 operations, and replace them with the result -- reducing the list by one. You recurse until one number remains and check if it equals 24. The key insight is that this naturally enumerates all possible expression trees (all ways to parenthesize), not just left-to-right evaluation. **Algorithm:** 1. If the list has one number, check if it equals 24 (within floating-point tolerance). 2. Otherwise, pick every ordered pair $(i, j)$ of distinct indices. 3. For each pair, try all 4 operations on `nums[i] op nums[j]`. 4. Build a new list with the remaining numbers plus the result, and recurse. 5. Return `True` if any branch succeeds. Note: the tolerance check in step 1 is required. Float division makes $8/(3 - 8/3)$ come out as $23.99999999999999$, so an exact `== 24` test wrongly returns `False` for $[8, 3, 8, 3]$ and $[3, 3, 8, 8]$. Using `fractions.Fraction` for every value avoids the issue entirely. Note: we use ordered pairs (not just combinations) because subtraction and division are not commutative -- $a - b \neq b - a$. **Code:** ```python def game24(nums): if len(nums) == 1: return abs(nums[0] - 24) < 1e-9 for i in range(len(nums)): for j in range(len(nums)): if i == j: continue remaining = [nums[k] for k in range(len(nums)) if k != i and k != j] for op in ['+', '-', '*', '/']: if op == '+': val = nums[i] + nums[j] elif op == '-': val = nums[i] - nums[j] elif op == '*': val = nums[i] * nums[j] elif op == '/': if abs(nums[j]) < 1e-9: continue val = nums[i] / nums[j] if game24(remaining + [val]): return True return False ``` **Why this covers all expression trees:** With 4 numbers, there are 5 distinct binary tree shapes (Catalan number $C_3 = 5$). By choosing any two numbers at each step (not just adjacent ones), we implicitly try every tree structure. For example, computing $(a + b)$ then combining with $(c \times d)$ corresponds to a balanced tree, while computing $(a + b)$, then $((a+b) + c)$, then $(((a+b)+c) \times d)$ corresponds to a left-skewed tree. **Complexity:** For 4 numbers, the branching is $4 \times 3 \times 4 = 48$ at the first level, $3 \times 2 \times 4 = 24$ at the second, $2 \times 1 \times 4 = 8$ at the third. Total: $48 \times 24 \times 8 = 9{,}216$ leaf evaluations -- trivially fast. For $n$ numbers: $O(n!^2 \times 4^{n-1})$, but since $n$ is fixed at 4, this is $O(1)$. **Answer:** Recursively pick any two numbers, apply each operation, and recurse on the reduced list. The brute-force search runs in $O(1)$ for fixed input size of 4. --- Closest Median Profit Between Traders [medium, Coding] --- https://quantvault.org/problem-686-closest-median-profit-between-traders.html Q: You are evaluating traders at your firm during a performance review. You want to find the two traders whose median profit per trade is closest to each other. Write a SQL query that returns the pair of traders with the smallest absolute difference in median profit per trade. Only include traders who made at least one trade. Output columns: `trader1_name`, `trader2_name`, `median_profit_diff`. **Tables:** `traders` | Column | Type | |--------|---------| | id | INTEGER | | name | VARCHAR | `trades` | Column | Type | |-----------|---------| | id | INTEGER | | trader_id | INTEGER | | profit | FLOAT | **Example output:** | trader1_name | trader2_name | median_profit_diff | |--------------|--------------|--------------------| | James | Kendrick | 17 | A: **How to Think About It:** This is a three-step SQL problem: (1) compute the median profit per trader, (2) generate all pairs of traders, (3) find the pair with the smallest difference. The main subtlety is computing the median in SQL, which is not a built-in aggregate in many dialects. We use `PERCENTILE_CONT(0.5)` which is available in PostgreSQL and most modern SQL engines. **Algorithm:** 1. **CTE -- median per trader:** Join `traders` to `trades`, group by trader, and compute the median using `PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY profit)`. The `INNER JOIN` naturally filters to traders with at least one trade. 2. **Cross join for pairs:** Self-join the CTE on itself with the condition `t1.id < t2.id` to avoid duplicate pairs and self-pairs. 3. **Find minimum difference:** Compute `ABS(t1.median_profit - t2.median_profit)`, order ascending, and take the top row. **Code:** ```sql WITH median_profit_per_trader AS ( SELECT t.id, t.name, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY tr.profit) AS median_profit FROM traders t INNER JOIN trades tr ON t.id = tr.trader_id GROUP BY t.id, t.name ) SELECT a.name AS trader1_name, b.name AS trader2_name, ABS(a.median_profit - b.median_profit) AS median_profit_diff FROM median_profit_per_trader a CROSS JOIN median_profit_per_trader b WHERE a.id < b.id ORDER BY median_profit_diff ASC, a.name ASC, b.name ASC LIMIT 1; ``` **Complexity:** If there are $k$ traders with trades, the CTE runs in $O(k \cdot m \log m)$ where $m$ is the average number of trades per trader (for the sort inside `PERCENTILE_CONT`). The cross join produces $O(k^2)$ pairs. For most firm sizes this is fine. **Answer:** Use a CTE to compute median profit per trader via `PERCENTILE_CONT`, then cross join to find all pairs, and select the pair with the smallest absolute difference in median profit. --- Conditional Expectation of Brownian Motion Given Its Absolute Value [medium, Expectation] --- https://quantvault.org/problem-694-conditional-expectation-of-brownian-motion-given-its-absolut.html Q: Let $W_t$ be a standard Brownian motion. For $0 \leq s \leq t$, compute: $$E[W_s \mid |W_t|]$$ What is the numerical answer when $s = 1$, $t = 3$, and $|W_t| = 6$? A: **How to Think About It:** We condition on the *magnitude* $|W_t|$ but not the *sign* of $W_t$ — strictly weaker than conditioning on $W_t$ itself. If we knew $W_t = +6$, the Brownian bridge gives $E[W_s\mid W_t] = \frac{s}{t}W_t$; if $W_t=-6$ we'd get the negative. Knowing only $|W_t|=6$ leaves the sign a fair coin, and the two signed answers are exact negatives. The heuristic is **symmetry / odd-function cancellation**: $W_s$ is an odd functional of the path, we've thrown away the only information that could break the $\pm$ symmetry, so the conditional mean must be $0$ — independent of $s$, $t$, and the magnitude. **Quick Estimate (sanity check).** No number to estimate — the answer is exactly $0$ — so probe the structure instead. Under $W_t = c$, the bridge mean of $W_s$ is $\frac{s}{t}c$, *linear and odd in $c$*. Given $|W_t|=6$, the sign is $\pm$ with probability $\tfrac12$ each (Brownian motion is symmetric about $0$). Average: $\tfrac12\cdot\frac{s}{t}(+6) + \tfrac12\cdot\frac{s}{t}(-6) = 0$. Plug the specific numbers ($s=1$, $t=3$): $\tfrac12\cdot\tfrac13(6) + \tfrac12\cdot\tfrac13(-6) = 1 - 1 = 0$. The two signed drifts ($+2$ and $-2$) cancel exactly. **Formal Solution:** Condition on the sign of $W_t$ (Law of Total Expectation): $$E[W_s\mid |W_t|] = E[W_s\mid |W_t|, W_t>0]\,P(W_t>0) + E[W_s\mid |W_t|, W_t\le 0]\,P(W_t\le 0).$$ Since $W_t\sim N(0,t)$, symmetry gives $P(W_t>0)=P(W_t\le 0)=\tfrac12$. The Brownian bridge mean $E[W_s\mid W_t=c]=\frac{s}{t}c$ gives, with $v=|W_t|\ge 0$: $$E[W_s\mid |W_t|=v, W_t>0] = \frac{s}{t}v, \qquad E[W_s\mid |W_t|=v, W_t\le 0] = -\frac{s}{t}v.$$ Therefore $$E[W_s\mid |W_t|=v] = \tfrac12\cdot\frac{s}{t}v + \tfrac12\cdot\left(-\frac{s}{t}v\right) = \boxed{0},$$ for every $s\le t$ and every $v\ge 0$. At $s=1$, $t=3$, $|W_t|=6$: the answer is $0$. **Answer:** $E[W_1 \mid |W_3|=6] = \mathbf{0}$ (and $E[W_s\mid|W_t|]=0$ generally). --- Convex Quadrilateral from Random Chords [medium, Probability] --- https://quantvault.org/problem-699-convex-quadrilateral-from-random-chords.html Q: Place $10$ points on a circle and draw all $\binom{10}{2} = 45$ chords between them. You then pick $4$ of these chords uniformly at random. What is the probability that the $4$ selected chords form a convex quadrilateral? (A convex quadrilateral is a four-sided polygon in which every interior angle is less than $180^\circ$.) A: **How to Think About It:** The question is really asking: out of all the ways to grab 4 chords from 45, how many of those selections happen to trace out a convex quadrilateral? The key geometric fact is that any 4 points on a circle determine exactly one convex quadrilateral -- you just connect consecutive points in cyclic order. Any other way of pairing those 4 points into edges would produce a self-intersecting figure, not a convex polygon. So the numerator is just the number of ways to choose 4 points, and the denominator is the number of ways to choose 4 chords. **Quick Estimate:** There are $\binom{45}{4} \approx 150{,}000$ ways to pick 4 chords, but only $\binom{10}{4} = 210$ ways to pick 4 points. So even before computing we know the probability is roughly $210 / 150{,}000 \approx 0.0014$ -- a bit more than 1 in 700. Not surprising: grabbing 4 random chords and having them form a nice closed polygon is a very specific event. **Approach:** Count favorable outcomes (4-chord sets forming a convex quadrilateral) and divide by total outcomes (all 4-chord subsets). **Formal Solution:** *Denominator.* There are $\binom{10}{2} = 45$ chords. The number of ways to choose 4 of them is $$\binom{45}{4} = \frac{45 \times 44 \times 43 \times 42}{4!} = 148{,}995.$$ *Numerator.* A convex quadrilateral inscribed in the circle has 4 vertices, all lying on the circle, connected by 4 edges in cyclic order. Given any 4 of the 10 points, there is exactly one way to connect them as a convex quadrilateral: join consecutive points around the circle. (Any other pairing of the 4 points into 4 edges produces a self-intersecting figure.) So the number of favorable 4-chord selections is exactly $$\binom{10}{4} = 210.$$ *Probability.* Dividing and simplifying (both are divisible by 105): $$P = \frac{210}{148{,}995} = \frac{2}{1419}.$$ **Answer:** The probability is $\dfrac{2}{1419} \approx 0.00141$. --- Correlation Under Linear Transformations [easy, Random Variables] --- https://quantvault.org/problem-701-correlation-under-linear-transformations.html Q: The correlation between random variables $X$ and $Y$ is $0.56$. What is the correlation between $8X$ and $Y + 7$? A: **How to Think About It:** Correlation measures the linear relationship between two variables after stripping out their individual scales. It is defined as covariance divided by the product of standard deviations -- and that ratio is invariant to rescaling or shifting either variable. Multiplying $X$ by 8 scales both the covariance and the standard deviation of $X$ by 8, so they cancel. Adding 7 to $Y$ shifts the mean but does not change how $Y$ moves around its mean, so covariance is unaffected and the standard deviation of $Y$ is unchanged. **Quick Estimate:** The answer is obviously still 0.56. The transformation $Z = 8X$ is just a change of units (like converting meters to centimeters). The transformation $W = Y + 7$ is just a level shift. Neither changes how tightly $Z$ and $W$ move together. **Formal Solution:** Recall the definition of correlation: $$\rho(U, V) = \frac{\text{Cov}(U, V)}{\text{SD}(U) \cdot \text{SD}(V)}$$ Compute each piece for $U = 8X$ and $V = Y + 7$: - $\text{Cov}(8X, Y + 7) = 8 \cdot \text{Cov}(X, Y + 7) = 8 \cdot \text{Cov}(X, Y)$ (covariance is linear in each argument; additive constants drop out) - $\text{SD}(8X) = 8 \cdot \text{SD}(X)$ (scaling by a positive constant scales the standard deviation by the same factor) - $\text{SD}(Y + 7) = \text{SD}(Y)$ (adding a constant does not change the spread) Therefore: $$\rho(8X, Y + 7) = \frac{8 \cdot \text{Cov}(X, Y)}{8 \cdot \text{SD}(X) \cdot \text{SD}(Y)} = \frac{\text{Cov}(X, Y)}{\text{SD}(X) \cdot \text{SD}(Y)} = \rho(X, Y)$$ **Answer:** $\rho(8X, Y + 7) = 0.56$. --- Eigenvalues of a Rank-1 Matrix [easy, Linear Algebra] --- https://quantvault.org/problem-713-eigenvalues-of-a-rank-1-matrix.html Q: Let $A = \mathbf{u}\mathbf{v}^T$ be the outer product of two vectors $\mathbf{u}, \mathbf{v} \in \mathbb{R}^n$. What are all the eigenvalues of $A$, and what are their corresponding eigenvectors? Justify your answer. A: **How to Think About It:** $A = \mathbf{u}\mathbf{v}^T$ is a rank-1 matrix -- it collapses all of $\mathbb{R}^n$ onto a one-dimensional subspace spanned by $\mathbf{u}$. That geometric fact immediately tells you almost everything: rank 1 means the null space has dimension $n-1$, so $\lambda = 0$ must be an eigenvalue with multiplicity $n-1$. The only remaining eigenvalue is pinned down by the trace. Start there and the problem solves itself. **Quick Estimate:** For a concrete check, try $n = 2$, $\mathbf{u} = (1, 2)^T$, $\mathbf{v} = (3, 1)^T$. Then $A = \begin{pmatrix} 3 & 1 \\ 6 & 2 \end{pmatrix}$. The trace is $5 = \mathbf{v}^T \mathbf{u} = 3 + 2 = 5$. The determinant is $0$ (rank 1). So eigenvalues are $5$ and $0$. That matches $\lambda_1 = \mathbf{v}^T \mathbf{u}$ and $\lambda_2 = 0$. **Approach:** Use rank to count zero eigenvalues, then find the nonzero one via direct computation of $A\mathbf{u}$. **Formal Solution:** **Step 1 -- The zero eigenvalues.** Since $A = \mathbf{u}\mathbf{v}^T$ has rank at most 1, its null space has dimension at least $n-1$. Any vector $\mathbf{x}$ orthogonal to $\mathbf{v}$ satisfies $A\mathbf{x} = \mathbf{u}(\mathbf{v}^T \mathbf{x}) = \mathbf{u} \cdot 0 = \mathbf{0}$. So $\lambda = 0$ is an eigenvalue with multiplicity $n-1$, and the corresponding eigenspace is $\{\mathbf{x} : \mathbf{v}^T \mathbf{x} = 0\}$. **Step 2 -- The nonzero eigenvalue.** Try $\mathbf{u}$ as an eigenvector: $$A\mathbf{u} = (\mathbf{u}\mathbf{v}^T)\mathbf{u} = \mathbf{u}(\mathbf{v}^T \mathbf{u})$$ Since $\mathbf{v}^T \mathbf{u}$ is a scalar, this gives $A\mathbf{u} = (\mathbf{v}^T \mathbf{u})\,\mathbf{u}$. So $\mathbf{u}$ is an eigenvector with eigenvalue $\lambda_1 = \mathbf{v}^T \mathbf{u}$. **Step 3 -- Sanity check via trace.** The trace equals the sum of eigenvalues: $$\text{tr}(A) = \sum_i (\mathbf{u}\mathbf{v}^T)_{ii} = \sum_i u_i v_i = \mathbf{v}^T \mathbf{u} = \lambda_1 + 0 + \cdots + 0 \checkmark$$ **Answer:** - One nonzero eigenvalue $\lambda_1 = \mathbf{v}^T \mathbf{u}$ with eigenvector $\mathbf{u}$ (assuming $\mathbf{u} \neq \mathbf{0}$ and $\mathbf{v}^T \mathbf{u} \neq 0$) - $n-1$ zero eigenvalues with eigenspace $\ker(\mathbf{v}^T) = \{\mathbf{x} : \mathbf{v}^T \mathbf{x} = 0\}$ If $\mathbf{v}^T \mathbf{u} = 0$, then $A$ is nilpotent and all eigenvalues are zero. --- Expected Adjacent Boy-Girl Pairs in a Random Line [medium, Expectation] --- https://quantvault.org/problem-720-expected-adjacent-boy-girl-pairs-in-a-random-line.html Q: $b$ boys and $g$ girls stand in a line of length $b + g$, arranged uniformly at random. A "boy-girl position" is any pair of consecutive spots in the line where one person is a boy and the other is a girl. Find the expected number of boy-girl positions. As a concrete example, the arrangement $BGGBBGB$ (with $b = 4$, $g = 3$) has 4 boy-girl positions: $(B,G)$ at spots 1-2, $(G,B)$ at spots 3-4, $(B,G)$ at spots 5-6, and $(G,B)$ at spots 6-7. Evaluate your answer for $b = 10$ and $g = 15$. A: **How to Think About It:** Whenever you need the expected count of events that are not independent, reach for indicator random variables and linearity of expectation. Here, define an indicator for each consecutive pair of positions and ask: does that pair contain one boy and one girl? The beauty of linearity of expectation is that you never need to worry about dependence between different pairs -- you just compute the probability for a single pair and multiply by the number of pairs. **Quick Estimate:** There are $b + g - 1 = 24$ consecutive pairs. For any single pair, the chance of getting one boy and one girl is roughly $2 \times \frac{b}{b+g} \times \frac{g}{b+g} = 2 \times \frac{10}{25} \times \frac{15}{25} = 0.48$. Multiply: $24 \times 0.48 = 11.52$. So the answer should be around 12. (This slightly underestimates because the exact calculation uses sampling without replacement, but for $n = 25$ the correction is small.) **Approach:** Define indicator variables $I_i$ for each consecutive pair $(i, i+1)$, compute $P(I_i = 1)$ exactly using combinatorics, and apply linearity of expectation. **Formal Solution:** Let $I_i$ be the indicator that positions $i$ and $i+1$ contain one boy and one girl, for $i = 1, 2, \ldots, b+g-1$. The total count of boy-girl positions is: $$T = \sum_{i=1}^{b+g-1} I_i$$ Since the line is a uniformly random permutation, every pair of positions is exchangeable. So $E[I_i] = E[I_1]$ for all $i$, and by linearity of expectation: $$E[T] = (b+g-1) \cdot E[I_1]$$ Now compute $E[I_1] = P(\text{positions 1 and 2 have different genders})$. The total number of ordered ways to fill two spots from $b+g$ people is $(b+g)(b+g-1)$. The number of ways to place one boy and one girl in those two spots is $2bg$ (choose which spot the boy occupies, then pick a boy and a girl). So: $$E[I_1] = \frac{2bg}{(b+g)(b+g-1)}$$ Plugging in: $$E[T] = (b+g-1) \cdot \frac{2bg}{(b+g)(b+g-1)} = \frac{2bg}{b+g}$$ The $(b+g-1)$ terms cancel cleanly, giving a remarkably simple formula. **Answer:** The expected number of adjacent boy-girl positions is $\frac{2bg}{b+g}$. For $b = 10$, $g = 15$: $\frac{2 \cdot 10 \cdot 15}{25} = \frac{300}{25} = 12$. --- Expected Length of a Zigzag Prefix [hard, Expectation] --- https://quantvault.org/problem-738-expected-length-of-a-zigzag-prefix.html Q: Numbers $U_1, U_2, U_3, \ldots$ are drawn i.i.d. from $\text{Unif}(0,1)$. Starting from $U_1$, you keep going as long as the sequence alternates in strict inequalities: $$U_1 > U_2 U_4 \cdots$$ The moment an inequality is violated, you stop. Let $L$ be the length of this maximal alternating prefix. 1. Set up a Markov chain or system of integral equations that characterizes $E[L]$. 2. Solve the system and evaluate $E[L]$ to at least three decimal places. A: **How to Think About It:** After drawing $U_1 = x$, you need $U_2 U_2$ (go up), then $U_4 U_2) = 1/2$). A third requires $U_1 > U_2 1 + 0.5 + 0.33 = 1.83$, and these tail probabilities are shrinking, so $E[L]$ should be somewhere around $2.3$-$2.5$. **Quick Estimate:** The probability that a prefix of length $n$ is zigzag equals $T_n / n!$, where $T_n$ is the $n$-th zigzag (or tangent/secant) number. The first few: $T_1 = 1, T_2 = 1, T_3 = 2, T_4 = 5, T_5 = 16, T_6 = 61$. Summing $P(L \geq n)$ for $n = 1, \ldots, 6$: $1 + 1/2 + 1/3 + 5/24 + 16/120 + 61/720 \approx 1 + 0.5 + 0.333 + 0.208 + 0.133 + 0.085 = 2.260$. The tail is still nontrivial, so the final answer should be around $2.4$. **Approach:** Define two functions on $[0,1]$: $D(x)$ is the expected number of additional elements appended starting from value $x$ when the next step must go down, and $U(x)$ is the same when the next step must go up. Set up coupled integral equations, convert to an ODE, and solve in closed form. **Formal Solution:** From the Markov structure: $$D(x) = \int_0^x \bigl(1 + U(y)\bigr)\,dy, \qquad U(x) = \int_x^1 \bigl(1 + D(y)\bigr)\,dy$$ The logic: if you are at value $x$ and need to go down, the next draw $y$ must land in $[0, x)$. Each such $y$ contributes 1 to the length and transitions to "up from $y$." If $y \geq x$, the zigzag breaks and you add 0. Differentiate both equations: $$D'(x) = 1 + U(x), \qquad U'(x) = -\bigl(1 + D(x)\bigr)$$ Differentiate $D'$ again and substitute: $$D''(x) = U'(x) = -1 - D(x)$$ $$D''(x) + D(x) = -1$$ This is a second-order linear ODE with constant coefficients. The general solution is: $$D(x) = A\cos x + B\sin x - 1$$ Boundary conditions: - $D(0) = 0$ (from the integral definition): $A - 1 = 0$, so $A = 1$. - $U(1) = 0$ (from the integral definition, since $\int_1^1 = 0$). Using $U(x) = D'(x) - 1 = -\sin x + B\cos x - 1$, we get $U(1) = -\sin 1 + B\cos 1 - 1 = 0$, so: $$B = \frac{1 + \sin 1}{\cos 1}$$ The full solutions are: $$D(x) = \cos x + \frac{1 + \sin 1}{\cos 1}\,\sin x - 1$$ $$U(x) = -\sin x + \frac{1 + \sin 1}{\cos 1}\,\cos x - 1$$ Since $U_1$ is uniform on $[0,1]$ and always counts (length at least 1), and the first required direction is "down": $$E[L] = 1 + \int_0^1 D(x)\,dx$$ Evaluating: $$\int_0^1 D(x)\,dx = \bigl[\sin x - B\cos x - x\bigr]_0^1 = \sin 1 - B\cos 1 - 1 + B$$ $$= \sin 1 + B(1 - \cos 1) - 1 = \sin 1 + (1 + \sin 1)\,\frac{1 - \cos 1}{\cos 1} - 1$$ Simplifying: $$= \frac{\sin 1\cos 1 + (1 + \sin 1)(1 - \cos 1) - \cos 1}{\cos 1} = \frac{1 + \sin 1 - \cos 1}{\cos 1} - 1$$ So: $$E[L] = 1 + \frac{1 + \sin 1 - \cos 1}{\cos 1} - 1 = \frac{1 + \sin 1 - \cos 1}{\cos 1}$$ $$= \sec 1 + \tan 1 - 1$$ **Verification via alternating permutations:** $P(L \geq n) = T_n/n!$ where $T_n$ are the zigzag numbers, and the EGF $\sum T_n x^n/n! = \sec x + \tan x$. Evaluating at $x = 1$ and subtracting $T_0/0! = 1$ gives $E[L] = \sec 1 + \tan 1 - 1$, confirming the integral equation result. **Answer:** $$E[L] = \sec 1 + \tan 1 - 1 \approx 2.408$$ Numerically: $\sec 1 \approx 1.8508$, $\tan 1 \approx 1.5574$, so $E[L] \approx 2.408$. --- Expected Positions Exceeding Values in a Random Permutation [easy, Expectation] --- https://quantvault.org/problem-753-expected-positions-exceeding-values-in-a-random-permutation.html Q: A permutation $\sigma$ of $\{1, 2, \dots, n\}$ is chosen uniformly at random. An index $i$ is called a "winner" if $\sigma(i) > i$ -- that is, the value at position $i$ is strictly larger than $i$ itself. What is the expected number of winners? Give a general formula in terms of $n$, then evaluate it for $n = 25$. A: **How to Think About It:** You have a random permutation and you want to count how many positions end up with a value bigger than their index. The events "position $i$ is a winner" are dependent -- if $\sigma(1) = 5$, that changes what is available for position 5. But the beauty of linearity of expectation is that you do not need independence. Just compute $P(\sigma(i) > i)$ for each $i$ separately and add them up. Quick gut check: by symmetry, for each position $i$, the value $\sigma(i)$ is equally likely to be any of the $n$ values. So $P(\sigma(i) > i) = (n - i)/n$. Sum that over $i = 1, \dots, n$ and you get something close to $n/2$. **Quick Estimate:** For $n = 25$, the expected count is $\sum_{i=1}^{25} (25 - i)/25 = (24 + 23 + \cdots + 0)/25 = 300/25 = 12$. Done. **Approach:** Indicator random variables plus linearity of expectation. **Formal Solution:** Define $X_i = \mathbf{1}\{\sigma(i) > i\}$ for $i = 1, \dots, n$. The total count of winners is $T = \sum_{i=1}^n X_i$. By linearity of expectation: $$E[T] = \sum_{i=1}^n E[X_i] = \sum_{i=1}^n P(\sigma(i) > i)$$ Since $\sigma$ is a uniform random permutation, $\sigma(i)$ is equally likely to be any element of $\{1, \dots, n\}$. The number of values strictly greater than $i$ is $n - i$, so: $$P(\sigma(i) > i) = \frac{n - i}{n}$$ Summing: $$E[T] = \sum_{i=1}^n \frac{n - i}{n} = \frac{1}{n} \sum_{j=0}^{n-1} j = \frac{1}{n} \cdot \frac{(n-1)n}{2} = \frac{n-1}{2}$$ **Answer:** The expected number of winners is $(n-1)/2$. For $n = 25$, this equals $\boxed{12}$. --- Expected Runs in a Card Deck [medium, Expectation] --- https://quantvault.org/problem-763-expected-runs-in-a-card-deck.html Q: A standard 52-card deck has 26 red cards and 26 black cards. You shuffle the deck thoroughly and lay the cards out in a row. A "run" is a maximal consecutive sequence of cards of the same color -- for example, if the first four cards are Red, Red, Red, Black, that is two runs (one of length 3 and one of length 1 so far). What is the expected number of runs in the full 52-card sequence? A: **How to Think About It:** Don't try to enumerate the possible run-length patterns -- that is a combinatorial nightmare. Instead, think about it locally: a new run starts at position 1 (always) and at every subsequent position where the color changes. So the number of runs equals 1 plus the number of color changes. That immediately converts a global counting problem into a sum of local indicators, and linearity of expectation does the rest. **Quick Estimate:** There are 51 adjacent pairs. At each pair, roughly half the time the colors differ (since the deck is evenly split). So you expect about $51 \times 0.5 \approx 25$-$26$ color changes, giving roughly 26-27 runs. That is our target. **Approach:** Define indicator variables for color changes at each position and use linearity of expectation. **Formal Solution:** Define $X_i = 1$ if card $i$ differs in color from card $i-1$, for $i = 2, 3, \ldots, 52$. Then: $$\text{Runs} = 1 + \sum_{i=2}^{52} X_i$$ By linearity of expectation: $$E[\text{Runs}] = 1 + \sum_{i=2}^{52} P(X_i = 1)$$ Now compute $P(X_i = 1)$. By symmetry, every card is equally likely to be any of the 52 cards, and the pair at positions $(i-1, i)$ is a uniformly random ordered pair drawn without replacement. If card $i-1$ is red (which happens with probability $26/52$), then card $i$ is drawn from the remaining 51 cards, of which 26 are black. If card $i-1$ is black, the same argument gives $P(\text{card } i \text{ is red}) = 26/51$. Either way: $$P(X_i = 1) = \frac{26}{51}$$ Since this probability is the same for every $i$: $$E[\text{Runs}] = 1 + 51 \cdot \frac{26}{51} = 1 + 26 = 27$$ **Generalization:** With $r$ red and $b$ black cards in a deck of $n = r + b$, the probability that any adjacent pair differs is $\frac{2rb}{n(n-1)}$, so: $$E[\text{Runs}] = 1 + \frac{2rb}{n}$$ For $r = b = 26$: $1 + \frac{2 \cdot 26 \cdot 26}{52} = 1 + 26 = 27$. **Answer:** The expected number of runs is $\boxed{27}$. --- Expected Steps to Remove a Random Subtree Tree [medium, Expectation] --- https://quantvault.org/problem-765-expected-steps-to-remove-a-random-subtree-tree.html Q: You are given a rooted tree with $n$ nodes. At each step, you pick one of the remaining non-root nodes uniformly at random and remove it along with its entire subtree. You repeat this process until only the root remains. Find the expected number of steps to clear the tree. A: **How to Think About It:** The naive approach -- trying to track what the tree looks like after each removal -- is a mess because the state space is enormous. The right move is to step back and think about each node's fate independently. For a given non-root node $i$, ask: what is the probability that $i$ itself is directly chosen (as opposed to being swept away when one of its ancestors is chosen)? That probability is all you need, because by linearity of expectation the total expected steps is just the sum of these probabilities over all non-root nodes. **Quick Estimate:** Consider a simple path graph: root -> A -> B -> C (so 4 nodes, depths 1, 2, 3, 4). Node A has depth 2 (root + A on its path), so $P(A \text{ chosen}) = 1/2$. Node B has depth 3, $P = 1/3$. Node C has depth 4, $P = 1/4$. Expected steps $= 1/2 + 1/3 + 1/4 = 6/12 + 4/12 + 3/12 = 13/12 \approx 1.08$. This makes intuitive sense -- on a long chain you usually just pick the node near the root and wipe out everyone below in one shot. For a balanced binary tree of depth 3 (root + 2 children + 4 grandchildren = 7 nodes), depth-2 nodes contribute $2 \times 1/2 = 1$ and depth-3 nodes contribute $4 \times 1/3 \approx 1.33$. Expected steps $\approx 1 + 1.33 = 2.33$. **Approach:** Linearity of expectation plus a symmetry argument to compute each node's selection probability. **Formal Solution:** Define depth so that the root has depth 0 and its children have depth 1. For any non-root node $i$, let $S_i = \{i, \text{parent}(i), \text{grandparent}(i), \ldots, \text{child-of-root}(i)\}$ be the set consisting of $i$ and all of its proper ancestors excluding the root (since the root is never chosen). Then $|S_i| = \text{depth}(i)$. Node $i$ is directly chosen (contributes a step) if and only if $i$ is the first member of $S_i$ to be selected across the entire process. By the symmetry of uniform sampling, each member of $S_i$ is equally likely to be the first one selected. Therefore: $$P(i \text{ is directly chosen}) = \frac{1}{|S_i|} = \frac{1}{\text{depth}(i)}$$ where depth is measured with root at depth 0, so $\text{depth}(i) \geq 1$ for all non-root nodes. Define indicator $X_i = 1$ if node $i$ is directly chosen. The total number of steps is $X = \sum_{i \,:\, i \neq \text{root}} X_i$. By linearity of expectation: $$E[X] = \sum_{i \,:\, i \neq \text{root}} E[X_i] = \sum_{i \,:\, i \neq \text{root}} \frac{1}{\text{depth}(i)}$$ **Justifying the symmetry argument:** Why is the "first selected" uniform? At each step we draw uniformly among all remaining nodes. The relative order in which nodes in $S_i$ are reached (ignoring steps that touch neither) is a uniformly random permutation of $S_i$. This is a standard result: for any finite set of items drawn uniformly without replacement (or equivalently, a random permutation), each element is equally likely to appear first. **Answer:** $$\boxed{E[\text{steps}] = \sum_{i \,:\, i \neq \text{root}} \frac{1}{\text{depth}(i)}}$$ where $\text{depth}(i)$ counts the number of non-root ancestors of $i$ plus $i$ itself (equivalently, depth of root $= 0$, depth of root's children $= 1$, etc.). --- Fair Dice Comparison: d20 vs. 3d6 [medium, Probability] --- https://quantvault.org/problem-777-fair-dice-comparison-d20-vs-3d6.html Q: Two players each roll dice independently and repeatedly. Player A rolls a single fair 20-sided die (faces 1 through 20). Player B rolls three fair 6-sided dice and takes their sum. Each round, both players roll simultaneously. If their results are equal, they roll again. The game ends the first time the results differ, and whichever player has the higher number wins. What is the probability that Player A wins? A: **How to Think About It:** At first glance this looks messy -- you would need the full PMF of the sum of three d6s, which ranges from 3 to 18. But the question is really asking about a comparison game conditioned on disagreement. The key observation is a symmetry property of the 3d6 distribution: the PMF is symmetric around 10.5, meaning $P(Y = k) = P(Y = 21 - k)$ for all $k$. If you can prove that, the rest falls out cleanly. **Quick Estimate:** Player A's die covers 1-20 uniformly, while Player B's sum covers 3-18. Player A auto-loses on rolls of 1 or 2 (B can never roll that low), but auto-wins on 19 or 20 (B can never roll that high). That is 2 values each way out of 20, so those cancel. The interior overlap region (3-18) has the symmetry of the 3d6 sum, which suggests the game might be perfectly fair. Gut estimate: $P(A \text{ wins}) = 1/2$. **Approach:** Prove the PMF symmetry of 3d6, then use the law of total probability conditioning on Player A's roll. **Formal Solution:** Let $X$ be Player A's roll (uniform on $\{1, \ldots, 20\}$) and $Y$ be Player B's sum of three d6s. Since the game continues until $X \neq Y$, we need $P(X > Y \mid X \neq Y)$. By symmetry of the stopping rule, this equals $P(X > Y) / P(X \neq Y)$, and similarly for $P(X Y) = P(X Y) = \sum_{k=1}^{20} P(X > Y \mid X = k) \cdot P(X = k) = \frac{1}{20} \sum_{k=1}^{20} P(Y 21 - k)$. Substituting and re-indexing ($k \to 21 - k$): $$P(X > Y) = \frac{1}{20} \sum_{k=1}^{20} P(Y > 21 - k) = \frac{1}{20} \sum_{k=1}^{20} P(Y > k) = P(X Y) = P(X Y)}{P(X > Y) + P(X < Y)} = \frac{1}{2}.$$ **Answer:** The probability that Player A wins is $\dfrac{1}{2}$. The game is perfectly fair. --- Hitting a Fixed Target with a Growing Die [hard, Probability] --- https://quantvault.org/problem-786-hitting-a-fixed-target-with-a-growing-die.html Q: You roll a fair $n$-sided die (faces $1, 2, \ldots, n$) infinitely many times and keep a running total of all the rolls. Let $f(n,k)$ be the probability that this running total is ever exactly equal to $k$, for a fixed integer $k \geq 1$. For a fixed value of $k$, compute $$\lim_{n \to \infty} f(n,k).$$ Report your answer for $k = 6$. A: **How to Think About It:** Picture the situation: you are rolling a die with $n$ sides, and you want the cumulative sum to land exactly on a target $k$. The minimum roll is $1$ and the maximum is $n$. As $n$ grows, the expected value of a single roll is $(n+1)/2$, which diverges. So for large $n$, the very first roll almost certainly overshoots the fixed target $k$. Once you have overshot, the sum only increases, so you can never come back. The only way to ever hit $k$ is to get there through a sequence of small rolls, each at most $k$, and each of those small rolls has probability at most $1/n$ -- vanishingly small. **Quick Estimate:** The probability of rolling any value $\leq k$ on a single roll is $k/n$. You need at least one such small roll just to have a chance. For $k = 6$ and large $n$, the probability of the first roll being $\leq 6$ is $6/n$, which is tiny. Even if the first roll is small, subsequent rolls face the same problem. So we expect $f(n, 6) \approx 1/n \to 0$. More precisely, we can track the leading-order term. The dominant way to hit $k$ is to roll exactly $k$ on the first roll, which happens with probability $1/n$. Any multi-roll path to $k$ requires at least two small rolls, contributing terms of order $1/n^2$ or smaller. So $f(n,k) \sim 1/n$ for every fixed $k$. **Approach:** Write a recursion for $f(n,k)$ by conditioning on the first roll, then take the limit. **Formal Solution:** Condition on the outcome of the first roll. If you roll $j$: - If $j = k$: you have hit the target. This contributes probability $1/n$. - If $j k$: you have overshot $k$, and since rolls are positive, the sum only increases. You can never reach $k$ exactly. Contributes $0$. This gives the recursion: $$f(n, k) = \frac{1}{n}\left(1 + \sum_{j=1}^{k-1} f(n, k - j)\right).$$ Now take $n \to \infty$. For $k = 1$: $$f(n, 1) = \frac{1}{n} \to 0.$$ For $k = 2$: $$f(n, 2) = \frac{1}{n}\bigl(1 + f(n, 1)\bigr) = \frac{1}{n} + \frac{1}{n^2} \to 0.$$ By induction, suppose $f(n, j) \to 0$ for all $j < k$. Then $$f(n, k) = \frac{1}{n}\left(1 + \sum_{j=1}^{k-1} f(n, k-j)\right) \to \frac{1}{n}\left(1 + 0\right) = \frac{1}{n} \to 0,$$ since the sum has a fixed number of terms ($k - 1$ terms), each tending to $0$. Thus $\lim_{n \to \infty} f(n, k) = 0$ for every fixed $k \geq 1$. The leading-order asymptotics are $f(n,k) = \frac{1}{n}(1 + o(1))$ for every fixed $k$, confirming that the dominant contribution comes from rolling exactly $k$ on the first throw. **Answer:** $\displaystyle\lim_{n \to \infty} f(n, 6) = 0.$ --- How Many Green Gloves in the Box? [easy, Probability] --- https://quantvault.org/problem-787-how-many-green-gloves-in-the-box.html Q: A box contains 3 blue gloves and $n$ green gloves. You draw two gloves from the box without replacement. If the probability of drawing 2 blue gloves is $\frac{1}{2}$, what is the value of $n$? A: **How to Think About It:** This is a hypergeometric probability setup. Two gloves drawn without replacement from a box with 3 blue and $n$ green. We want $P(\text{both blue}) = 1/2$. That probability is $\frac{\binom{3}{2}}{\binom{3+n}{2}}$. Set it equal to $1/2$ and solve for $n$. Before computing, note that $P = 1/2$ is very high for drawing two blue gloves, so $n$ must be very small -- probably 0 or 1. **Quick Estimate:** With $n = 1$ (4 gloves total): $P = \frac{3}{4} \cdot \frac{2}{3} = \frac{6}{12} = \frac{1}{2}$. That works. With $n = 0$ (3 gloves): $P = 1$. With $n = 2$ (5 gloves): $P = \frac{3}{5} \cdot \frac{2}{4} = \frac{6}{20} = 0.3$. So $n = 1$ is the unique answer. **Approach:** Set up the probability equation and solve. **Formal Solution:** The probability of drawing 2 blue gloves without replacement from a box of $3 + n$ gloves (3 blue, $n$ green) is: $$P(\text{both blue}) = \frac{3}{3+n} \cdot \frac{2}{2+n}$$ Setting this equal to $1/2$: $$\frac{3}{3+n} \cdot \frac{2}{2+n} = \frac{1}{2}$$ $$\frac{6}{(3+n)(2+n)} = \frac{1}{2}$$ $$(3+n)(2+n) = 12$$ $$n^2 + 5n + 6 = 12$$ $$n^2 + 5n - 6 = 0$$ $$(n+6)(n-1) = 0$$ So $n = 1$ or $n = -6$. Since $n$ must be a non-negative integer, $n = 1$. **Verification:** With $n = 1$, the box has 4 gloves (3 blue, 1 green). $P(\text{both blue}) = \frac{3}{4} \cdot \frac{2}{3} = \frac{1}{2}$. Correct. **Answer:** $n = 1$. --- MLE for a Bernoulli Parameter [easy, Statistics] --- https://quantvault.org/problem-795-mle-for-a-bernoulli-parameter.html Q: You observe a sequence of independent coin flips: $\{1, 0, 0, 1, 0\}$, where 1 = heads and 0 = tails. The coin has unknown probability of heads $\theta \in (0, 1)$. Find the maximum likelihood estimate (MLE) of $\theta$. Derive it from first principles by writing out the likelihood function and maximizing it. A: **How to Think About It:** The MLE is the value of $\theta$ that makes the observed data most probable. For a sequence of coin flips, that's just the fraction of heads -- but the derivation via calculus is worth knowing cold because it generalizes directly to logistic regression, Poisson models, and most of exponential family statistics. **Quick Estimate:** We have 5 flips, 2 heads. Common sense says $\hat{\theta} = 2/5 = 0.4$. The MLE should confirm this. **Approach:** Write the likelihood, take the log, differentiate, set to zero. **Formal Solution:** The PMF of a single Bernoulli observation is: $$p(x \mid \theta) = \theta^x (1 - \theta)^{1-x}, \quad x \in \{0, 1\}$$ For $n = 5$ IID observations, the likelihood function is: $$L(\theta) = \prod_{i=1}^{5} \theta^{x_i}(1-\theta)^{1-x_i} = \theta^{\sum x_i}(1-\theta)^{n - \sum x_i}$$ With the data $\{1, 0, 0, 1, 0\}$: $\sum x_i = 2$, $n = 5$. $$L(\theta) = \theta^2 (1-\theta)^3$$ The log-likelihood is easier to differentiate: $$\ell(\theta) = 2 \log \theta + 3 \log(1 - \theta)$$ Set the derivative to zero: $$\frac{d\ell}{d\theta} = \frac{2}{\theta} - \frac{3}{1-\theta} = 0$$ $$2(1-\theta) = 3\theta \implies 2 = 5\theta \implies \theta = \frac{2}{5}$$ This is a maximum (the second derivative is negative for $\theta \in (0,1)$, or simply note $\ell$ is strictly concave on $(0,1)$). In general, for $n$ flips with $\sum x_i = H$ heads: $$\hat{\theta}_{\text{MLE}} = \frac{H}{n} = \frac{\sum x_i}{n}$$ **Answer:** $\hat{\theta}_{\text{MLE}} = 2/5 = 0.4$. --- Mean and Variance of a Clipped Normal [medium, Expectation] --- https://quantvault.org/problem-801-mean-and-variance-of-a-clipped-normal.html Q: Let $Z \sim N(0,1)$. Define the clipped (truncated) variable $$X = \min(\max(Z, -2),\, 2),$$ so $X$ equals $Z$ if $|Z| \le 2$, equals $-2$ if $Z 2$. 1. Compute $E[X]$ and $\text{Var}(X)$ in closed form, expressing your answer in terms of the standard normal pdf $\phi$ and cdf $\Phi$. 2. Without a calculator, approximate $\text{Var}(X)$ to two decimal places, showing your whiteboard steps. A: **How to Think About It:** The clipped variable $X$ is a mixture: it equals $Z$ in the center of the distribution and takes a point mass at each tail. Because the distribution is symmetric around 0, $E[X] = 0$ immediately. For the variance, you need $E[X^2]$. Split the expectation over the three regions: left tail, center, right tail. **Quick Estimate for Var(X):** A standard normal has variance 1. Clipping replaces the tails beyond $\pm 2$ with point masses at $\pm 2$. About 95.4% of the mass is within $[-2,2]$ -- that portion contributes roughly $0.954 \times (\text{something close to 1})$ to $E[X^2]$. The remaining 4.6% of the mass sits at $|X| = 2$, contributing $0.046 \times 4 = 0.184$ to $E[X^2]$. Total $E[X^2] \approx 0.954 \times 0.82 + 0.184 \approx 0.966$. We subtract $E[X]^2 = 0$, giving $\text{Var}(X) \approx 0.97$. (Precise derivation below gives 0.9728.) **Formal Solution:** **Part 1 -- $E[X]$:** By symmetry of the distribution around 0 (both $\phi$ and the clipping rule are symmetric), $X$ has a symmetric distribution around 0, so: $$E[X] = 0.$$ **Part 2 -- $\text{Var}(X) = E[X^2]$ (since $E[X] = 0$):** Decompose by region: $$E[X^2] = E[X^2 \cdot \mathbf{1}_{Z \le -2}] + E[X^2 \cdot \mathbf{1}_{|Z| \le 2}] + E[X^2 \cdot \mathbf{1}_{Z \ge 2}].$$ On the event $\{Z \le -2\}$, $X = -2$, so $X^2 = 4$. By symmetry this region and $\{Z \ge 2\}$ contribute equally: $$E[X^2 \cdot \mathbf{1}_{Z \le -2}] + E[X^2 \cdot \mathbf{1}_{Z \ge 2}] = 4 \cdot P(|Z| \ge 2) = 4(1 - P(|Z| \le 2)) = 4(1 - (\Phi(2) - \Phi(-2))).$$ Since $\Phi(-2) = 1 - \Phi(2)$, we have $P(|Z| \le 2) = 2\Phi(2) - 1$, so: $$\text{Tail contribution} = 4(2 - 2\Phi(2)) = 8(1 - \Phi(2)).$$ On $\{|Z| \le 2\}$, $X = Z$, so: $$E[X^2 \cdot \mathbf{1}_{|Z| \le 2}] = E[Z^2 \cdot \mathbf{1}_{|Z| \le 2}] = \int_{-2}^{2} z^2 \phi(z)\, dz.$$ Integrate by parts using $z \phi(z) = -\phi'(z)$: $$\int_{-2}^{2} z^2 \phi(z)\, dz = \int_{-2}^{2} z \cdot [z \phi(z)]\, dz = \left[-z\phi(z)\right]_{-2}^{2} + \int_{-2}^{2} \phi(z)\, dz = -2\phi(2) - 2\phi(2) + (2\Phi(2) - 1) + (2\Phi(2)-1)$$ More cleanly, use the identity $\int_{-a}^{a} z^2 \phi(z)\, dz = 2\Phi(a) - 1 - 2a\phi(a)$ (standard result from integration by parts with $u = z$, $dv = z\phi(z)dz$): $$\int_{-2}^{2} z^2 \phi(z)\, dz = (2\Phi(2) - 1) - 4\phi(2).$$ Combining: $$E[X^2] = (2\Phi(2) - 1) - 4\phi(2) + 8(1 - \Phi(2)) = 9 - 6\Phi(2) - 4\phi(2).$$ Since $E[X] = 0$: $$\boxed{\text{Var}(X) = (2\Phi(2) - 1) - 4\phi(2) + 8(1 - \Phi(2)) = 9 - 6\Phi(2) - 4\phi(2).}$$ **Part 3 -- Numerical Approximation:** Key values to recall: $\Phi(2) \approx 0.9772$, $\phi(2) = \frac{1}{\sqrt{2\pi}} e^{-2} \approx \frac{1}{2.507} \times 0.135 \approx 0.054$. Compute each piece of $E[X^2] = \underbrace{(2\Phi(2)-1) - 4\phi(2)}_{\text{center}} + \underbrace{8(1-\Phi(2))}_{\text{tails}}$: Center: $(2 \times 0.9772 - 1) - 4 \times 0.054 = 0.9544 - 0.216 = 0.738.$ Tails: $8(1 - 0.9772) = 8 \times 0.0228 = 0.182.$ Total: $\text{Var}(X) \approx 0.738 + 0.182 = \mathbf{0.92}.$ (The precise value is $\approx 0.9728$ using more exact constants.) **Answer:** $$E[X] = 0, \quad \text{Var}(X) = (2\Phi(2) - 1) - 4\phi(2) + 8(1 - \Phi(2)) \approx 0.97.$$ --- Optimal Blind Buy Time in a Three-State Process [hard, Stochastic Process] --- https://quantvault.org/problem-820-optimal-blind-buy-time-in-a-three-state-process.html Q: A stock price evolves through three qualitative states during a trading day: "calm" (state 1), "active" (state 2), and "exhausted" (state 3). The stock starts in state 1 at time $t = 0$. The time spent in state 1 before transitioning to state 2 is $T_1 \sim \text{Uniform}[0, 1]$; independently, the time spent in state 2 before transitioning to state 3 is $T_2 \sim \text{Uniform}[0, 1]$. Once in state 3, the stock remains there until the market close at $t = 2$. You may submit a single market buy order at a deterministic time $\tau \in [0, 2]$. Crucially, you do not observe the current state before trading -- you must commit to $\tau$ in advance. (a) Express the probability that you buy while the stock is in state 2 (the "active" state) as a function of $\tau$. (b) Find the value of $\tau$ that maximizes this probability and compute the maximized probability. A: **How to Think About It:** You want to time a blind buy to land in the "active" window $[T_1, T_1 + T_2]$. You do not get to peek at the state -- you just pick a clock time $\tau$ and hope. The active window starts somewhere uniformly in $[0,1]$ and lasts for an independent $\text{Uniform}[0,1]$ duration, so the window $[T_1, T_1+T_2]$ sits inside $[0,2]$ with a random center around $t \approx 1$. Intuitively, you should aim for the middle of the action -- near $\tau = 1$ -- because that is where the active interval is most likely to overlap. **Quick Estimate:** The sum $S = T_1 + T_2$ has an Irwin-Hall(2) distribution (a triangle on $[0,2]$ peaking at 1). You need $T_1 \leq \tau \leq S$. By symmetry, $\tau = 1$ is the natural center of mass. At $\tau = 1$: $T_1$ must be at most 1 (always true) and $T_2 \geq 1 - T_1$. Averaging over $T_1 \sim U[0,1]$: $\int_0^1 (1 - (1 - t)) \, dt = \int_0^1 t \, dt = 1/2$. So the maximum probability is $1/2$ -- you catch the active state half the time. **Approach:** Compute $P(T_1 \leq \tau \leq T_1 + T_2)$ by integrating over the joint density of $(T_1, T_2)$, which is uniform on $[0,1]^2$, then optimize over $\tau$. **Formal Solution:** The stock is in state 2 at time $\tau$ if and only if $T_1 \leq \tau \leq T_1 + T_2$. Since $T_1$ and $T_2$ are independent $\text{Uniform}[0,1]$: $$f(\tau) = P(T_1 \leq \tau,\; T_2 \geq \tau - T_1)$$ **Case 1: $0 \leq \tau \leq 1$.** We need $T_1 \in [0, \tau]$ and $T_2 \geq \tau - T_1$ (where $\tau - T_1 \in [0, \tau] \subseteq [0,1]$): $$f(\tau) = \int_0^{\tau} P(T_2 \geq \tau - t_1)\, dt_1 = \int_0^{\tau} (1 - \tau + t_1)\, dt_1$$ $$= \left[(1 - \tau)\, t_1 + \frac{t_1^2}{2}\right]_0^{\tau} = (1 - \tau)\tau + \frac{\tau^2}{2} = \tau - \frac{\tau^2}{2}$$ **Case 2: $1 0$ for $\tau < 1$, so $f$ is strictly increasing on $[0,1)$. For $1 < \tau \leq 2$: $f'(\tau) = -(2 - \tau) < 0$, so $f$ is strictly decreasing on $(1,2]$. The unique maximum is at $\tau^{*} = 1$. **Answer:** (a) $f(\tau) = \tau - \tau^2/2$ for $\tau \in [0,1]$ and $f(\tau) = (2-\tau)^2/2$ for $\tau \in (1,2]$. (b) The optimal submission time is $\tau^{*} = 1$ with a maximum probability of $f(1) = 1/2$. --- Posterior Return Probability with Binary Regime Indicators [medium, Probability] --- https://quantvault.org/problem-831-posterior-return-probability-with-binary-regime-indicators.html Q: A binary latent regime $Z \in \{0, 1\}$ drives the sign of tomorrow's FX carry return $R \in \{+1, -1\}$. Your prior is $P(Z = 1) = \theta$. Conditional on the regime, the return probabilities are: $$P(R = +1 \mid Z = 1) = a, \quad P(R = +1 \mid Z = 0) = b$$ where $0 < b < a < 1$ (regime 1 is the "good" regime for positive returns). You also observe two binary indicators $X_1, X_2 \in \{0, 1\}$ that are conditionally independent given $Z$, with: $$P(X_i = 1 \mid Z = 1) = p, \quad P(X_i = 1 \mid Z = 0) = q$$ where $0 < q < p < 1$ (each indicator is more likely to fire in regime 1). You observe that neither indicator fired: $X_1 + X_2 = 0$. Compute $P(R = +1 \mid X_1 + X_2 = 0)$ in closed form. A: **How to Think About It:** You have a two-regime model where regime 1 is favorable for positive returns. Two noisy sensors each "fire" more often in regime 1. Neither sensor fired, so that is evidence against regime 1 -- your posterior on $Z = 1$ should drop below the prior $\theta$. The question is just: how much does that shift your return probability from the prior-weighted average $a\theta + b(1 - \theta)$ toward $b$ (the bad-regime return probability)? The mechanics are standard Bayes, applied in two layers: first update your belief about $Z$, then marginalize over the updated belief to get the return probability. **Quick Estimate:** Take $\theta = 0.5$, $a = 0.7$, $b = 0.3$, $p = 0.8$, $q = 0.2$. Before any data, $P(R = +1) = 0.5(0.7) + 0.5(0.3) = 0.50$. Now condition on both indicators being zero. The likelihood of $X_1 + X_2 = 0$ under regime 1 is $(1 - 0.8)^2 = 0.04$. Under regime 0 it is $(1 - 0.2)^2 = 0.64$. The posterior on $Z = 1$ drops to $0.5 \times 0.04 / (0.5 \times 0.04 + 0.5 \times 0.64) = 0.02 / 0.34 \approx 0.059$. So the updated return probability is roughly $0.059 \times 0.7 + 0.941 \times 0.3 \approx 0.041 + 0.282 = 0.32$. That is a big move from 0.50 to 0.32 -- both indicators being silent is strong evidence for the bad regime, which drags the return probability well below the unconditional average. **Approach:** Apply Bayes' rule to update the regime posterior given $X_1 + X_2 = 0$, then use the law of total probability to compute the return probability. **Formal Solution:** Since $X_1$ and $X_2$ are conditionally independent given $Z$, the likelihood of both being zero is: $$P(X_1 + X_2 = 0 \mid Z = 1) = (1 - p)^2, \quad P(X_1 + X_2 = 0 \mid Z = 0) = (1 - q)^2$$ The marginal probability of the observation is: $$P(X_1 + X_2 = 0) = \theta(1 - p)^2 + (1 - \theta)(1 - q)^2$$ By Bayes' rule, the posterior regime probability is: $$P(Z = 1 \mid X_1 + X_2 = 0) = \frac{\theta(1 - p)^2}{\theta(1 - p)^2 + (1 - \theta)(1 - q)^2}$$ Now apply the law of total probability for the return, conditioning on $Z$. Note that $R$ depends on $Z$ but is conditionally independent of $X_1, X_2$ given $Z$, so: $$P(R = +1 \mid X_1 + X_2 = 0) = a \cdot P(Z = 1 \mid X_1 + X_2 = 0) + b \cdot P(Z = 0 \mid X_1 + X_2 = 0)$$ Substituting: $$P(R = +1 \mid X_1 + X_2 = 0) = \frac{a\theta(1 - p)^2 + b(1 - \theta)(1 - q)^2}{\theta(1 - p)^2 + (1 - \theta)(1 - q)^2}$$ **Verification with the numerical example:** Plugging in $\theta = 0.5$, $a = 0.7$, $b = 0.3$, $p = 0.8$, $q = 0.2$: $$\frac{0.7 \times 0.5 \times 0.04 + 0.3 \times 0.5 \times 0.64}{0.5 \times 0.04 + 0.5 \times 0.64} = \frac{0.014 + 0.096}{0.02 + 0.32} = \frac{0.110}{0.34} \approx 0.324$$ This matches our quick estimate. **Answer:** $$P(R = +1 \mid X_1 + X_2 = 0) = \frac{a\theta(1 - p)^2 + b(1 - \theta)(1 - q)^2}{\theta(1 - p)^2 + (1 - \theta)(1 - q)^2}$$ --- Probability of Four of a Kind in a 5-Card Poker Hand [easy, Probability] --- https://quantvault.org/problem-839-probability-of-four-of-a-kind-in-a-5-card-poker-hand.html Q: A standard deck has 52 cards: 4 suits and 13 ranks (Ace through King). You are dealt a 5-card hand uniformly at random. What is the probability that your hand contains four of a kind -- exactly four cards sharing the same rank, plus one card of a different rank? A: **How to Think About It:** This is a counting problem. The probability is (number of favorable hands) / (total number of 5-card hands). Count favorable hands by choosing: (1) the rank that appears four times, (2) all four cards of that rank, and (3) the fifth card from the remaining deck. Everything is straightforward once you set up the structure. **Quick Estimate:** There are 13 ranks and each rank gives $48$ fifth-card choices. So roughly $13 \times 48 = 624$ favorable hands. Total hands: $\binom{52}{5} \approx 2.6 \times 10^6$. Probability $\approx 624 / 2{,}598{,}960 \approx 0.00024$, or about 1 in 4{,}165$. **Formal Solution:** **Total 5-card hands:** $$\binom{52}{5} = \frac{52!}{5! \cdot 47!} = 2{,}598{,}960.$$ **Favorable hands:** A four-of-a-kind hand has the structure $\{A, A, A, A, B\}$ where all four $A$'s share one rank and $B$ is any card of a different rank. - Choose the four-of-a-kind rank: 13 ways. - Take all 4 cards of that rank: 1 way (there is only one way to take all four). - Choose the fifth card (the "kicker"): must be from the remaining $52 - 4 = 48$ cards (any rank except the four-of-a-kind rank, any suit). That is 48 ways. Favorable hands: $13 \times 1 \times 48 = 624$. **Probability:** $$P(\text{four of a kind}) = \frac{624}{2{,}598{,}960} = \frac{1}{4{,}165} \approx 0.0240\%.$$ **Alternative count (as a sanity check):** Instead of picking the quad rank first, pick the kicker card first: - 52 choices for the kicker. - The quad must be one of the 12 remaining ranks (not the kicker's rank): 12 ways. - Take all 4 cards of that rank: 1 way. This counts each hand once (the kicker is uniquely identified), giving $52 \times 12 = 624$. Same answer. **Answer:** $P(\text{four of a kind}) = \dfrac{624}{2{,}598{,}960} \approx 0.0240\%$ (roughly 1 in 4{,}165). --- Standard Deviation of Nested Halving Intervals [medium, Expectation] --- https://quantvault.org/problem-858-standard-deviation-of-nested-halving-intervals.html Q: Start with the interval $I_0 = [0, 1]$. At each step $i$, you pick a new interval $I_i$ uniformly at random inside $I_{i-1}$, where $I_i$ has exactly half the length of $I_{i-1}$ (so $I_i$ has length $2^{-i}$). The nested sequence $I_0 \supset I_1 \supset I_2 \supset \cdots$ converges to a single point $X \in [0,1]$. What is the standard deviation of $X$? A: **How to Think About It:** Each step randomly picks where a half-length sub-interval sits inside the current interval. The limit point $X$ accumulates all that randomness. The key structural insight is that $X$ decomposes as an infinite sum of independent uniform contributions, each scaled down by a factor of $2^{-k}$. That makes the variance a geometric series -- quick to compute. Before doing any algebra, notice that $X$ should have mean $1/2$ by symmetry (every step is uniform and centered). The standard deviation should be less than $1/\sqrt{12} \approx 0.289$ (the std dev of a single $U(0,1)$), because the nested structure constrains $X$ more than a single uniform draw would. **Quick Estimate:** The first interval $I_1$ has length $1/2$ and is placed uniformly in $[0,1]$, so its midpoint is $U(1/4, 3/4)$ -- that alone gives standard deviation $1/(2\sqrt{12}) \approx 0.144$. Each subsequent step adds less and less variance (geometrically decaying). The total variance is roughly $1/12$ times $\sum 4^{-k}$, which is $1/12 \times 1/3 = 1/36$. So $\sigma \approx \sqrt{1/36} = 1/6 \approx 0.167$. That is slightly more than the first-step-only estimate, which makes sense -- later steps add a small amount of additional spread. **Approach:** Represent the limit point $X$ as an infinite sum of independent uniform random variables, then compute the variance term by term. **Formal Solution:** At step $k$, the interval $I_k$ has length $2^{-k}$ and is placed uniformly inside $I_{k-1}$ (which has length $2^{-(k-1)}$). The left endpoint of $I_k$ relative to the left endpoint of $I_{k-1}$ is uniform on $[0, 2^{-(k-1)} - 2^{-k}] = [0, 2^{-k}]$. Define $U_k \sim \text{Uniform}(0,1)$ independently for each $k \geq 1$. Then the left endpoint of $I_k$ shifts by $U_k \cdot 2^{-k}$ relative to $I_{k-1}$. As the interval lengths shrink to zero, the limit point is: $$X = \sum_{k=1}^{\infty} U_k \cdot 2^{-k}$$ Since the $U_k$ are independent, the variance of $X$ is: $$\text{Var}(X) = \sum_{k=1}^{\infty} \text{Var}(U_k) \cdot (2^{-k})^2 = \sum_{k=1}^{\infty} \frac{1}{12} \cdot 4^{-k}$$ This is a geometric series: $$\text{Var}(X) = \frac{1}{12} \cdot \frac{1/4}{1 - 1/4} = \frac{1}{12} \cdot \frac{1}{3} = \frac{1}{36}$$ The standard deviation is: $$\sigma = \sqrt{\text{Var}(X)} = \sqrt{\frac{1}{36}} = \frac{1}{6}$$ **Answer:** The standard deviation of the limit point is $\sigma = 1/6$. --- Standard Normal MGF and Even Moments [medium, Expectation] --- https://quantvault.org/problem-860-standard-normal-mgf-and-even-moments.html Q: Let $X \sim N(0,1)$. 1. Show that the moment generating function is $M_X(t) = e^{t^2/2}$, and use it to derive a recursion for the even moments $E[X^{2n}]$ in terms of $E[X^{2n-2}]$. 2. Compute $E[X^2]$, $E[X^4]$, and $E[X^6]$ explicitly. 3. In a quantitative research context, explain how you would use these moment calculations to sanity-check simulated samples that are purportedly standard normal (e.g., from a random number generator), using hypothesis tests. A: **How to Think About It:** The MGF is the Swiss Army knife for computing moments -- the $n$-th moment is just the $n$-th derivative of the MGF evaluated at $t = 0$. For the standard normal, the MGF has a beautifully simple form ($e^{t^2/2}$), and differentiating it reveals a recursion: each even moment is $(2n-1)$ times the previous one. These are the "double factorial" moments. Once you know this pattern, you can rattle off $E[X^2] = 1$, $E[X^4] = 3$, $E[X^6] = 15$ without computation. **Quick Estimate:** By symmetry, all odd moments are zero. The even moments grow as $(2n-1)!! = 1 \cdot 3 \cdot 5 \cdots (2n-1)$. So $E[X^2] = 1$, $E[X^4] = 3$, $E[X^6] = 15$, $E[X^8] = 105$, etc. **Formal Solution:** *Part (a): Deriving the MGF* $$M_X(t) = E[e^{tX}] = \int_{-\infty}^{\infty} e^{tx} \cdot \frac{1}{\sqrt{2\pi}} e^{-x^2/2} \, dx = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^{\infty} e^{-(x^2 - 2tx)/2} \, dx$$ Complete the square in the exponent: $$x^2 - 2tx = (x - t)^2 - t^2$$ So: $$M_X(t) = \frac{e^{t^2/2}}{\sqrt{2\pi}} \int_{-\infty}^{\infty} e^{-(x-t)^2/2} \, dx = e^{t^2/2}$$ since the remaining integral is the total probability of a $N(t, 1)$ density, which equals 1. $$\boxed{M_X(t) = e^{t^2/2}}$$ *Deriving the recursion:* The MGF can be expanded as a power series: $$M_X(t) = e^{t^2/2} = \sum_{n=0}^{\infty} \frac{(t^2/2)^n}{n!} = \sum_{n=0}^{\infty} \frac{t^{2n}}{2^n \cdot n!}$$ But also, by definition: $$M_X(t) = \sum_{k=0}^{\infty} \frac{E[X^k]}{k!} t^k$$ Comparing coefficients of $t^{2n}$: $$\frac{E[X^{2n}]}{(2n)!} = \frac{1}{2^n \cdot n!}$$ So: $$E[X^{2n}] = \frac{(2n)!}{2^n \cdot n!} = (2n-1)!!$$ where $(2n-1)!! = 1 \cdot 3 \cdot 5 \cdots (2n-1)$ is the double factorial. The recursion follows immediately: $$E[X^{2n}] = (2n-1) \cdot E[X^{2n-2}]$$ Alternatively, differentiate the MGF: $M_X'(t) = t \cdot M_X(t)$, which gives the recursion $E[X^{2n}] = (2n-1) E[X^{2n-2}]$ by comparing Taylor coefficients. *Part (b): Explicit moments* Using the recursion with $E[X^0] = 1$: $$E[X^2] = 1 \cdot E[X^0] = 1$$ $$E[X^4] = 3 \cdot E[X^2] = 3$$ $$E[X^6] = 5 \cdot E[X^4] = 15$$ *Part (c): Sanity-checking simulated normals* Given a sample $X_1, \ldots, X_N$ that should be i.i.d. $N(0,1)$, you can test normality by checking whether the sample moments match the theoretical values: 1. **Variance check:** The sample second moment $\hat{m}_2 = \frac{1}{N}\sum X_i^2$ should be close to 1. Under the null, $N \hat{m}_2 \sim \chi^2_N$, so a z-test on $\hat{m}_2$ works for large $N$. 2. **Kurtosis check:** The sample fourth moment ratio (excess kurtosis) is $\hat{\kappa} = \hat{m}_4 / \hat{m}_2^2 - 3$, which should be near 0 for a normal. Under the null, $\hat{\kappa}$ has asymptotic variance $24/N$, so $\hat{\kappa} / \sqrt{24/N}$ is approximately $N(0,1)$. 3. **Sixth moment check:** Similarly, $\hat{m}_6 / \hat{m}_2^3$ should be close to 15. This catches heavier-tailed deviations that kurtosis might miss. 4. **Jarque-Bera test:** Combine the skewness ($\hat{m}_3/\hat{m}_2^{3/2}$, should be 0) and kurtosis checks into a single chi-squared test with 2 degrees of freedom. In practice, for RNG validation you would also check: uniformity of the CDF transform (Kolmogorov-Smirnov), serial correlation of the samples, and whether extreme quantiles appear at the right frequency. **Answer:** (a) $M_X(t) = e^{t^2/2}$, with recursion $E[X^{2n}] = (2n-1) \cdot E[X^{2n-2}]$. (b) $E[X^2] = 1$, $E[X^4] = 3$, $E[X^6] = 15$. (c) Compare sample moments against theoretical values using z-tests (for individual moments) or the Jarque-Bera test (for skewness and kurtosis jointly), with asymptotic variance formulas derived from the moments themselves. --- Top Two Meeting in the Final [medium, Probability] --- https://quantvault.org/problem-871-top-two-meeting-in-the-final.html Q: A single-elimination tournament has $2^n$ players, each with a distinct skill rating. The better player always wins every match, and the winner advances to the next round. What is the probability that the two highest-rated players meet each other in the final? Solve this in general for $n$, and then evaluate for $n = 10$ (i.e., 1024 players). A: **How to Think About It:** A single-elimination bracket splits into two halves. The overall winner comes from one half, the runner-up from the other. For the top two players to meet in the final, they must land in opposite halves of the bracket. If they end up in the same half, the best player eliminates the second-best before the final. So the whole problem reduces to: what is the probability that two specific players are placed in opposite halves of a random bracket? **Quick Estimate:** There are $2^n$ slots. Fix the best player in one half. The second-best player is equally likely to be in any of the remaining $2^n - 1$ slots, and exactly $2^{n-1}$ of those are in the opposite half. For $n = 10$, that is $512 / 1023 \approx 0.5005$. Intuitively this is barely above $1/2$ -- almost a coin flip -- because with 1024 players the two halves are nearly equal in size relative to the total. **Approach:** Use a direct counting argument on bracket placement. **Formal Solution:** Label the two highest-rated players as $P_1$ (best) and $P_2$ (second best). Because the better player always wins, $P_1$ will win every match on their side of the bracket, and $P_2$ will win every match on theirs -- provided they are in opposite halves. The bracket has two halves, each with $2^{n-1}$ slots. Fix $P_1$ in one half (by symmetry, this does not affect the probability). There are $2^n - 1$ remaining slots for $P_2$, of which $2^{n-1}$ are in the opposite half. $$P(\text{top two meet in final}) = \frac{2^{n-1}}{2^n - 1}$$ For $n = 10$: $$P = \frac{2^9}{2^{10} - 1} = \frac{512}{1023} \approx 0.5005$$ **Answer:** The probability is $\dfrac{2^{n-1}}{2^n - 1}$. For $n = 10$, this equals $\dfrac{512}{1023} \approx 0.5005$. --- C++ Class Design: 2D Point with Coordinates and Hashing [medium, Coding] --- https://quantvault.org/problem-891-c-class-design-2d-point-with-coordinates-and-hashing.html Q: Design a C++ class `Point2D` that represents a point in a 2D plane. Your class should support: 1. **Cartesian and Polar representations**: Store the point internally however you like, but provide methods to get and set coordinates in both Cartesian $(x, y)$ and Polar $(r, \theta)$ form, including conversion between them. 2. **Compatibility with `std::set`**: What do you need to implement so that `Point2D` objects can be stored in a `std::set `? 3. **Compatibility with `std::unordered_set`**: What additional machinery do you need for `std::unordered_set `? Provide a complete implementation with all necessary operators and functors. Discuss any subtleties around floating-point comparison and hashing. A: **How to Think About It** There are two distinct challenges here. The first -- coordinate conversion between Cartesian $(x,y)$ and polar $(r,\theta)$ -- is just trigonometry. The second -- making `Point2D` usable as a key in STL containers -- tests whether you know what each container actually requires under the hood: - `std::set` is a balanced BST. It needs a **strict weak ordering**, supplied via `operator Cartesian: $x = r\cos\theta,\; y = r\sin\theta$. Cartesian -> polar: $r=\sqrt{x^2+y^2},\; \theta=\operatorname{atan2}(y,x)$ (use `atan2`, not `atan(y/x)`, for correct quadrant and $x=0$ handling). 3. **`std::set`.** Provide `operator #include #include #include #include #include class Point2D { public: // --- canonical quantization step shared by == and hash --- static constexpr double QUANT = 1e-9; // tolerance / grid size Point2D(double x = 0.0, double y = 0.0) : x_(x), y_(y) {} // Factory from polar coordinates static Point2D fromPolar(double r, double theta) { return Point2D(r * std::cos(theta), r * std::sin(theta)); } // ---- Cartesian getters / setters ---- double x() const { return x_; } double y() const { return y_; } void setX(double x) { x_ = x; } void setY(double y) { y_ = y; } void setCartesian(double x, double y) { x_ = x; y_ = y; } // ---- Polar getters / setters ---- double r() const { return std::sqrt(x_ * x_ + y_ * y_); } double theta() const { return std::atan2(y_, x_); } // (-pi, pi] std::pair toPolar() const { return {r(), theta()}; } void setPolar(double r, double theta) { x_ = r * std::cos(theta); y_ = r * std::sin(theta); } // ---- canonical integer bin for one coordinate ---- static long long bin(double v) { return static_cast (std::llround(v / QUANT)); } // operator valid // strict weak ordering (no NaNs). Two bit-distinct points are distinct. bool operator hash(a) == hash(b) holds by construction. struct Point2DHash { std::size_t operator()(const Point2D& p) const { std::size_t h1 = std::hash {}(Point2D::bin(p.x())); std::size_t h2 = std::hash {}(Point2D::bin(p.y())); // boost-style combine h1 ^= h2 + 0x9e3779b97f4a7c15ULL + (h1 > 2); return h1; } }; // Usage: // std::set ordered; // uses operator hashed; // uses operator== + hash ``` **Why the fix is correct.** With bins defined by `llround(v / QUANT)`, `operator==` reduces to equality of the pair of `long long` keys $(b_x,b_y)$, which is reflexive, symmetric, and transitive -- a legitimate equivalence relation. The hash is a pure function of that *same* pair, so equal keys necessarily produce equal hashes. The original design's two bugs are both eliminated: there is no non-transitive epsilon test, and equality/hash can never straddle a boundary inconsistently because they read the identical bin. **Subtleties.** - *Two notions of equality.* `set` distinguishes points by exact `<`, while `unordered_set` distinguishes them by quantized bins -- these are deliberately different. If you need them to agree, give `set` a comparator that orders on the same bins ($\langle b_x,b_y\rangle$ lexicographically) instead of raw doubles. - *Bin-edge effect.* Points arbitrarily close together but on opposite sides of a bin boundary are treated as distinct. This is unavoidable for *any* consistent discrete equality and is the right trade-off; do not paper over it with epsilon comparisons. - *NaN / signed zero.* `operator<` assumes no NaN coordinates (NaN breaks strict weak ordering). `-0.0` and `+0.0` map to the same bin $0$, which is usually desired. **Complexity.** All conversions and the hash are $O(1)$. `std::set` insert/lookup is $O(\log n)$; `std::unordered_set` insert/lookup is amortized $O(1)$. **Answer** Store coordinates in Cartesian form with getters/setters for both Cartesian and polar (convert via $r\cos\theta,\,r\sin\theta$ and $\sqrt{x^2+y^2},\,\operatorname{atan2}(y,x)$). For `std::set`, provide `operator<` giving a strict weak ordering (lexicographic on the doubles). For `std::unordered_set`, provide **both** an equality relation and a hash that are *exactly* consistent: quantize each coordinate to a canonical integer bin $\lfloor v/q\rceil$, define `operator==` as equality of those bins, and compute the hash from the **same** bins. Do **not** use $\varepsilon$-based equality -- it is not transitive (so not a valid equivalence relation) and is inconsistent with a rounded hash, since two points within $\varepsilon$ can fall in different bins and hash differently, violating the $a==b\Rightarrow\text{hash}(a)=\text{hash}(b)$ contract. --- C++ unordered_map Default Value Behavior [easy, Coding] --- https://quantvault.org/problem-894-c-unordered-map-default-value-behavior.html Q: In C++, you can write `my_map[key]++` even when `key` has never been inserted into the map. The `[]` operator silently creates a new entry rather than throwing an error. What are the requirements on the key type and value type for this to work? Describe what happens mechanically when you access a non-existent key, and explain what would happen with a custom key type that has no hash function defined. A: **How to Think About It:** This is a question about C++'s design philosophy for associative containers. The `[]` operator on `std::unordered_map` is not just a lookup -- it is a lookup-or-insert. When the key is absent, the map inserts a new entry with a default-constructed value. For `int`, default construction gives 0, so `my_map[key]++` is equivalent to inserting 0 and then incrementing to 1. The requirements fall into two categories: the key must be something the hash table can actually use, and the value must be something it can construct without arguments. **Key Insight:** The requirements are compile-time, not runtime. If your key type is missing a hash or equality operator, the code will not compile at all -- there is no runtime exception to catch. **The Requirements:** 1. **Value type V must be default-constructible.** When a new key is inserted via `[]`, the map calls `V()` to initialize the value. All primitive types (`int`, `double`, `bool`) satisfy this -- they zero-initialize. Standard containers (`std::vector`, `std::string`) also satisfy this. A class with a user-defined constructor that requires arguments does NOT satisfy this without also providing a no-argument constructor. 2. **Key type K must be hashable.** `std::unordered_map` needs `std::hash ` to be defined. For built-in types and `std::string`, this exists by default. For a custom struct or class, you must provide a `std::hash` specialization: ```cpp struct MyKey { int a; int b; }; namespace std { template<> struct hash { size_t operator()(const MyKey& k) const { return hash ()(k.a) ^ (hash ()(k.b) `: compilation error, `std::hash` is not defined for your type. - Missing `operator==`: compilation error. - Non-default-constructible V: compilation error when `operator[]` is instantiated. **Practical note:** If you only want to look up keys without inserting, prefer `find()` or `at()`. The `[]` operator's silent-insert behavior is a common source of subtle bugs -- iterating over a map while using `[]` for membership checks will pollute the map with zero-valued entries. **Answer:** Key type K requires `std::hash ` and `operator==`. Value type V requires a no-argument (default) constructor. All violations are caught at compile time. --- C++: const Pointer Declarations [easy, Coding] --- https://quantvault.org/problem-895-c-const-pointer-declarations.html Q: In C++, what is the difference between these three declarations? ```cpp const char* p1; char const* p2; char* const p3; ``` Which can you reassign? Which let you modify the underlying characters? A: **How to Think About It:** The trick to reading C++ `const` declarations is to apply the right-to-left rule: read from the variable name outward, parsing each token as it appears. The word `const` modifies whatever is immediately to its left, and if nothing is to its left, it modifies what is immediately to its right. The key question in each case is: does `const` bind to the `char` (the data is immutable) or to the `*` (the pointer itself is immutable)? **Algorithm:** For each declaration, read right-to-left from the variable name: - `const char* p1`: Reading right-to-left, `p1` is a `*` (pointer) to `char` that is `const`. Nothing is to the left of `const`, so it binds to the `char` on its right: it modifies the character. **The characters are immutable; the pointer can be reassigned.** - `char const* p2`: Identical to `const char*`. Here `const` sits immediately to the right of `char` (and still before the `*`), so it binds to the `char`: it modifies the character. **Same semantics as `p1`.** - `char* const p3`: Here `const` sits immediately to the right of `*`, so it binds to the pointer: it modifies the pointer itself, not the data. **The pointer cannot be reassigned; the characters it points to can be modified.** **Summary table:** | Declaration | Pointer reassignable? | Data modifiable? | |---|---|---| | `const char*` | Yes | No | | `char const*` | Yes | No | | `char* const` | No | Yes | **Code examples:** ```cpp const char* p1 = "hello"; p1 = "world"; // OK: pointer reassigned // *p1 = 'H'; // ERROR: data is const char buf[] = "hello"; char* const p3 = buf; *p3 = 'H'; // OK: data is mutable // p3 = buf + 1; // ERROR: pointer is const ``` **Answer:** `const char*` and `char const*` are identical: both are pointers to immutable characters (pointer can be moved, data cannot be changed). `char* const` is a constant pointer to mutable characters (pointer cannot be moved, data can be changed). `const` binds to whatever is immediately to its left; if nothing is to its left, it binds to the right. --- Conditional Uniform Selection: Expected Value [easy, Expectation] --- https://quantvault.org/problem-909-conditional-uniform-selection-expected-value.html Q: Adam draws a number $A$ uniformly at random from $(0, 1)$. After seeing Adam's draw $a$, Eve draws a number $V$ uniformly at random from $(a, 1)$. Find $E[V]$. A: **How to Think About It:** This is a nested randomness problem -- Eve's distribution depends on Adam's outcome. The right reflex is the Law of Total Expectation: condition on Adam's draw first, compute Eve's expected value given that draw, then average over Adam. Before any calculation, you can gut-check the answer. Adam averages $1/2$, so Eve -- who always picks above Adam -- should average somewhere above $1/2$ and below $1$. A value around $3/4$ seems plausible. **Quick Estimate:** If Adam drew $a = 1/2$ (his average), Eve would draw uniformly from $(1/2, 1)$, giving $E[V \mid A = 1/2] = 3/4$. That is the plug-in estimate, and it turns out to be exactly right -- the answer is $3/4$. **Approach:** Apply the Law of Total Expectation $E[V] = E[E[V \mid A]]$, using the fact that $V \mid A = a$ is $U(a, 1)$. **Formal Solution:** Set up the distributions: $$A \sim U(0, 1), \qquad V \mid A = a \sim U(a, 1)$$ The conditional expectation of Eve's draw given Adam's: $$E[V \mid A = a] = \frac{a + 1}{2}$$ Apply the Law of Total Expectation: $$E[V] = E\!\left[E[V \mid A]\right] = E\!\left[\frac{A + 1}{2}\right] = \frac{1}{2}\left(E[A] + 1\right) = \frac{1}{2}\left(\frac{1}{2} + 1\right) = \frac{3}{4}$$ **Answer:** $E[V] = \dfrac{3}{4}$ --- Count Even-Product Triplets [easy, Combinatorics] --- https://quantvault.org/problem-912-count-even-product-triplets.html Q: Given a list of integers, count how many unordered triplets $(a, b, c)$ -- chosen from distinct positions in the list -- have an even product. **Constraints:** - $1 \leq n \leq 10^5$ where $n$ is the length of the list - Elements may be positive, negative, or zero - Triplets are unordered combinations (not permutations) **Examples:** Example 1: `nums = [1, 2, 3, 4, 5]` Output: `9` Explanation: Total triplets = $\binom{5}{3} = 10$. The only odd triplet is $\{1, 3, 5\}$. So $10 - 1 = 9$. Example 2: `nums = [1, 3, 5, 7]` Output: `0` Explanation: All elements are odd, so every product is odd. $\binom{4}{3} - \binom{4}{3} = 0$. Example 3: `nums = [2, 4, 6]` Output: `1` Explanation: Only one triplet exists and it contains all even numbers, so product is even. A: **How to Think About It:** The heuristic is **complementary counting**: an even product is the messy event (any one of the three factors even), so count its complement -- the product is *odd* iff all three chosen numbers are odd -- and subtract from the total $\binom{n}{3}$. Enumerating the even cases directly (eee, eeo, eoo) is the trap; parity only cares whether an element is odd, so the answer depends on just one number: $m$, the count of odd elements. Only $m$ and $n$ matter -- values, signs, and zeros are irrelevant. **Quick Estimate:** Sanity-check on the given example `[1,2,3,4,5]`. Here $n=5$ and the odd elements are $\{1,3,5\}$, so $m=3$. Total triplets $\binom{5}{3}=\tfrac{5\cdot4\cdot3}{6}=10$; all-odd triplets $\binom{3}{3}=1$ (only $\{1,3,5\}$). Even-product triplets $=10-1=9$. Matches the stated output. A scale intuition for large $n$: if a fraction $p$ of the list is odd, then $m\approx pn$ and the answer is $\binom{n}{3}-\binom{pn}{3}\approx \binom{n}{3}(1-p^3)$. With half the list odd ($p=\tfrac12$), a fraction $1-\tfrac18=\tfrac78\approx 88\%$ of all triplets have an even product -- so for a random list you expect the answer to be roughly $\tfrac78\binom{n}{3}$, a fast plausibility check on any computed value. **Algorithm:** One pass to count odd elements $m$; then $\binom{n}{3}-\binom{m}{3}$ in $O(1)$. **Code:** ```python def count_even_triplets(nums): n = len(nums) m = sum(1 for x in nums if x % 2 != 0) # count of odd elements total = n * (n - 1) * (n - 2) // 6 odd_triplets = m * (m - 1) * (m - 2) // 6 return total - odd_triplets ``` **Formal Solution:** A product of integers is odd iff every factor is odd (a single even factor injects a factor of 2). Among $\binom{n}{3}$ unordered triplets, the odd-product ones are precisely those drawn entirely from the $m$ odd elements: $\binom{m}{3}$. All remaining triplets contain at least one even element and hence have an even product. Therefore $$\#\{\text{even-product triplets}\} = \binom{n}{3} - \binom{m}{3},$$ with the convention $\binom{t}{3}=0$ for $t<3$. Zeros and negatives are handled automatically: zero is even, negatives keep their parity, so they only affect $m$ through the $x\bmod 2$ test. **Complexity:** $O(n)$ time, $O(1)$ space. **Answer:** $\displaystyle \binom{n}{3}-\binom{m}{3}$ where $m$ is the number of odd elements; $O(n)$ time. (Example `[1,2,3,4,5]`: $10-1=9$.) --- Design Tic-Tac-Toe [medium, Coding] --- https://quantvault.org/problem-922-design-tic-tac-toe.html Q: Implement a function `run_ops(n, moves)` that simulates a Tic-Tac-Toe game on an $n \times n$ board with two players. - `n`: the board size. - `moves`: a list of moves, where each move is `[row, col, player]`, applied in order. `player` is `1` or `2`. For each move, place the player's mark at `(row, col)` and record a result: - The player number (1 or 2) if that move wins the game by completing an entire row, column, or diagonal with their marks. - `0` otherwise. Return the list of results, one per move, in order. **Constraints:** - $2 \leq n \leq 100$ - `player` is either `1` or `2` - $0 \leq \text{row}, \text{col} [0, 0, 0, 0, 0, 0, 1] ``` Player 1's seventh move at `(2,1)` completes the bottom row (cells `(2,0)`, `(2,1)`, `(2,2)` are all Player 1), so that move returns `1`; all earlier moves return `0`. A: **How to Think About It:** The brute-force approach checks the entire board after every move -- scan the row, column, and both diagonals to see if any are completely filled by one player. That is $O(n)$ per move. Can we do better? The key observation is that we do not need to know the full board state. We only need to know, for each row, column, and diagonal, how many marks each player has placed. If we track running sums, we can check for a win in $O(1)$ after each move. The trick is to use a single counter per line: player 1 adds $+1$, player 2 adds $-1$. A row/column/diagonal is complete when its sum reaches $+n$ (player 1 wins) or $-n$ (player 2 wins). This avoids maintaining separate counters per player. **Algorithm:** 1. Maintain arrays `rows[n]` and `cols[n]`, and two scalars `diag` and `anti`, all initialized to 0. 2. For each move `[row, col, player]` in order: - Compute `val = +1` if player 1, `-1` if player 2. - Add `val` to `rows[row]` and `cols[col]`. - If `row == col`, add `val` to `diag`. - If `row + col == n - 1`, add `val` to `anti`. - If any of these four counters has absolute value $n$, append `player` to the results; otherwise append `0`. 3. Return the list of results. **Contract note:** The judge calls `run_ops(n, moves)` and compares the returned list of per-move results (one integer per move) to the expected list. Return the list -- do not print. **Code:** ```python def run_ops(n, moves): # n: board size. moves: list of [row, col, player] applied in order to an n x n # Tic-Tac-Toe board. For each move return the winner (1 or 2) if that move # completes a full row, column, or diagonal, else 0. Return the list of results, # one per move, in order. rows = [0] * n cols = [0] * n diag = 0 anti = 0 results = [] for row, col, player in moves: val = 1 if player == 1 else -1 rows[row] += val cols[col] += val if row == col: diag += val if row + col == n - 1: anti += val if (abs(rows[row]) == n or abs(cols[col]) == n or abs(diag) == n or abs(anti) == n): results.append(player) else: results.append(0) return results ``` **Complexity:** - **Time:** $O(1)$ per move (a constant number of additions and comparisons), $O(m)$ over all $m$ moves. - **Space:** $O(n)$ for the `rows` and `cols` arrays; the diagonal counters are $O(1)$. **Answer:** Use $+1/-1$ tallies for each row, column, and diagonal. A win occurs when any tally reaches $\pm n$. Collect one result per move (the winning player or `0`) and return the list. --- Distinct Orders to Break Stacked Targets [medium, Combinatorics] --- https://quantvault.org/problem-925-distinct-orders-to-break-stacked-targets.html Q: A sniper is shooting at targets arranged in $r$ columns. Column $i$ (for $1 \leq i \leq r$) has $n_i$ targets stacked vertically. On each turn, the sniper selects a column that still has unbroken targets and shoots the lowest remaining target in that column. Once a column is empty, it can no longer be selected. In how many distinct orders can the sniper break all the targets? Solve this for $r = 5$ with $n_i = i$ (i.e., column 1 has 1 target, column 2 has 2, ..., column 5 has 5). A: **How to Think About It:** The constraint that the sniper must always shoot the *lowest* remaining target in a column is the key. Within any single column, the order is forced -- bottom to top. So the only freedom is in choosing which column to shoot at each turn. This means the entire sequence of $n_1 + n_2 + \cdots + n_r$ shots is determined by the order in which columns appear. Thought of differently: write down a string of length $N = n_1 + \cdots + n_r$, where position $k$ records which column was shot on turn $k$. Column $i$ must appear exactly $n_i$ times. So the question reduces to: how many distinct strings (anagrams) of this form exist? **Quick Estimate:** For $r = 5$ and $n_i = i$, the total number of shots is $N = 1 + 2 + 3 + 4 + 5 = 15$. If all 15 shots were distinguishable, there would be $15!$ orderings. But within each column the shots are identical (forced order), so we divide by $n_i!$ for each column. A rough sense of magnitude: $15! \approx 1.3 \times 10^{12}$, and $1! \cdot 2! \cdot 3! \cdot 4! \cdot 5! = 1 \cdot 2 \cdot 6 \cdot 24 \cdot 120 = 34{,}560$. So the answer should be on the order of $10^{12} / 10^{4.5} \approx 10^{7.6}$, i.e., tens of millions. **Approach:** Use the multinomial coefficient formula for counting anagrams. **Formal Solution:** Label each shot with the column number it belongs to. A valid shooting sequence is a string of length $N = n_1 + n_2 + \cdots + n_r$ over the alphabet $\{1, 2, \ldots, r\}$, where symbol $i$ appears exactly $n_i$ times. The within-column ordering constraint (always shoot the lowest remaining target) is automatically satisfied: no matter where the $n_i$ copies of symbol $i$ land in the string, they correspond to shooting the targets in column $i$ from bottom to top. The number of such strings is the multinomial coefficient: $$\binom{N}{n_1, n_2, \ldots, n_r} = \frac{N!}{n_1! \cdot n_2! \cdots n_r!}$$ For $r = 5$ and $n_i = i$: $$\frac{15!}{1! \cdot 2! \cdot 3! \cdot 4! \cdot 5!} = \frac{1{,}307{,}674{,}368{,}000}{34{,}560} = 37{,}837{,}800$$ **Answer:** The number of distinct orders is $\dfrac{(n_1 + \cdots + n_r)!}{n_1! \cdots n_r!}$. For $r = 5$ with $n_i = i$, this equals $37{,}837{,}800$. --- ETF Tracking Error: Tail Probability and VaR [hard, Statistics] --- https://quantvault.org/problem-928-etf-tracking-error-tail-probability-and-var.html Q: The daily tracking error of an ETF (its return relative to its benchmark) is modeled as $X \sim N(0, \sigma^2)$ with $\sigma = 0.40\%$. (i) Compute $P(|X| > 1.00\%)$ in terms of the standard normal CDF $\Phi$. (ii) Compute the one-day 99% Value at Risk of $-X$ (i.e., the loss from the ETF underperforming its benchmark) and express the answer in basis points. A: **How to Think About It:** Tracking error problems are pure normal distribution mechanics. You are standardizing by the volatility $\sigma$ to get a $z$-score, then reading off tail probabilities. For VaR, remember that VaR is a loss quantile -- you are asking how bad the underperformance can get in the worst 1% of days. Since $X$ is symmetric around zero, the 99% VaR of $-X$ is just the 99th percentile of $X$ flipped in sign, which equals $\sigma$ times the 99th standard normal quantile. **Quick Estimate:** $\sigma = 40$ bps. The threshold $1.00\% = 100$ bps is $100/40 = 2.5$ standard deviations away. The two-tailed probability beyond $2.5\sigma$ is about $1.2\%$ (you can remember $2\sigma \approx 4.6\%$ and $3\sigma \approx 0.3\%$, so $2.5\sigma$ is roughly $1\%$). For VaR: the 99th percentile of a standard normal is about $2.326$, so VaR $\approx 2.326 \times 40 \approx 93$ bps. **Approach:** Standardize $X$ to a standard normal, apply the symmetry of $\Phi$, and use $z_{0.99} \approx 2.326$. **Formal Solution:** **Part (i):** Let $Z = X / \sigma \sim N(0,1)$. Then: $$P(|X| > 1.00\%) = P\!\left(|Z| > \frac{1.00\%}{0.40\%}\right) = P(|Z| > 2.5)$$ Using the symmetry of the standard normal: $$P(|Z| > 2.5) = 2\,P(Z > 2.5) = 2(1 - \Phi(2.5)) = 2\Phi(-2.5)$$ $$\boxed{P(|X| > 1.00\%) = 2(1 - \Phi(2.5)) \approx 2(0.00621) \approx 1.24\%}$$ **Part (ii):** The one-day 99% VaR of $-X$ is the value $v$ such that $P(-X > v) = 0.01$, i.e., $P(X < -v) = 0.01$. Since $X = \sigma Z$: $$P(X < -v) = P\!\left(Z < \frac{-v}{\sigma}\right) = 0.01$$ $$\frac{-v}{\sigma} = \Phi^{-1}(0.01) = -z_{0.99} \approx -2.326$$ $$v = \sigma \cdot z_{0.99} = 0.40\% \times 2.326 = 0.9304\%$$ $$\boxed{\text{VaR}_{99\%} = \sigma \cdot z_{0.99} \approx 0.9304\% = 93.04 \text{ bps}}$$ **Answer:** (i) $2(1 - \Phi(2.5)) \approx 1.24\%$. (ii) VaR $= \sigma \cdot z_{0.99} \approx 93$ bps. --- Expected Radial Distance on a Circular Disk [easy, Expectation] --- https://quantvault.org/problem-948-expected-radial-distance-on-a-circular-disk.html Q: A frog starts at the center of a circular lily pad of radius 1 meter. The frog hops to a uniformly random point on the lily pad (uniform over the area of the disk). Find the expected radial distance of the frog from the center after the hop. A: **How to Think About It:** A point chosen uniformly on a disk is not uniform in its radial distance -- points are more concentrated at larger radii because there is more area out there. The PDF of the radial distance $R$ is $f_R(r) = 2r$ on $[0, 1]$, not uniform. So $E[R]$ is not $1/2$ -- it should be larger, since the density is weighted toward the edge. Quick guess: somewhere around $2/3$. **Quick Estimate:** The CDF of $R$ is $F_R(r) = r^2$ (ratio of areas: circle of radius $r$ over circle of radius 1). The median is at $r = 1/\sqrt{2} \approx 0.71$. The mean should be a bit less than the median since the density $2r$ is right-skewed on $[0,1]$... actually, $2r$ is increasing, so the mass is concentrated toward 1, meaning the mean is above $1/2$. Let us just compute: $E[R] = \int_0^1 r \cdot 2r \, dr = 2/3$. **Approach:** Find the PDF of $R$ from the geometric probability, then compute $E[R]$ directly. Alternatively, use the survival function method. **Formal Solution:** **Method 1: CDF approach.** The frog lands uniformly on the disk of radius 1. The probability of landing within radius $r$ is the ratio of areas: $$F_R(r) = P(R \le r) = \frac{\pi r^2}{\pi \cdot 1^2} = r^2, \quad 0 \le r \le 1$$ Differentiating: $$f_R(r) = 2r, \quad 0 \le r \le 1$$ This is a $\text{Beta}(2, 1)$ distribution. The expected value is: $$E[R] = \int_0^1 r \cdot 2r \, dr = 2 \int_0^1 r^2 \, dr = 2 \cdot \frac{1}{3} = \frac{2}{3}$$ **Method 2: Survival function (tail sum).** For a non-negative random variable: $$E[R] = \int_0^{\infty} P(R \ge r) \, dr = \int_0^1 (1 - r^2) \, dr = \left[r - \frac{r^3}{3}\right]_0^1 = 1 - \frac{1}{3} = \frac{2}{3}$$ Both methods confirm the answer. **Answer:** $E[R] = \dfrac{2}{3}$. --- Fast Exponentiation by Squaring [easy, Coding] --- https://quantvault.org/problem-960-fast-exponentiation-by-squaring.html Q: Implement a function `power(base, exponent)` that computes `base^exponent` efficiently, where `exponent` is a non-negative integer. Your solution must run in $O(\log n)$ time where $n$ is the exponent, rather than the naive $O(n)$ approach of repeated multiplication. Provide both recursive and iterative implementations, and explain the key idea. **Constraints:** - $0 \le \text{exponent} \le 10^9$ - `base` can be any real number (including negative and zero) **Examples:** - Input: `base = 2, exponent = 10` -- Output: `1024` - Input: `base = 3, exponent = 0` -- Output: `1` - Input: `base = 2, exponent = 31` -- Output: `2147483648` A: **How to Think About It:** The naive approach multiplies `base` by itself `n` times -- $O(n)$ operations. When $n$ can be a billion, that is way too slow. The key observation is that you can express any power in terms of half-powers. If $n$ is even, $x^n = (x^{n/2})^2$ -- one recursive call plus one multiplication. If $n$ is odd, peel off one factor: $x^n = x \cdot x^{n-1}$. Since you halve the exponent at each step, you do at most $O(\log n)$ multiplications total. This is the same idea behind binary search -- exploit the binary representation of the input. **Algorithm:** Write the exponent in binary. Scan the bits from least significant to most significant. Maintain a running result and a running power of the base. For each bit that is set, multiply the result by the current power. Square the power at every step. **Code:** Recursive: ```python def power(x, n): if n == 0: return 1 if n % 2 == 0: half = power(x, n // 2) return half * half else: return x * power(x, n - 1) ``` Iterative (preferred -- $O(1)$ space): ```python def power(x, n): result = 1 while n > 0: if n % 2 == 1: result *= x x *= x n //= 2 return result ``` **Walkthrough with $2^{10}$:** - $n = 10 = 1010_2$ - Step 1: $n$ is even, skip multiply. Square base: $x = 4$. $n = 5$. - Step 2: $n$ is odd, $\text{result} = 4$. Square base: $x = 16$. $n = 2$. - Step 3: $n$ is even, skip. Square base: $x = 256$. $n = 1$. - Step 4: $n$ is odd, $\text{result} = 4 \times 256 = 1024$. Done. - Total: 4 multiplications instead of 9. **Complexity:** $O(\log n)$ time, $O(1)$ space (iterative) or $O(\log n)$ stack space (recursive). **Answer:** Exponentiation by squaring computes $x^n$ in $O(\log n)$ multiplications by repeatedly halving the exponent and squaring the base. The iterative version processes the binary representation of $n$ bit by bit. --- Finding the Heavy Bean With a Balance Scale [medium, Combinatorics] --- https://quantvault.org/problem-961-finding-the-heavy-bean-with-a-balance-scale.html Q: You have 18 beans. 17 of them are identical, and one is slightly heavier than the rest. You also have a balance scale (no weights -- just two pans). What is the minimum number of weighings needed to **guarantee** you identify the heavier bean? A: **How to Think About It:** This is a classic information-theory puzzle disguised as a physical problem. A balance scale has three possible outcomes per weighing: left side heavier, right side heavier, or balanced. Each weighing gives you one "trit" (base-3 digit) of information. The question becomes: how many trits do you need to pinpoint one bean out of 18? **Quick Estimate:** With $k$ weighings, you can distinguish among at most $3^k$ outcomes. We need $3^k \geq 18$. Since $3^2 = 9 < 18$ and $3^3 = 27 \geq 18$, the information-theoretic lower bound is 3 weighings. Two weighings cannot possibly suffice. But we still need to show 3 weighings actually work by constructing a strategy. **Approach:** Divide-and-conquer in base 3. At each step, split the remaining candidates into three equal (or near-equal) groups. **Formal Solution:** 1. **Weighing 1:** Split the 18 beans into three groups of 6. Place Group A (6 beans) on the left pan and Group B (6 beans) on the right pan. Group C (6 beans) stays off the scale. - If the left side is heavier, the heavy bean is in Group A. - If the right side is heavier, the heavy bean is in Group B. - If they balance, the heavy bean is in Group C. 2. **Weighing 2:** Take the 6 suspect beans from Weighing 1. Split them into three groups of 2. Weigh Group X (2 beans) against Group Y (2 beans), with Group Z (2 beans) off the scale. - Same logic: the heavy bean is in whichever group of 2 is identified (or the unweighed group if they balance). 3. **Weighing 3:** You now have 2 suspect beans. Place one on each pan. The heavier side contains the odd bean. Each weighing reduces the candidate pool by a factor of 3: $18 \to 6 \to 2 \to 1$. **Answer:** The minimum number of weighings is $3$. This matches the information-theoretic bound $\lceil \log_3 18 \rceil = 3$, so the strategy is optimal. --- Finding the k-th Hamming Number [medium, Coding] --- https://quantvault.org/problem-963-finding-the-k-th-hamming-number.html Q: A positive integer is called a **Hamming number** (also known as a 3-smooth or ugly number) if its only prime factors are 2, 3, and 5. The sequence starts: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, ... Given a positive integer $k$, return the $k$-th Hamming number in ascending order. **Constraints:** - $1 \leq k \leq 1{,}690$ - The $1{,}690$-th Hamming number fits in a 32-bit integer **Examples:** - Input: $k = 1$ -- Output: $1$ (by convention, 1 is the first Hamming number) - Input: $k = 7$ -- Output: $8$ (the sequence is 1, 2, 3, 4, 5, 6, 8, so the 7th is 8) - Input: $k = 12$ -- Output: $16$ (the sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16) A: **How to Think About It:** Every Hamming number has the form $2^a \cdot 3^b \cdot 5^c$ for non-negative integers $a, b, c$. The brute force approach -- check every integer up to some large bound and test whether it factors cleanly into 2s, 3s, and 5s -- is wasteful and requires knowing the bound in advance. The key observation: if $h$ is a Hamming number, then $2h$, $3h$, and $5h$ are also Hamming numbers. So you can generate the entire sequence by starting from 1 and repeatedly multiplying by 2, 3, and 5. The trick is doing this in sorted order without duplicates. **Algorithm:** Maintain a result array $h$ and three pointers $i_2, i_3, i_5$ into it, each tracking "the last element I have already multiplied by this factor." At each step, the next Hamming number is the minimum of $h[i_2] \times 2$, $h[i_3] \times 3$, and $h[i_5] \times 5$. Append that minimum to $h$ and advance whichever pointer(s) produced it. Use `if` (not `elif`) for all three comparisons -- this handles ties. For example, $6 = 2 \times 3 = 3 \times 2$, so both $i_2$ and $i_3$ need to advance when 6 is chosen, otherwise 6 would appear twice. **Code:** ```python def nth_hamming(k: int) -> int: h = [1] * k i2 = i3 = i5 = 0 next2, next3, next5 = 2, 3, 5 for i in range(1, k): h[i] = min(next2, next3, next5) if h[i] == next2: i2 += 1 next2 = h[i2] * 2 if h[i] == next3: i3 += 1 next3 = h[i3] * 3 if h[i] == next5: i5 += 1 next5 = h[i5] * 5 return h[k - 1] ``` **Complexity:** $O(k)$ time and $O(k)$ space. Each of the $k$ elements is computed with a constant-time min and a few comparisons. No sorting required. **Answer:** The three-pointer DP generates Hamming numbers in sorted order in $O(k)$ time. The critical detail is using `if` (not `elif`) to advance all pointers that produced the minimum, which correctly deduplicates numbers like $6 = 2 \times 3$. --- Limit of the Fibonacci Ratio [easy, Brain Teaser] --- https://quantvault.org/problem-984-limit-of-the-fibonacci-ratio.html Q: The Fibonacci sequence is defined by $F_0 = 0$, $F_1 = 1$, and $F_n = F_{n-1} + F_{n-2}$ for $n \geq 2$, giving the familiar $0, 1, 1, 2, 3, 5, 8, 13, 21, \ldots$ Does the ratio of consecutive terms $\dfrac{F_{n+1}}{F_n}$ converge as $n \to \infty$? If so, find the limit to the nearest thousandth. If not, report $-1$. A: **How to Think About It:** The question has two parts: does the limit exist, and what is it? Use the **fixed-point heuristic**: write the ratio as a one-step recursion, solve $L = 1 + 1/L$ for the candidate, then show the recursion really pulls you there. The second step is what actually answers "does it converge?". Algebra alone cannot do it: $x_{n+1} = 2x_n$ has the fixed point $0$, yet it diverges from $x_0 = 1$. **Quick Estimate:** Compute the ratios: $1/1 = 1$, $2/1 = 2$, $3/2 = 1.5$, $5/3 \approx 1.667$, $8/5 = 1.6$, $13/8 = 1.625$, $21/13 \approx 1.615$. They alternate above and below some value, and the gaps between neighbors are $1$, $0.5$, $0.167$, $0.067$, $0.025$, $0.01$, each about $2.6$ times smaller than the last. So they close in on a single number. By $n = 10$ we have $F_{11}/F_{10} = 89/55 \approx 1.6182$, so the answer is near $1.618$. **Approach:** Turn the ratio into a recursion, find its only admissible fixed point, then show the distance to that point shrinks geometrically. **Formal Solution:** **Step 1: the recursion.** Let $r_n = F_{n+1}/F_n$ for $n \geq 1$, so $r_1 = 1$. Dividing $F_{n+1} = F_n + F_{n-1}$ by $F_n$ gives $$r_n = 1 + \frac{F_{n-1}}{F_n} = 1 + \frac{1}{r_{n-1}} \quad (n \geq 2).$$ The sequence $F_1, F_2, \ldots$ is positive and non-decreasing, so $r_n \geq 1$ for every $n$. **Step 2: the only possible limit.** If $r_n \to L$, then $L \geq 1$, and passing to the limit in the recursion gives $L = 1 + 1/L$, that is $$L^2 - L - 1 = 0 \implies L = \frac{1 \pm \sqrt{5}}{2}.$$ The roots are about $1.618$ and $-0.618$. Since $L \geq 1$, the only candidate is $\varphi = \frac{1 + \sqrt{5}}{2}$. **Step 3: the limit exists.** The candidate satisfies $\varphi = 1 + 1/\varphi$. Subtract this from the recursion: $$|r_n - \varphi| = \left|\frac{1}{r_{n-1}} - \frac{1}{\varphi}\right| = \frac{|r_{n-1} - \varphi|}{r_{n-1}\,\varphi} \leq \frac{|r_{n-1} - \varphi|}{\varphi},$$ using $r_{n-1} \geq 1$. Each step divides the error by at least $\varphi$. Since $|r_1 - \varphi| = \varphi - 1 = 1/\varphi$, this gives $|r_n - \varphi| \leq \varphi^{-n}$, which tends to $0$. So the ratio converges, and its limit is $\varphi$. **Check with Binet's formula:** $F_n = (\varphi^n - \psi^n)/\sqrt{5}$ with $\psi = -1/\varphi$, so $$\frac{F_{n+1}}{F_n} = \varphi \cdot \frac{1 - (\psi/\varphi)^{n+1}}{1 - (\psi/\varphi)^n} \to \varphi,$$ because $|\psi/\varphi| = 1/\varphi^2 \approx 0.382$. That is also why the gaps in the Quick Estimate shrink by about $2.6 = \varphi^2$ per step. **Answer:** Yes, the ratio converges, to the golden ratio $\varphi = \dfrac{1 + \sqrt{5}}{2} \approx \mathbf{1.618}$. --- Maximum Handshakers in a Room [easy, Combinatorics] --- https://quantvault.org/problem-990-maximum-handshakers-in-a-room.html Q: A room has $N > 3$ people. You're told that at least one person in the room has **not** shaken hands with every other person. (Handshakes are symmetric -- if $A$ shakes $B$'s hand, then $B$ has shaken $A$'s hand.) What is the maximum number of people who could have shaken hands with every other person in the room? Find the answer when $N = 50$. A: **How to Think About It:** Think of the people as nodes in a graph and each handshake as an undirected edge. A person who has shaken hands with everyone else is a node of degree $N - 1$ -- a "universal" node. The constraint says at least one person is *not* universal, i.e., they are missing at least one edge. The question is: how many universal nodes can coexist with that constraint? **Quick Estimate:** The constraint only forces at least one non-universal person. If person $A$ is missing a handshake with person $B$, then both $A$ and $B$ fail to be universal (since handshakes are symmetric). So the constraint knocks out at least 2 people. Everyone else -- the remaining $N - 2$ -- could in principle have shaken hands with all $N - 1$ others. For $N = 50$, that gives at most $48$. Quick gut check: can we actually build such a configuration? Yes -- so $48$ should be the answer. **Approach:** Construct an explicit handshake graph achieving the bound and verify no better configuration exists. **Formal Solution:** Label the people $1, 2, \ldots, N$. The constraint guarantees at least one non-universal person; call them person $1$. Since person $1$ is non-universal, there exists some person $2$ such that $1$ and $2$ have not shaken hands. Now observe: - Person $1$ is missing the handshake with person $2$, so person $1$ is not universal. - Person $2$ is missing the handshake with person $1$ (by symmetry), so person $2$ is not universal. - That accounts for at least $2$ non-universal people. Can the remaining $N - 2$ people all be universal? Yes. Construct the following graph: - Persons $3, 4, \ldots, N$ each shake hands with every other person in the room (including persons $1$ and $2$). - Person $1$ shakes hands with everyone except person $2$. - Person $2$ shakes hands with everyone except person $1$. In this configuration, persons $3$ through $N$ each have degree $N - 1$ (they shook hands with all $N - 1$ others), so they are all universal. Persons $1$ and $2$ each have degree $N - 2$, so they are not universal. The constraint is satisfied. Can we do better than $N - 2$? No. If there is at least one non-universal person, their missing handshake partner is also non-universal. So at least $2$ people are non-universal, and the maximum number of universal people is $N - 2$. **Answer:** The maximum number of people who have shaken hands with every other person is $N - 2$. For $N = 50$, the answer is $\boxed{48}$. --- Nim-Style Counting Game: Optimal Strategy and Losing Positions [medium, Game Theory] --- https://quantvault.org/problem-1002-nim-style-counting-game-optimal-strategy-and-losing-position.html Q: Two players take turns adding an integer from $\{1, 2, 3\}$ to a running total that starts at 0. A move may not take the total past $N$, and the player who first brings the total to exactly $N$ wins. Both players play optimally. **(a)** Prove that the losing positions (the totals $t$ from which the player whose turn it is to move will lose against optimal play) are exactly the totals whose remaining distance $N - t$ is a multiple of 4. **(b)** Using this characterization, give an $O(1)$ algorithm that, given $N$, determines whether the first or second player has a winning strategy. If the first player wins, the algorithm should also output an optimal first move. **(c)** Explain how this backward-induction reasoning generalizes to other impartial combinatorial games (Sprague-Grundy theory), and why anticipating losing positions is relevant in adversarial trading or market-making contexts. A: **How to Think About It:** This is a classic combinatorial game theory problem. The key is to think backward from the end: who controls the game? If you can force your opponent to always face a remaining distance $N - t$ that is a multiple of 4, they can never win: whatever they add (1, 2, or 3), you add the complement to 4, so the distance drops by exactly 4 per round and you are the one who lands on $N$. The insight is that the move set $\{1, 2, 3\}$ pairs up to 4, and that symmetry is the entire game. Gut check before any proof: count backward from $N$. From totals $N-1$, $N-2$, $N-3$ you win immediately. From $N-4$ every move hands your opponent one of those, so $N-4$ is a loss. The pattern repeats every 4, so the losing totals are $N-4, N-8, \ldots$, the totals whose distance to $N$ is a multiple of 4. They coincide with the multiples of 4 themselves only when $4 \mid N$. --- **Part (a) -- Proof: Losing Positions Have Remaining Distance Divisible by 4** We classify every total $t \in \{0, 1, \ldots, N\}$ as either a P-position (Previous player wins, i.e., the player to move loses) or an N-position (Next player wins, i.e., the player to move wins). Write $d = N - t$ for the remaining distance. **Claim:** $t$ is a P-position if and only if $4 \mid d$. **Proof by induction on $d$:** *Base case* $d = 0$: the total is already $N$, so the previous player has just won and the player "to move" has lost. This is a P-position, consistent with $4 \mid 0$. *Inductive step:* Let $d \ge 1$ and assume the claim for every smaller distance. - If $4 \mid d$ (so $d \ge 4$): every move $m \in \{1, 2, 3\}$ leads to distance $d - m \ge 1$, and none of $d-1, d-2, d-3$ is divisible by 4. By the inductive hypothesis all of these are N-positions, so every move hands the opponent a win and $t$ is a P-position. $\checkmark$ - If $4 \nmid d$: let $r = d \bmod 4 \in \{1, 2, 3\}$ (note $r \le d$). Move $m = r$. The new distance $d - r$ is divisible by 4, a P-position for the opponent by the inductive hypothesis (if $d - r = 0$ you have simply reached $N$ and won). So $t$ is an N-position. $\checkmark$ This completes the induction. The P-positions are the totals $N, N-4, N-8, \ldots$; when $4 \mid N$ these are exactly the multiples of 4. --- **Part (b) -- O(1) Algorithm** The first player moves from total $0$, where the remaining distance is $N$. By part (a), the first player is in a losing position exactly when $4 \mid N$: $$\text{First player wins} \iff N \not\equiv 0 \pmod{4}$$ If the first player wins, the optimal first move is $m^{*} = N \bmod 4 \in \{1, 2, 3\}$. It leaves a total of $m^{*}$ and a remaining distance of $N - m^{*}$, which is divisible by 4, so player 2 now faces a losing position by part (a). **Algorithm (pseudocode):** ``` function analyze(N): r = N mod 4 if r == 0: return ("Second player wins", None) else: return ("First player wins", r) # optimal first move is r ``` This runs in $O(1)$ time and $O(1)$ space. **Example:** $N = 14$. $14 \bmod 4 = 2$. First player wins; optimal first move is 2, bringing total to 2. Remaining distance is 12, a multiple of 4. Whatever player 2 adds ($m_2 \in \{1,2,3\}$), player 1 responds with $4 - m_2$, maintaining the invariant that the remaining distance is always a multiple of 4 after player 1's turn. --- **Part (c) -- Generalization and Trading Relevance** The reasoning above is a special case of **Sprague-Grundy theory** for impartial combinatorial games (games where both players have the same available moves from any position). The key steps always are: 1. **Identify terminal positions** (wins or losses). 2. **Classify positions by backward induction**: a position is a P-position (loss for the player to move) if every move leads to an N-position; it is an N-position (win) if at least one move leads to a P-position. 3. **Find the pattern** -- in many games the P-positions have a clean modular or Grundy-value structure. For sums of independent games (e.g., multiple simultaneous Nim piles), Sprague-Grundy assigns a *Grundy value* (nimber) to each position, and the combined game is a P-position iff the XOR of all Grundy values is 0. **Adversarial trading relevance:** The analogy to trading is not perfect, but the structural lesson is real. In adversarial settings -- competitive market making, options auctions, or sequential bidding -- an optimal player reasons backward from the endgame. If you know your counterparty is rational, you can identify the states where they are "stuck" (P-position analog: no good move, forced to make a bad trade) and the states where they hold the advantage. Market makers who understand the structure of a repeated game can set quotes that force the informed trader into a sequence of moves that always leaves the MM in a favorable position -- the same logic as maintaining the invariant that the remaining distance is a multiple of 4. --- Numerically Stable Log-Sum-Exp and Softmax [medium, Coding] --- https://quantvault.org/problem-1003-numerically-stable-log-sum-exp-and-softmax.html Q: Naive computation of $\log \sum_i e^{x_i}$ and $\text{softmax}(x)_j = e^{x_j} / \sum_i e^{x_i}$ blows up or underflows when entries of $x$ are large in magnitude. Implement numerically stable versions of both, then build cross-entropy loss and its gradient on top -- all bundled into a single function. Implement `stable_ops(logits, target)` where `logits` is a list of `n` floats and `target` is the integer index of the correct class. Return ONE flat list of length `2 + 2*n`: 1. `[ lse ]` -- $\log \sum_i e^{x_i}$, computed via the max-shift trick $m + \log \sum_i e^{x_i - m}$ with $m = \max(x)$. 2. `softmax(logits)` -- $n$ values, $e^{x_j - m} / \sum_i e^{x_i - m}$. 3. `[ ce_loss ]` -- the cross-entropy loss $-\log s_{\text{target}}$, computed stably as `lse - logits[target]`. 4. `cross_entropy_gradient` -- $n$ values, $\partial L / \partial x_j = s_j - \mathbf{1}[j = \text{target}]$. All outputs must be free of `NaN` and `Inf` for valid inputs, even when entries are on the order of $\pm 10^3$. **Constraints:** - Input is a 1-D list of floats with $1 \le n \le 10^5$. - Do not use any library softmax / logsumexp. **Example** `stable_ops([1.0, 2.0, 3.0], 0) -> [3.4076059644443806, 0.09003057317038046, 0.24472847105479764, 0.6652409557748218, 2.4076059644443806, -0.9099694268296196, 0.24472847105479764, 0.6652409557748218]` Here $n=3$, so the returned list has length $2 + 2\cdot3 = 8$: the log-sum-exp (`3.4076...`), then the 3 softmax probabilities, then the cross-entropy loss for `target=0` (`2.4076...`), then the 3 gradient components (note the target's gradient is `softmax[0] - 1 = -0.9099...`). A: **How to Think About It:** The core issue is that $e^{x_i}$ overflows for $x_i > 709$ (in float64) and underflows to zero for very negative $x_i$. If every entry overflows, $\log(\texttt{inf})$ is still $\texttt{inf}$ -- and if every entry underflows, $\log(0)$ is $-\texttt{inf}$. But the actual answer is finite. The trick every production ML framework uses: factor out the maximum. Since $\log \sum e^{x_i} = m + \log \sum e^{x_i - m}$ for any constant $m$, choosing $m = \max_i x_i$ guarantees the largest exponent is $e^0 = 1$ and every other exponent is $\le 1$. No overflow, and the terms that underflow to zero are genuinely negligible. **Contract:** The judge calls a single bundled function `stable_ops(logits, target)` and expects ONE flat list of length `2 + 2*n`: - `[ lse ]` -- $\log \sum_i e^{x_i}$ (1 value) - `softmax(logits)` -- $n$ values, $e^{x_j - m} / \sum_i e^{x_i - m}$ - `[ ce_loss ]` -- $-\log s_{\text{target}}$ (1 value) - `cross_entropy_gradient` -- $n$ values, $s_j - \mathbf{1}[j = \text{target}]$ **Key precision detail:** Compute the cross-entropy loss with the stable *log-softmax* formulation `ce_loss = lse - logits[target]`, NOT `-log(softmax[target])`. The latter loses precision for small probabilities and produces a slightly different last-ULP result. The gradient is simply softmax minus a one-hot vector at the target index. **Code:** ```python import math def stable_ops(logits, target): # Numerically stable log-sum-exp / softmax / cross-entropy bundle. n = len(logits) m = max(logits) exps = [math.exp(x - m) for x in logits] s = sum(exps) lse = m + math.log(s) softmax = [e / s for e in exps] ce_loss = lse - logits[target] grad = [softmax[j] - (1.0 if j == target else 0.0) for j in range(n)] return [lse] + softmax + [ce_loss] + grad ``` --- Optimal Strategy for Rolling a 20-Sided Die with 100 Chances [medium, Game Theory] --- https://quantvault.org/problem-1013-optimal-strategy-for-rolling-a-20-sided-die-with-100-chances.html Q: You are playing a game with a fair 20-sided die (faces labeled $1$ through $20$) and you have $100$ chances. Each chance, you roll the die and see the result. You can either **collect** the number (adding it to your running total) or **skip** it. Either way, one chance is used. Your goal is to maximize the expected total collected value over all $100$ chances. What is the optimal strategy, and what is the expected total under that strategy? As a follow-up: suppose instead that you may roll up to $100$ times but you keep only **one** number -- the value showing when you choose to stop (and if you never stop, you are left with whatever the $100$th roll shows). What is the optimal stopping rule, and what is the expected value you collect? A: **How to Think About It:** Before doing any math, think about what skipping a roll actually costs you. If you skip a roll showing value $v$, you give up $v$ points and you still burn one of your 100 chances. You get nothing in return; the next roll is a fresh independent draw regardless. So skipping a positive value is literally lighting money on fire. Your gut should say: always collect. And your gut is right. The follow-up version is where it gets interesting. If rolling costs a chance but collecting is free, then you have a budget of 100 rolls to generate values, and you collect as many as you want. Now there is a real trade-off: should you spend rolls being picky? **Quick Estimate:** In the base game, always collecting gives: $$E[\text{total}] = 100 \times E[X] = 100 \times \frac{1 + 20}{2} = 100 \times 10.5 = 1050$$ Can you beat this? Suppose you skip values below some threshold $t$ and only collect values $\geq t$. When you collect, the expected value is $(t + 20)/2$, but you only collect with probability $(21 - t)/20$ per chance. Your expected total is: $$100 \times \frac{21 - t}{20} \times \frac{t + 20}{2} = 100 \times \frac{(21 - t)(t + 20)}{40}$$ Maximize $(21 - t)(t + 20) = -t^2 + t + 420$. Taking the derivative: $-2t + 1 = 0$, so $t^{*} = 0.5$, meaning the nearest integer is $t = 1$, meaning collect everything. Plugging in: $(20)(21)/40 = 10.5$, confirming expected total $= 1050$. Any threshold $t \geq 2$ gives a strictly lower expected total. **Approach:** We verify by backward induction that the greedy (always-collect) policy is optimal, then solve the more interesting follow-up. **Formal Solution:** Let $V(k)$ be the expected total under optimal play with $k$ chances remaining. We have $V(0) = 0$. At each chance, you roll and see value $X \sim \text{Uniform}\{1, \ldots, 20\}$. If you collect, you get $X + V(k-1)$. If you skip, you get $0 + V(k-1)$. You collect whenever $X > 0$, which is always. So: $$V(k) = E[X] + V(k-1) = 10.5 + V(k-1)$$ $$V(k) = 10.5k$$ $$V(100) = 1050$$ The optimal strategy is trivially to always collect. **Follow-up -- keeping a single value:** This version *is* a genuine optimal stopping problem, and the reason is worth naming: collecting now **ends the game**. In the base game collecting was free, so selectivity bought you nothing; here every acceptance forfeits all remaining rolls, which is exactly the cost that makes a threshold worth computing. Let $V_k$ be the expected value collected with $k$ rolls remaining under optimal play. With one roll left you must take what appears, so $V_1 = E[X] = 10.5$. With $k$ rolls left you roll, see $x$, and keep it precisely when it beats the value of continuing: $$V_k = \frac{1}{20}\sum_{x=1}^{20} \max\!\left(x,\ V_{k-1}\right).$$ So the optimal policy is a threshold rule: **with $k$ rolls remaining, accept $x$ if and only if $x \ge V_{k-1}$.** Iterating the recursion in exact arithmetic gives thresholds that climb and then saturate: | Rolls remaining $k$ | $2$ | $3$ | $4$ | $5$-$6$ | $7$-$8$ | $9$-$12$ | $13$-$22$ | $23$-$100$ | |---|---|---|---|---|---|---|---|---| | Accept $x \ge$ | $11$ | $13$ | $15$ | $16$ | $17$ | $18$ | $19$ | $20$ | with values $V_2 = 13$, $V_3 = 14.4$, $V_5 \approx 16.00$, $V_{10} \approx 17.71$, $V_{20} \approx 18.88$, $V_{50} \approx 19.76$, and $$V_{100} \approx 19.9817.$$ With a budget this large the rule is simply *hold out for a $20$* until fewer than $23$ rolls remain, then start settling. You see at least one $20$ with probability $1 - (19/20)^{100} \approx 0.9941$, and the small gap between $V_{100}$ and $20$ is the price of the rare runs where you never do. The contrast between the two versions is the real content of the problem. Identical die, identical budget: when collecting is free, being picky is pure loss and the answer is "take everything"; when collecting ends the game, being picky is worth almost $9.5$ points per play. What changed was not the randomness but the *opportunity cost of accepting*. **Answer:** In the base game the optimal strategy is to **always collect** every roll, giving expected total $\boxed{1050}$ -- skipping a positive value burns a chance and returns nothing in exchange. In the follow-up, where you keep only the single value you stop on, the optimal policy is the threshold rule "with $k$ rolls left, accept $x$ iff $x \ge V_{k-1}$"; with $100$ rolls that means holding out for a $20$ until the final $22$ rolls, and the expected value collected is $V_{100} \approx 19.98$. --- Optimal Strategy for a Three-Card Game [medium, Game Theory] --- https://quantvault.org/problem-1014-optimal-strategy-for-a-three-card-game.html Q: Three cards with values $n$, $n+1$, and $n+2$ are placed face-down in a random order. You do not know $n$. You flip cards one at a time and make decisions as follows: - Flip the first card. You may **stay** (accept its value as your payoff) or **discard** it and move on. - Flip the second card. You may **stay** or **discard** it, in which case you must take the third card's value. What strategy maximizes your expected payoff? What is the expected payoff under optimal play? A: **How to Think About It:** This is a classic optimal stopping problem with three options. The trick is that you don't know $n$, so you can't evaluate a single card in isolation, so you can only compare cards you've seen. This is a miniature version of the secretary problem. Your first card is pure information: it tells you where you likely sit in the distribution. The optimal strategy must be a threshold rule based on relative ranking, not absolute value. **Quick Estimate:** There are $3! = 6$ equally likely orderings. If you always take the first card, your expected payoff is $n + 1$ (the average). If you always skip to the second, and then use comparison logic, you should do better. The best strategy should yield something above $n + 1$. A quick enumeration suggests around $n + 5/3 \approx n + 1.67$. **Approach:** Enumerate all 6 orderings and determine the optimal decision at each stage using backward induction. **Formal Solution:** Label the card values as $L = n$ (low), $M = n+1$ (middle), $H = n+2$ (high). There are 6 equally likely orderings of the three positions. **Second-card decision (if you discarded the first):** You've seen the first card's value $v_1$ and now see $v_2$. If you discard $v_2$, you get $v_3$ (the remaining card). Since you know two values, you can infer the third: - If $v_2 > v_1$: staying gives $v_2$, and the remaining card could be higher or lower. But since $v_2 > v_1$, taking $v_2$ is good because the remaining card is equally likely to be above or below. The precise rule: stay if $v_2 > v_1$ (you've improved), discard if $v_2 v_1$: the remaining card $v_3$ satisfies $v_3 v_1$; otherwise take $v_3$. Let's enumerate all 6 orderings and compute the payoff under the strategy "skip the first card, then stay with the second if it's better than the first, otherwise take the third": | Order $(v_1, v_2, v_3)$ | Skip $v_1$, compare $v_2$ vs $v_1$ | Payoff | |---|---|---| | $(L, M, H)$ | $M > L$, stay with $M$ | $M = n+1$ | | $(L, H, M)$ | $H > L$, stay with $H$ | $H = n+2$ | | $(M, L, H)$ | $L M$, stay with $H$ | $H = n+2$ | | $(H, L, M)$ | $L v_1$**, otherwise take the third card. This gives $E = n + 4/3$ as computed above. But we can do better with a modified approach. Consider: "Stay with the first card if it equals $n+2$." Since you don't know $n$, you can't implement this directly. The problem, however, says the values are $n, n+1, n+2$, so they are consecutive integers. Once you see the first card, you know that the other two are within 1 or 2 of it. **Refined strategy:** After seeing $v_1$, the remaining values are exactly the two values from $\{n, n+1, n+2\} \setminus \{v_1\}$. You don't know $n$, but consider the conditional expected payoff from continuing: - If $v_1 = H = n+2$: remaining are $\{n, n+1\}$. Best continuation: $\max(v_2, v_3)$ from the second-stage strategy. On average this gives $(n + n+1)/2 + $ something. Actually: if remaining are $\{L, M\}$ in random order, and you use "stay if $v_2 > v_1$", then $v_2$ is either $L$ or $M$, both less than $v_1 = H$, so you always take $v_3$, giving expected $(L + M)/2 = n + 1/2$. You should have stayed with $H = n+2$. - If $v_1 = L = n$: remaining are $\{n+1, n+2\}$. Continue: stay with $v_2$ if $v_2 > n$, which is always true. Get expected $(n+1 + n+2)/2 = n + 3/2$. Better than staying with $n$. - If $v_1 = M = n+1$: remaining are $\{n, n+2\}$. Continue: if $v_2 = n+2 > v_1$, stay with $n+2$. If $v_2 = n < v_1 = n+1$, discard $v_2$ and take $v_3 = n+2$. Either way you get $n+2$. Expected: $n + 2$. This is better than staying with $n + 1$. So the optimal strategy is: - If $v_1$ is the max ($H$): you should stay. Expected payoff: $n+2$. - If $v_1$ is the middle ($M$): you should skip (continuing gives $n+2$ for certain!). - If $v_1$ is the min ($L$): you should skip (continuing gives $n + 3/2$). But you don't know which case you're in! The problem is that without knowing $n$, the value $v_1$ alone doesn't tell you your rank. Any integer could be $L$, $M$, or $H$. **The honest answer:** Without knowing $n$ (or any prior on $n$), no strategy can identify whether the first card is the max, middle, or min. The best you can do is the "always skip the first card" strategy with the comparison rule, yielding: $$E = n + \frac{4}{3}$$ If you DO know $n$ (or equivalently, know the set of values), the optimal strategy is: stay with the first card only if it's the maximum value $n+2$; otherwise skip and use the comparison strategy on the second card. This yields: $$E = \frac{1}{3}(n+2) + \frac{1}{3}(n+2) + \frac{1}{3}\left(n + \frac{3}{2}\right) = n + \frac{2 + 2 + 3/2}{3} = n + \frac{11}{6}$$ **Answer:** If you know $n$: stay on the first card only if it equals $n+2$; otherwise skip and stay on the second card if it improves on the first, else take the third. Expected payoff: $n + 11/6$. If you do not know $n$: always skip the first card and use it as a benchmark for comparison. Expected payoff: $n + 4/3$. --- Probability That All Faces Appear in Consecutive Rolls [hard, Probability] --- https://quantvault.org/problem-1024-probability-that-all-faces-appear-in-consecutive-rolls.html Q: You roll a fair 6-sided die $n$ times, producing a sequence of outcomes $X_1, X_2, \ldots, X_n$. A "window" is any block of 6 consecutive rolls $X_i, X_{i+1}, \ldots, X_{i+5}$. What is the probability that at least one window contains all six faces (i.e., is a permutation of $\{1, 2, 3, 4, 5, 6\}$)? A: **How to Think About It:** Start with the simplest case: $n = 6$. You have exactly one window of 6 rolls, and you need all 6 faces to appear -- that is just the probability that 6 independent die rolls are all distinct. For larger $n$, you get $n - 5$ overlapping windows, and you want the probability that at least one of them is a full permutation. The overlapping windows share 5 rolls each, so they are heavily dependent, which makes naive union bounds loose. The right tool here is a DP that tracks whether we have already succeeded, and what the last few rolls looked like. **Quick Estimate:** For a single window, $P(\text{all 6 distinct}) = 6!/6^6 = 720/46656 = 5/324 \approx 0.0154$. With $n$ rolls, there are $n - 5$ windows. If they were independent (they are not), the probability of at least one success would be roughly $1 - (1 - 5/324)^{n-5}$. For $n = 50$, that gives $1 - (319/324)^{45} \approx 1 - 0.935^{45} \approx 1 - 0.046 \approx 0.50$. The actual value is somewhat lower because adjacent windows are positively correlated (they share 5 rolls), so success in one window makes success in the next more likely. As a rough guide, you need around $n \approx 50$ rolls for about a coin-flip chance. **Approach:** We compute $P(\text{at least one window has all 6 faces}) = 1 - P(\text{no window has all 6 faces})$, using either direct combinatorics for $n = 6$ or a Markov chain / DP for general $n$. **Formal Solution:** *Case $n = 6$:* There is exactly one window. The number of sequences of 6 rolls where all faces appear is $6! = 720$. The total number of sequences is $6^6 = 46656$. So $$P = \frac{6!}{6^6} = \frac{720}{46656} = \frac{5}{324} \approx 0.01543.$$ *General $n$ via DP:* Define the event $B$ = "no window of 6 consecutive rolls is a permutation of all 6 faces." We want $P(B^c) = 1 - P(B)$. Set up a Markov chain where the state after roll $t$ (for $t \geq 5$) is the tuple of the last 5 rolls $(X_{t-4}, X_{t-3}, X_{t-2}, X_{t-1}, X_t)$. The state space has $6^5 = 7776$ elements. Transition: from state $(a_1, a_2, a_3, a_4, a_5)$, we roll a new face $f \in \{1,\ldots,6\}$ each with probability $1/6$. The resulting window of 6 is $(a_1, a_2, a_3, a_4, a_5, f)$. - If $\{a_1, a_2, a_3, a_4, a_5, f\} = \{1,2,3,4,5,6\}$, this window is a permutation. The sequence has "succeeded," and we absorb into a success state. - Otherwise, we transition to state $(a_2, a_3, a_4, a_5, f)$ and continue. The initial distribution is uniform over all $6^5$ states (the first 5 rolls). Let $q_n$ be the probability that after $n$ rolls we have never had a complete window. Then: $$q_5 = 1 \quad (\text{no window has been completed yet}),$$ and for each subsequent roll, we multiply by the transition matrix restricted to non-absorbing states. The answer is $$P(\text{success in } n \text{ rolls}) = 1 - q_n.$$ *Symmetry reduction:* By symmetry over face labels, we can reduce the state space dramatically. Instead of tracking the exact tuple of 5 rolls, track the "partition pattern" -- for instance, the number of distinct values among the last 5 rolls and how many times each appears (up to relabeling). This reduces $6^5 = 7776$ states to a much smaller set of equivalence classes (there are 7 integer partitions of 5 into at most 6 parts: $5, 4{+}1, 3{+}2, 3{+}1{+}1, 2{+}2{+}1, 2{+}1{+}1{+}1, 1{+}1{+}1{+}1{+}1$). However, the transition probabilities depend on which face the oldest roll was and whether the new face matches an existing one, so the partition alone is not quite a sufficient statistic. The exact implementation requires care, but the full $6^5$-state DP is perfectly tractable by computer. *Checking the window condition:* In state $(a_1, a_2, a_3, a_4, a_5)$, the new roll $f$ completes all 6 faces if and only if $f$ is the unique face missing from $\{a_1, \ldots, a_5\}$. This requires that $a_1, \ldots, a_5$ already contain exactly 5 distinct faces. The probability of absorbing on this step is $1/6$ if the current state has 5 distinct faces, and $0$ otherwise. **Answer:** For $n = 6$, the probability is $\frac{5}{324}$. For general $n$, there is no simple closed-form expression. The answer is computed via a Markov chain DP with state = last 5 rolls, absorbing whenever a window of 6 consecutive rolls contains all 6 faces. The result is $P = 1 - q_n$, where $q_n$ is obtained by iterating the $(7776 \times 7776)$ non-absorbing transition matrix $n - 5$ times from the uniform initial distribution. --- Quickselect: Finding the K-th Smallest Element [medium, Coding] --- https://quantvault.org/problem-1033-quickselect-finding-the-k-th-smallest-element.html Q: You have an unsorted array of $n$ elements and you want the $k$-th smallest one -- not the whole sorted array, just that one element. Describe the Quickselect algorithm. What is its average-case time complexity, and what is the worst case? How can you guarantee the worst case doesn't happen? A: **How to Think About It:** Quickselect is the selection analog of Quicksort -- same partitioning idea, but instead of recursing on both halves, you only recurse on the half that contains the $k$-th element. That is the insight that drops the complexity from $O(n \log n)$ to $O(n)$ on average. The worst case is the same trap as Quicksort: if you always pick the worst pivot, you do $O(n)$ work per level for $O(n)$ levels, giving $O(n^2)$. The fix is randomization or a clever pivot strategy. **Algorithm:** 1. Pick a pivot (randomly, or using median-of-3). 2. Partition the array into three groups: elements less than the pivot, elements equal to the pivot, elements greater than the pivot. 3. Let $L$ = size of the left group, $E$ = size of the equal group. - If $k \leq L$: recurse on the left group. - If $k \leq L + E$: the pivot is the answer. - Otherwise: recurse on the right group with target rank $k - L - E$. **Code:** ```python import random def quickselect(arr, k): """Returns the k-th smallest element (1-indexed).""" if len(arr) == 1: return arr[0] pivot = random.choice(arr) left = [x for x in arr if x pivot] if k <= len(left): return quickselect(left, k) elif k <= len(left) + len(mid): return pivot else: return quickselect(right, k - len(left) - len(mid)) ``` **Complexity:** - **Average case:** $O(n)$. With a random pivot, each level of recursion eliminates a constant fraction of elements in expectation. The recurrence $T(n) = T(n/2) + O(n)$ solves to $O(n)$ by the geometric series. - **Worst case:** $O(n^2)$. Occurs when the pivot is always the minimum or maximum -- you reduce the problem by only 1 element per level. - **Guaranteed $O(n)$:** Use the median-of-medians algorithm to choose a pivot guaranteed to be within the middle 30-70% of the data. This makes the worst case $O(n)$, though with a larger constant than randomized Quickselect in practice. **Answer:** Quickselect runs in $O(n)$ expected time with a random pivot, $O(n^2)$ worst case. Median-of-medians achieves $O(n)$ worst case. --- Ruler Length from Colored Segments [easy, Combinatorics] --- https://quantvault.org/problem-1042-ruler-length-from-colored-segments.html Q: A ruler is divided into four colored segments that together cover its entire length: - A blue segment that is $\frac{1}{4}$ of the ruler's total length - A red segment that is $\frac{1}{5}$ of the ruler's total length - A yellow segment that is $\frac{1}{6}$ of the ruler's total length - A green segment that is exactly 23 units long How long is the ruler? A: **How to Think About It:** The four segments partition the ruler with no overlap and no gap, so their lengths must sum to the total. Three of the four segments are given as fractions of the unknown total $L$, and one has a fixed length. Write one equation in $L$ and solve. **Quick Estimate:** The three fractional pieces take up $\frac{1}{4} + \frac{1}{5} + \frac{1}{6}$ of $L$. Finding a common denominator: $\frac{15}{60} + \frac{12}{60} + \frac{10}{60} = \frac{37}{60}$. So the green piece is the remaining $1 - \frac{37}{60} = \frac{23}{60}$ of $L$, and that equals 23 units. Immediately: $L = 60$. **Approach:** Set up the partition equation and solve for $L$. **Formal Solution:** Let $L$ be the total length of the ruler. The four segments are: $$\frac{L}{4} + \frac{L}{5} + \frac{L}{6} + 23 = L$$ Combine the fractional terms using LCD $= 60$: $$\frac{15L}{60} + \frac{12L}{60} + \frac{10L}{60} + 23 = L$$ $$\frac{37L}{60} + 23 = L$$ $$23 = L - \frac{37L}{60} = \frac{23L}{60}$$ $$L = \frac{23 \times 60}{23} = 60$$ **Answer:** The ruler is $\boxed{60}$ units long. --- Shortest Subarray with Exactly K Distinct Values [hard, Coding] --- https://quantvault.org/problem-1045-shortest-subarray-with-exactly-k-distinct-values.html Q: You are given an array $a_1, a_2, \ldots, a_n$ of integers and a positive integer $K$. A subarray is a contiguous slice of the array. Find the length of the shortest subarray that contains **exactly** $K$ distinct values. If no such subarray exists, return $-1$. Implement the function: ```python def shortest_exactly_k(arr, K): ... ``` It must return an `int` (the length), or `-1` when no qualifying subarray exists. Aim for $O(n)$ time using the two-window trick: express "exactly $K$ distinct" as the gap between an "at most $K$" window and an "at most $K-1$" window, maintaining element frequency counts as each window expands and contracts. **Example** `shortest_exactly_k([1, 2, 1, 3, 4], 3) -> 3` The shortest subarray with exactly 3 distinct values is length 3 (e.g. `[2, 1, 3]` or `[1, 3, 4]`); no shorter window reaches 3 distinct values. A: **How to Think About It:** The word "exactly" is what makes this tricky. Sliding windows naturally handle inequalities ("at most K distinct"): expand the right edge, and shrink the left edge whenever the constraint is violated. "Exactly K" is both a lower and an upper bound at once. The key trick is to run **two windows sharing the same right endpoint**: - one kept at **at most K** distinct (left pointer `left_k`), - one kept at **at most K-1** distinct (left pointer `left_k1`). For a subarray ending at `right`, the number of distinct values is non-increasing as the left endpoint moves rightward. So the left endpoints giving *exactly* K distinct form the contiguous range `[left_k, left_k1 - 1]`. The **shortest** window ending at `right` uses the largest valid left, `left_k1 - 1`, giving length `right - (left_k1 - 1) + 1`. Both pointers advance monotonically, so it is a single O(n) pass. **Function signature:** `shortest_exactly_k(arr, K)` returns an `int` — the length of the shortest contiguous subarray of `arr` with exactly `K` distinct values, or `-1` if none exists. **Code:** ```python from collections import defaultdict def shortest_exactly_k(arr, K): # Length of the shortest contiguous subarray with EXACTLY K distinct # values, or -1 if none exists. n = len(arr) if n == 0 or K K: out = arr[left_k] count_k[out] -= 1 if count_k[out] == 0: del count_k[out] left_k += 1 while len(count_k1) > K - 1: out = arr[left_k1] count_k1[out] -= 1 if count_k1[out] == 0: del count_k1[out] left_k1 += 1 # Exactly-K window exists iff the "at most K" window truly holds K # distinct and there is room between the two left pointers. if len(count_k) == K and left_k1 - 1 >= left_k: ans = min(ans, right - (left_k1 - 1) + 1) return ans if ans < float('inf') else -1 ``` **Complexity:** - **Time:** O(n) — each element enters and leaves each frequency map at most once, so all pointer motion amortizes to O(n). - **Space:** O(K) — each frequency map holds at most K distinct keys. --- Sum of Weights From Pairwise Sums [easy, Combinatorics] --- https://quantvault.org/problem-1054-sum-of-weights-from-pairwise-sums.html Q: You have five objects with distinct weights. When weighed in all possible pairs of two, the pairwise sums are: $$31, 32, 33, 34, 35, 36, 37, 38, 39, 40 \text{ kg}$$ What is the total weight of all five objects? A: **How to Think About It:** You have 5 unknowns and are given 10 pairwise sums. Solving a system of 10 equations in 5 unknowns is overkill if all you need is the total. The key observation is a symmetry argument: each weight appears in the same number of pairwise sums. If you add up all the pairwise sums, each individual weight gets counted the same number of times, so you can recover the total directly. **Quick Estimate:** The 10 pairwise sums range from 110 to 121, with mean $115.6$. Each pairwise sum averages two weights, so the average individual weight is about $115.6 / 2 \approx 57.8$. The total of five weights is roughly $5 \times 57.8 = 289$. Let's see if the exact answer is close. **Approach:** Count how many times each weight appears across all pairwise sums, then relate the sum of pairwise sums to the total weight. **Formal Solution:** Label the five weights $a, b, c, d, e$. The $\binom{5}{2} = 10$ pairwise sums are: $$a+b,\; a+c,\; a+d,\; a+e,\; b+c,\; b+d,\; b+e,\; c+d,\; c+e,\; d+e$$ Each weight is paired with the other 4 weights, so each weight appears in exactly 4 of the 10 sums. Adding all pairwise sums: $$\sum_{\text{all pairs}} = 4(a + b + c + d + e) = 4S$$ where $S = a + b + c + d + e$ is the total weight. The sum of the given pairwise values is: $$110 + 112 + 113 + 114 + 115 + 116 + 117 + 118 + 120 + 121 = 1156$$ So: $$4S = 1156 \implies S = \frac{1156}{4} = 289$$ **Answer:** The total weight of all five objects is $\dfrac{1156}{4} = 289$ kg. --- Three Sum [medium, Coding] --- https://quantvault.org/problem-1057-three-sum.html Q: Given an array of integers `nums`, return all unique triplets `[a, b, c]` drawn from elements at distinct indices such that `a + b + c == 0`. Implement the function: ```python def three_sum(nums): ``` The answer must be canonical and gradable: - Each triplet is sorted ascending: `a [[-1, -1, 2], [-1, 0, 1]]` Sorting the input gives `[-4, -1, -1, 0, 1, 2]`; the two zero-sum triplets are `[-1, -1, 2]` and `[-1, 0, 1]`, listed in lexicographic order with duplicates removed. A: **Approach.** Sort the array, then for each index `i` run a two-pointer scan over the remaining suffix. Because the array is sorted, moving `lo` right increases the sum and moving `hi` left decreases it, so each fixed `i` is solved in O(n). Skip repeated values at `i`, `lo`, and `hi` to avoid duplicate triplets. Overall time is O(n^2). Since the input is sorted first, triplets are naturally emitted in ascending lexicographic order and each triplet is already ascending, matching the required canonical form. ```python def three_sum(nums): nums = sorted(nums) n = len(nums) res = [] for i in range(n - 2): if i > 0 and nums[i] == nums[i - 1]: continue lo, hi = i + 1, n - 1 while lo 0: hi -= 1 else: res.append([nums[i], nums[lo], nums[hi]]) lo += 1 hi -= 1 while lo < hi and nums[lo] == nums[lo - 1]: lo += 1 while lo < hi and nums[hi] == nums[hi + 1]: hi -= 1 return res ``` --- Three-Card Straight From Combined Decks [medium, Probability] --- https://quantvault.org/problem-1058-three-card-straight-from-combined-decks.html Q: You take $k \geq 1$ standard 52-card decks and shuffle them together into one big deck. You draw 3 cards uniformly at random without replacement. A "three-card straight" means the three cards have three consecutive ranks (e.g., 5-6-7 or Q-K-A). An Ace can serve as either the low card (A-2-3) or the high card (Q-K-A). Suits don't matter -- straight flushes count too. Let $p_k$ be the probability the three drawn cards form a three-card straight. Find $\displaystyle\lim_{k \to \infty} p_k$. A: **How to Think About It:** When you combine $k$ decks and only draw 3 cards, the key question is: what happens to "without replacement" as $k$ grows? With $52k$ cards in the deck, removing 3 is negligible -- the conditional probabilities barely shift from the unconditional ones. In the limit, drawing without replacement from $k$ decks behaves exactly like drawing with replacement from a single deck. So the problem reduces to: pick 3 cards independently and uniformly from a standard 52-card deck. What is the probability they form a consecutive-rank triple? **Quick Estimate:** There are 13 ranks. The valid lowest ranks for a 3-card straight are A through Q, giving 12 starting points. For each starting point, the 3 ranks are fixed. Each rank has 4 suits, so $4^3 = 64$ suit combinations per straight. The 3 cards can arrive in any of $3! = 6$ orderings. Total favorable outcomes: $12 \times 6 \times 64 = 4{,}608$. Total outcomes (with replacement): $52^3 = 140{,}608$. Probability: $4{,}608 / 140{,}608 = 72/2{,}197 \approx 0.03277$. So roughly a 3.3% chance. **Approach:** Formalize the with-replacement argument via the $k \to \infty$ limit. **Formal Solution:** When $k \to \infty$, the deck has $52k$ cards. The probability of drawing any particular card on the first draw is $k/(52k) = 1/52$. After removing one card, the probability of drawing a particular card on the second draw is $k/(52k - 1)$ (or $(k-1)/(52k-1)$ if it shares a rank-suit with the first card). Either way, as $k \to \infty$ this approaches $1/52$. The same holds for the third draw. So in the limit, the three draws are independent, each uniform over the 52 card types. Now count three-card straights under with-replacement sampling: 1. **Number of valid rank triples:** The lowest rank in a straight can be A, 2, 3, ..., Q -- that is 12 choices. Each choice fixes a unique triple of consecutive ranks. 2. **Orderings:** The three ranks can appear in any of $3! = 6$ orders among the three draws. 3. **Suit choices:** Each of the 3 cards can be any of 4 suits independently, giving $4^3 = 64$ combinations. 4. **Total favorable:** $12 \times 6 \times 64 = 4{,}608$. 5. **Total outcomes:** $52^3 = 140{,}608$. Therefore: $$\lim_{k \to \infty} p_k = \frac{12 \times 3! \times 4^3}{52^3} = \frac{4{,}608}{140{,}608} = \frac{72}{2{,}197}$$ **Answer:** $\displaystyle\lim_{k \to \infty} p_k = \frac{72}{2{,}197} \approx 0.03277$. --- Three-State Subset Count with Bold Elements [easy, Combinatorics] --- https://quantvault.org/problem-1059-three-state-subset-count-with-bold-elements.html Q: Erica is constructing a subset $S$ of $[n] = \{1, 2, \ldots, n\}$. For any element $x \in [n]$, she has three options: - Exclude $x$ from $S$ - Include $x$ in $S$ (confident) - Include $x$ in $S$ but boldface it (uncertain) An element may appear at most once in $S$, so the set $\{1, 2, \mathbf{2}, 4\}$ is invalid. For example, $\{1, 2\}$, $\{1, \mathbf{3}, 4\}$, and $\{\mathbf{1}, \mathbf{2}, \mathbf{3}, \mathbf{4}\}$ are all valid. How many valid subsets $S$ can Erica construct when $n = 5$? A: **How to Think About It:** The trick is recognizing that each element is handled independently. For any single element, there are exactly three mutually exclusive states: out, in (confident), in (bold). Once you see that, the counting is immediate -- it is just $3^n$. **Quick Estimate:** With $n = 5$ and 3 choices per element: $3^5 = 243$. Sanity check: ordinary subsets (without bold) give $2^5 = 32$. Adding a third option per element multiplies the count by another factor: $32 \times (3/2)^5 \approx 32 \times 7.59 \approx 243$. Checks out. **Formal Solution:** For each element $x \in \{1, 2, 3, 4, 5\}$, there are exactly 3 choices: 1. Exclude $x$ (not in $S$) 2. Include $x$ normally 3. Include $x$ in bold These choices are independent across elements, so by the multiplication principle: $$|\mathcal{S}| = 3^n$$ For $n = 5$: $$3^5 = 243$$ **Answer:** $\boxed{243}$ --- Two-Suit Five-Card Hand Count [easy, Combinatorics] --- https://quantvault.org/problem-1068-two-suit-five-card-hand-count.html Q: A standard deck has 52 cards divided into 4 suits of 13 cards each. How many 5-card hands contain exactly 2 cards from one suit and exactly 3 cards from a different suit? Note: the two suits play different roles (one contributes 2 cards, the other contributes 3), so pay attention to whether the order of suit selection matters. A: **How to Think About It:** The key question is whether swapping the two suits gives a different hand. Yes -- a hand with 3 hearts and 2 spades is a different hand from 3 spades and 2 hearts. So we need an ordered selection of 2 suits from 4, not just an unordered pair. Once the suits are assigned, the card selection within each suit is independent and unordered. **Quick Estimate:** There are 4 suits, so there are $4 \times 3 = 12$ ordered ways to pick the "3-card suit" and the "2-card suit". Within each suit, we are choosing from 13 cards. The cards-per-suit counts are $\binom{13}{3} = 286$ and $\binom{13}{2} = 78$. Rough product: $12 \times 286 \times 78 \approx 12 \times 22{,}000 \approx 264{,}000$. The exact answer should be close to that. **Approach:** Use the multiplication principle: choose the ordered pair of suits, then choose cards from each suit independently. **Formal Solution:** **Step 1: Choose the suits.** We need to assign one suit the role of "3-card suit" and another the role of "2-card suit". Since these roles are distinct, we use a permutation: $$P(4,2) = 4 \times 3 = 12$$ **Step 2: Choose cards from the 3-card suit.** Select 3 cards from 13 in that suit: $$\binom{13}{3} = 286$$ **Step 3: Choose cards from the 2-card suit.** Select 2 cards from 13 in the other suit: $$\binom{13}{2} = 78$$ **Step 4: Multiply.** $$P(4,2) \cdot \binom{13}{3} \cdot \binom{13}{2} = 12 \times 286 \times 78 = 267{,}696$$ **Answer:** $\boxed{267{,}696}$ --- Alternating Sum of Squares [easy, Combinatorics] --- https://quantvault.org/problem-1082-alternating-sum-of-squares.html Q: Define the alternating sum of squares: $$S_n = (2n)^2 - (2n-1)^2 + (2n-2)^2 - (2n-3)^2 + \cdots + 2^2 - 1^2$$ Find a closed-form expression for $S_n$, and compute $S_{50}$. A: **How to Think About It:** This is a telescoping/pairing problem. The terms alternate in sign, which screams "pair them up." Each pair is a difference of consecutive squares, and there is a clean factorization for $a^2 - b^2 = (a+b)(a-b)$. Once you pair and simplify, you will get a sum of a simple arithmetic expression. **Quick Estimate:** For $n = 50$, the sum starts with $100^2 - 99^2 = 199$ and ends with $2^2 - 1^2 = 3$. There are 50 pairs. The average pair value is roughly $(199 + 3)/2 = 101$, so the sum is approximately $50 \times 101 = 5050$. Let's see if the exact answer matches. **Approach:** Group the $2n$ terms into $n$ consecutive pairs, apply the difference-of-squares identity, and sum. **Formal Solution:** Group the terms into pairs of the form $(2i)^2 - (2i-1)^2$ for $i = 1, 2, \ldots, n$: $$S_n = \sum_{i=1}^{n} \left[(2i)^2 - (2i-1)^2\right]$$ Apply the difference-of-squares factorization: $$(2i)^2 - (2i-1)^2 = (2i + 2i - 1)(2i - 2i + 1) = (4i - 1)(1) = 4i - 1$$ So: $$S_n = \sum_{i=1}^{n} (4i - 1) = 4 \sum_{i=1}^{n} i - \sum_{i=1}^{n} 1 = 4 \cdot \frac{n(n+1)}{2} - n = 2n(n+1) - n = n(2n + 1)$$ For $n = 50$: $$S_{50} = 50 \times 101 = 5050$$ **Answer:** $S_n = n(2n+1)$, and $S_{50} = 5050$. --- Approximating Zeros of a Continuous Function [medium, Coding] --- https://quantvault.org/problem-1085-approximating-zeros-of-a-continuous-function.html Q: You are given a polynomial $f(x) = c_0 + c_1 x + \dots + c_d x^d$, specified by its coefficient list `coeffs = [c0, c1, ..., cd]`, on an interval $[a, b]$ where $f(a)$ and $f(b)$ have strictly opposite signs. By the Intermediate Value Theorem a zero lies in $[a, b]$. Implement the function: ```python def find_root(coeffs, a, b, tol): ... ``` Use **bisection**: repeatedly halve the bracketing interval, keeping the half that still contains a sign change, until the interval width is $\le$ `tol`. Return the **midpoint of the final bracket** (a float). 1. Explain the simplest guaranteed approach (bisection) and analyze its convergence. 2. Describe at least one faster alternative (Newton's method, secant, or Brent) and explain the trade-off. 3. What method would you recommend in practice, and why? **Example** `find_root([-2.0, 0.0, 1.0], 0.0, 2.0, 1e-10) -> 1.414213562355144` Here $f(x) = x^2 - 2$ on $[0, 2]$: $f(0) = -2 0$, so bisection narrows the bracket to width $\le 10^{-10}$ around $\sqrt{2}$ and returns the final midpoint. A: **How to Think About It:** The Intermediate Value Theorem guarantees a zero exists in $[a, b]$ when $f(a)$ and $f(b)$ have opposite signs. The most natural strategy is to narrow that interval. If you can query $f$ at one point, the midpoint gives the most information — it halves the search space every step. That is bisection: rock-solid, linear convergence (one bit of accuracy per iteration). Faster methods (Newton, secant, Brent) trade robustness for speed. **Contract for this problem:** You must implement `find_root(coeffs, a, b, tol)` where `coeffs = [c0, c1, ..., cd]` describes $f(x) = c_0 + c_1 x + \dots + c_d x^d$. It is guaranteed that $f(a)$ and $f(b)$ have strictly opposite signs. Halve the bracket until its width is $\le$ `tol`, then return the **midpoint of the final bracket** (a float), not the list of coefficients or the number of iterations. **Key details that make the tests pass:** - Evaluate the polynomial with Horner's rule for stability. - Loop while `(b - a) > tol`. - Keep the sub-interval that still brackets the root: if `f(a) * f(m) tol: m = (a + b) / 2.0 fm = f(m) if fa * fm <= 0: b = m else: a = m fa = fm return (a + b) / 2.0 ``` **Faster alternatives (interview talking points):** Newton's method $x_{n+1} = x_n - f(x_n)/f'(x_n)$ converges quadratically but needs a derivative and a good starting guess. The secant method is superlinear (order $\approx 1.618$) with no derivative. **Brent's method** is the production standard (`scipy.optimize.brentq`): it combines bisection's guaranteed convergence with interpolation's speed. In practice, recommend Brent for general use, or bisection when you need a guarantee with a possibly nasty function. --- Correct Interpretation of a Frequentist Confidence Interval [easy, Statistics] --- https://quantvault.org/problem-1102-correct-interpretation-of-a-frequentist-confidence-interval.html Q: What is the correct frequentist interpretation of a $100(1-\alpha)\%$ confidence interval for a parameter $\theta$? Specifically: is it correct to say that $\theta$ lies in the interval with probability $1-\alpha$? Why or why not? What does the interval actually tell you? A: **How to Think About It:** This is one of the most commonly misunderstood concepts in statistics. The tempting (wrong) interpretation is: 'There is a $95\%$ probability that $\theta$ is inside this interval.' But from the frequentist perspective, $\theta$ is a fixed, non-random constant -- it either is or is not in any given interval, with probability 0 or 1. The probability $1-\alpha$ refers to the procedure, not to any particular realized interval. **Key Insight:** In frequentist statistics, randomness comes from the data (the sample), not from the parameter. The confidence interval is random (because it is computed from random data); the parameter is not. The correct interpretation must live in the world of hypothetical repeated sampling. **The Correct Interpretation:** A $100(1-\alpha)\%$ confidence interval $[L(X), U(X)]$ satisfies: $$P\left(L(X) \leq \theta \leq U(X)\right) = 1 - \alpha$$ where the probability is over the randomness in the data $X$. That is: > If you repeated the experiment many times -- drawing a new sample each time, computing a new interval each time -- then approximately $100(1-\alpha)\%$ of those intervals would contain the true $\theta$. Once you have computed a specific interval from your data, say $[2.1, 4.7]$, you cannot say 'there is a 95% probability that $\theta$ is between 2.1 and 4.7.' At that point, $\theta$ either is or is not in $[2.1, 4.7]$ -- no probability is involved. **What is and is not random:** - The interval endpoints $L(X)$ and $U(X)$ are random (they depend on the sample $X$). - The parameter $\theta$ is fixed and non-random (frequentist assumption). - The statement '$\theta$ falls in the interval' is what has a $1-\alpha$ probability -- before the data is collected. **Contrast with Bayesian credible intervals:** A Bayesian $95\%$ credible interval does allow the statement 'there is a 95% probability that $\theta$ is in this interval' -- because in the Bayesian framework, $\theta$ itself has a probability distribution (the posterior). Frequentist and Bayesian intervals are computed differently and have different interpretations. **Answer:** The correct interpretation is: the procedure that produced this interval will cover the true $\theta$ in $100(1-\alpha)\%$ of repeated experiments. You cannot assign a probability to whether $\theta$ is inside any specific computed interval. --- Counting Digit Occurrences in a Range [hard, Combinatorics] --- https://quantvault.org/problem-1106-counting-digit-occurrences-in-a-range.html Q: Start with a warm-up: how many times does the digit $4$ appear in the decimal representations of the integers $1, 2, \ldots, 1000$? Now generalize. Given an integer $N \leq 10^{18}$ and a digit $d \in \{0, 1, \ldots, 9\}$, design an $O(\log N)$-time, $O(1)$-space algorithm that returns the total number of times digit $d$ appears across all integers in $[1, N]$. Be explicit about the handling of $d = 0$ -- leading zeros should not be counted (e.g., the number $7$ is just "7", not "007"). Finally, extend your algorithm to work in an arbitrary base $b \geq 2$. A: **How to Think About It:** The brute-force approach -- iterate through every number and count digits -- is $O(N \log N)$, which is hopeless for $N = 10^{18}$. The key insight is to think about each digit position independently. For a given position (ones, tens, hundreds, ...), you can compute exactly how many numbers in $[1, N]$ have digit $d$ in that position using simple arithmetic on the digits of $N$ itself. This gives you an $O(\log N)$ algorithm with $O(1)$ space. **Quick Estimate (warm-up):** Count the digit $4$ in $1$ through $1000$. Consider a three-digit template $\_ \_ \_$ representing numbers $000$ to $999$ (we will think of all numbers as three-digit with leading zeros for now). There are $1000$ numbers and $3$ digit positions, so $3000$ total digit slots. By symmetry each digit $0$-$9$ occupies exactly $1/10$ of those slots, giving $300$ occurrences of each digit. So digit $4$ appears exactly $300$ times in $\{0, 1, \ldots, 999\}$. The number $1000$ contains no $4$'s, so the answer for $[1, 1000]$ is $\boxed{300}$. **Approach:** Process $N$ one digit position at a time, from the most significant to the least significant. For each position, split $N$'s digits into the "high" part (digits above the current position), the "current" digit, and the "low" part (digits below). **Formal Solution:** Let $N$ have digits $a_k a_{k-1} \cdots a_1 a_0$ in base $10$. For position $i$ (where position $0$ is the ones place), define: - $\text{high}_i = \lfloor N / 10^{i+1} \rfloor$ (the number formed by digits above position $i$) - $\text{cur}_i = a_i$ (the digit at position $i$) - $\text{low}_i = N \bmod 10^i$ (the number formed by digits below position $i$) - $p = 10^i$ (the place value) The count of times digit $d > 0$ appears at position $i$ across $[1, N]$ is: $$\text{count}_i = \begin{cases} \text{high}_i \times p & \text{if } \text{cur}_i d \end{cases}$$ The total count is $\sum_i \text{count}_i$. **Handling $d = 0$:** When $d = 0$, leading zeros must be excluded. The formula above would count leading zeros (e.g., it would count "007" as having two zeros). The fix: for position $i$, the leading-zero contribution is exactly $p$ (the numbers $0$ through $p - 1$ all have a leading zero at position $i$). So subtract $p$ from the formula for each position, which means replacing $\text{high}_i$ with $(\text{high}_i - 1)$ in the $\text{cur}_i d$ cases, and adjusting accordingly: $$\text{count}_i^{(d=0)} = \begin{cases} (\text{high}_i - 1) \times p + (\text{low}_i + 1) & \text{if } \text{cur}_i = 0 \\ \text{high}_i \times p & \text{if } \text{cur}_i > 0 \end{cases}$$ (When $\text{cur}_i = 0$, the original formula gives $\text{high}_i \times p + \text{low}_i + 1$; subtracting $p$ gives $(\text{high}_i - 1) \times p + \text{low}_i + 1$. When $\text{cur}_i > 0$, the original gives $(\text{high}_i + 1) \times p$; subtracting $p$ gives $\text{high}_i \times p$.) **Code:** ```python def count_digit(N: int, d: int) -> int: """Count occurrences of digit d in [1, N], base 10.""" if N 0: if cur int: """Count occurrences of digit d in [1, N], base b.""" if N 0: if cur < d: count += high * p elif cur == d: count += high * p + low + 1 else: count += (high + 1) * p else: if cur == 0: count += (high - 1) * p + low + 1 else: count += high * p p *= b return count ``` **Complexity:** Time $O(\log_b N)$ (one pass per digit position). Space $O(1)$. **Verification of warm-up:** `count_digit(1000, 4)` returns $300$, matching our symmetry argument. **Answer:** The digit $4$ appears $300$ times in $[1, 1000]$. The general algorithm processes each of the $O(\log N)$ digit positions independently, computing the contribution using the high/current/low decomposition. For $d = 0$, subtract one place value per position to exclude leading zeros. The same logic extends to any base $b$ by replacing $10$ with $b$. --- Design a Class with Save and N-th Smallest Query [medium, Coding] --- https://quantvault.org/problem-1119-design-a-class-with-save-and-n-th-smallest-query.html Q: Implement `run_ops(ops)`, which replays a list of operations on an order-statistic store. Each operation is one of: - `["save", x]` — store the integer `x` (duplicates are kept). - `["query", n]` — return the n-th smallest stored integer (1-indexed; duplicates count as distinct positions). Return the list of results, one per `query` op, in the order the queries appear. `save` ops produce no output. **Function signature:** `run_ops(ops)` where `ops` is a list of `["save", x]` / `["query", n]` operations, returning a list of ints. **Example** `run_ops([["save",5],["save",3],["save",8],["save",1],["save",3],["query",1],["query",2],["query",3],["query",4]]) -> [1, 3, 3, 5]` After the saves the sorted store is `[1, 3, 3, 5, 8]`, so the 1st/2nd/3rd/4th smallest values are `1, 3, 3, 5` respectively. A: Maintain a running collection of every integer added by a `save` op. For each `query n` op, the n-th smallest stored value (1-indexed, duplicates counted) is simply the element at index `n-1` of the sorted collection. Collect one result per `query` op and return them in order — `save` ops produce no output. ```python def run_ops(ops): store = [] results = [] for op in ops: kind = op[0] if kind == "save": store.append(op[1]) elif kind == "query": n = op[1] results.append(sorted(store)[n - 1]) return results ``` --- Digits of 2^1000 and 5^1000 Written Side by Side [easy, Combinatorics] --- https://quantvault.org/problem-1120-digits-of-2-1000-and-5-1000-written-side-by-side.html Q: Write $2^{1000}$ and $5^{1000}$ next to each other in decimal -- just concatenate their decimal representations. How many digits does the resulting number have? For example, if you concatenated $2^3 = 8$ and $5^3 = 125$, you would get the number $8125$, which has 4 digits. A: **How to Think About It:** The first thing to notice is that $2^{1000} \times 5^{1000} = 10^{1000}$. This is the key. It means the number of digits in these two numbers must be tightly coupled -- together they almost "add up" to the digits of $10^{1000}$, which has exactly 1001 digits. So before doing any arithmetic, your gut should say: the answer is probably 1001 digits. Now let's verify that and see exactly why. **Quick Estimate:** The number of digits of a positive integer $n$ is $\lfloor \log_{10} n \rfloor + 1$. We know $\log_{10} 2 \approx 0.30103$, so: $$1000 \log_{10} 2 \approx 301.03$$ So $2^{1000}$ has $\lfloor 301.03 \rfloor + 1 = 302$ digits. Since $\log_{10} 2 + \log_{10} 5 = 1$, we get $1000 \log_{10} 5 \approx 698.97$, so $5^{1000}$ has $\lfloor 698.97 \rfloor + 1 = 699$ digits. Total: $302 + 699 = 1001$. **Approach:** Show rigorously why $a + b = 1001$ without needing to know the decimal expansion of $\log_{10} 2$. **Formal Solution:** Let $a$ be the number of digits of $2^{1000}$ and $b$ the number of digits of $5^{1000}$. Then: $$a = \lfloor 1000 \log_{10} 2 \rfloor + 1, \quad b = \lfloor 1000 \log_{10} 5 \rfloor + 1$$ So $a + b = \lfloor 1000 \log_{10} 2 \rfloor + \lfloor 1000 \log_{10} 5 \rfloor + 2$. Let $x = 1000 \log_{10} 2$. Then $1000 \log_{10} 5 = 1000 - x$ (since $\log_{10} 2 + \log_{10} 5 = 1$). The key question is whether $x$ is an integer. $x$ is an integer only if $2^{1000}$ is a power of 10, which is impossible since $2^{1000}$ is not divisible by 5. Therefore $x$ is not an integer, meaning $x$ has a nonzero fractional part, and the fractional parts of $x$ and $1000 - x$ sum to exactly 1. By the identity $\lfloor x \rfloor + \lfloor 1000 - x \rfloor = 999$ (when $x$ is not an integer), we get: $$a + b = 999 + 2 = 1001$$ **Answer:** The concatenated number has $\boxed{1001}$ digits. --- Dividing Students into Labeled Groups [medium, Combinatorics] --- https://quantvault.org/problem-1122-dividing-students-into-labeled-groups.html Q: A class of 12 students must split into 4 groups of 3, where the groups are labeled 1 through 4 (i.e., the groups are distinguishable). How many distinct ways can the class form these groups? A: **How to Think About It:** The key question in any "partition into groups" problem is: are the groups distinguishable or not? Here, the groups are numbered 1-4, so they are distinguishable. That makes things simpler -- assigning student A to Group 1 and student B to Group 2 is different from assigning A to Group 2 and B to Group 1. If the groups were unlabeled, you would need to divide by the number of ways to permute the groups, but that is not the case here. **Quick Estimate:** We are distributing 12 people into 4 labeled bins of size 3. A rough sanity check: there are $12!$ ways to line up all 12 students, and we need to divide by $3!$ for the ordering within each group (since groups are sets, not sequences). That gives $12! / (3!)^4$. With $12! = 479{,}001{,}600$ and $(3!)^4 = 1296$, we get about 370,000. Let's verify. **Approach:** This is a multinomial coefficient problem. **Formal Solution:** Assigning 12 distinct students to 4 labeled groups of size 3 is equivalent to counting anagrams of a string with 3 copies each of 4 distinct symbols (say, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4). Each arrangement corresponds to a group assignment: if the $i$-th character is $j$, student $i$ goes to group $j$. The number of such anagrams is the multinomial coefficient: $$\binom{12}{3, 3, 3, 3} = \frac{12!}{3! \cdot 3! \cdot 3! \cdot 3!} = \frac{12!}{(3!)^4}$$ Computing: $$\frac{479{,}001{,}600}{6^4} = \frac{479{,}001{,}600}{1{,}296} = 369{,}600$$ Alternatively, think of it sequentially: choose 3 of 12 for group 1 ($\binom{12}{3}$), then 3 of the remaining 9 for group 2 ($\binom{9}{3}$), then 3 of 6 for group 3 ($\binom{6}{3}$), and the last 3 go to group 4 ($\binom{3}{3}$): $$\binom{12}{3}\binom{9}{3}\binom{6}{3}\binom{3}{3} = 220 \times 84 \times 20 \times 1 = 369{,}600$$ **Answer:** There are $\dfrac{12!}{(3!)^4} = 369{,}600$ distinct ways. --- Expected Value and Variance of the Absolute Difference of Uniforms [medium, Expectation] --- https://quantvault.org/problem-1130-expected-value-and-variance-of-the-absolute-difference-of-un.html Q: Let $X$ and $Y$ be independent, each uniformly distributed on $[0, 60]$. Compute: 1. $E[|X - Y|]$ 2. $\text{Var}(|X - Y|)$ A: **How to Think About It:** You have two independent uniform draws from $[0, 60]$ and want the expected absolute difference and its variance. For the expectation, symmetry helps: $|X - Y|$ has the same distribution as $|Y - X|$, so you can compute $E[|X - Y|] = 2 E[\max(X - Y, 0)]$ and split the integral into the region where $X > Y$. For a general Uniform$(0, a)$, dimensional analysis suggests $E[|X - Y|]$ scales linearly with $a$, and a quick computation confirms $E[|X - Y|] = a/3$. **Quick Estimate:** For $\text{Uniform}(0, a)$, the expected absolute difference is $a/3$. With $a = 60$, that gives $E[|X - Y|] = 20$. For the variance, we need $E[(X-Y)^2]$. Since $X$ and $Y$ are independent with the same distribution, $\text{Var}(X - Y) = 2 \text{Var}(X) = 2 \times 60^2/12 = 600$. Then $\text{Var}(|X - Y|) = E[(X-Y)^2] - (E[|X-Y|])^2 = 600 - 400 = 200$. **Approach:** Compute $E[|X - Y|]$ by splitting into two symmetric regions, then use the identity $\text{Var}(|X-Y|) = E[(X-Y)^2] - (E[|X-Y|])^2$. **Formal Solution:** **Part 1: $E[|X - Y|]$** The joint density is $f(x, y) = 1/3600$ on $[0, 60]^2$. $$E[|X - Y|] = \int_0^{60} \int_0^{60} \frac{|x - y|}{3600} \, dx \, dy$$ By symmetry ($|x - y| = |y - x|$), split into $x > y$ and double: $$= 2 \int_0^{60} \int_0^x \frac{x - y}{3600} \, dy \, dx$$ The inner integral: $$\int_0^x (x - y) \, dy = \left[xy - \frac{y^2}{2}\right]_0^x = x^2 - \frac{x^2}{2} = \frac{x^2}{2}$$ So: $$E[|X - Y|] = \frac{2}{3600} \int_0^{60} \frac{x^2}{2} \, dx = \frac{1}{3600} \int_0^{60} x^2 \, dx = \frac{1}{3600} \cdot \frac{60^3}{3} = \frac{216000}{10800} = 20$$ **Part 2: $\text{Var}(|X - Y|)$** Note that $|X - Y|^2 = (X - Y)^2$, so: $$E[|X - Y|^2] = E[(X - Y)^2] = \text{Var}(X - Y) + (E[X - Y])^2$$ Since $E[X] = E[Y] = 30$, we have $E[X - Y] = 0$. And by independence: $$\text{Var}(X - Y) = \text{Var}(X) + \text{Var}(Y) = 2 \cdot \frac{60^2}{12} = 2 \times 300 = 600$$ Therefore: $$\text{Var}(|X - Y|) = E[(X-Y)^2] - (E[|X-Y|])^2 = 600 - 20^2 = 600 - 400 = 200$$ **General formula for $\text{Uniform}(0, a)$:** - $E[|X - Y|] = a/3$ - $\text{Var}(|X - Y|) = a^2/18$ Check: $60^2/18 = 3600/18 = 200$. **Answer:** $E[|X - Y|] = 20$ and $\text{Var}(|X - Y|) = 200$. --- Expected Value of the Geometric PMF at Its Own Random Variable [medium, Expectation] --- https://quantvault.org/problem-1131-expected-value-of-the-geometric-pmf-at-its-own-random-variab.html Q: Let $X \sim \text{Geom}(p)$ with PMF $p(k) = P(X = k) = p(1-p)^{k-1}$ for $k = 1, 2, 3, \ldots$ Compute $E[p(X)]$ -- the expected value of the PMF evaluated at the random variable itself -- when $p = 1/3$. **Note:** There is a tempting but incorrect shortcut. Identify the error in the following reasoning, then compute the correct answer: > "$E[p(X)] = E[P(X = X)] = E[1] = 1$." A: **How to Think About It:** The expression $E[p(X)]$ is asking you to plug the random variable $X$ into its own PMF and then take the expectation. This is a perfectly well-defined quantity -- it is just $\sum_k p(k)^2$, a sum of squared PMF values. The wrong shortcut confuses two different uses of the variable: $P(X = X)$ is not a valid probability statement because you are conflating the random variable $X$ (which takes a specific value on each draw) with a generic argument $k$. You must evaluate $p(k)$ as a function of $k$ first, then substitute $X$ for $k$. **Quick Estimate:** With $p = 1/3$, the PMF $p(k) = (1/3)(2/3)^{k-1}$ decays geometrically. The dominant term is $k=1$: $p(1) = 1/3$, contributing $(1/3)^2 = 1/9 \approx 0.111$ to $E[p(X)]$. The next term ($k=2$) contributes $(2/9)^2 = 4/81 \approx 0.049$. The sum will be dominated by small $k$, so a rough answer is around $0.11 + 0.05 + \ldots \approx 0.2$. We will see the exact answer is $1/5 = 0.2$. **Why the shortcut fails:** The expression $P(X = X)$ is ambiguous. The correct reading of $p(X)$ is: fix the realization $X = k$, evaluate $p(k) = p(1-p)^{k-1}$, then take the expectation over $k$. Writing $P(X = X)$ treats the argument and the random variable as the same object in the probability statement, which is a category error. **Formal Solution:** Apply LOTUS (Law of the Unconscious Statistician): $$E[p(X)] = \sum_{k=1}^{\infty} p(k) \cdot p(k) = \sum_{k=1}^{\infty} [p(k)]^2 = \sum_{k=1}^{\infty} \left[p(1-p)^{k-1}\right]^2$$ $$= p^2 \sum_{k=1}^{\infty} (1-p)^{2(k-1)} = p^2 \sum_{j=0}^{\infty} \left[(1-p)^2\right]^j = \frac{p^2}{1-(1-p)^2}$$ Simplify the denominator: $1-(1-p)^2 = 1 - 1 + 2p - p^2 = 2p - p^2 = p(2-p)$. So: $$E[p(X)] = \frac{p^2}{p(2-p)} = \frac{p}{2-p}$$ **Sanity check:** As $p \to 1$, $X = 1$ with probability $1$, so $p(X) = p(1) = 1$ with probability $1$, giving $E[p(X)] = 1$. Formula: $\frac{1}{2-1} = 1$. Correct. As $p \to 0$, the PMF spreads out and $E[p(X)] \to 0$. Formula: $\frac{p}{2} \to 0$. Correct. **Answer:** For $p = 1/3$: $$E[p(X)] = \frac{1/3}{2 - 1/3} = \frac{1/3}{5/3} = \frac{1}{5}$$ --- Expected Value of the Minimum of Two Uniforms [easy, Expectation] --- https://quantvault.org/problem-1132-expected-value-of-the-minimum-of-two-uniforms.html Q: Let $A \sim U(0, 5)$ and $B \sim U(0, 10)$ be independent uniform random variables. Compute $E[\min(A, B)]$. A: **How to Think About It:** When you see $E[\min(\cdot)]$ for independent continuous variables, your first instinct should be the survival function method: $E[X] = \int_0^\infty P(X > t)\,dt$ for non-negative $X$. For a minimum of independent variables, $P(\min(A,B) > t) = P(A > t)P(B > t)$, which factors nicely. This avoids the messier double integral approach. **Quick Estimate:** The minimum is bounded above by $A$, so $E[\min(A,B)] t)P(B > t)\,dt$. **Formal Solution:** The survival functions are: - $P(A > t) = \frac{5 - t}{5}$ for $t \in [0, 5]$, and $0$ for $t > 5$ - $P(B > t) = \frac{10 - t}{10}$ for $t \in [0, 10]$, and $0$ for $t > 10$ Since $P(A > t) = 0$ for $t > 5$, the integral is: $$E[\min(A,B)] = \int_0^5 \frac{(5-t)(10-t)}{50}\,dt$$ Expand the numerator: $$(5-t)(10-t) = 50 - 15t + t^2$$ Integrate: $$E[\min(A,B)] = \frac{1}{50}\int_0^5 (50 - 15t + t^2)\,dt = \frac{1}{50}\left[50t - \frac{15t^2}{2} + \frac{t^3}{3}\right]_0^5$$ Put the bracket over a common denominator of $6$ — note $250 = \frac{1500}{6}$, $\frac{375}{2} = \frac{1125}{6}$, and $\frac{125}{3} = \frac{250}{6}$: $$= \frac{1}{50}\left(250 - \frac{375}{2} + \frac{125}{3}\right) = \frac{1}{50}\cdot\frac{1500 - 1125 + 250}{6} = \frac{1}{50}\cdot\frac{625}{6}$$ Decimal check: $250 - 187.5 + 41.\overline{6} = 104.1\overline{6} = \frac{625}{6}$. $$E[\min(A,B)] = \frac{625}{6 \times 50} = \frac{625}{300} = \frac{25}{12}$$ **Answer:** $E[\min(A,B)] = \dfrac{25}{12} \approx 2.083$. --- Integer Arithmetic on Decimal Strings [medium, Coding] --- https://quantvault.org/problem-1149-integer-arithmetic-on-decimal-strings.html Q: You are given two non-negative integers represented as strings of decimal digits, plus an operation selector. Implement a single function: ``` arith(op, a, b) ``` where: - `op` is either `'add'` (addition) or `'mul'` (multiplication) - `a` and `b` are non-negative integers as decimal strings (only characters `'0'`-`'9'`, no leading zeros except the number `"0"` itself) Perform the requested operation using grade-school digit-by-digit arithmetic (addition with carry propagation, or long multiplication). Return the result as a decimal string with no leading zeros. Do **not** convert the whole string to an integer via `int(a)`. **Constraints:** - $1 \le |a|, |b| \le 10^4$ - Inputs contain only characters `'0'`-`'9'`, no leading zeros (except `"0"`) Analyze the time complexity of both operations. **Example** `arith("mul", "123", "456") -> "56088"` -- long multiplication of 123 and 456 yields 56088, returned as a leading-zero-free decimal string. Likewise `arith("add", "123", "456") -> "579"`. A: **How to Think About It:** This is grade-school arithmetic implemented from scratch. The judge calls a single entry point `arith(op, a, b)` where `op` is `'add'` or `'mul'`, and `a`, `b` are non-negative integer decimal strings. It dispatches to hand-rolled addition or long multiplication and returns the result as a decimal string with no leading zeros. The only subtlety is managing carries correctly. For addition, walk right-to-left through both strings, adding digits plus carry. For multiplication, compute partial products (one digit of `b` times all of `a`), shift them, and accumulate with repeated addition. **Algorithm:** *Addition (`op == 'add'`):* Scan both strings from right to left. At each position, add the two digits plus the incoming carry. The result modulo 10 is the output digit; the result divided by 10 is the new carry. Continue until both strings and the carry are exhausted, then strip leading zeros. *Multiplication (`op == 'mul'`):* For each digit `b[j]` (right to left), multiply it against every digit of `a` (right to left), accumulating carries into a partial product. Shift it left by appending `len(b)-1-j` zeros, then add all partial products together. Short-circuit to `'0'` if either operand is `'0'`. **Code:** ```python def _add(a, b): result = [] carry = 0 i, j = len(a) - 1, len(b) - 1 while i >= 0 or j >= 0 or carry: d1 = ord(a[i]) - 48 if i >= 0 else 0 d2 = ord(b[j]) - 48 if j >= 0 else 0 s = d1 + d2 + carry result.append(chr(s % 10 + 48)) carry = s // 10 i -= 1 j -= 1 out = ''.join(reversed(result)).lstrip('0') return out or '0' def _mul(a, b): if a == '0' or b == '0': return '0' result = "0" for j in range(len(b) - 1, -1, -1): carry = 0 partial = [] bj = ord(b[j]) - 48 for i in range(len(a) - 1, -1, -1): prod = (ord(a[i]) - 48) * bj + carry partial.append(chr(prod % 10 + 48)) carry = prod // 10 if carry: partial.append(chr(carry + 48)) partial_str = ''.join(reversed(partial)) partial_str += '0' * (len(b) - 1 - j) result = _add(result, partial_str) return result.lstrip('0') or '0' def arith(op, a, b): # op is 'add' or 'mul'. a and b are non-negative integer decimal strings. if op == 'add': return _add(a, b) elif op == 'mul': return _mul(a, b) raise ValueError("unknown op: " + str(op)) ``` **Complexity:** Let $n = |a|$ and $m = |b|$. - *Addition:* $O(\max(n, m))$ single-digit additions, constant work per step. - *Multiplication:* The outer loop runs $m$ times; each iteration does $n$ single-digit multiplications plus one addition of length up to $n + m$. Overall $O(nm)$. **Answer:** Addition runs in $O(\max(n, m))$ and multiplication runs in $O(nm)$ using only per-digit operations. This is the grade-school algorithm -- optimal without advanced techniques like Karatsuba. Note the earlier version defined separate `add`/`multiply` functions, but the judge only ever calls `arith(op, a, b)`, so the dispatch must live there. --- Largest Fancy Number At Most N [hard, Combinatorics] --- https://quantvault.org/problem-1154-largest-fancy-number-at-most-n.html Q: Call a positive integer *fancy* if every digit in its base-4 representation is either 0 or 1 (so 1, 4, 5, 16, 17, 20, 21, ... are fancy in decimal). Given an integer $N$ with $1 \leq N \leq 10^{18}$, return the largest fancy number that is $\leq N$. Your solution must run in $O(\log N)$ time. **Examples:** - $N = 6$: Base-4 digits of 6 are `12`. The largest fancy number $\leq 6$ is 5 (base-4: `11`). Output: `5` - $N = 1$: Already fancy. Output: `1` - $N = 100$: Base-4 is `1210`. Largest fancy $\leq 100$ is 85 (base-4: `1111`). Output: `85` A: **How to Think About It:** Fancy numbers -- integers whose base-4 digits are all in $\{0, 1\}$ -- are in bijection with binary strings: read a fancy number's base-4 digits as bits and you get a unique natural number, and vice versa. The heuristic for "largest X $\leq N$ with a digit constraint" is a greedy left-to-right scan: match $N$'s digits for as long as they are legal, and the first illegal digit decides everything. The key simplification here -- and the step many solutions overcomplicate -- is that **no branching or backtracking is ever needed**. You might worry that at a digit equal to 1 you should also consider dropping it to 0 (freeing the suffix to be all 1s). You never should: that dropped candidate equals $\text{prefix} + \underbrace{11\cdots1_4}_{r \text{ ones}} = \text{prefix} + \tfrac{4^r - 1}{3}$, which is *strictly less* than $\text{prefix} + 4^r$, the smallest value any tight continuation can reach. Staying tight always ends at something at least that large (either $N$ itself, or a later clamp that keeps this 1 in place), so the dropped candidate can never win. Greedy dominance kills the branch. **Quick Estimate:** $N = 100 = 1210_4$. Scan: $1$ (keep), $2$ (illegal -- clamp here). Answer $= 11\,11_4$: keep the leading 1, clamp the 2 down to 1, fill with 1s $= 64 + 16 + 4 + 1 = 85$. Matches the expected output with zero bookkeeping. **Algorithm:** 1. Write $N$ in base 4 as $d_k d_{k-1} \cdots d_0$ (most significant first). 2. Walk left to right, keeping a running prefix value. At position with $r$ digits remaining after it: - If $d_i \in \{0, 1\}$: keep it (add $d_i \cdot 4^r$ to the prefix) and continue. - If $d_i \geq 2$: clamp this digit to 1 and fill the rest with 1s. Return $\text{prefix} + 4^r + \tfrac{4^r - 1}{3}$ immediately. 3. If the scan finishes, every digit of $N$ is 0 or 1, so $N$ itself is fancy -- return $N$. Why the clamp is optimal: any fancy number sharing the prefix is at most the clamped value, and any fancy number that deviates from the prefix earlier had to turn some 1 into a 0, making it strictly smaller. So the first position where $N$'s digit is $\geq 2$ (if any) pins down the whole answer. **Code:** ```python def largest_fancy(N: int) -> int: # Base-4 digits of N, most significant first digits = [] tmp = N while tmp > 0: digits.append(tmp % 4) tmp //= 4 digits.reverse() k = len(digits) prefix = 0 for i, d in enumerate(digits): r = k - i - 1 # digits remaining after position i if d <= 1: prefix += d * (4 ** r) # keep the digit, stay tight else: ones = (4 ** r - 1) // 3 # base-4 '11...1' with r ones return prefix + 4 ** r + ones # clamp to 1, fill with 1s return N # every digit was 0/1 -- N is already fancy ``` Verified by brute force against direct enumeration for all $N \leq 2 \times 10^5$: exact agreement. **Complexity:** $O(\log_4 N) = O(\log N)$ time, one pass; $O(\log N)$ space for the digits. **Answer:** One greedy pass over $N$'s base-4 digits. Keep digits while they are 0 or 1; at the first digit $\geq 2$, clamp it to 1 and fill the suffix with 1s; if no such digit exists, $N$ is fancy. No candidate tracking or branching is needed -- dropping a 1 is always dominated by staying tight. --- Limit of Integer Partition Count [medium, Combinatorics] --- https://quantvault.org/problem-1156-limit-of-integer-partition-count.html Q: Let $x, y, z$ be positive integers satisfying $x + y + z = N$ and $x \le y \le z$. Define $f(N)$ as the number of such ordered triples. Find $\displaystyle\lim_{N \to \infty} \frac{f(N)}{N^2}$. A: **How to Think About It:** The question is asking how many ways you can split $N$ into three positive integer parts when you only count each multiset once (i.e., $\{1, 2, 5\}$ counts the same as $\{5, 1, 2\}$). The natural first move is to count the easier thing -- ordered triples -- and then divide out the symmetry. Most triples have all three parts distinct, so dividing by $3! = 6$ should get you close. The correction for ties is lower-order, so the limit is clean. **Quick Estimate:** The total number of ordered triples $(x, y, z)$ with $x, y, z \ge 1$ and $x + y + z = N$ is $\binom{N-1}{2} = \frac{(N-1)(N-2)}{2} \approx \frac{N^2}{2}$ for large $N$. Dividing by $3! = 6$ to account for the $x \le y \le z$ ordering gives roughly $\frac{N^2}{12}$. So we expect $\lim_{N \to \infty} f(N)/N^2 = 1/12$. The cases where two or more parts are equal -- like $(k, k, N - 2k)$ or $(k, k, k)$ -- are $O(N)$ in count, so they vanish in the $N^2$ limit. **Approach:** Count all ordered triples, partition them by symmetry type, and take the limit. **Formal Solution:** **Step 1: Count ordered triples.** The number of ordered triples $(x, y, z)$ of positive integers with $x + y + z = N$ is, by stars and bars: $$T(N) = \binom{N-1}{2} = \frac{(N-1)(N-2)}{2}$$ **Step 2: Classify by symmetry type.** Every ordered triple $(x, y, z)$ belongs to exactly one unordered multiset $\{x, y, z\}$. There are three cases: - **All distinct** ($x < y < z$): each multiset generates $3! = 6$ ordered triples. - **Exactly two equal** ($x = y < z$, or $x < y = z$, or $x = z$, etc.): each generates $3$ ordered triples. - **All equal** ($x = y = z$): each generates $1$ ordered triple (only possible when $3 \mid N$, giving the single triple $(N/3, N/3, N/3)$). Let $A$ = number of "all distinct" multisets, $B$ = number of "two equal" multisets. Then: $$T(N) = 6A + 3B + [3 \mid N]$$ and $$f(N) = A + B + [3 \mid N]$$ **Step 3: Count $B$.** Triples with exactly two equal parts have the form $(k, k, N - 2k)$ with $k \ge 1$, $N - 2k \ge 1$, $N - 2k \ne k$. This gives $k$ ranging from $1$ to $\lfloor (N-1)/2 \rfloor$, minus $k = N/3$ if $3 \mid N$. So $B = O(N)$. **Step 4: Solve for $f(N)$.** From the two equations: $$6A + 3B + [3 \mid N] = T(N) \implies A = \frac{T(N) - 3B - [3 \mid N]}{6}$$ $$f(N) = A + B + [3 \mid N] = \frac{T(N) - 3B - [3 \mid N]}{6} + B + [3 \mid N]$$ $$f(N) = \frac{T(N)}{6} + \frac{3B}{6} + \frac{5[3 \mid N]}{6}$$ $$f(N) = \frac{T(N)}{6} + \frac{B}{2} + O(1)$$ **Step 5: Take the limit.** Since $T(N) \sim N^2/2$ and $B = O(N)$: $$\frac{f(N)}{N^2} = \frac{T(N)/6}{N^2} + \frac{B/2}{N^2} + O(N^{-2})$$ $$\to \frac{1}{6} \cdot \frac{1}{2} + 0 = \frac{1}{12}$$ **Answer:** $$\lim_{N \to \infty} \frac{f(N)}{N^2} = \frac{1}{12}$$ --- Maximum Drawdown in a Sliding Window [hard, Time Series] --- https://quantvault.org/problem-1163-maximum-drawdown-in-a-sliding-window.html Q: Given an array of prices `prices` of length $n$ and a window size $k$, compute the maximum drawdown within each sliding window of size $k$. Implement the function with signature: ```python def max_drawdown(prices, k): ``` It must return a **list** of length $n - k + 1$, where the entry for each window is the largest peak-to-trough decline: $\max_{i \le j}(\text{prices}[i] - \text{prices}[j])$ where $i$ and $j$ are indices within the window and $i \le j$ (the peak must come before the trough). If a window is non-decreasing the drawdown is $0$. **Constraints:** - $1 \le k \le n \le 10^5$ - $0 \le \text{prices}[i] \le 10^6$ **Example** `max_drawdown([10, 8, 12, 7, 11, 9, 6], 4) -> [5, 5, 5, 5]` The windows are `[10,8,12,7]`, `[8,12,7,11]`, `[12,7,11,9]`, `[7,11,9,6]`. In the first three the peak `12` precedes the trough `7`, giving `12 - 7 = 5`; in the last, peak `11` precedes trough `6`, giving `11 - 6 = 5`. A: **How to Think About It:** This is a harder variant of the sliding window maximum problem (LeetCode 239). The twist is that drawdown is not just a single extremum -- it is the difference between a running prefix-max and the current price, with the constraint that the peak must come before the trough. The brute force $O(nk)$ approach scans each window left to right, tracking a running peak, and records the largest drop from that peak. **Algorithm:** *Brute force $O(nk)$* -- For each window of size $k$, scan from left to right, maintain a running `peak`, and track the largest value of `peak - prices[j]`. Because the peak is only ever updated by earlier indices, the constraint that the peak precedes the trough is respected automatically. Append the per-window maximum to the result list. *Faster $O(n \log k)$ (optional)* -- Max drawdown over an interval can be decomposed via a merge: each segment stores (its max drawdown, its prefix-max, its suffix-min); merging two segments gives `dd = max(left.dd, right.dd, left.prefix_max - right.suffix_min)`. This makes it compatible with a segment tree, giving $O(n \log k)$ total time for all sliding windows. The brute-force version below is the reference implementation the judge checks. **Code:** ```python def max_drawdown(prices, k): # For each sliding window of size k, return the maximum peak-to-trough # decline max(prices[i] - prices[j]) over indices i peak: peak = prices[j] drop = peak - prices[j] if drop > max_dd: max_dd = drop result.append(max_dd) return result ``` **Complexity:** Brute force: $O(nk)$ time, $O(1)$ extra space (aside from the output list). **Answer:** For each window, a single left-to-right pass tracking the running peak and the largest drop from it yields the window's max drawdown; collecting these gives the required list of length $n - k + 1$. --- Merging Two Sorted Lists by City and Timestamp [easy, Coding] --- https://quantvault.org/problem-1171-merging-two-sorted-lists-by-city-and-timestamp.html Q: You have two sorted lists of records, each sorted first by city name and then by timestamp within each city. Merge them into a single sorted list (same sort order) in $O(n + m)$ time, where $n$ and $m$ are the sizes of the two lists. **Constraints:** - Both lists are pre-sorted by (city, timestamp) - $1 \leq n, m \leq 10^6$ - City names are arbitrary strings; timestamps are comparable integers - The merge should be stable (equal timestamps preserve original relative order) **Example:** List 1: `[(Austin, 1), (Austin, 3), (Boston, 2)]` List 2: `[(Austin, 2), (Boston, 1), (Boston, 4)]` Output: `[(Austin, 1), (Austin, 2), (Austin, 3), (Boston, 1), (Boston, 2), (Boston, 4)]` A: **How to Think About It:** The two input lists are each already sorted by the *compound* key (city, timestamp), and the output must be sorted by the same key. Whenever you must merge two sequences that are each already sorted by the very key you want in the output, the textbook tool is a single **two-pointer merge** -- the same merge step as in mergesort. The only twist here is that the comparison key is a pair, but pairs have a perfectly good total order (lexicographic: compare city first, break ties by timestamp). There is no need to bucket by city or to sort the set of distinct cities; doing so would needlessly add an $O(C\log C)$ term. A flat two-pointer pass over the lexicographic key is already globally correct and runs in $O(n+m)$. **Why the naive "bucket and sort cities" idea is unnecessary (and slower).** One might think you must first group records by city and then arrange the city groups in order, which costs $O(C\log C)$ to sort the $C$ distinct city names. But that work is redundant: because each input list is *already* in (city, timestamp) order, the smaller of the two current heads under the lexicographic key is always the global next record. The two-pointer merge discovers the correct city ordering for free as it advances -- it never needs an explicit sort of the city keys. **Approach:** Keep one pointer into each list. Repeatedly append whichever current record has the smaller (city, timestamp) key; on an exact key tie, take from list 1 first to preserve stability. When one list is exhausted, append the remainder of the other. ```python def merge_city_time(list1, list2): i = j = 0 n, m = len(list1), len(list2) result = [] while i stable. if (a[0], a[1]) take `(Austin,1)`. Then `(Austin,3)` vs `(Austin,2)` -> take `(Austin,2)`. Then `(Austin,3)` vs `(Boston,1)` -> take `(Austin,3)`. Then `(Boston,2)` vs `(Boston,1)` -> take `(Boston,1)`. Then `(Boston,2)` vs `(Boston,4)` -> take `(Boston,2)`. List 1 exhausted; append `(Boston,4)`. - Output: `[(Austin,1), (Austin,2), (Austin,3), (Boston,1), (Boston,2), (Boston,4)]` -- exactly as required. **Complexity.** - **Time:** $O(n+m)$. Each iteration appends exactly one record and advances one pointer, so there are at most $n+m$ iterations; each does an $O(1)$ tuple comparison (string-equality on cities aside, comparisons are constant in the usual model). No sorting of city keys is performed, so there is **no** $O(C\log C)$ term. - **Space:** $O(1)$ auxiliary beyond the output list itself (just two indices); the output is necessarily $O(n+m)$. **Stability.** Using `<=` (rather than `<`) when the full keys tie means equal-key records from list 1 are emitted before those from list 2, and within a single list the original relative order is preserved because we advance that list's pointer in order. So the merge is stable. **Answer:** Run a single two-pointer merge over the lexicographic key (city, then timestamp): at each step append the record with the smaller key, breaking exact-key ties by taking from list 1 first, then append the remainder of whichever list is left. This is $O(n+m)$ time, $O(1)$ extra space (besides the output), and stable -- no bucketing or city-key sort is needed. --- Number of Islands in a Grid [easy, Coding] --- https://quantvault.org/problem-1180-number-of-islands-in-a-grid.html Q: Given an `m x n` 2D grid of `'1'`s (land) and `'0'`s (water), count the number of islands. An island is a group of adjacent land cells connected horizontally or vertically (not diagonally). Land cells on the grid boundary are not wrapped -- the grid is surrounded by water. Implement the function with signature `num_islands(grid)`, where `grid` is a 2D list of the single-character strings `'0'` and `'1'`. Return the number of islands as an `int`. **Constraints:** - $1 \leq m, n \leq 300$ - `grid[i][j]` is `'0'` or `'1'` **Example 1:** ``` Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] Output: 1 ``` **Example 2:** ``` Input: grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] Output: 3 ``` **Example** `num_islands([["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]) -> 3` The top-left 2x2 block of land forms one island, the single land cell in the middle forms a second, and the two connected cells in the bottom-right form a third -- three connected components in total. A: **How to Think About It:** This is a connected components problem on a grid graph. Each land cell is a node, and edges connect horizontally/vertically adjacent land cells. The number of islands is just the number of connected components. Scan the grid, and whenever you hit an unvisited `'1'`, run a flood fill (DFS/BFS) to mark all connected land cells as visited, then increment your island count. **Algorithm:** 1. Iterate over every cell in the grid. 2. When you find a `'1'` that has not been visited, increment the island counter. 3. Flood fill from that cell, marking all reachable `'1'` cells as visited (here we overwrite them with `'0'` in place, avoiding a separate visited array). 4. Continue scanning. Note: the judge calls the function named `num_islands` (snake_case), taking the grid as its single argument and returning an `int`. The implementation below uses an **iterative** DFS with an explicit stack to avoid Python recursion-limit issues on large all-land grids. **Code:** ```python def num_islands(grid): if not grid or not grid[0]: return 0 m, n = len(grid), len(grid[0]) count = 0 def dfs(i, j): stack = [(i, j)] grid[i][j] = '0' while stack: r, c = stack.pop() for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)): if 0 <= nr < m and 0 <= nc < n and grid[nr][nc] == '1': grid[nr][nc] = '0' stack.append((nr, nc)) for i in range(m): for j in range(n): if grid[i][j] == '1': count += 1 dfs(i, j) return count ``` **Walkthrough with Example 2:** ``` 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1 ``` - Scan hits `(0,0)` = `'1'`. Island #1. Flood fills `(0,0)`, `(0,1)`, `(1,0)`, `(1,1)` -- all set to `'0'`. - Continue scanning. Next `'1'` at `(2,2)`. Island #2. Floods just `(2,2)`. - Next `'1'` at `(3,3)`. Island #3. Floods `(3,3)`, `(3,4)`. - Result: 3 islands. **Complexity:** - **Time:** $O(m \times n)$ -- each cell is visited a constant number of times. - **Space:** $O(m \times n)$ worst case for the explicit stack (a grid that is all land). **Answer:** Count connected components of `'1'` cells via flood fill. Scan the grid; on each unvisited `'1'`, increment the counter and flood-fill to mark all connected land. Time $O(m \times n)$. --- Poisson Probability Generating Function [easy, Expectation] --- https://quantvault.org/problem-1195-poisson-probability-generating-function.html Q: For a non-negative integer-valued random variable $X$, the **probability generating function** (PGF) is defined as $$p_X(z) = E[z^X], \quad |z| \leq 1.$$ Derive the PGF for $X \sim \text{Poisson}(\lambda)$. Then, with $\lambda = 2$, compute $p_X(1/2)$ to the nearest thousandth. A: **How to Think About It:** The PGF is just an expectation, so reach for LOTUS (Law of the Unconscious Statistician): write out $E[z^X]$ as a sum over all non-negative integers, weighted by the Poisson PMF. Once you group the terms, you will recognize the Taylor series for $e^x$ -- and the whole thing collapses to a clean closed form. This is one of those derivations worth memorizing because PGFs make compound Poisson sums trivial. **Quick Estimate:** The Poisson(2) mean is 2, so $X$ is typically 0, 1, 2, or 3. At $z = 1/2$, $z^X$ is being down-weighted for large $X$. A rough bound: $E[z^X] \leq 1$ (since $z^X \leq 1$) and $E[z^X] \geq z^{E[X]} = (1/2)^2 = 0.25$ by Jensen (since $z^x$ is convex in $x$ for $z < 1$... actually concave -- so Jensen flips). The true answer should be between 0.25 and 1. Since $e^{-1} \approx 0.368$, we will land right in that range. **Approach:** LOTUS + recognizing the $e^x$ Taylor series. **Formal Solution:** Using the Poisson PMF $P(X = k) = \dfrac{\lambda^k}{k!} e^{-\lambda}$ and LOTUS: $$p_X(z) = E[z^X] = \sum_{k=0}^{\infty} z^k \cdot \frac{\lambda^k}{k!} e^{-\lambda} = e^{-\lambda} \sum_{k=0}^{\infty} \frac{(\lambda z)^k}{k!}.$$ The remaining sum is exactly the Maclaurin series for $e^{\lambda z}$, so: $$\boxed{p_X(z) = e^{\lambda(z-1)}.}$$ With $\lambda = 2$ and $z = 1/2$: $$p_X(1/2) = e^{2(1/2 - 1)} = e^{-1} \approx 0.368.$$ **Answer:** $p_X(z) = e^{\lambda(z-1)}$. For $\lambda = 2$, $z = 1/2$: $p_X(1/2) = e^{-1} \approx \mathbf{0.368}$. --- Restricted Rock-Paper-Scissors Nash Equilibrium [medium, Game Theory] --- https://quantvault.org/problem-1203-restricted-rock-paper-scissors-nash-equilibrium.html Q: You play rock-paper-scissors with a friend, but your friend is not allowed to play paper. The winner collects $\$1$ from the loser; ties result in no payment. Both players know the restriction (your friend cannot play paper). Both play optimally. Find the Nash equilibrium mixed strategies for both players and compute your expected payout per round. A: **How to Think About It:** Your friend can only play rock or scissors. Given that, ask: should you ever play scissors? No -- scissors can only tie (against scissors) or lose (against rock), and it never beats anything your friend can play. So you play only rock or paper. Now you have a 2x2 game. In a 2x2 zero-sum game like this, neither player wants to play a pure strategy -- if you always played rock, your friend would always play scissors to tie (and never lose). So both players mix. Find the Nash equilibrium: the mixing probability where each player is indifferent between their two pure strategies. **Quick Estimate:** The game is symmetric-ish but not quite. Your friend can play rock (beats your scissors, which you don't play anyway) or scissors (loses to your paper). You can play rock (beats friend's scissors, ties friend's rock) or paper (beats friend's rock, loses to friend's scissors). Intuitively you should weight paper heavily since it beats rock and only loses to scissors. Rough guess: you play paper about 2/3 of the time, friend plays rock about 2/3 of the time. Expected payout probably around $1/3$ per round in your favor. **Formal Solution:** You play rock with probability $p$ and paper with probability $1-p$. Friend plays rock with probability $q$ and scissors with probability $1-q$. Payoff matrix (your payout) for the 2x2 game: | | Friend: Rock | Friend: Scissors | |---|---|---| | You: Rock | 0 | +1 | | You: Paper | +1 | -1 | Your expected payout: $$E(p,q) = p(1-q)(+1) + (1-p)q(+1) + (1-p)(1-q)(-1) + pq(0)$$ $$= p(1-q) + q(1-p) - (1-p)(1-q)$$ Expanding: $$= p - pq + q - pq - (1 - p - q + pq) = p - pq + q - pq - 1 + p + q - pq$$ $$= 2p + 2q - 3pq - 1$$ At a Nash equilibrium, each player is indifferent between their pure strategies. **Find $p^{*}$ (friend's indifference):** Your friend is indifferent between rock and scissors when $\partial E/\partial q = 0$: $$\frac{\partial E}{\partial q} = 2 - 3p = 0 \implies p^{*} = \frac{2}{3}$$ **Find $q^{*}$ (your indifference):** You are indifferent between rock and paper when $\partial E/\partial p = 0$: $$\frac{\partial E}{\partial p} = 2 - 3q = 0 \implies q^{*} = \frac{2}{3}$$ **Nash equilibrium strategies:** - You: rock with probability $2/3$, paper with probability $1/3$ - Friend: rock with probability $2/3$, scissors with probability $1/3$ **Expected payout:** $$E(2/3, 2/3) = 2(2/3) + 2(2/3) - 3(2/3)^2 - 1 = \frac{4}{3} + \frac{4}{3} - \frac{4}{3} - 1 = \frac{4}{3} - 1 = \frac{1}{3}$$ **Answer:** Your expected payout per round is $\boxed{+\$1/3}$. You have a structural edge because the restriction benefits you: paper -- which your friend cannot beat -- is available to you, while your friend's best response is to play scissors more to discourage it. --- Sharing a Secret: Locks and Keys [medium, Combinatorics] --- https://quantvault.org/problem-1212-sharing-a-secret-locks-and-keys.html Q: Five people want to store a secret document in a safe. The rule is simple: any group of 3 or more people should be able to open the safe together, but no group of 2 (or fewer) people should ever be able to open it alone. To enforce this, they decide to put multiple locks on the safe -- every single lock must be opened to access it. Each lock can have multiple copies of its key distributed to different people, but each physical key only opens one lock. 1. What is the minimum number of locks required? 2. How many keys will each person carry? A: **How to Think About It:** A threshold ($3$-of-$5$) secret-sharing puzzle. The pivotal reframing: a group *fails* to open the safe iff there is some lock none of them holds a key to. So the design constraint is dual -- **for every forbidden set (each pair) there must exist a dedicated lock that exactly that pair cannot open**, while all 3 outsiders can. The heuristic is 'one obstruction per forbidden coalition': count the coalitions you must block, and that lower-bounds the locks. Then verify the natural construction meets it with equality. **Quick Estimate:** The maximal groups that must still be *denied* are the 2-person groups (any 3+ must succeed). Each such pair needs its own private lock -- a lock keyed to the *other* 3 people. So locks $=$ number of pairs $=\binom{5}{2}=10$. For keys per person, flip the view: person $P$ holds a key to a lock iff $P$ is one of the 3 outsiders, i.e. iff $P$ is *not* in that lock's blocked pair. The pairs not containing $P$ number $\binom{4}{2}=6$. So each person carries $6$ keys. Sanity by double counting: $10$ locks $\times 3$ keys each $=30$ keys, shared over $5$ people $=6$ each -- consistent. **Approach:** One lock per 2-person coalition; key it to the complementary trio. **Formal Solution:** **Part 1 -- minimum locks.** Label people $1,\dots,5$. Take any pair $\{i,j\}$. For them to be blocked, some lock must be unopenable by both -- so *neither* $i$ nor $j$ holds its key; the 3 remaining people must all hold it (else a valid 3-group containing them could be blocked). Distinct pairs need distinct locks: suppose one lock blocked both $\{1,2\}$ and $\{1,3\}$. Then person $1$ (in both) holds no key to it, so the legitimate majority $\{1,4,5\}$ could not open that lock -- contradiction. Hence each of the $\binom{5}{2}=10$ pairs demands its own lock, giving $$\boxed{10\text{ locks}}.$$ This is achievable: for pair $\{i,j\}$ create one lock and give its key to the other three people. Any 3-group opens every lock (for each lock, at most 2 of its 3 keyholders can be absent, so at least one is present), while any pair is blocked by its own lock. So $10$ is exactly the minimum. **Part 2 -- keys per person.** Each lock is keyed to the 3 people outside its blocked pair, so it distributes $3$ keys; with $10$ locks that is $30$ key-copies. By symmetry each person holds $$\frac{30}{5}=6\text{ keys}.$$ Equivalently, person $P$ holds the key to lock $\{i,j\}$ precisely when $P\notin\{i,j\}$, and the number of pairs avoiding $P$ is $\binom{4}{2}=6$. Both counts agree: $\boxed{6\text{ keys per person}}$. **Answer:** Minimum $\boxed{10}$ locks; each person carries $\boxed{6}$ keys. --- Space Station Population Capacity [easy, Brain Teaser] --- https://quantvault.org/problem-1220-space-station-population-capacity.html Q: A space station on a recolonization mission has a population that quadruples every 54 years, and a life support system whose capacity only doubles every 54 years. Initially, the population is 2 people and the life support capacity is 16,384 people. How many years will pass before the population first reaches (equals) the capacity of the life support system? A: **How to Think About It:** Both quantities grow exponentially with the same period of 54 years, but different base rates: population grows as $4^k$ and capacity grows as $2^k$, where $k$ counts 54-year periods. Population grows faster (base 4 vs. base 2), but it starts far behind (2 vs. 16,384). The question is just asking when the faster-growing quantity catches up. A quick order-of-magnitude check: $4^k / 2^k = 2^k$, so the ratio of population to capacity grows as $2^k / 8192$ -- it starts tiny and doubles every period. We need $2^k / 8192 = 1$, meaning $2^k = 8192 = 2^{13}$, so $k = 13$. That is $13 \times 54 = 702$ years. **Quick Estimate:** At $k = 10$, population is $2 \cdot 4^{10} \approx 2 \times 10^6$ and capacity is $16384 \cdot 2^{10} \approx 1.7 \times 10^7$. Population is still about 8x smaller. At $k = 13$, population is $2 \cdot 4^{13} \approx 1.34 \times 10^8$ and capacity is $16384 \cdot 2^{13} \approx 1.34 \times 10^8$. They match. Confirmed. **Formal Solution:** Let $k = t / 54$ denote the number of 54-year periods elapsed. Then: $$P(k) = 2 \cdot 4^k \qquad \text{and} \qquad C(k) = 16384 \cdot 2^k$$ Set population equal to capacity: $$2 \cdot 4^k = 16384 \cdot 2^k$$ Since $4^k = (2^2)^k = 2^{2k}$: $$2 \cdot 2^{2k} = 16384 \cdot 2^k$$ $$2^{2k+1} = 2^{14} \cdot 2^k$$ $$2^{2k+1} = 2^{k+14}$$ Equating exponents: $$2k + 1 = k + 14 \implies k = 13$$ Converting back to years: $$t = 54k = 54 \times 13 = 702 \text{ years}$$ **Answer:** The population reaches the life support capacity after $\boxed{702}$ years. --- Sum of Integers Without Digits 7 or 8 [easy, Combinatorics] --- https://quantvault.org/problem-1224-sum-of-integers-without-digits-7-or-8.html Q: What is the sum of all integers from 1 to 100 that do not contain the digit 7 or the digit 8? For example, 17, 72, and 88 would be excluded, while 23, 59, and 100 would be included. A: **How to Think About It:** Adding up the valid numbers one by one works but is tedious. A cleaner approach is complement counting: total sum minus the sum of numbers that contain at least one 7 or 8. There is also a fast digit-by-digit shortcut, shown as an alternative after the formal solution. **Quick Estimate:** Out of 100 numbers, how many contain a 7 or 8? In each decade (e.g., 1-10, 11-20), exactly 2 numbers have a 7 or 8 in the ones place (like 7, 8, 17, 18, ...). That gives 20 numbers. But the 70s and 80s contribute an additional 16 numbers (70-79 and 80-89, minus the 4 already counted: 77, 78, 87, 88). So roughly 36 numbers are excluded. Their average might be around 60, so excluded sum is about $36 \times 60 \approx 2160$. Answer is roughly $5050 - 2160 \approx 2890$. **Approach:** Complement method: compute the total sum and subtract the sum of the excluded numbers. **Formal Solution:** Total sum: $\sum_{k=1}^{100} k = \frac{100 \times 101}{2} = 5050$. Note: 100 has digits 1, 0, 0, with no 7 or 8, so 100 is included. We only need to check 1-99. Numbers from 1 to 99 that contain digit 7 or 8: - Ones digit is 7: $7, 17, 27, 37, 47, 57, 67, 77, 87, 97$ (10 numbers) - Ones digit is 8: $8, 18, 28, 38, 48, 58, 68, 78, 88, 98$ (10 numbers) - Tens digit is 7 (not already counted): $70, 71, 72, 73, 74, 75, 76, 79$ (8 numbers; 77 already counted) - Tens digit is 8 (not already counted): $80, 81, 82, 83, 84, 85, 86, 89$ (8 numbers; 87, 88 already counted) Total excluded: $10 + 10 + 8 + 8 = 36$ numbers. Sum of excluded numbers: - Ones digit 7: $7 + 17 + 27 + 37 + 47 + 57 + 67 + 77 + 87 + 97 = 520$ - Ones digit 8: $8 + 18 + 28 + 38 + 48 + 58 + 68 + 78 + 88 + 98 = 530$ - Tens digit 7 (remaining): $70 + 71 + 72 + 73 + 74 + 75 + 76 + 79 = 590$ - Tens digit 8 (remaining): $80 + 81 + 82 + 83 + 84 + 85 + 86 + 89 = 670$ Total excluded sum: $520 + 530 + 590 + 670 = 2310$. $$\text{Answer} = 5050 - 2310 = 2740$$ **Alternative (digit symmetry, a user suggested this):** Set $100$ aside (it is valid) and write every number from $0$ to $99$ as two digits, so $8$ becomes $08$. A number is valid exactly when both digits come from the $8$ allowed digits $\{0, 1, 2, 3, 4, 5, 6, 9\}$, whose sum is $30$. So there are $8 \times 8 = 64$ valid two-digit strings ($00$ adds nothing to the sum). Across these $64$ strings each digit position is uniform over the allowed digits, with mean $30/8 = 3.75$, so the average valid number is $10 \times 3.75 + 3.75 = 41.25$ and $$64 \times 41.25 = 2640, \qquad 2640 + 100 = 2740.$$ Equivalently, each allowed digit appears $8$ times in the tens place and $8$ times in the ones place, so the sum is $8 \times 30 \times 10 + 8 \times 30 = 2400 + 240 = 2640$. No case lists and no double-counting to watch for. **Answer:** The sum of all integers from 1 to 100 that contain no digit 7 or 8 is $\boxed{2740}$. --- Top META Trader by Transaction Volume [medium, Coding] --- https://quantvault.org/problem-1230-top-meta-trader-by-transaction-volume.html Q: Write a SQL query to find the trader(s) who had the most combined buy and sell transactions in META stock over the past 30 days. Include ties and return results ordered by total transaction count descending. You have access to the following table: **transactions_table** | Column | Type | | --- | --- | | transaction_date | datetime | | stock_symbol | text | | buyer_id | bigint | | seller_id | bigint | | transaction_id | bigint | | purchase_price | float | Expected output format: | trader_id | transactions | | --- | --- | | 12345 | 85 | **Constraints:** - A trader appears as `buyer_id` when buying and `seller_id` when selling - Count each transaction once per role (a transaction has one buyer and one seller) - Return all traders tied for the maximum - Filter to stock_symbol = 'META' and transaction_date within the last 30 days **Example:** - Trader 101 bought 50 times and sold 35 times -> 85 total transactions - Trader 202 bought 85 times and sold 0 times -> 85 total transactions - Both appear in output (tied at 85) **Auto-grader note (Solve mode):** The in-browser grader runs SQLite. The graded table is `transactions_table(day_offset, stock_symbol, buyer_id, seller_id, transaction_id)`, where `day_offset` is the integer number of days ago the transaction occurred -- "within the last 30 days" means `day_offset < 30` (this replaces the `transaction_date` datetime so the tests are deterministic). Output rows in any order; the grader sorts before comparing. A: **How to Think About It:** Each row in the table represents one transaction, and a trader can appear in that row as either the buyer or the seller -- not both. To count a trader's total activity, you need to union their appearances as buyer with their appearances as seller, then sum. The tricky part is handling ties: using `RANK()` with `RANK() = 1` naturally handles ties, but you need to be careful about window function placement -- the `RANK()` must be applied after the aggregation, not alongside it in the same SELECT. Note: the original solution has a bug -- `RANK() OVER(PARTITION BY tt.trader_id ORDER BY SUM(tt.transactions) DESC)` partitions by `trader_id`, which gives every trader rank 1. The correct approach is to rank globally (no `PARTITION BY`) after summing. **Algorithm:** 1. Use `UNION ALL` to combine buyer and seller appearances for META in the past 30 days, each contributing 1 transaction per row. 2. Group by trader and sum to get total transactions. 3. Rank globally by total transactions descending. 4. Return traders with rank 1. **Code:** ```sql WITH trader_activity AS ( -- Count buys SELECT buyer_id AS trader_id, COUNT(transaction_id) AS transactions FROM transactions_table WHERE transaction_date >= CURRENT_DATE - INTERVAL '30' DAY AND stock_symbol = 'META' GROUP BY buyer_id UNION ALL -- Count sells SELECT seller_id AS trader_id, COUNT(transaction_id) AS transactions FROM transactions_table WHERE transaction_date >= CURRENT_DATE - INTERVAL '30' DAY AND stock_symbol = 'META' GROUP BY seller_id ), trader_totals AS ( SELECT trader_id, SUM(transactions) AS total_transactions, RANK() OVER (ORDER BY SUM(transactions) DESC) AS rnk FROM trader_activity GROUP BY trader_id ) SELECT trader_id, total_transactions AS transactions FROM trader_totals WHERE rnk = 1 ORDER BY total_transactions DESC; ``` **Complexity:** $O(n)$ scan of the transactions table (filtered by date and symbol), plus a sort for ranking. In practice, indexes on `(stock_symbol, transaction_date)` and `(buyer_id)` / `(seller_id)` are critical for performance. **Answer:** Union buyer and seller counts, aggregate per trader, rank globally by total -- return rank-1 traders. Key fix over the naive approach: `RANK()` must have no `PARTITION BY` to rank traders against each other. **Graded (SQLite) version** -- passes the in-browser judge, using `day_offset < 30` for the 30-day window: ```sql WITH activity AS ( SELECT buyer_id AS trader_id FROM transactions_table WHERE stock_symbol = 'META' AND day_offset < 30 UNION ALL SELECT seller_id FROM transactions_table WHERE stock_symbol = 'META' AND day_offset < 30 ), counts AS ( SELECT trader_id, COUNT(*) AS transactions FROM activity GROUP BY trader_id ) SELECT trader_id, transactions FROM counts WHERE transactions = (SELECT MAX(transactions) FROM counts) ORDER BY transactions DESC, trader_id; ``` --- Two-Sum on a Sorted Array [easy, Coding] --- https://quantvault.org/problem-1234-two-sum-on-a-sorted-array.html Q: Given a sorted array of $n$ integers `nums` (non-decreasing, duplicates allowed) and a target integer `target`, return **every** index pair $(i, j)$ with $i < j$ and `nums[i] + nums[j] == target`. Return the pairs as `[i, j]` lists sorted by `i`, then by `j` (lexicographic order). This makes the answer unique and auto-gradable. **Constraints:** - $1 \leq n \leq 2 \cdot 10^5$ - $-10^9 \leq$ `nums[i]` $\leq 10^9$ - The array is sorted in non-decreasing order and may contain repeated values - The number of valid pairs $K$ is at most $10^5$ **Examples:** - Input: `nums = [1, 2, 3, 4, 6]`, `target = 6`. Output: `[[1, 3]]`. Explanation: `nums[1] + nums[3] = 2 + 4 = 6`, and no other pair sums to 6 (index 2 cannot pair with itself). - Input: `nums = [1, 1, 2, 3, 4, 5]`, `target = 6`. Output: `[[0, 5], [1, 5], [2, 4]]`. Explanation: both copies of 1 pair with the 5, and the 2 pairs with the 4. - Input: `nums = [1, 1, 2, 2]`, `target = 3`. Output: `[[0, 2], [0, 3], [1, 2], [1, 3]]`. Explanation: each of the two 1s pairs with each of the two 2s, giving $2 \times 2 = 4$ pairs. Solve it in $O(n + K)$ time with the two-pointer technique (the array is already sorted). Compare with the hash-map approach for unsorted arrays. A: **How to Think About It** Heuristic: *sorted array plus a pair condition means two pointers converging from the ends*. The sum `nums[left] + nums[right]` is monotone in each pointer, so one comparison tells you which pointer can never be part of a new match. The one extra idea is **runs of equal values**: a match is a match between two *values*, and every copy of the left value pairs with every copy of the right value. **Quick Estimate** Take `nums = [1, 1, 2, 2]`, `target = 3`. The value 1 occupies indices 0 and 1 (run length 2); the value 2 occupies indices 2 and 3 (run length 2). Every copy of 1 pairs with every copy of 2, so there are $2 \times 2 = 4$ pairs: `[0, 2], [0, 3], [1, 2], [1, 3]`. A scan that records `[left, right]` and then moves both pointers by one finds only `[0, 3]` and `[1, 2]`, so it misses half the answer. In general, a match between a left run of length $a$ and a right run of length $b$ contributes $a \cdot b$ pairs, and a single run of length $m$ whose value $v$ has $2v = $ `target` contributes $\binom{m}{2}$ pairs (for `[3, 3, 3, 3, 3]`, `target = 6`, that is $\binom{5}{2} = 10$). **Approach** Start `left = 0`, `right = n - 1`. While `left target`, only retreating `right` can decrease the sum, so `right -= 1`. - If `s == target` and `nums[left] != nums[right]`, extend `left` to the end of its run of equal values and `right` to the start of its run. Record every `[i, j]` with `i` in the left run and `j` in the right run, then jump both pointers past their runs. - If `s == target` and `nums[left] == nums[right]`, every element from `left` to `right` equals $v$ with $2v = $ `target`. Record every pair `i target`, index `right` has no remaining partner. On a match with different values $u target: right -= 1 elif nums[left] == nums[right]: # One run of equal values v with 2v == target: every pair inside it. for i in range(left, right + 1): for j in range(i + 1, right + 1): result.append([i, j]) break else: # Find the full run of the left value and of the right value. l_end = left while nums[l_end + 1] == nums[left]: l_end += 1 r_start = right while nums[r_start - 1] == nums[right]: r_start -= 1 for i in range(left, l_end + 1): for j in range(r_start, right + 1): result.append([i, j]) left, right = l_end + 1, r_start - 1 return result ``` *Complexity.* The pointer moves (including the run extensions) total at most $n$, and every inner-loop step writes one output pair, so the time is $\boxed{O(n + K)}$ with $K$ the number of pairs returned, and extra space is $O(1)$ beyond the output. $K$ can be as large as $\binom{n}{2}$ when every value equals `target` $/2$, and no algorithm can beat the size of its output. *Hash-map comparison.* For an unsorted array, map each value to its list of indices, and for each `i` look up the indices `j > i` holding `target - nums[i]`. That is also $O(n + K)$ expected time but needs $O(n)$ extra memory; the sorted two-pointer version needs none. **Answer** Two pointers from both ends: move `left` up when the sum is too small and `right` down when it is too large. On a match, pair the entire run of the left value with the entire run of the right value (or, if the two values are equal, every pair inside that run). This returns all index pairs in lexicographic order in $O(n + K)$ time. Examples: `[[1, 3]]`, `[[0, 5], [1, 5], [2, 4]]`, and `[[0, 2], [0, 3], [1, 2], [1, 3]]`. --- Bayes-Optimal Decision Rule for a Noisy Signal [medium, Game Theory] --- https://quantvault.org/problem-1247-bayes-optimal-decision-rule-for-a-noisy-signal.html Q: You observe a noisy signal $S \in \{-1, +1\}$ about an ETF's true value move $V \in \{-1, +1\}$. The prior on $V$ is uniform: $P(V = +1) = P(V = -1) = 0.5$. The signal is informative but imperfect: $P(S = V) = 0.65$. After observing $S$, you must choose an action $a \in \{-1, 0, +1\}$, corresponding to sell, do nothing, or buy. The payoffs are: - If $a \neq 0$ and $a = V$: you earn $+1$ - If $a \neq 0$ and $a \neq V$: you earn $-1$ - If $a = 0$: you earn $0$ 1. Derive the Bayes-optimal decision rule -- that is, the action $a(S)$ that maximizes expected payoff given the observed signal. 2. Compute the overall expected payoff of this optimal strategy. A: **How to Think About It:** This is a classic signal extraction problem. You have a noisy binary signal about which way an ETF will move, and you need to decide whether to trade on it or sit out. The key tension: the signal is right 65% of the time, so it is informative but far from certain. The question boils down to whether the edge from following the signal (which pays off 65% of the time) outweighs the cost of being wrong (35% of the time). Since doing nothing always pays 0, you just need the expected payoff from acting to be positive. Intuitively, 65% accuracy on a symmetric $\pm 1$ payoff gives you an edge of $0.65 - 0.35 = 0.30$ per trade, which is solidly positive -- so you should always trade in the direction of your signal. **Quick Estimate:** If you always follow the signal, you are right 65% of the time and wrong 35% of the time. Expected payoff per round: $0.65 \times (+1) + 0.35 \times (-1) = 0.65 - 0.35 = 0.30$. Since the signal is symmetric and the prior is symmetric, this is the same regardless of which signal you observe. So the overall expected value is $0.30$. The alternative -- doing nothing -- gives $0$. So following the signal is strictly better. **Approach:** Compute the posterior on $V$ given each signal realization, then evaluate the expected payoff of each action under that posterior. **Formal Solution:** *Step 1: Posterior probabilities.* By Bayes' theorem, when $S = +1$: $$P(V = +1 \mid S = +1) = \frac{P(S = +1 \mid V = +1) \, P(V = +1)}{P(S = +1)}$$ Since $P(S = V) = 0.65$, we have $P(S = +1 \mid V = +1) = 0.65$ and $P(S = +1 \mid V = -1) = 0.35$. With a uniform prior: $$P(S = +1) = 0.65 \times 0.5 + 0.35 \times 0.5 = 0.5$$ So: $$P(V = +1 \mid S = +1) = \frac{0.65 \times 0.5}{0.5} = 0.65$$ $$P(V = -1 \mid S = +1) = 0.35$$ By symmetry, $P(V = -1 \mid S = -1) = 0.65$ and $P(V = +1 \mid S = -1) = 0.35$. *Step 2: Expected payoff of each action.* Given $S = +1$: - $a = +1$ (buy): $E[\text{payoff}] = 0.65 \times (+1) + 0.35 \times (-1) = +0.30$ - $a = -1$ (sell): $E[\text{payoff}] = 0.65 \times (-1) + 0.35 \times (+1) = -0.30$ - $a = 0$ (nothing): $E[\text{payoff}] = 0$ The optimal action when $S = +1$ is $a = +1$ (buy), with expected payoff $+0.30$. By symmetry, when $S = -1$, the optimal action is $a = -1$ (sell), also with expected payoff $+0.30$. *Step 3: Overall expected payoff.* Since each signal is equally likely ($P(S = +1) = P(S = -1) = 0.5$): $$E[\text{payoff}] = 0.5 \times 0.30 + 0.5 \times 0.30 = 0.30$$ **Answer:** The Bayes-optimal rule is to follow the signal: buy when $S = +1$, sell when $S = -1$, never do nothing. The overall expected payoff is $0.30$. More generally, for signal accuracy $q > 0.5$ on a symmetric $\pm 1$ payoff, the edge from following the signal is $2q - 1$. --- Counting Game to 200 [medium, Game Theory] --- https://quantvault.org/problem-1259-counting-game-to-200.html Q: You and a friend take turns picking numbers. The first player starts by choosing any integer from $1$ to $8$. After that, each player on their turn adds between $1$ and $8$ to the running total. Whoever is the player that makes the total hit exactly $200$ wins. Both players play optimally. You go first. What number should you pick on your first turn to guarantee a win? A: **How to Think About It:** Work backwards from the goal. If you can say $200$, you win. So the question becomes: what positions guarantee you can say $200$ on your next turn? If the total is at $191$, then no matter what your opponent adds (1 through 8), the total lands in $192$-$199$ -- and you can always bump it to exactly $200$. So $191$ is a winning position for whoever says it. The same logic applies recursively: to guarantee you can say $191$, you need to be the one who says $182$, then $173$, $164$, and so on. Each "safe" position is $9$ less than the next. You start to see the pattern -- the key positions are all congruent to $200 \bmod 9$. **Quick Estimate:** The step size on each turn is $1$ to $8$, so the combined move of you plus your opponent spans $2$ to $16$, but the critical observation is that $1 + 8 = 9$. Whatever your opponent adds (call it $x$), you can respond with $9 - x$, advancing the total by exactly $9$ every two turns. Since $200 = 9 \times 22 + 2$, you need $22$ full rounds of $9$ after your opening move. That opening move must be $2$. **Approach:** Use backward induction and modular arithmetic. **Formal Solution:** Define a position as the current running total. A position $s$ is a *winning position* for the player whose turn it is if they can force a win from $s$. - From position $s$, the current player can move to any total in $\{s+1, s+2, \ldots, s+8\}$. - The current player wins immediately if they can reach $200$, i.e., if $200 - s \leq 8$ and $200 - s \geq 1$, meaning $s \in \{192, 193, \ldots, 199\}$. - If you land on $s = 191$, the opponent is forced into $\{192, \ldots, 199\}$. Then on your next turn you pick $200$ and win. So $191$ is a winning position for the player who says it. More generally, define the set of "winning milestones" as: $$W = \{200, 191, 182, 173, 164, \ldots, 11, 2\}$$ which is $\{200 - 9k : k = 0, 1, 2, \ldots, 22\}$, or equivalently all values congruent to $2 \pmod{9}$. The strategy: always move to the next value in $W$. If you are at some $w \in W$ and your opponent adds $x$ (where $1 \leq x \leq 8$), you add $9 - x$ to reach $w + 9$, the next milestone. Since $1 \leq 9 - x \leq 8$, this is always a legal move. The first milestone is $2$, which is in the range $\{1, \ldots, 8\}$, so the first player can reach it on the opening move. **Answer:** Pick $2$ on your first turn, then always respond with $9 - x$ where $x$ is your opponent's move. This locks you onto the sequence $2, 11, 20, 29, \ldots, 182, 191, 200$ and guarantees you say $200$. --- Eigenvalue Interpretation in PCA [easy, Linear Algebra] --- https://quantvault.org/problem-1265-eigenvalue-interpretation-in-pca.html Q: In Principal Component Analysis (PCA), what do the eigenvalues of the covariance matrix represent? Suppose you have a 100-dimensional dataset. The first three eigenvalues of the covariance matrix are $50$, $30$, and $10$, and the remaining 97 eigenvalues sum to $10$. How much variance is captured by the first 3 principal components? A: **How to Think About It:** PCA finds the directions in which your data varies the most. The covariance matrix $\Sigma$ encodes all the second-order structure of the data. When you diagonalize $\Sigma = Q \Lambda Q^T$, each eigenvector $q_j$ is a direction (a principal component), and the corresponding eigenvalue $\lambda_j$ is the variance of the data when projected onto that direction. The eigenvalues are ordered from largest to smallest, so the first PC captures the most variance, the second captures the most of what's left, and so on. **Quick Estimate:** Total variance = trace of the covariance matrix = sum of all eigenvalues = $50 + 30 + 10 + 10 = 100$. The first 3 components capture $50 + 30 + 10 = 90$ out of 100. That's $90\%$. **Approach:** Use the spectral decomposition of the covariance matrix. **Formal Solution:** The covariance matrix of centered data $X$ (with $n$ observations in $\mathbb{R}^{100}$) is: $$\Sigma = \frac{1}{n-1} X^T X$$ Since $\Sigma$ is symmetric positive semi-definite, it has an eigendecomposition: $$\Sigma = Q \Lambda Q^T$$ where $Q$ is orthogonal (columns are the principal components $q_1, \ldots, q_{100}$) and $\Lambda = \text{diag}(\lambda_1, \ldots, \lambda_{100})$ with $\lambda_1 \geq \lambda_2 \geq \cdots \geq \lambda_{100} \geq 0$. The $j$-th eigenvalue equals the variance of the data projected onto the $j$-th principal component: $$\lambda_j = \text{Var}(X q_j)$$ The total variance across all dimensions is: $$\text{Total variance} = \text{tr}(\Sigma) = \sum_{j=1}^{100} \lambda_j = 50 + 30 + 10 + 10 = 100$$ Variance captured by the first $k$ principal components: $$\text{Fraction explained} = \frac{\sum_{j=1}^{k} \lambda_j}{\sum_{j=1}^{100} \lambda_j}$$ For $k = 3$: $$\text{Fraction explained} = \frac{50 + 30 + 10}{100} = \frac{90}{100} = 90\%$$ **Answer:** Each eigenvalue $\lambda_j$ represents the variance of the data along the $j$-th principal component. The first 3 principal components capture $90\%$ of the total variance. --- Expected Wait for Two Independent Train Lines [medium, Expectation] --- https://quantvault.org/problem-1271-expected-wait-for-two-independent-train-lines.html Q: You arrive at a train station at a random time. Two train lines, $A$ and $B$, serve the station. Trains on line $A$ arrive according to a Poisson process with rate $\lambda_1$ (so the inter-arrival time is $\text{Exp}(\lambda_1)$), and trains on line $B$ arrive independently with rate $\lambda_2$ (inter-arrival time $\text{Exp}(\lambda_2)$). You board whichever train comes first, regardless of the line. 1. Find the expected time you wait until the next train arrives. 2. Evaluate your answer when $\lambda_1 = 1/10$ and $\lambda_2 = 3/20$. A: **How to Think About It:** You show up at the station and two independent Poisson clocks are ticking -- one for each line. You leave on whichever fires first. Before writing anything, think about rates: line $A$ sends a train every 10 minutes on average, line $B$ every $20/3 \approx 6.7$ minutes. Together, trains arrive at a combined rate. By the memoryless property of the exponential distribution, it does not matter when you arrive -- the residual wait on each line is still exponential with the same rate. So your wait is just the minimum of two independent exponentials. **Quick Estimate:** Line $A$ alone gives an average wait of 10 minutes; line $B$ alone gives about 6.7 minutes. Having both lines can only help, so the wait is less than 6.7 minutes. A natural guess: the rates add, so the expected wait is $1/(\lambda_1 + \lambda_2) = 1/(1/10 + 3/20) = 1/(5/20) = 4$ minutes. That passes the sniff test -- it is well below either individual mean. **Approach:** Use the survival function (tail integral) formula for the expectation of a non-negative random variable, combined with the independence of the two lines. **Formal Solution:** Let $T_A \sim \text{Exp}(\lambda_1)$ and $T_B \sim \text{Exp}(\lambda_2)$ be the residual waiting times for each line (justified by memorylessness). Your actual wait is $T = \min(T_A, T_B)$. Using the tail expectation formula: $$E[T] = \int_0^{\infty} P(T \geq t) \, dt = \int_0^{\infty} P(T_A \geq t, \, T_B \geq t) \, dt$$ By independence: $$= \int_0^{\infty} P(T_A \geq t) \cdot P(T_B \geq t) \, dt = \int_0^{\infty} e^{-\lambda_1 t} \cdot e^{-\lambda_2 t} \, dt = \int_0^{\infty} e^{-(\lambda_1 + \lambda_2) t} \, dt = \frac{1}{\lambda_1 + \lambda_2}$$ In fact, the minimum of independent exponentials is itself exponential: $T \sim \text{Exp}(\lambda_1 + \lambda_2)$. This follows because the survival functions multiply, and the product of exponential survival functions is again an exponential survival function. Plugging in $\lambda_1 = 1/10$ and $\lambda_2 = 3/20$: $$E[T] = \frac{1}{1/10 + 3/20} = \frac{1}{2/20 + 3/20} = \frac{1}{5/20} = \frac{20}{5} = 4 \text{ minutes}$$ **Answer:** The expected wait is $\dfrac{1}{\lambda_1 + \lambda_2}$. For the given rates, this equals $4$ minutes. --- Joint Normal Conditional Probability [hard, Probability] --- https://quantvault.org/problem-1280-joint-normal-conditional-probability.html Q: Let $(R_1, R_2)$ be jointly normal with means $\mu_1 = \mu_2 = 0$, standard deviations $\sigma_1 = \sigma_2 = 1\%$, and correlation $\rho = 0.85$. Compute $P(R_1 > 1\%, \, R_2 < 0)$ by using the conditional distribution of $R_1 \mid R_2$. A numeric approximation is fine -- you can quote standard normal table values. A: **How to Think About It:** You have two highly correlated returns. One return is strongly positive (one standard deviation up) while the other is negative. With $\rho = 0.85$, these two returns move together most of the time, so asking for one to be up while the other is down is asking for a rare event. Your gut should say this probability is small -- much smaller than the unconditional $P(R_1 > 1\%) = \Phi(-1) \approx 15.9\%$. Conditioning on $R_2 1\%$ very unlikely. **Quick Estimate:** Standardize by setting $Z_i = R_i / \sigma$. Then $(Z_1, Z_2)$ is standard bivariate normal with $\rho = 0.85$, and the event becomes $P(Z_1 > 1, Z_2 1$ becomes $(1 - (-0.68))/0.527 \approx 3.19$, giving $P \approx \Phi(-3.19) \approx 0.07\%$. This is only a rough proxy (we used the truncated mean instead of integrating), but it tells us the answer is on the order of a few tenths of a percent. **Approach:** Use the law of total probability by conditioning on $R_2$, then integrate over the region $R_2 1, \, Z_2 1 \mid Z_2 = z) = 1 - \Phi\!\left(\frac{1 - \rho z}{\sigma_c}\right) = \Phi\!\left(\frac{\rho z - 1}{\sigma_c}\right).$$ By the law of total probability, $$P(Z_1 > 1, \, Z_2 1, Z_2 1, Z_2 1\%, \, R_2 < 0) \approx 0.26\%$, obtained by integrating $\Phi\!\left(\frac{\rho z - 1}{\sqrt{1-\rho^2}}\right)\phi(z)$ over $z < 0$, which equals $\Phi_2(-1, 0; -\rho) \approx 0.0026$. --- Market Making the Population Guessing Game [medium, Game Theory] --- https://quantvault.org/problem-1285-market-making-the-population-guessing-game.html Q: You are making a market on the population of a country -- say, Indonesia. The interviewer knows the true population and will trade against you: if they think the true value is above your ask, they buy; if it's below your bid, they sell. Your job is to quote a bid and ask, observe whether the interviewer buys or sells, update your estimate, and repeat. Each round, the interviewer's trade tells you something about where the true value lies relative to your quotes. **Rules:** 1. Your spread (ask minus bid) can be at most $20$ million at any point during the game. 2. The interviewer always trades optimally -- they will buy if the true population is above your ask, sell if it is below your bid, and do nothing if the true value is inside your spread. 3. You lose money on every trade: if they buy at your ask and the truth is higher, you are short at too low a price. If they sell at your bid and the truth is lower, you are long at too high a price. **Questions:** 1. What strategy should you use to set and update your bid-ask spread to minimize your total losses? 2. How many rounds does it take to pin down the population to within your spread? 3. What is your expected total loss under the optimal strategy? A: **How to Think About It:** This is a classic desk interview game. The interviewer has a number, you have to find it, and every wrong guess costs you real P&L. The key insight: each trade is a binary signal. If they buy, the truth is above your ask. If they sell, the truth is below your bid. This is exactly binary search -- but with a twist: you pay for each "query" in the form of a spread crossing. The practical instinct is: start with a wide range, center your market on your best guess, and use each trade to cut the remaining uncertainty in half. Each trade is a costly "query": when the interviewer trades, the truth lies beyond your quote, so your loss on that trade is the full distance from your trade price to the true value -- potentially a large fraction of the remaining feasible range. The narrower you can make the remaining uncertainty, the fewer rounds you need, and the less total P&L you bleed. **Key Insight:** Each trade reveals whether the true value is above your ask or below your bid. If you center your spread on the midpoint of the remaining feasible range, each trade eliminates roughly half the remaining uncertainty -- classic binary search. Your loss on each trade is the distance from your quote to the truth, so the early trades -- when the feasible range is widest -- dominate your total loss. **The Method:** 1. **Initialize:** Start with a feasible range $[L, U]$ for the population. A reasonable prior for Indonesia might be $[100M, 400M]$, giving an initial range of $R_0 = 300M$. Set your midpoint $m = (L + U) / 2$ and quote bid $= m - 10M$, ask $= m + 10M$ (using the full $20M$ spread). 2. **Observe the trade:** - If the interviewer **buys** (hits your ask), the true value is above your ask: $m + 10M$. Update $L \leftarrow m + 10M$. - If the interviewer **sells** (hits your bid), the true value is below your bid: $m - 10M$. Update $U \leftarrow m - 10M$. - If the interviewer **does not trade**, the true value is inside your spread. You have pinned it down -- stop. 3. **Re-center:** Set the new midpoint $m = (L + U) / 2$, quote bid $= m - 10M$, ask $= m + 10M$. Repeat. 4. **Termination:** The feasible range after $k$ rounds is roughly $R_0 / 2^k + 20M$ (the $20M$ from the spread itself). You stop when the interviewer does not trade, meaning the truth is inside your $20M$ spread. With $R_0 = 300M$, after about $k = 4$ rounds the remaining range is $300/16 \approx 19M$, so your spread covers the entire feasible region. The game ends in roughly $4$-$5$ rounds. 5. **Loss per round:** When the interviewer trades, the truth lies strictly *outside* your quotes, so your loss on that trade is the distance from your trade price to the true value -- which can be most of the remaining feasible range, not just half the spread. With a $300M$ prior range and centered quotes, the first trade misses by up to $\sim 140M$ (truth at the far end of the range) and by about $65M$ on average over a uniform prior. Because the feasible range roughly halves each round, subsequent expected losses shrink geometrically (about $26M$, then $7M$, and so on). Binary search minimizes the total by keeping you centered, but it cannot make the early trades cheap. **Practical Considerations:** - **Starting estimate matters.** If you have a decent prior (e.g., you know Indonesia is somewhere around $270M$), you can start with $[200M, 340M]$ instead of $[100M, 400M]$, saving $1$-$2$ rounds and reducing total loss. - **Asymmetric information.** The interviewer knows the truth and trades optimally. You cannot "trick" them. Every trade costs you money. The goal is to minimize the number of trades. - **Spread width trade-off.** A wider spread means less loss per round (the interviewer is less likely to trade, and when they do, your midpoint is closer to the truth). But the constraint caps you at $20M$. - **Don't get cute with asymmetric spreads.** Centering on the midpoint of the feasible range is optimal for binary search. Shifting your quotes to one side wastes information. **Answer:** Use binary search. Center your $20M$ spread on the midpoint of the feasible range, update the range based on each trade, and repeat. Starting from a reasonable prior range of $\sim 300M$, the game ends in about $4$-$5$ rounds. Your expected total loss is on the order of $100M$ in population units: a trade happens only when the truth is *outside* your quotes, so each trade costs the full distance from your trade price to the truth -- up to $\sim 140M$ (about $65M$ on average) on the first trade, with per-round losses roughly halving thereafter ($\sim 65M + 26M + 7M + \cdots \approx 95M$ in expectation, and above $200M$ in bad cases). The key is that each trade gives you one bit of information, and binary search is the information-theoretically optimal way to use it -- but the size of your loss is driven by how far the truth can sit beyond your quote in the early rounds, not by the spread width. --- Maximum Profit from Stock Trades [easy, Coding] --- https://quantvault.org/problem-1287-maximum-profit-from-stock-trades.html Q: You are given a list `prices`, where `prices[i]` is the stock price on day `i`. Implement the function: ```python def max_profit(prices): ... ``` Return a two-element list `[single, unlimited]`: - `single` — the maximum profit from a **single** buy-then-sell (buy on one day, sell on a later day). If no profitable trade exists, this is `0`. - `unlimited` — the maximum total profit with **unlimited** buy/sell transactions, where you must sell before re-buying. This equals the sum of every upward price move. **Example** `max_profit([7, 1, 5, 3, 6, 4]) -> [5, 7]` For a single trade, buy at 1 and sell at 6 for a profit of 5. With unlimited trades, capture every rise: (5-1) + (6-3) = 7. A: **Two independent sub-problems on the same price series.** **Single transaction** — scan left to right tracking the minimum price seen so far. At each day, the best sale is `price - min_so_far`; keep the largest such value. This is the classic O(n) best-time-to-buy-and-sell. **Unlimited transactions** — the maximum total profit equals the sum of all positive consecutive differences `prices[i] - prices[i-1]`. Capturing every upward step is equivalent to buying at every local minimum and selling at every local maximum. Both scans are O(n) time, O(1) space, and are computed independently, then returned together as `[single, unlimited]`. ```python def max_profit(prices): # prices[i] is the stock price on day i. # Return [single, unlimited]: # single = max profit with ONE buy-then-sell (0 if no profit possible) # unlimited = max total profit with unlimited buy/sell (sell before re-buying) if not prices: return [0, 0] # Single transaction: track min price seen so far. min_price = prices[0] single = 0 for p in prices[1:]: if p - min_price > single: single = p - min_price if p 0: unlimited += diff return [single, unlimited] ``` --- Minimax Card Number Assignment [hard, Game Theory] --- https://quantvault.org/problem-1289-minimax-card-number-assignment.html Q: You have 100 cards and must assign each card a number from 1 to 5. Once you have fixed your assignment, an opponent draws a card uniformly at random and guesses the number written on it. If the guess is correct, you pay the opponent an amount equal to that number; if the guess is wrong, you pay nothing. The opponent knows your full distribution (i.e., how many cards carry each number) and will choose whichever guess maximizes their expected payoff. How should you distribute the numbers across the 100 cards to minimize your expected loss? What is the resulting expected loss per draw? A: **How to Think About It:** This is a classic minimax problem. You pick a distribution, then the opponent best-responds. The opponent will always guess the number $k$ that maximizes $k \cdot n_k / 100$, where $n_k$ is how many cards carry value $k$. So your expected loss is $\max_k\, k \cdot n_k / 100$. You want to choose the $n_k$ values to make that maximum as small as possible. The key intuition: if one product $k \cdot n_k$ sticks out above the rest, the opponent exploits it. So you want to equalize all the products. **Quick Estimate:** If we set $k \cdot n_k = C$ for every $k$, then $n_k = C/k$, and the constraint $\sum n_k = 100$ gives $$C\left(1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \frac{1}{5}\right) = 100.$$ The harmonic piece is $1 + 0.5 + 0.333 + 0.25 + 0.2 = 137/60 \approx 2.283$, so $C = 100 / 2.283 \approx 43.8$. That means the expected loss is roughly $C/100 \approx 0.438$. But since the $n_k$ must be integers, we will not hit this exactly. **Approach:** Solve the continuous relaxation (set $k \cdot n_k$ constant), then round to integers and check which guess the opponent exploits. **Formal Solution:** Let $n_k$ be the number of cards with value $k$. The opponent's expected payoff from guessing $k$ is $k \cdot n_k / 100$. They pick $\arg\max_k\, k \cdot n_k$. We want to minimize $\max_k\, k \cdot n_k$ subject to $\sum_{k=1}^{5} n_k = 100$, $n_k \geq 0$, $n_k \in \mathbb{Z}$. Continuous relaxation: set $k \cdot n_k = C$ for all $k$, so $n_k = C/k$. Summing: $$\sum_{k=1}^{5} \frac{C}{k} = C \cdot \frac{137}{60} = 100 \implies C = \frac{6000}{137} \approx 43.80.$$ The ideal (non-integer) counts are $n_1 = 43.80$, $n_2 = 21.90$, $n_3 = 14.60$, $n_4 = 10.95$, $n_5 = 8.76$. Rounding to integers while keeping the sum at 100, a natural choice is: | $k$ | $n_k$ | $k \cdot n_k$ | |-----|--------|----------------| | 1 | 44 | 44 | | 2 | 22 | 44 | | 3 | 15 | 45 | | 4 | 11 | 44 | | 5 | 8 | 40 | Total cards: $44 + 22 + 15 + 11 + 8 = 100$. The products are $\{44, 44, 45, 44, 40\}$. The opponent guesses $k = 3$ (the largest product, 45) and the expected loss is $45/100 = 0.45$. Can we do better? Try shifting one card from $k = 3$ to $k = 5$: $n_3 = 14$, $n_5 = 9$. Then $3 \times 14 = 42$, $5 \times 9 = 45$ -- no improvement; the max is still 45. Try $n_1 = 43$, $n_3 = 15$, $n_5 = 9$: $1 \times 43 = 43$, $3 \times 15 = 45$, $5 \times 9 = 45$ -- the max is still 45. Due to the integer constraint, you cannot push the maximum product below 44, and the rounding of $n_3$ forces it to 45 (since $14 \times 3 = 42$ would make other counts push past 44 elsewhere). The distribution $(44, 22, 15, 11, 8)$ is optimal or within one unit of optimal. **Answer:** Assign $n_1 = 44$, $n_2 = 22$, $n_3 = 15$, $n_4 = 11$, $n_5 = 8$ cards to values 1 through 5 respectively. The counts are inversely proportional to the card values (the equalizing principle $n_k \propto 1/k$). The opponent is nearly indifferent across guesses, and your minimax expected loss is $\mathbf{0.45}$ per draw. --- Newton's Method for Square Roots [medium, Optimization] --- https://quantvault.org/problem-1293-newton-s-method-for-square-roots.html Q: You need to compute $\sqrt{c}$ for some positive number $c$ using Newton's iterative method. Set up the root-finding problem: what function $f(x)$ do you solve, and what is the Newton update step? Starting from an initial guess $x_0 > 0$, write out the iteration formula and simplify it. Then answer the following: 1. What is the rate of convergence of this method (linear, quadratic, etc.), and why? 2. Demonstrate the iteration numerically: compute $\sqrt{2}$ starting from $x_0 = 1$, showing at least 3 iterations. 3. Does the method converge for any starting guess $x_0 > 0$? What about $x_0 < 0$? A: **How to Think About It:** Newton's method is the go-to tool for solving $f(x) = 0$ iteratively. The idea is dead simple: at your current guess $x_n$, approximate $f$ by its tangent line and step to where the tangent hits zero. For square roots, we want to solve $x^2 - c = 0$, and Newton's update turns into a beautiful formula that people have been using since ancient Babylon -- literally. The key insight is that each step averages your current guess with $c$ divided by your guess, which automatically corrects overshoots and undershoots. **Quick Estimate:** Before deriving anything, let's build intuition. If you want $\sqrt{2}$ and guess $x_0 = 1$, that's too low. Then $c/x_0 = 2/1 = 2$ is too high. Averaging gives $1.5$ -- already within 6% of $\sqrt{2} \approx 1.4142$. One more step and you are within 0.2%. The convergence is shockingly fast. **Approach:** Apply Newton's method to $f(x) = x^2 - c$ and simplify. **Formal Solution:** We want to find $\sqrt{c}$, i.e., the positive root of: $$f(x) = x^2 - c = 0$$ Newton's update formula is: $$x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}$$ Here $f'(x) = 2x$, so: $$x_{n+1} = x_n - \frac{x_n^2 - c}{2x_n} = \frac{2x_n^2 - (x_n^2 - c)}{2x_n} = \frac{x_n^2 + c}{2x_n}$$ This simplifies to the elegant form: $$\boxed{x_{n+1} = \frac{1}{2}\left(x_n + \frac{c}{x_n}\right)}$$ This is the **Babylonian method** (or Heron's method). The geometric interpretation: if $x_n$ is your guess for $\sqrt{c}$, then $c/x_n$ is the "complementary" guess (since their product is $c$). If $x_n$ is too large, $c/x_n$ is too small, and vice versa. Averaging the two corrects in both directions. **Part 1 -- Convergence rate:** Newton's method converges **quadratically** near a simple root. To see why, define the error $e_n = x_n - \sqrt{c}$. Then: $$x_{n+1} = \frac{1}{2}\left(x_n + \frac{c}{x_n}\right) = \frac{x_n^2 + c}{2x_n}$$ The error at step $n+1$ is: $$e_{n+1} = x_{n+1} - \sqrt{c} = \frac{x_n^2 + c}{2x_n} - \sqrt{c} = \frac{x_n^2 - 2x_n\sqrt{c} + c}{2x_n} = \frac{(x_n - \sqrt{c})^2}{2x_n} = \frac{e_n^2}{2x_n}$$ So $e_{n+1} \approx \frac{e_n^2}{2\sqrt{c}}$ once $x_n \approx \sqrt{c}$. This means the number of correct digits roughly **doubles** with each iteration -- that is quadratic convergence. **Part 2 -- Numerical demonstration for $\sqrt{2}$:** Starting with $x_0 = 1$: - $x_1 = \frac{1}{2}(1 + 2/1) = 1.5$ - $x_2 = \frac{1}{2}(1.5 + 2/1.5) = \frac{1}{2}(1.5 + 1.\overline{3}) = 1.41\overline{6}$ - $x_3 = \frac{1}{2}(1.41\overline{6} + 2/1.41\overline{6}) \approx 1.414216$ The true value is $\sqrt{2} = 1.414213\ldots$ Three iterations give 5 correct digits, and the error went $0.414 \to 0.086 \to 0.0025 \to 0.000003$ -- each step roughly squares the relative error. **Part 3 -- Convergence from any $x_0 > 0$:** Yes, the method converges for **any** $x_0 > 0$. To see why: after one step, $x_1 = \frac{1}{2}(x_0 + c/x_0) \geq \sqrt{c}$ by AM-GM (the arithmetic mean of two positive numbers exceeds their geometric mean $\sqrt{x_0 \cdot c/x_0} = \sqrt{c}$). So $x_n \geq \sqrt{c}$ for all $n \geq 1$, and from there the sequence is monotonically decreasing toward $\sqrt{c}$. Combined with the quadratic error bound, convergence is guaranteed. For $x_0 0$, and to $-\sqrt{c}$ for any $x_0 < 0$. --- Single-Elimination Tournament Bracket Count [easy, Combinatorics] --- https://quantvault.org/problem-1323-single-elimination-tournament-bracket-count.html Q: A single-elimination tournament has $N$ teams (where $N$ is a power of 2). In each round, teams are paired up, and the loser of each match is eliminated. This continues until one champion remains. A "bracket" specifies which teams play each other in the first round, and how the winners are matched up in subsequent rounds. Two brackets are different if any first-round matchup differs, or if the way winners feed into later rounds differs. How many distinct tournament brackets are possible for $N$ labeled teams? A: **How to Think About It:** A single-elimination bracket is really just a full binary tree with $N$ leaves, where you assign teams to the leaves. The tree structure determines who can potentially meet whom and in which round. The key insight is that a bracket is defined by its matchups, and swapping the left and right children of any internal node gives you the same set of matchups -- it does not matter which side of the bracket a team is drawn on, only who they face. **Quick Estimate:** Start small to build intuition. With $N = 2$: one match, one bracket. With $N = 4$ teams $\{A, B, C, D\}$: the possible first-round pairings are $\{AB, CD\}$, $\{AC, BD\}$, $\{AD, BC\}$ -- that is 3 brackets. Notice $4! / 2^3 = 24/8 = 3$. With $N = 8$: $8! / 2^7 = 40320 / 128 = 315$. The pattern is $N! / 2^{N-1}$. **Approach:** Count the total number of ways to assign $N$ labeled teams to the leaves of a complete binary tree, then divide out the symmetries (left-right swaps at each internal node). **Formal Solution:** 1. Fix a complete binary tree with $N$ leaves and $N - 1$ internal nodes. There are $N!$ ways to assign the $N$ labeled teams to the $N$ leaf positions. 2. However, at each internal node, swapping the left and right subtrees produces the same matchups -- the two teams in a match play each other regardless of which is "on the left." Since there are $N - 1$ internal nodes, each with an independent left-right symmetry, there are $2^{N-1}$ arrangements that correspond to the same bracket. 3. Therefore the number of distinct brackets is: $$\text{Brackets}(N) = \frac{N!}{2^{N-1}}$$ **Verification with small cases:** - $N = 2$: $2!/2^1 = 1$. One match, one bracket. - $N = 4$: $4!/2^3 = 3$. The three ways to partition four teams into two first-round pairs. - $N = 8$: $8!/2^7 = 315$. - $N = 16$: $16!/2^{15} = 638,512,875$. **Answer:** The number of distinct single-elimination tournament brackets for $N$ labeled teams is $\dfrac{N!}{2^{N-1}}$. --- Streaming Maximum Drawdown With Rolling Window [medium, Coding] --- https://quantvault.org/problem-1333-streaming-maximum-drawdown-with-rolling-window.html Q: You are running a real-time risk dashboard for a single-asset trading strategy. As each trade $i$ arrives, it updates the cumulative P&L to a value $C_i$. You need to continuously report the **maximum drawdown** so far -- that is, the largest peak-to-trough decline in cumulative P&L observed up to the current trade. Formally, after $n$ trades the maximum drawdown is: $$\text{MDD}_n = \max_{1 \le j \le k \le n} (C_j - C_k)$$ **Required function.** Implement `running_max_drawdown(C)` where `C` is the list of cumulative P&L values (`C[i]` is the cumulative P&L after trade `i`). Return a **list** whose `i`-th entry is the maximum drawdown observed over `C[0..i]` inclusive. The result has the same length as `C`, every entry is non-negative, and the first entry is always `0`. **Part 1.** Design an algorithm that, upon receiving each new $C_i$, updates the maximum drawdown in $O(1)$ time and $O(1)$ space, and returns the running list of maximum drawdowns. Prove that your algorithm is correct. **Part 2.** Now suppose you only care about the maximum drawdown over the **last $M$ trades** (a rolling window). Design a data structure that maintains this rolling-window maximum drawdown with $O(\log M)$ or better time per update. Discuss the trade-offs between time and space for different data structure choices. **Example** `running_max_drawdown([0, 10, 4, 8, 2]) -> [0, 0, 6, 6, 8]` The peak reaches 10 at index 1; at index 2 the value drops to 4 (drawdown 6), and by index 4 the value is 2, a decline of 8 from that peak of 10 -- so the running max drawdown ends at 8. A: **How to Think About It:** Maximum drawdown is the biggest drop from any peak to a subsequent trough. Track the running peak of cumulative P&L; after each new trade the *current* drawdown is `peak_so_far - current_value`. The maximum drawdown observed up to trade `i` is the largest current drawdown seen so far. Both the peak and the running max are `O(1)` updates, so we produce the whole answer list in a single `O(n)` pass. **Contract:** Implement `running_max_drawdown(C)`. `C[i]` is the cumulative P&L after trade `i`. Return a **list** whose `i`-th entry is the maximum drawdown observed over `C[0..i]` inclusive (a running max drawdown, not a single scalar). Drawdowns are non-negative; the first entry is always `0`. **Algorithm (Part 1):** Maintain two scalars while scanning left to right: - `peak`: the running maximum of all `C_j` seen so far - `max_dd`: the maximum drawdown seen so far On each new value `c`: 1. `peak = max(peak, c)` 2. `dd = peak - c` 3. `max_dd = max(max_dd, dd)` 4. Append `max_dd` to the output list. **Proof of Correctness:** At step `i`, `peak = max(C_0, ..., C_i)`, so `dd_i = max_{j peak: peak = c dd = peak - c if dd > max_dd: max_dd = dd result.append(max_dd) return result ``` **Algorithm (Part 2) -- Rolling Window (last M trades):** The window version is harder because the peak can *expire* when it leaves the window, so a plain running max breaks. Use a **segment tree** over a circular buffer of size `M`, where each node stores `maxVal`, `minVal`, and `maxDD`, merged by $$\text{maxDD}_{\text{parent}} = \max(\text{maxDD}_L,\ \text{maxDD}_R,\ \text{maxVal}_L - \text{minVal}_R),$$ giving `O(log M)` worst-case per update and `O(M)` space. A monotone **two-stack deque** carrying the same `(max, min, maxDD)` aggregate achieves `O(1)` amortized per update but occasionally rebuilds in `O(M)`. For a latency-sensitive risk dashboard the segment tree's worst-case guarantee is preferable; the deque is simpler when average throughput dominates. **Complexity (Part 1):** `O(n)` total, `O(1)` state per update (excluding the output list). --- Streaming Quantile Approximation with T-Digest [hard, Coding] --- https://quantvault.org/problem-1334-streaming-quantile-approximation-with-t-digest.html Q: You need to track approximate quantiles on a stream of numbers in a single pass. Your data structure should give higher resolution in the tails (near quantile 0 and quantile 1) and coarser resolution in the middle. Design a T-Digest-style structure that maintains $C$ cluster centroids, where each cluster has a mean and a weight. The key constraint is that cluster sizes must shrink near quantiles 0 and 1 -- specifically, the maximum weight a cluster is allowed to have depends on how close its quantile rank is to the edges. 1. Define the data structure and its invariants. What does each cluster store, and what size-limit function controls how large a cluster can grow as a function of its quantile position $q \in [0,1]$? 2. Describe the **insert** procedure for a new data point and the **merge** procedure for combining two T-Digests. Both should maintain the size-limit invariant. Target $O(\log C)$ time per insert and $O(C)$ total space. 3. Prove a bound on the rank error of a quantile query as a function of the total number of clusters $C$. 4. Explain why this variable-size clustering gives better tail accuracy than uniform binning (i.e., splitting the data range into $C$ equal-width or equal-count bins). A: **How to Think About It:** The core tension in streaming quantiles is that you cannot store every data point, so you must compress. Uniform compression -- equal-sized bins across the range -- treats the 50th percentile the same as the 99.9th. But in practice, the tails are where the action is: VaR, CVaR, tail risk, extreme event detection. The T-Digest insight is dead simple: let clusters near the edges be tiny (high resolution) and clusters in the middle be large (low resolution). This is just a non-uniform allocation of your compression budget toward where it matters most. **Key Insight:** Use a scale function $k(q) = (\delta / 2\pi) \arcsin(2q - 1)$ (or equivalently tied to the CDF of an arcsine-like distribution) that maps quantile position $q$ to a "scale space." The maximum weight a cluster at quantile $q$ can absorb is proportional to $k'(q)$, which is small near $q = 0$ and $q = 1$ and large near $q = 0.5$. This single function drives the entire design. **The Method:** *Data Structure:* Maintain a sorted array (or balanced BST) of clusters $\{(\mu_i, w_i)\}_{i=1}^{C}$, where $\mu_i$ is the weighted mean of points absorbed into cluster $i$ and $w_i$ is the count. Define the cumulative weight up to cluster $i$ as $W_i = \sum_{j \leq i} w_j$ and total weight $N = W_C$. The quantile position of cluster $i$ is approximately $q_i = (W_{i-1} + w_i/2) / N$. The size-limit function is: $$k(q) = \frac{\delta}{2\pi} \arcsin(2q - 1)$$ where $\delta$ is a compression parameter (typically $\delta \approx 100{-}300$). A cluster at quantile $q_i$ with current weight $w_i$ can absorb a new point only if the resulting cluster would satisfy: $$w_i + 1 \leq \delta \cdot k'(q_i) = \frac{\delta}{\pi \sqrt{q_i(1 - q_i)}}$$ Note that $k'(q) \to 0$ as $q \to 0$ or $q \to 1$, so tail clusters stay small. Near $q = 0.5$, clusters can grow large. *Insert Procedure:* 1. Binary search the sorted cluster array to find the cluster $c_j$ whose mean $\mu_j$ is closest to the new point $x$. Time: $O(\log C)$. 2. Check the size limit: if $w_j + 1 \leq \delta \cdot k'(q_j)$, merge $x$ into $c_j$ by updating $\mu_j \leftarrow (\mu_j w_j + x) / (w_j + 1)$ and $w_j \leftarrow w_j + 1$. 3. If the size limit is violated, create a new singleton cluster $(x, 1)$ and insert it into the sorted array. 4. If the number of clusters exceeds a threshold (e.g., $10 \cdot \delta / 2\pi$), trigger a compression pass: scan clusters in sorted order, merging adjacent pairs whenever the combined weight stays within the size limit for the resulting quantile position. *Merge Procedure (combining two T-Digests $A$ and $B$):* 1. Concatenate all clusters from $A$ and $B$ into a single list and sort by centroid mean. This takes $O(C \log C)$. 2. Walk through the sorted list, greedily merging consecutive clusters as long as the combined weight satisfies the size-limit function evaluated at the new cumulative quantile position. 3. Alternate the scan direction (left-to-right, then right-to-left) across multiple merges to avoid systematic bias toward one tail. *Quantile Query:* To estimate the value at quantile $q$, compute the target cumulative weight $t = q \cdot N$. Binary search for the two adjacent clusters $c_i, c_{i+1}$ that straddle $t$, then linearly interpolate between $\mu_i$ and $\mu_{i+1}$. **Rank Error Bound:** The rank error at quantile $q$ is bounded by the maximum weight of the cluster containing $q$, divided by $N$. Since the maximum cluster weight at position $q$ is at most $\delta \cdot k'(q) = \delta / (\pi \sqrt{q(1-q)})$, the rank error is: $$\epsilon(q) \leq \frac{\delta}{\pi N \sqrt{q(1-q)}}$$ But since we have at most $C$ clusters and total weight $N$, the average cluster weight is $N/C$. In the tails where $q \approx 1/N$ (a single observation), the cluster weight is $O(1)$, giving rank error $O(1/N)$ -- essentially exact. In the middle where $q \approx 0.5$, the cluster weight can be up to $O(N/C)$, giving rank error $O(1/C)$. More precisely: $$\epsilon(q) = O\!\left(\frac{1}{C \cdot \min(q, 1-q)}\right) \quad \text{for } q \text{ bounded away from 0 and 1}$$ The total number of clusters is $C = O(\delta)$, so increasing $\delta$ (and thus $C$) tightens the bound everywhere. **Why This Beats Uniform Binning:** Uniform binning (equal-count or equal-width) allocates the same resolution everywhere. With $C$ uniform bins, the rank error is $O(1/C)$ at every quantile -- including the tails. The T-Digest achieves $O(1/C)$ in the middle but much better in the tails: - At $q = 0.01$ (the 1st percentile), a uniform scheme has error $O(1/C)$, while T-Digest has error $O(1/(100C))$ -- two orders of magnitude better for the same budget $C$. - At the extremes ($q = 1/N$), T-Digest stores individual observations, giving exact answers. - The cost is slightly worse resolution near the median, but nobody needs six decimal places at the 50th percentile -- they need it at the 99th. This is exactly the trade-off you want in risk management: your VaR and CVaR estimates (which live in the tails) get dramatically better accuracy without increasing memory. **Answer:** Maintain $C = O(\delta)$ sorted cluster centroids with a size-limit function $k'(q) = \delta / (\pi \sqrt{q(1-q)})$ that forces small clusters in the tails and allows large clusters in the middle. Insert via binary search in $O(\log C)$; merge by sorted concatenation and greedy compression. Rank error is $O(1/(C \cdot \min(q, 1-q)))$, which beats uniform binning's flat $O(1/C)$ by a factor proportional to how extreme the quantile is. --- Variance of an Exponentiated Gamma Random Variable [medium, Expectation] --- https://quantvault.org/problem-1343-variance-of-an-exponentiated-gamma-random-variable.html Q: Let $X \sim \text{Gamma}(a, b)$ with shape parameter $a$ and scale parameter $b$. Compute $\text{Var}(e^X)$ when $a = 4$ and $b = 1/4$. Round your answer to the nearest thousandth. A: **How to Think About It:** Whenever you see $e^X$ for some random variable $X$, your first reflex should be moment generating functions. The MGF is literally $M_X(\theta) = E[e^{\theta X}]$, so $E[e^X] = M_X(1)$ and $E[e^{2X}] = M_X(2)$. That is all the machinery you need. The only subtlety is checking that the MGF is actually finite at the values you need -- for Gamma this constrains $\theta < 1/b$. **Quick Estimate:** The Gamma$(4, 1/4)$ distribution has mean $ab = 1$ and variance $ab^2 = 1/4$, so $X$ is concentrated around 1 with moderate spread. A rough lower bound: if $X$ were exactly 1, then $e^X = e \approx 2.72$ and the variance would be 0. The spread in $X$ inflates $E[e^{2X}]$ much more than $(E[e^X])^2$ because the exponential amplifies the right tail. The MGF of Gamma$(4,1/4)$ at $\theta = 1$ is $(3/4)^{-4} \approx 3.16$, so $E[e^X] \approx 3.16$. At $\theta = 2$, it is $(1/2)^{-4} = 16$. So $\text{Var}(e^X) \approx 16 - 3.16^2 \approx 16 - 9.99 \approx 6.0$. We should land close to 6. **Approach:** Use the variance identity $\text{Var}(e^X) = E[e^{2X}] - (E[e^X])^2$ and express each term via the Gamma MGF. **Formal Solution:** The variance identity gives $$\text{Var}(e^X) = E[(e^X)^2] - (E[e^X])^2 = E[e^{2X}] - (E[e^X])^2 = M_X(2) - (M_X(1))^2$$ The MGF of a $\text{Gamma}(a, b)$ random variable (with scale parameterization) is $$M_X(\theta) = (1 - b\theta)^{-a}, \quad \theta < \frac{1}{b}$$ We need $M_X(2)$, which requires $2 < 1/b = 4$. This holds, so we proceed. With $a = 4$ and $b = 1/4$: $$M_X(1) = \left(1 - \frac{1}{4}\right)^{-4} = \left(\frac{3}{4}\right)^{-4} = \left(\frac{4}{3}\right)^4 = \frac{256}{81}$$ $$M_X(2) = \left(1 - \frac{2}{4}\right)^{-4} = \left(\frac{1}{2}\right)^{-4} = 2^4 = 16$$ Therefore $$\text{Var}(e^X) = 16 - \left(\frac{256}{81}\right)^2 = 16 - \frac{65536}{6561} = \frac{104976 - 65536}{6561} = \frac{39440}{6561} \approx 6.011$$ **Answer:** $\text{Var}(e^X) = 16 - (4/3)^8 = 39440/6561 \approx 6.011$. --- Bayesian Posterior for a Biased Die [medium, Statistics] --- https://quantvault.org/problem-1359-bayesian-posterior-for-a-biased-die.html Q: A biased die has an unknown probability $\theta = P(\text{roll} = 6)$. You place a $\text{Beta}(2, 8)$ prior on $\theta$ -- reflecting a prior belief that sixes are rare (prior mean $= 0.2$). You roll the die $n = 20$ times and observe $k = 6$ sixes. 1. Derive the posterior distribution of $\theta$ given the data. 2. Compute $P(\theta > 0.30 \mid \text{data})$ and express it in terms of the regularized incomplete beta function $I_x(a, b)$. No numeric integration required. A: **How to Think About It:** The Beta-Binomial model is the canonical conjugate pair: a Beta prior on a success probability combined with Binomial observations always yields a Beta posterior. The update rule is simple -- just add the observed successes and failures to the prior shape parameters. The prior $\text{Beta}(2, 8)$ encodes the equivalent of having seen 1 success and 7 failures before (the extra 1 in each comes from the pseudo-count interpretation). After 6 actual sixes in 20 rolls, you just update those counts. **Quick Estimate:** Prior mean: $\hat{\theta}_{\text{prior}} = 2/(2+8) = 0.20$. Posterior mean will be a weighted average toward the data: observed rate is $6/20 = 0.30$. After the update, posterior mean $= (2+6)/(2+8+20) = 8/30 \approx 0.267$. So the data pulled your estimate from 0.20 toward 0.30, landing at about 0.27. The question asks whether there is meaningful probability mass above 0.30 -- given the posterior mean is 0.267 and the distribution has some spread, there is a non-trivial chunk above 0.30. **Formal Solution:** **Part 1: Posterior Distribution** The likelihood for $k$ sixes in $n$ rolls is: $$L(\theta \mid k, n) \propto \theta^k (1-\theta)^{n-k}$$ With a $\text{Beta}(\alpha, \beta) = \text{Beta}(2, 8)$ prior: $$\pi(\theta) \propto \theta^{\alpha - 1}(1-\theta)^{\beta - 1}$$ By Bayes' theorem: $$\pi(\theta \mid k, n) \propto \theta^{\alpha + k - 1}(1-\theta)^{\beta + n - k - 1}$$ This is the kernel of a $\text{Beta}(\alpha + k,\; \beta + n - k)$ distribution. Substituting $\alpha = 2$, $\beta = 8$, $n = 20$, $k = 6$: $$\boxed{\theta \mid \text{data} \sim \text{Beta}(8, 22)}$$ Posterior mean: $8/30 \approx 0.267$. Posterior variance: $8 \cdot 22 / (30^2 \cdot 31) \approx 0.0063$. **Part 2: $P(\theta > 0.30 \mid \text{data})$** The CDF of a $\text{Beta}(a, b)$ random variable evaluated at $x$ is the regularized incomplete beta function $I_x(a, b)$. So: $$P(\theta \leq 0.30 \mid \text{data}) = I_{0.30}(8, 22)$$ Therefore: $$\boxed{P(\theta > 0.30 \mid \text{data}) = 1 - I_{0.30}(8, 22)}$$ **Answer:** Posterior is $\text{Beta}(8, 22)$; the probability that $\theta > 0.30$ is $1 - I_{0.30}(8, 22)$. --- Binomial Tree Option Pricer [medium, Options Pricing] --- https://quantvault.org/problem-1360-binomial-tree-option-pricer.html Q: Implement a recombining binomial tree to price options under the Cox-Ross-Rubinstein (CRR) model with $N$ time steps. Define the function `binomial_price(S0, K, r, sigma, T, N, option_type, exercise)`: - `S0` stock price, `K` strike, `r` risk-free rate, `sigma` volatility, `T` time to expiry, `N` number of steps. - `option_type` is `'call'` or `'put'`. - `exercise` is `'european'` or `'american'` (American allows early exercise). Requirements: 1. Use CRR parameters with $\Delta t = T/N$: $u = e^{\sigma\sqrt{\Delta t}}$, $d = 1/u$, $q = (e^{r\Delta t} - d)/(u - d)$. 2. Backward-induct the discounted risk-neutral expectation; for `'american'`, take $\max(\text{continuation}, \text{immediate exercise})$ at every node. 3. Return the present value at the root as a **float**. 4. Run in $O(N^2)$ time and $O(N)$ memory. **Constraints:** - $1 \leq N \leq 10{,}000$ - $S_0, K, r, \sigma, T > 0$ **Example** `binomial_price(100, 100, 0.05, 0.2, 1.0, 200, 'call', 'european') -> 10.440591259859872` A 200-step CRR tree prices this at-the-money European call, matching the Black-Scholes value of about 10.45 to two decimals. A: **How to Think About It:** A binomial tree discretizes the continuous stock price process into $N$ time steps. At each step, the stock goes up by factor $u$ or down by factor $d$. Under risk-neutral pricing, you discount expected payoffs at the risk-free rate, using the risk-neutral probability $q$ (not the real-world probability). The tree is recombining ($u \cdot d = 1$), so after $N$ steps there are only $N+1$ distinct stock prices instead of $2^N$ -- this is what makes $O(N^2)$ possible. For the American option, at each node you check whether exercising early beats the continuation value. The key trick for $O(N)$ memory: keep one array of option values, updated backwards from expiry. **CRR Parameters** (with $\Delta t = T/N$): $$u = e^{\sigma \sqrt{\Delta t}}, \quad d = e^{-\sigma \sqrt{\Delta t}} = 1/u, \quad q = \frac{e^{r \Delta t} - d}{u - d}$$ The risk-neutral probability $q$ ensures $q \cdot u + (1-q) \cdot d = e^{r \Delta t}$. **Backward Induction:** 1. Compute the $N+1$ terminal stock prices $S_j = S_0 \cdot u^{2j - N}$ and their payoffs ($\max(S_j-K,0)$ for a call, $\max(K-S_j,0)$ for a put). 2. Step backwards from $i = N-1$ to $0$: continuation value $C_j = e^{-r\Delta t}(q V_{j+1} + (1-q) V_j)$. - European: $V_j = C_j$. - American: $V_j = \max(C_j, \text{exercise value at node }(i,j))$. 3. The price is $V_0$. **Signature:** `binomial_price(S0, K, r, sigma, T, N, option_type, exercise)` where `option_type` is `'call'` or `'put'` and `exercise` is `'european'` or `'american'`. It returns the present value at the root as a float. **Code:** ```python import math def binomial_price(S0, K, r, sigma, T, N, option_type, exercise): dt = T / N u = math.exp(sigma * math.sqrt(dt)) d = 1.0 / u q = (math.exp(r * dt) - d) / (u - d) disc = math.exp(-r * dt) # Terminal stock prices and payoffs V = [0.0] * (N + 1) for j in range(N + 1): S = S0 * (u ** (2 * j - N)) if option_type == 'call': V[j] = max(S - K, 0.0) else: V[j] = max(K - S, 0.0) # Backward induction for i in range(N - 1, -1, -1): for j in range(i + 1): V[j] = disc * (q * V[j + 1] + (1 - q) * V[j]) if exercise == 'american': S = S0 * (u ** (2 * j - i)) if option_type == 'call': V[j] = max(V[j], S - K) else: V[j] = max(V[j], K - S) return V[0] ``` **Convergence:** Compare against the closed-form Black-Scholes price for a European call, running $N = 50, 100, 200, 500, 1000$. The binomial price oscillates around Black-Scholes (even/odd $N$ effect) with error decreasing as $O(1/N)$; convergence follows from the CLT applied to the log-return distribution. **Complexity:** Time $O(N^2)$ (triangle of nodes), space $O(N)$ (single reused array of size $N+1$). --- Black-Scholes Assumptions and the American Call Delta [medium, Options Pricing] --- https://quantvault.org/problem-1361-black-scholes-assumptions-and-the-american-call-delta.html Q: This is a three-part question about the Black-Scholes framework for a non-dividend-paying stock. 1. **State the key assumptions** of the Black-Scholes model. 2. **Show that an American call on a non-dividend-paying stock has the same value as the corresponding European call.** Outline a no-arbitrage argument -- why is early exercise never optimal? 3. **Derive the delta** $\Delta = \partial C / \partial S$ of a European call with strike $K$ and maturity $T$. Express your answer in terms of $N(d_1)$, and explicitly define $d_1$. A: **How to Think About It:** This is a foundational derivatives question that tests three things: do you know the modeling framework, can you reason about early exercise economically, and can you differentiate the Black-Scholes formula cleanly. Before diving into math, think about each part qualitatively. The assumptions are a checklist. The American call result follows from a simple "why would you ever exercise early?" argument. And the delta derivation is an exercise in careful differentiation where most of the messy terms cancel -- if you know that trick, the answer is clean. **Quick Sanity Checks:** - Delta of a call should be between 0 and 1 (you benefit from spot going up, but the payoff is capped at linear growth). - Deep ITM: delta approaches 1 (the call behaves like the stock). Deep OTM: delta approaches 0 (the call is nearly worthless). - An American call on a non-dividend-paying stock should never be exercised early, so its value equals the European call. If there were dividends, this would break. **Derivation:** **Part 1: Black-Scholes Assumptions** The standard assumptions are: 1. The stock price follows a geometric Brownian motion: $dS = \mu S \, dt + \sigma S \, dW$, where $\sigma$ is constant. 2. The risk-free interest rate $r$ is constant and the same for all maturities. 3. The stock pays no dividends. 4. Markets are frictionless: no transaction costs, no taxes, continuous trading is possible. 5. No arbitrage opportunities exist. 6. Short selling is permitted with full use of proceeds. 7. Securities are infinitely divisible. These together imply that the stock price at time $T$ is log-normally distributed and that we can form a self-financing replicating portfolio. **Part 2: American Call Equals European Call (No Dividends)** We want to show that early exercise of an American call on a non-dividend-paying stock is never optimal, so $C_{\text{American}} = C_{\text{European}}$. *Lower bound argument:* At any time $t S - K$$ where the last inequality holds because $e^{-r(T-t)} 0$ and $t < T$. The quantity $S - K$ is exactly the payoff from early exercise. So the value of holding the option always exceeds the value of exercising it. Therefore early exercise is never optimal, and the American call has the same value as the European call. *Economic intuition:* By exercising early you give up two things -- the time value of money on the strike (you pay $K$ now instead of $Ke^{-r(T-t)}$ later) and the insurance value of the option (if the stock drops below $K$, you lose nothing by holding). With no dividends to capture, there is no offsetting benefit to early exercise. **Part 3: Deriving Delta** The Black-Scholes formula for a European call is $$C = S N(d_1) - K e^{-r(T-t)} N(d_2)$$ where $$d_1 = \frac{\ln(S/K) + (r + \sigma^2/2)(T-t)}{\sigma \sqrt{T-t}}, \quad d_2 = d_1 - \sigma \sqrt{T-t}$$ and $N(\cdot)$ is the standard normal CDF. Differentiating with respect to $S$: $$\Delta = \frac{\partial C}{\partial S} = N(d_1) + S N'(d_1) \frac{\partial d_1}{\partial S} - K e^{-r(T-t)} N'(d_2) \frac{\partial d_2}{\partial S}$$ Since $d_2 = d_1 - \sigma\sqrt{T-t}$, we have $\partial d_2 / \partial S = \partial d_1 / \partial S$. Also, $$\frac{\partial d_1}{\partial S} = \frac{1}{S \sigma \sqrt{T-t}}$$ So the remaining terms become $$S N'(d_1) \frac{1}{S \sigma \sqrt{T-t}} - K e^{-r(T-t)} N'(d_2) \frac{1}{S \sigma \sqrt{T-t}}$$ $$= \frac{1}{S \sigma \sqrt{T-t}} \left[ S N'(d_1) - K e^{-r(T-t)} N'(d_2) \right]$$ The key identity is that $S N'(d_1) = K e^{-r(T-t)} N'(d_2)$. To see this, note $N'(x) = \frac{1}{\sqrt{2\pi}} e^{-x^2/2}$ and $$\frac{d_1^2 - d_2^2}{2} = \frac{(d_1 - d_2)(d_1 + d_2)}{2} = \frac{\sigma\sqrt{T-t} \cdot (2d_1 - \sigma\sqrt{T-t})}{2}$$ Expanding and simplifying with the definition of $d_1$, one finds $N'(d_2)/N'(d_1) = S / (Ke^{-r(T-t)})$, confirming the identity. The bracketed term vanishes, and we are left with $$\boxed{\Delta = N(d_1)}$$ where $d_1 = \dfrac{\ln(S/K) + (r + \sigma^2/2)(T-t)}{\sigma\sqrt{T-t}}$. **Practical Interpretation:** The delta $N(d_1)$ tells a trader the hedge ratio -- to delta-hedge a short call, buy $N(d_1)$ shares of stock per option. Since $N(d_1) \in [0, 1]$, you never need more than one share per call. As the option moves deeper ITM, $d_1 \to +\infty$ and $\Delta \to 1$ (the option behaves like stock). As it moves OTM, $d_1 \to -\infty$ and $\Delta \to 0$ (the option is nearly worthless). Near the money and close to expiry, delta swings rapidly -- this is where gamma risk is highest and hedging becomes expensive. **Answer:** The Black-Scholes assumptions define a frictionless, continuous-time market with constant volatility and rates, no dividends, and log-normal stock dynamics. Under these assumptions, an American call is never optimally exercised early (its alive value $S - Ke^{-r(T-t)}$ always exceeds the exercise value $S - K$), so it equals the European call. The European call delta is $\Delta = N(d_1)$ where $d_1 = \frac{\ln(S/K) + (r + \sigma^2/2)(T-t)}{\sigma\sqrt{T-t}}$. --- Blackjack Bonus Wagering Strategy [medium, Game Theory] --- https://quantvault.org/problem-1362-blackjack-bonus-wagering-strategy.html Q: A gambling site offers a 100% deposit match up to $1000. You deposit $1000, giving you \$2000 to play with. On each hand, you win (doubling your bet) with probability 0.45 and lose your bet with probability 0.55. The catch: you can only cash out once your cumulative betting turnover exceeds $10,000. 1. Should you take this promotion? Is the expected value positive? 2. What is your optimal betting strategy to maximize your expected profit while satisfying the $10,000 turnover requirement? 3. How does ruin probability factor into your strategy? A: **How to Think About It:** This is a classic wagering requirement problem that shows up in sports betting promotions and casino bonuses. The key tension: you got \$1000 of free money, but you have to churn through $10,000 of bets on a negative-EV game to unlock it. The question is whether the bonus is large enough to overcome the house edge you'll lose while meeting the turnover requirement -- and, more subtly, whether the amount you actually end up wagering is under your control. It is: busting early truncates the churn, and that changes the expected-value answer. **Quick Estimate:** The house edge per dollar bet is $0.55 - 0.45 = 0.10$, i.e., you lose 10 cents per dollar wagered in expectation. If you complete the full \$10,000 turnover requirement, you expect to lose roughly $10,000 \times 0.10 = \$1,000$. But you started with a \$1,000 bonus, so your expected profit is approximately $\$1,000 - \$1,000 = \$0$. This is roughly break-even -- *if you always survive to complete the full turnover*. Bet sizing changes how much you expect to wager at all, and that is where the strategy question gets interesting. **Approach:** Use the optional-stopping identity $E[\text{final bankroll}] = \text{starting bankroll} - \text{edge} \times E[\text{total wagered}]$, then compare bet-sizing policies on both expected profit and ruin risk. **Formal Solution:** **The key identity.** Every dollar wagered has expected P&L $-0.10$, regardless of bet size or sequencing. So if $W$ denotes the total dollars you wager before you stop (either by clearing the turnover or by busting), optional stopping gives: $$E[\text{final bankroll}] = 2{,}000 - 0.10 \, E[W], \qquad E[\text{profit}] = 1{,}000 - 0.10 \, E[W],$$ since $1,000 of the starting $2,000 was your own deposit. Expected profit is maximized by **minimizing** $E[W]$ -- and $W$ reaches \$10,000 only on paths where you survive; on paths where you bust, $W$ stops early. **Minimum betting: variance-minimizing, EV $\approx 0$.** If you bet $b$ per hand, you need $N = 10{,}000/b$ hands. Expected loss is \$1,000 regardless of $b$. The per-hand P&L is $\pm b$, with variance $b^2\left[1 - (0.10)^2\right] = 0.99\,b^2$, so the total P&L has variance $\approx 0.99 \times 10{,}000 \times b = 9{,}900\,b$ and standard deviation $\approx 99.5\sqrt{b}$. With $b = 1$: standard deviation $\approx \$100$ against a $\$2,000$ bankroll and $\$1,000$ expected loss -- you essentially never bust. Then $E[W] = 10{,}000$ exactly, so $E[\text{profit}] \approx \$0$: min-betting locks in break-even with near-certainty. By the law of large numbers your realized loss concentrates tightly around $\$1,000$, and you reliably cash out close to your original $\$1,000$ deposit. **Aggressive betting: EV-maximizing.** Busting is not just a risk here -- it is the only way to escape the mandatory negative-EV churn. On paths where you bust early, you stop wagering, so $E[W] 0$. Concrete example -- an all-in ladder (each bet is the full bankroll, capped at the remaining turnover): bet $\$2,000$; if you win, bet $\$4,000$; if you win again, bet the final $\$4,000$ needed to reach $\$10,000$ of turnover. - $E[W] = 2{,}000 + 0.45 \times 4{,}000 + 0.45^2 \times 4{,}000 = \$4{,}610$ - $E[\text{final}] = 2{,}000 - 0.10 \times 4{,}610 = \$1{,}539$, so $E[\text{profit}] = +\$539$ - Outcome distribution: bust with probability $0.55 + 0.45 \times 0.55 \approx 0.80$ (forfeit everything, including your $\$1,000$ deposit); finish with $\$4,000$ w.p. $\approx 0.111$; finish with $\$12,000$ w.p. $\approx 0.091$. **To maximize expected profit, bet large.** Aggressive sizing cuts expected turnover from $\$10,000$ to under $\$5,000$, converting the promotion from break-even to solidly positive expected value -- at the price of walking away with nothing about $80\%$ of the time. **Part 3 -- how ruin probability factors in.** Ruin plays opposite roles for the two objectives: - For *expected profit*, ruin is helpful: it truncates a wagering obligation whose every dollar costs you 10 cents in expectation. Bigger bets mean more ruin risk, lower $E[W]$, and higher EV. - For *realized outcomes*, ruin is catastrophic: you forfeit your own $\$1,000$ deposit. Minimum bets drive ruin probability to essentially zero and let the law of large numbers deliver the $\approx \$0$ EV with tiny variance. Intermediate bet sizes interpolate: as bets grow, EV rises and survival probability falls. The right choice depends on risk preference -- a risk-neutral player bets as big as the house allows; a risk-averse player grinds minimum bets for a near-certain wash. **Answer:** (1) The promotion is worth taking only if you are willing to gamble: played with minimum bets it is roughly break-even ($EV \approx \$0$); played aggressively it has genuinely positive expected value. (2) To maximize expected profit, bet large: by optional stopping, $E[\text{profit}] = 1{,}000 - 0.10 \times E[\text{total wagered}]$, and busting early on unlucky paths truncates the turnover, so $E[W] < 10{,}000$ -- e.g., the all-in ladder yields $E[\text{profit}] \approx +\$539$, versus $\approx \$0$ for minimum bets, which force $E[W] = 10{,}000$. (3) Ruin risk is the price of that EV: the ladder busts $\approx 80\%$ of the time, while minimum betting makes ruin negligible and reliably returns roughly your $\$1,000$ deposit. Bet sizing is a dial between EV-maximization (big bets) and variance/ruin-minimization (minimum bets). --- Card Game: Redraw vs. Switch Option [medium, Game Theory] --- https://quantvault.org/problem-1366-card-game-redraw-vs-switch-option.html Q: Two players each draw one card from a well-shuffled deck of 100 cards numbered 1 through 100. The player with the higher card wins the difference -- if you hold card $c$ and your opponent holds card $d$, you collect $c - d$ dollars (negative means you pay). Before the game is resolved, you are offered one of two options (you must choose exactly one): 1. **Redraw option**: You may put your card back, the deck is reshuffled, and you draw a fresh card. 2. **Switch option**: You may swap cards with your opponent. Neither you nor your opponent can see each other's card when you decide. (a) Which option should you prefer? (b) How much would you pay for each option separately, before drawing your own card? A: **How to Think About It:** At first glance both options seem equivalent: they look like symmetric reshufflings. But they are not. The key is that you see your own card before deciding whether to exercise the option, and that private information makes *both* options valuable: you exercise only when your card is bad. The tempting intuition that the switch is worthless ("you cannot see your opponent's card, so swapping is just trading one unknown for another") is wrong -- your own card tells you whether you are likely ahead or behind. The redraw *flattens* a bad position (back to roughly average); the switch *reverses* it -- you hand your bad card to your opponent and take their better-on-average card. Reversing a losing position swings the payoff by twice as much as flattening it. Start by pinning down the baseline: with no options, your expected payoff is zero (by symmetry, $E[c - d] = 0$). Any option value comes purely from the ability to act on private information. **Quick Estimate:** For the **redraw option**: you see your card $c$. Your current payoff is $c - E[d] = c - 50.5$. If you redraw, you get a fresh card with expected value $\approx 50.5$, for expected payoff $\approx 0$. So you exercise the option when $c V_{\text{keep}}(c)$, i.e., when $\frac{98}{99} V_{\text{keep}}(c) < 0$, i.e., $c \leq 50$, with net gain $V_{\text{redraw}}(c) - V_{\text{keep}}(c) = \frac{494900 - 9800c}{9801}$. The value of the option before drawing (the no-option baseline is 0): $$W_{\text{redraw}} = \frac{1}{100}\sum_{c=1}^{100} \max\!\left(V_{\text{keep}}(c),\; V_{\text{redraw}}(c)\right) = \frac{1}{100}\left[\sum_{c=51}^{100} \frac{100c - 5050}{99} + \sum_{c=1}^{50} \frac{100c - 5050}{9801}\right]$$ $$= \frac{125000}{9900} - \frac{125000}{980100} = \frac{1250}{99} - \frac{1250}{9801} = \frac{122500}{9801} = \left(\frac{350}{99}\right)^{2} \approx 12.50$$ (confirmed by exact enumeration over all $(c, d)$ pairs). A common variant instead draws the fresh card from the 98 cards excluding *both* drawn cards; in that model $V_{\text{redraw}}(c) = 0$ exactly and $W_{\text{redraw}} = \frac{1250}{99} \approx 12.63$ -- slightly more, because your bad card can no longer be redrawn. *Switch option:* You observe $c$ but not $d$. If you switch, your payoff becomes $d - c$. Conditioned on $c$: $$E[d - c \mid c] = E[d \mid c] - c = \frac{5050 - c}{99} - c = -\frac{100c - 5050}{99} = -V_{\text{keep}}(c)$$ So switching always flips the sign of your expected payoff. You benefit from switching when $V_{\text{keep}}(c) < 0$, i.e., when $c < 50.5$ (the same threshold as redrawing). When $c \leq 50$, switching converts a negative expected payoff into a positive one; when $c \geq 51$, you hold. The value of the switch option: $$W_{\text{switch}} = \frac{1}{100}\sum_{c=1}^{100}\max\!\left(V_{\text{switch}}(c),\; V_{\text{keep}}(c)\right) - \underbrace{\frac{1}{100}\sum_{c=1}^{100} V_{\text{keep}}(c)}_{=\;0}$$ Since $V_{\text{switch}} = -V_{\text{keep}}$, whenever $V_{\text{keep}}(c) < 0$ you switch -- and the gain *relative to keeping* is $-2V_{\text{keep}}(c)$, not $-V_{\text{keep}}(c)$: switching moves your expected payoff from $V_{\text{keep}}(c)$ all the way to $-V_{\text{keep}}(c)$, a swing of twice the edge. Equivalently, $\max(V_{\text{keep}}, -V_{\text{keep}}) = |V_{\text{keep}}|$: $$W_{\text{switch}} = \frac{1}{100}\sum_{c=1}^{100}\left|V_{\text{keep}}(c)\right| = \frac{2}{100}\sum_{c=1}^{50}\frac{5050 - 100c}{99}$$ $$= \frac{2}{9900}\left(5050 \times 50 - 100\sum_{c=1}^{50}c\right) = \frac{2\,(252500 - 127500)}{9900} = \frac{250000}{9900} = \frac{2500}{99} \approx 25.25$$ The switch option is worth about **twice** the redraw option: $\frac{2500}{99} \approx 25.25$ versus $\frac{122500}{9801} \approx 12.50$. A tempting slip is to count only $-V_{\text{keep}}(c)$ as the switching gain -- that drops the factor of 2 and wrongly suggests the two options are equivalent. **Answer:** - (a) **Prefer the switch option.** It is worth $W_{\text{switch}} = \frac{2500}{99} \approx \$25.25$, roughly twice the redraw option's $W_{\text{redraw}} = \frac{122500}{9801} \approx \$12.50$. - (b) You would pay up to $\frac{122500}{9801} = \left(\frac{350}{99}\right)^{2} \approx \$12.50$ for the redraw option (fresh card drawn from the 99-card deck with only the opponent's card removed) and up to $\frac{2500}{99} \approx \$25.25$ for the switch option. - The intuition: redrawing replaces your below-average card with a roughly average one (gain $\approx |V_{\text{keep}}|$), while switching hands your bad card to your opponent *and* takes their better-on-average card (gain $= 2|V_{\text{keep}}|$). Both options are exercised on the same set $c \leq 50$, so reversing a bad position is worth about twice flattening it. --- Compiler-Generated Special Member Functions in C++ [easy, Coding] --- https://quantvault.org/problem-1372-compiler-generated-special-member-functions-in-c.html Q: You define an empty class in C++: ```cpp class A {}; ``` No constructors, no destructor, no operators -- nothing. What member functions will the compiler generate for you automatically? What does each one do, and when does the compiler stop generating them? A: **How to Think About It:** This is a standard C++ interview question that tests whether you understand the "Rule of Five" (and its history). The compiler's job is to make basic operations work on your class even if you don't write anything -- but it can only do that safely when there's nothing non-trivial to manage. The moment you take ownership of a resource (say, a raw pointer), the compiler's auto-generated versions become wrong, and you have to write your own. **The Six Auto-Generated Functions** For `class A {};`, the compiler generates all six special member functions: 1. **Default constructor** `A()` -- constructs an object with no arguments. Does nothing for an empty class. 2. **Copy constructor** `A(const A&)` -- constructs a new object as a copy. Performs member-wise copy of all data members. 3. **Copy assignment operator** `A& operator=(const A&)` -- assigns from another existing object. Performs member-wise copy assignment. 4. **Move constructor** `A(A&&)` *(C++11)* -- constructs from an rvalue (typically a temporary). Performs member-wise move. 5. **Move assignment operator** `A& operator=(A&&)` *(C++11)* -- move-assigns from an rvalue. Performs member-wise move assignment. 6. **Destructor** `~A()` -- cleans up when the object goes out of scope. Does nothing for an empty class. **When Does the Compiler Stop Generating Them?** C++11 introduced suppression rules to prevent the compiler from silently generating incorrect code: - If you declare a **destructor, copy constructor, or copy assignment**, the compiler does *not* implicitly generate move operations. - If you declare a **move constructor or move assignment**, the compiler does *not* implicitly generate the other move operation, and suppresses implicit copy operations. - The default constructor is suppressed if you declare *any* constructor. Practical implication: if your class manages a resource (owns a pointer, holds a file handle), you need to think about all five copy/move/destroy operations. That's the **Rule of Five**: if you need to explicitly define any one of the destructor, copy constructor, copy assignment, move constructor, or move assignment, you almost certainly need to define all five. **Answer:** Six functions are generated: default constructor, copy constructor, copy assignment, move constructor, move assignment, and destructor. The set actually generated for any class is the subset of these six left after applying the suppression rules in order. Note the legacy quirk: declaring only a destructor still implicitly generates the copy operations (this is deprecated) but suppresses both move operations; declaring any move operation suppresses both copy operations and the other move operation; and declaring any constructor suppresses the default constructor. C++11's suppression rules mean declaring any one of the five copy/move/destroy operations may prevent the compiler from generating the others. --- False Discovery in Alpha Mining [medium, Statistics] --- https://quantvault.org/problem-1400-false-discovery-in-alpha-mining.html Q: You are mining for trading alphas. You test $M$ independent strategies, each producing a t-statistic $Z_1, \ldots, Z_M \sim N(0,1)$ under the null hypothesis that the strategy has no alpha. You declare a strategy "significant" if $Z_j > z$, where $z = \Phi^{-1}(1 - \alpha)$ is the $(1 - \alpha)$-quantile of the standard normal. 1. Compute the expected number of false discoveries. 2. Compute the probability of at least one false discovery. 3. Choose $\alpha$ (in terms of $M$ and a target $\delta \in (0,1)$) so that the probability of any false discovery is at most $\delta$ (Bonferroni-style control). A: **How to Think About It:** This is the bread-and-butter problem of quantitative research: you test a huge number of strategies, and even if none of them are real, some will look significant just by chance. If you test $M = 1000$ strategies at $\alpha = 0.05$, you expect about 50 false discoveries. That is not a bug in the math -- it is why most published alphas do not survive out of sample. The fix is to tighten your significance threshold so that the chance of even one false positive stays small. This is exactly what Bonferroni does. **Quick Estimate:** Take $M = 1000$ and $\alpha = 0.05$. Each test has a 5% false positive rate, so the expected number of false discoveries is $1000 \times 0.05 = 50$. The probability that none of the 1000 tests falsely reject is $(1 - 0.05)^{1000} = 0.95^{1000}$, which is essentially 0. So the probability of at least one false discovery is nearly 1. To bring this down to, say, $\delta = 0.05$, Bonferroni says use $\alpha = 0.05 / 1000 = 0.00005$, corresponding to a z-threshold around $3.89$. **Approach:** Each part follows directly from properties of independent Bernoulli trials. **Formal Solution:** **Part (i): Expected number of false discoveries.** Under the null, each $Z_j \sim N(0,1)$, and a false discovery occurs when $Z_j > z$. The probability of this for any single test is: $$P(Z_j > z) = 1 - \Phi(z) = \alpha$$ Let $D = \sum_{j=1}^{M} \mathbf{1}\{Z_j > z\}$ be the total number of discoveries. Since the tests are independent, $D \sim \text{Binomial}(M, \alpha)$. By linearity of expectation: $$E[D] = M\alpha$$ **Part (ii): Probability of at least one false discovery.** The probability that test $j$ does not falsely reject is $1 - \alpha$. Since the $M$ tests are independent, the probability that none of them falsely reject is: $$P(D = 0) = (1 - \alpha)^M$$ So the probability of at least one false discovery is: $$P(D \geq 1) = 1 - (1 - \alpha)^M$$ For small $\alpha$ and large $M$, this is well approximated by $1 - e^{-M\alpha}$. When $M\alpha \gg 1$, this is essentially 1 -- false discoveries are nearly certain. **Part (iii): Bonferroni correction.** We want $P(D \geq 1) \leq \delta$, i.e.: $$1 - (1 - \alpha)^M \leq \delta$$ Solving: $$(1 - \alpha)^M \geq 1 - \delta$$ $$\alpha \leq 1 - (1 - \delta)^{1/M}$$ This is the exact solution. The Bonferroni approximation uses the simpler bound from the union inequality: $$P(D \geq 1) = P\!\left(\bigcup_{j=1}^{M} \{Z_j > z\}\right) \leq \sum_{j=1}^{M} P(Z_j > z) = M\alpha$$ Setting $M\alpha \leq \delta$ gives the Bonferroni threshold: $$\alpha = \frac{\delta}{M}$$ This is slightly more conservative than the exact answer $1 - (1 - \delta)^{1/M}$, but the two coincide for small $\delta/M$ since $1 - (1-\delta)^{1/M} \approx \delta/M$ when $\delta/M \ll 1$. **Answer:** 1. $E[D] = M\alpha$. 2. $P(D \geq 1) = 1 - (1 - \alpha)^M$. 3. Choose $\alpha = \delta / M$ (Bonferroni correction). The exact threshold is $\alpha = 1 - (1 - \delta)^{1/M}$, which is slightly less conservative. --- Feasible Range of Third Pairwise Correlation [medium, Linear Algebra] --- https://quantvault.org/problem-1401-feasible-range-of-third-pairwise-correlation.html Q: You have three random variables $X$, $Y$, and $Z$. **(a)** You know that $\text{Corr}(X, Y) = \rho$ and $\text{Corr}(Y, Z) = \rho$ for some fixed $\rho \in [-1, 1]$. What values can $\text{Corr}(X, Z)$ take? In other words, find the tightest interval $[a, b]$ such that $r = \text{Corr}(X, Z) \in [a, b]$ is feasible. Derive the bounds by requiring that the $3 \times 3$ correlation matrix is positive semidefinite, and verify your answer with a concrete example. **(b)** Now the two known correlations differ: $\text{Corr}(X, Y) = 0.8$ and $\text{Corr}(Y, Z) = 0.9$. What is the feasible range of $\text{Corr}(X, Z)$? Give the general bounds when $\text{Corr}(X, Y) = \alpha$ and $\text{Corr}(Y, Z) = \beta$. A: **How to Think About It:** Whenever you see "what values can a correlation take given other correlations," the tool is the positive semidefinite (PSD) constraint on the correlation matrix. A correlation matrix is PSD because $w^{T}Rw = \text{Var}(w_1\tilde X + w_2\tilde Y + w_3\tilde Z) \geq 0$ for the standardized variables and any weights $w$. A valid correlation matrix must have all eigenvalues $\geq 0$, which is equivalent to requiring all principal minors (determinants of submatrices) to be nonnegative. For a $3 \times 3$ matrix, the binding constraint is $\det(R) \geq 0$. Before computing, think about limiting cases: if $\rho = 1$, then $X = Y = Z$ (up to affine transforms), so $r$ must equal 1. If $\rho = 0$, then $X$ and $Z$ are each uncorrelated with $Y$ but could be anything with respect to each other, so $r \in [-1, 1]$. We should expect the feasible range to shrink as $|\rho|$ grows. **Quick Estimate:** Take $\rho = 0.5$. The third correlation $r$ is certainly at most 1. For the lower bound, guess: $X$ and $Z$ each have correlation $0.5$ with $Y$, so they share "half" their variation with $Y$. How anti-correlated can $X$ and $Z$ be? Split each of $X$ and $Z$ into a part along $Y$ (correlation contribution $\rho \cdot \rho = 0.25$) and a residual of variance $1 - \rho^2 = 0.75$. At worst the two residuals are perfectly negatively correlated. If those residuals are perfectly negatively correlated, $r = \rho^2 + (-1)(1 - \rho^2) = 2\rho^2 - 1 = -0.5$. So guess $r \in [-0.5, 1]$. **Approach:** Write down the $3 \times 3$ correlation matrix and enforce $\det(R) \geq 0$, which gives a quadratic inequality in $r$. **Formal Solution:** The correlation matrix is: $$R = \begin{pmatrix} 1 & \rho & r \\ \rho & 1 & \rho \\ r & \rho & 1 \end{pmatrix}$$ Compute the determinant by cofactor expansion along the first row: $$\det(R) = 1 \cdot (1 - \rho^2) - \rho \cdot (\rho - r\rho) + r \cdot (\rho^2 - r)$$ - First term: $1 \cdot (1 \cdot 1 - \rho \cdot \rho) = 1 - \rho^2$ - Second term: $-\rho \cdot (\rho \cdot 1 - \rho \cdot r) = -\rho(\rho - r\rho) = -\rho^2 + r\rho^2$ - Third term: $r \cdot (\rho \cdot \rho - 1 \cdot r) = r(\rho^2 - r) = r\rho^2 - r^2$ Summing these: $$\det(R) = 1 - \rho^2 - \rho^2 + r\rho^2 + r\rho^2 - r^2 = 1 - 2\rho^2 + 2r\rho^2 - r^2$$ Setting $\det(R) \geq 0$: $$1 - 2\rho^2 + 2r\rho^2 - r^2 \geq 0$$ Rearrange as a quadratic in $r$: $$r^2 - 2\rho^2 r + (2\rho^2 - 1) \leq 0$$ Apply the quadratic formula to find the roots: $$r = \frac{2\rho^2 \pm \sqrt{4\rho^4 - 4(2\rho^2 - 1)}}{2} = \rho^2 \pm \sqrt{\rho^4 - 2\rho^2 + 1}$$ The discriminant simplifies: $$\rho^4 - 2\rho^2 + 1 = (\rho^2 - 1)^2 = (1 - \rho^2)^2$$ So: $$r = \rho^2 \pm (1 - \rho^2)$$ This gives the two roots: - Upper root: $\rho^2 + (1 - \rho^2) = 1$ - Lower root: $\rho^2 - (1 - \rho^2) = 2\rho^2 - 1$ Since the quadratic in $r$ opens upward (coefficient of $r^2$ is positive), the inequality $\leq 0$ holds between the roots: $$r \in [2\rho^2 - 1, \; 1]$$ Note that $2\rho^2 - 1 \geq -1$ for all $\rho$, so this is always a valid sub-interval of $[-1, 1]$. **Verification:** For $\rho = 0.5$: $r \in [2(0.25) - 1, 1] = [-0.5, 1]$. For $\rho = 1$: $r \in [1, 1]$, so $r = 1$ is forced. For $\rho = 0$: $r \in [-1, 1]$, meaning no constraint at all. For $\rho = 0.8$: $r \in [0.28, 1]$, so two variables each correlated $0.8$ with $Y$ must be positively correlated with each other. All match our intuition. (The $2 \times 2$ principal minors $1 - \rho^2$ and $1 - r^2$ are automatically nonnegative on this interval, so $\det R \geq 0$ is the only binding condition.) **Geometric interpretation and a concrete example:** Write $X = \rho Y + \sqrt{1 - \rho^2} \, U$ and $Z = \rho Y + \sqrt{1 - \rho^2} \, V$ where $U, V$ are unit-variance and uncorrelated with $Y$. Then $\text{Corr}(X, Z) = \rho^2 + (1 - \rho^2) \text{Corr}(U, V)$. Since $\text{Corr}(U, V) \in [-1, 1]$, we get the same bounds, and every value in between is attained. Concretely, take $Y, U$ independent standard normals and $V = -U$: then $X = \rho Y + \sqrt{1-\rho^2}\,U$ and $Z = \rho Y - \sqrt{1-\rho^2}\,U$ have unit variance, correlation $\rho$ with $Y$, and $\text{Corr}(X, Z) = \rho^2 - (1 - \rho^2) = 2\rho^2 - 1$. Taking $Z = X$ attains $1$. (A simulation with $\rho = 0.5$ gives sample correlations $0.50$, $0.50$, $-0.50$.) In angle terms, $\rho = \cos\theta$ puts $X$ and $Z$ on a cone of half-angle $\theta$ around $Y$, so they are between $0$ and $2\theta$ apart, and $\cos 2\theta = 2\rho^2 - 1$. **Part (b): Unequal correlations.** With $\text{Corr}(X, Y) = \alpha$ and $\text{Corr}(Y, Z) = \beta$, the same expansion gives $$\det R = 1 - \alpha^2 - \beta^2 - r^2 + 2\alpha\beta r \ge 0 \iff (r - \alpha\beta)^2 \le (1 - \alpha^2)(1 - \beta^2),$$ so $$r \in \left[\alpha\beta - \sqrt{(1-\alpha^2)(1-\beta^2)},\ \alpha\beta + \sqrt{(1-\alpha^2)(1-\beta^2)}\right].$$ (The same decomposition shows it: $\text{Corr}(X, Z) = \alpha\beta + \sqrt{(1-\alpha^2)(1-\beta^2)}\,\text{Corr}(U, V)$.) With $\alpha = \beta = \rho$ this is $\rho^2 \pm (1 - \rho^2)$, part (a) again. For $\alpha = 0.8$, $\beta = 0.9$: $\alpha\beta = 0.72$ and $\sqrt{0.36 \cdot 0.19} = 0.6\sqrt{0.19} \approx 0.2615$, so $$\text{Corr}(X, Z) \in [0.4585,\ 0.9815].$$ Angles confirm it: $\arccos 0.8 \approx 36.87^\circ$ and $\arccos 0.9 \approx 25.84^\circ$, so the angle between $X$ and $Z$ lies between their difference $11.03^\circ$ and their sum $62.71^\circ$, whose cosines are $0.9815$ and $0.4585$. Two variables both tied tightly to $Y$ cannot even be uncorrelated with each other, and they cannot be perfectly correlated either, because they hug $Y$ at different strengths. The smallest eigenvalue of $R$ is zero at both endpoints and turns negative just outside them. **Answer:** (a) $\text{Corr}(X, Z) \in [2\rho^2 - 1, \; 1]$ (for example $[-0.5, 1]$ at $\rho = 0.5$ and $[0.28, 1]$ at $\rho = 0.8$). (b) In general $\text{Corr}(X, Z) \in \alpha\beta \pm \sqrt{(1-\alpha^2)(1-\beta^2)}$; for $0.8$ and $0.9$ this is $[0.4585, 0.9815]$. --- K-Means Clustering From Scratch [medium, Machine Learning] --- https://quantvault.org/problem-1424-k-means-clustering-from-scratch.html Q: Implement the k-means clustering algorithm from scratch and return its final **inertia**. Given a set of $n$ data points in $d$-dimensional space and a target number of clusters $k$, write a function ```python def kmeans_inertia(points, k): ... ``` that partitions the points into $k$ clusters by iteratively assigning points to their nearest centroid and updating centroids, then returns the **within-cluster sum of squared Euclidean distances** (the inertia) as a single float. **Deterministic initialization (required so the answer is unique):** - `centroid[i] = points[i]` for `i` in `0..k-1` (the first $k$ points). **Then run Lloyd iterations** until assignments stop changing (max 100 iterations): - **Assign:** each point → nearest centroid (Euclidean distance). - **Update:** each centroid → mean of its assigned points. If a cluster is empty, leave its centroid unchanged. Inertia $= \sum_{\text{points } p} \|p - \text{assigned\_centroid}(p)\|^2$. **Constraints:** - $1 \leq k \leq n \leq 10^4$ - $1 \leq d \leq 100$ - Use Euclidean distance. **Example** `kmeans_inertia([[1,2],[1,4],[1,0],[4,2],[4,4],[4,0]], 2) -> 17.5` Centroids are initialized to the first 2 points `[1,2]` and `[1,4]`. Lloyd iterations then converge to a local minimum whose within-cluster sum of squared distances is $17.5$. Because initialization is fixed to the first $k$ points rather than optimized, the result is a deterministic local optimum, not necessarily the globally best clustering. A: **How to Think About It:** K-means alternates two steps until convergence: (1) **assign** each point to its nearest centroid (Euclidean distance), and (2) **update** each centroid to the mean of its assigned points. Both steps decrease the objective $\sum_{i=1}^k \sum_{x \in C_i} \|x - \mu_i\|^2$ (the *inertia*), so Lloyd's algorithm converges to a local minimum in finitely many steps. **Contract for this problem:** The judge calls `kmeans_inertia(points, k)` and expects a single **float** — the final inertia (within-cluster sum of squared Euclidean distances). Initialization is **deterministic**: `centroid[i] = points[i]` for `i in 0..k-1` (the first `k` points). This is required so the returned number is unique. Do **not** use random or k-means++ init here, and do not return centroids/assignments — return the total. **Algorithm:** 1. Set centroids to the first `k` points. 2. Repeat up to 100 iterations: - **Assign:** each point → nearest centroid. - Stop if assignments are unchanged from the previous iteration. - **Update:** each centroid → mean of its assigned points; if a cluster is empty, leave that centroid unchanged. 3. Inertia = sum over all points of $\|point - assigned\_centroid\|^2$. **Code:** ```python import numpy as np def kmeans_inertia(points, k): # Run k-means (Lloyd's algorithm) from scratch with deterministic # initialization (first k points as centroids) and return the final # within-cluster sum of squared Euclidean distances (inertia). pts = np.asarray(points, dtype=float) n, d = pts.shape # Deterministic init: first k points centroids = pts[:k].copy() assignments = None for _ in range(100): # Assign step: each point -> nearest centroid (Euclidean distance) dists = np.linalg.norm(pts[:, None, :] - centroids[None, :, :], axis=2) new_assignments = np.argmin(dists, axis=1) if assignments is not None and np.array_equal(new_assignments, assignments): break assignments = new_assignments # Update step: centroid -> mean of assigned points (empty -> unchanged) for i in range(k): mask = assignments == i if mask.any(): centroids[i] = pts[mask].mean(axis=0) # Inertia: sum of squared distances of each point to its assigned centroid dists = np.linalg.norm(pts[:, None, :] - centroids[None, :, :], axis=2) final = np.argmin(dists, axis=1) diff = pts - centroids[final] inertia = float(np.sum(diff * diff)) return inertia ``` **Complexity:** - Time per iteration: $O(nkd)$ — all $n \times k$ pairwise distances in $d$ dimensions. - Space: $O(nd + kd)$. - Iterations bounded at 100 (typically far fewer before assignments stabilize). **Answer:** With deterministic first-`k`-points initialization, run Lloyd iterations to convergence and return the within-cluster sum of squared distances as a float. --- Mean as the Minimizer of Squared Loss [easy, Optimization] --- https://quantvault.org/problem-1437-mean-as-the-minimizer-of-squared-loss.html Q: You have a set of data points $x_1, x_2, \ldots, x_n \in \mathbb{R}$. You want to find the single number $y$ that is closest to all of them in the least-squares sense. Find $\arg\min_{y \in \mathbb{R}} \sum_{i=1}^{n} (x_i - y)^2$. Then: what changes if you use absolute value instead of squared differences? A: **How to Think About It:** You are finding the best single-number summary of a dataset under a specific loss function. This is a one-dimensional unconstrained optimization -- just differentiate and set to zero. The result connects directly to a fundamental principle: the sample mean is the point that minimizes squared distance to the data. This is not a coincidence; it is the definition of the mean from an optimization perspective. **Quick Estimate:** Intuition check -- if all $x_i$ are equal to some constant $c$, the answer is obviously $c$. If $x_i$ are spread around, the minimizer should be "in the middle" -- the mean is the right guess. **Formal Solution:** Let $f(y) = \sum_{i=1}^{n} (x_i - y)^2$. Differentiate with respect to $y$: $$f'(y) = \sum_{i=1}^{n} 2(x_i - y) \cdot (-1) = -2 \sum_{i=1}^{n} (x_i - y) = -2\left(\sum_{i=1}^{n} x_i - ny\right)$$ Setting $f'(y) = 0$: $$\sum_{i=1}^{n} x_i - ny = 0 \implies y = \frac{1}{n} \sum_{i=1}^{n} x_i = \bar{x}$$ Confirm this is a minimum, not a maximum: $f''(y) = 2n > 0$. Confirmed. **The $L_1$ analog:** For $g(y) = \sum_{i=1}^n |x_i - y|$, the minimizer is the sample median $\tilde{x}$. Intuitively, the mean is pulled by extreme values (outliers create large squared errors) while the median is not (absolute value grows linearly, not quadratically, so outliers have less leverage). **Answer:** $\arg\min_y \sum_i (x_i - y)^2 = \bar{x}$ (the sample mean). The analogous minimizer for $L_1$ loss is the sample median. --- Numerical / Monte Carlo Greek Estimation [hard, Options Pricing] --- https://quantvault.org/problem-1451-numerical-monte-carlo-greek-estimation.html Q: You are pricing a European call option under geometric Brownian motion and need to estimate the Greeks -- delta and vega -- by Monte Carlo simulation. 1. **Pathwise derivative estimator for delta.** Write down the pathwise (infinitesimal perturbation analysis) estimator for $\Delta = \partial C / \partial S_0$. State the conditions under which this estimator is unbiased. 2. **Likelihood ratio estimator for vega.** Write down the likelihood ratio (score function) estimator for vega, $\mathcal{V} = \partial C / \partial \sigma$. How does its variance compare to the pathwise estimator? 3. **Variance reduction and confidence intervals.** Propose a combined estimator that uses the Black-Scholes closed-form Greek as a control variate. Explain how you would construct a confidence interval for each Greek estimate. A: **How to Think About It:** When you have a closed-form price (like Black-Scholes), you also have closed-form Greeks, so Monte Carlo Greek estimation seems pointless. The reason this problem matters is that in practice, your payoff is rarely vanilla -- path-dependent exotics, barriers, and multi-asset deals all require numerical Greeks. The two main approaches are pathwise derivatives (differentiate through the payoff) and likelihood ratio / score function (differentiate the density). Each has strengths and failure modes, and a good quant knows when to use which. **Key Insight:** Pathwise differentiation pushes the derivative inside the expectation onto the payoff function, so it needs the payoff to be differentiable (or at least a.e. differentiable with bounded derivative). The likelihood ratio method pushes the derivative onto the log-density, so it works for discontinuous payoffs but typically has higher variance because you are multiplying by a score function that can be noisy. --- **Part (i): Pathwise Derivative Estimator for Delta** Under GBM, the terminal stock price is: $$S_T = S_0 \exp\left[(r - \tfrac{1}{2}\sigma^2)T + \sigma \sqrt{T}\, Z\right], \quad Z \sim N(0,1)$$ The call payoff is $f(S_T) = (S_T - K)^+$ and the discounted price is $C = e^{-rT} E[f(S_T)]$. To get the pathwise estimator, differentiate inside the expectation: $$\Delta = e^{-rT} E\left[\frac{\partial f}{\partial S_T} \cdot \frac{\partial S_T}{\partial S_0}\right]$$ Since $S_T$ is linear in $S_0$ up to a multiplicative constant (the exponential does not depend on $S_0$), we have $\partial S_T / \partial S_0 = S_T / S_0$. And $\partial (S_T - K)^+ / \partial S_T = \mathbf{1}_{\{S_T > K\}}$ a.e. So the pathwise estimator is: $$\hat{\Delta}_{\text{path}} = e^{-rT} \cdot \mathbf{1}_{\{S_T > K\}} \cdot \frac{S_T}{S_0}$$ Average this over $N$ simulated paths to get your Monte Carlo estimate. **Conditions for unbiasedness:** The interchange of differentiation and expectation is valid (by dominated convergence) when the payoff function is Lipschitz in $S_T$ -- i.e., the derivative $\partial f / \partial S_T$ exists a.e. and is bounded. For the vanilla call, $(S_T - K)^+$ has a kink at $K$ but is differentiable a.e., and the derivative is bounded by 1, so the pathwise estimator is unbiased. It would fail for a digital (binary) payoff where the derivative is a Dirac delta. --- **Part (ii): Likelihood Ratio Estimator for Vega** The likelihood ratio method differentiates the density rather than the payoff. Let $p(S_T; \sigma)$ be the lognormal density. Then: $$\mathcal{V} = e^{-rT} \int f(S_T) \frac{\partial}{\partial \sigma} p(S_T; \sigma)\, dS_T = e^{-rT} E\left[f(S_T) \cdot \frac{\partial \log p(S_T; \sigma)}{\partial \sigma}\right]$$ The score function with respect to $\sigma$ can be computed from the standard normal representation. Writing $S_T = S_0 \exp[(r - \sigma^2/2)T + \sigma \sqrt{T} Z]$, the log-density contribution from $Z$ is $-Z^2/2 - \tfrac{1}{2}\log(2\pi)$, but we also need the Jacobian. Working through the algebra (or differentiating the lognormal log-density directly), the score is: $$\frac{\partial \log p}{\partial \sigma} = \frac{(\log(S_T/S_0) - (r - \sigma^2/2)T)^2 - \sigma^2 T}{\sigma^3 T} - \frac{1}{\sigma}$$ Equivalently, in terms of $Z$: $$\frac{\partial \log p}{\partial \sigma} = \frac{Z^2 - 1}{\sigma} - Z\sqrt{T}$$ So the likelihood ratio estimator for vega is: $$\hat{\mathcal{V}}_{\text{LR}} = e^{-rT} (S_T - K)^+ \left(\frac{Z^2 - 1}{\sigma} - Z\sqrt{T}\right)$$ **Variance comparison:** The pathwise estimator for vega (if applicable) would involve differentiating the payoff and the path w.r.t. $\sigma$, giving $\hat{\mathcal{V}}_{\text{path}} = e^{-rT} \mathbf{1}_{\{S_T > K\}} \cdot S_T \cdot (-\sigma T + \sqrt{T}\, Z)$. This has lower variance than the likelihood ratio estimator because it avoids multiplying by the noisy score function. The LR estimator's variance grows because the score term $(Z^2 - 1)/\sigma$ has heavy tails. In general, pathwise estimators have lower variance whenever they are applicable. The LR method's advantage is generality -- it works even when the payoff is discontinuous. --- **Part (iii): Control Variate and Confidence Intervals** Since we know the Black-Scholes Greeks in closed form, we can use them as control variates. For delta, define: $$\hat{\Delta}_{\text{CV}} = \hat{\Delta}_{\text{path}} - \beta\left(\hat{C}_{\text{MC}} - C_{\text{BS}}\right)$$ where $\hat{C}_{\text{MC}}$ is the Monte Carlo price estimate, $C_{\text{BS}}$ is the Black-Scholes price (known), and $\beta$ is the optimal control variate coefficient: $$\beta^{*} = \frac{\text{Cov}(\hat{\Delta}_{\text{path}},\, \hat{C}_{\text{MC}})}{\text{Var}(\hat{C}_{\text{MC}})}$$ In practice, estimate $\beta^{*}$ from a pilot run or use the full sample. The idea is that the Monte Carlo price error is highly correlated with the Greek estimation error (they use the same paths), so subtracting off the known pricing error removes much of the Greek estimation noise. Alternatively, you can directly use the closed-form Greek as a control variate. Simulate a vanilla call alongside your exotic, compute pathwise delta for both, and subtract: $$\hat{\Delta}_{\text{exotic,CV}} = \hat{\Delta}_{\text{exotic}} - \beta(\hat{\Delta}_{\text{vanilla,MC}} - \Delta_{\text{BS}})$$ **Confidence interval construction:** For $N$ paths, compute the sample mean $\bar{G}$ and sample standard deviation $s_G$ of the Greek estimator (with or without control variate). The $(1-\alpha)$ confidence interval is: $$\bar{G} \pm z_{\alpha/2} \cdot \frac{s_G}{\sqrt{N}}$$ For small $N$ or when the estimator has heavy tails (common with LR), use a $t$-distribution or increase $N$ until CLT kicks in. The control variate typically reduces $s_G$ by a factor of 3-10x for vanilla-like payoffs, meaning you need 10-100x fewer paths for the same CI width. **Answer:** The pathwise delta estimator is $e^{-rT} \mathbf{1}_{\{S_T > K\}} S_T / S_0$ (unbiased when the payoff is Lipschitz). The likelihood ratio vega estimator multiplies the payoff by the score $(Z^2-1)/\sigma - Z\sqrt{T}$; it is more general but higher variance than pathwise. A control variate using the known Black-Scholes Greek dramatically reduces variance, and confidence intervals follow from standard CLT on the sample mean of the per-path Greek estimates. --- Omitted Variable Bias and Suppressor Variables [hard, Regression] --- https://quantvault.org/problem-1456-omitted-variable-bias-and-suppressor-variables.html Q: You have two OLS regressions on the same dataset: - **Model 1:** $Y = b_1 X_1 + \epsilon$ (simple regression) - **Model 2:** $Y = b_2 X_1 + \beta_2 X_2 + \epsilon$ (multiple regression) You are told that $\text{corr}(X_1, X_2) > 0$, $\text{corr}(Y, X_2) = 0$, and $b_2 > 0$. Which coefficient is larger, $b_1$ or $b_2$? Prove it. A: **How to Think About It:** The key tool here is the omitted variable bias (OVB) formula, which tells you exactly how a coefficient changes when you add or remove a regressor. Think of it this way: Model 1 omits $X_2$. If $X_2$ is correlated with $X_1$, then in Model 1, $X_1$ is "doing double duty" -- it is picking up its own effect on $Y$ plus whatever effect $X_2$ has on $Y$ that leaks through their correlation. The question is: does omitting $X_2$ make $b_1$ bigger or smaller than $b_2$? The answer turns on the sign of $\beta_2$ in Model 2. At first glance, $\text{corr}(Y, X_2) = 0$ looks like it means $X_2$ has no effect on $Y$. But that is the *marginal* correlation, not the *partial* effect. Once you condition on $X_1$, the partial effect of $X_2$ on $Y$ can be non-zero -- and in this case, it must be negative. That is what makes $X_2$ a suppressor variable. **Key Setup:** The omitted variable bias formula relates the two coefficients: $$b_1 = b_2 + \hat{\gamma} \cdot \beta_2$$ where $\hat{\gamma}$ is the coefficient from the auxiliary regression of $X_2$ on $X_1$ (i.e., how much $X_2$ changes per unit of $X_1$). This formula is exact in OLS. It says: the coefficient on $X_1$ in the short regression equals the coefficient in the long regression, plus a bias term equal to (how $X_2$ loads on $X_1$) times (the effect of $X_2$ on $Y$ in the long model). **Step 1 -- Sign of $\hat{\gamma}$:** Since $\text{corr}(X_1, X_2) > 0$, we have $\hat{\gamma} > 0$. **Step 2 -- Sign of $\beta_2$:** This is the subtle step. The unconditional correlation $\text{corr}(Y, X_2) = 0$ means: $$\text{Cov}(Y, X_2) = 0$$ In Model 2, the fitted $Y$ is $b_2 X_1 + \beta_2 X_2$. The OLS normal equations give: $$\text{Cov}(Y, X_2) = b_2 \text{Cov}(X_1, X_2) + \beta_2 \text{Var}(X_2) = 0$$ Solving for $\beta_2$: $$\beta_2 = -b_2 \cdot \frac{\text{Cov}(X_1, X_2)}{\text{Var}(X_2)}$$ Since $\text{Cov}(X_1, X_2) > 0$, $\text{Var}(X_2) > 0$ and $b_2 > 0$, we get $\beta_2 0$ and the inequality flips, and if $b_2 = 0$ then $b_1 = b_2$.) **Step 3 -- Direction of the bias:** Substituting back into the OVB formula: $$b_1 = b_2 + \underbrace{\hat{\gamma}}_{> 0} \cdot \underbrace{\beta_2}_{< 0} = b_2 - (\text{positive quantity})$$ Therefore $b_1 < b_2$. In fact the two coefficients are related exactly. Substituting $\hat{\gamma} = \text{Cov}(X_1, X_2)/\text{Var}(X_1)$ and $\beta_2 = -b_2\,\text{Cov}(X_1, X_2)/\text{Var}(X_2)$ into the OVB identity gives $$b_1 = b_2\,(1 - \rho^2), \qquad \rho = \text{corr}(X_1, X_2),$$ so $b_1$ always carries the sign of $b_2$ and is strictly shrunk toward zero whenever $X_1$ and $X_2$ are correlated. **Answer:** $b_1 < b_2$. The simple regression coefficient is smaller than the multiple regression coefficient. Omitting $X_2$ introduces a downward bias on $b_1$, because $X_2$ acts as a suppressor -- it is positively correlated with $X_1$ but has a negative partial effect on $Y$ (to offset the positive contribution of $X_1$ flowing through their correlation). --- Out-of-Distribution Prediction: Dog Weight Regression [easy, Machine Learning] --- https://quantvault.org/problem-1465-out-of-distribution-prediction-dog-weight-regression.html Q: A veterinarian builds a regression model to predict the weight of golden retriever puppies over their first 3 years of life. The model is trained on weight measurements from 2,000 golden retrievers, sampled every 3 months. A patient comes in with a 1-year-old poodle. The vet wants to use the model to predict the poodle's weight. Is this likely to produce an accurate prediction? Why or why not? A: **How to Think About It:** The key question is always: is the prediction target drawn from the same distribution as the training data? Here it is not -- the model was trained on one breed (golden retrievers) and is being applied to a different breed (poodles). These two breeds have different typical weights, growth curves, and size ranges. The model has learned the golden retriever growth pattern, not a general canine growth law. **Key Insight:** A regression model interpolates and extrapolates within the distribution it was trained on. Applying it to a structurally different population is out-of-distribution (OOD) generalization, which is unreliable. **The Analysis:** 1. **Training distribution:** Golden retrievers, a large breed. Adult males typically weigh 65-75 lbs. 2. **Test point:** A poodle (assuming standard/miniature), a medium or small breed. Adult weights vary widely by poodle type -- standard poodles are large, toy poodles are tiny. 3. **The mismatch:** The model's learned coefficients for age, growth rate, etc., are calibrated to golden retriever biology. Poodles have different growth trajectories. The model will produce a prediction, but there is no reason to trust it. 4. **What would help:** Retrain on poodle data, or build a multi-breed model with breed as a feature. **Practical Considerations:** This is the core challenge of model deployment -- training on one population and predicting on another. It is especially common in finance: a model trained on one market regime often fails badly when the regime changes. Domain shift, covariate shift, and distribution drift are all names for the same problem. **Answer:** No, the model is unlikely to be accurate. It was trained on golden retriever data and is being applied to a different breed with different biological characteristics -- this is out-of-distribution prediction, which is unreliable. --- Proportional Resource Sharing [easy, Combinatorics] --- https://quantvault.org/problem-1476-proportional-resource-sharing.html Q: Three players start a Minecraft world and agree to share all resources equally. Player 1 collects 30 apples; Player 2 collects 18 apples; Player 3 collects no apples but instead provides 48 gold nuggets. The 48 gold nuggets are to be split between Player 1 and Player 2 in proportion to how many apples each contributed to Player 3. How many more gold nuggets does Player 1 receive than Player 2? A: **How to Think About It:** The three players share apples equally, so figure out how many apples each player ends up with, and then compute how many apples each player gave to Player 3. That gives the contribution ratio, which determines how the 48 gold nuggets are split. **Quick Estimate:** Total apples: $30 + 18 = 48$. Split equally among 3 players: 16 each. Player 1 has 30, keeps 16, gives 14 to Player 3. Player 2 has 18, keeps 16, gives 2 to Player 3. Ratio of contributions: 14 to 2, or 7 to 1. Player 1 gets $\frac{7}{8} \times 48 = 42$ nuggets; Player 2 gets $\frac{1}{8} \times 48 = 6$ nuggets. Difference: $42 - 6 = 36$. **Formal Solution:** Step 1: Find the equal share of apples. $$\text{Total apples} = 30 + 18 = 48 \implies \text{each player gets } \frac{48}{3} = 16 \text{ apples}$$ Step 2: Find each player's contribution to Player 3. $$\text{Player 1 gives: } 30 - 16 = 14 \text{ apples}$$ $$\text{Player 2 gives: } 18 - 16 = 2 \text{ apples}$$ $$\text{Total given to Player 3: } 14 + 2 = 16 \text{ apples}$$ Step 3: Split 48 gold nuggets proportionally. $$\text{Player 1 receives: } \frac{14}{16} \times 48 = \frac{7}{8} \times 48 = 42 \text{ nuggets}$$ $$\text{Player 2 receives: } \frac{2}{16} \times 48 = \frac{1}{8} \times 48 = 6 \text{ nuggets}$$ Step 4: Compute the difference. $$42 - 6 = 36$$ **Answer:** Player 1 receives $\boxed{36}$ more gold nuggets than Player 2. --- Python yield and C++ friend/static Keywords [easy, Coding] --- https://quantvault.org/problem-1479-python-yield-and-c-friend-static-keywords.html Q: This problem covers three keywords that come up constantly in systems and quant coding interviews. **(a)** Explain the `yield` keyword in Python. What does it do, how does it differ from `return`, and why would you use it? **(b)** Explain the `friend` keyword in C++. What does it grant, and what are the typical use cases? **(c)** Explain the `static` keyword in C++. It has several distinct meanings depending on context -- cover at least three of them. A: **How to Think About It:** These are language-mechanics questions. The interviewer is testing whether you actually write code in these languages or just know them superficially. For each keyword, lead with the core idea in one sentence, then give the key use case. Don't recite a definition -- explain *why* the feature exists. --- **Part (a): Python `yield`** `yield` turns a function into a generator -- instead of computing all results and returning them at once, execution is suspended at each `yield` and resumed on the next call to `next()`. ```python def countdown(n): while n > 0: yield n n -= 1 for x in countdown(5): print(x) # prints 5, 4, 3, 2, 1 ``` Key properties: - The function returns a generator object when called. No code runs yet. - Each `next()` call runs until the next `yield`, then suspends. - Local state (variables, position) is preserved between calls. - Memory-efficient: you never hold all values in memory simultaneously. When to use it: large data pipelines, infinite sequences, lazy evaluation. Classic examples: reading large files line by line, implementing coroutines, building itertools-style utilities. --- **Part (b): C++ `friend`** `friend` grants a non-member function or an external class access to the private and protected members of the class that declares the friendship. ```cpp class Matrix { double data[4][4]; public: friend Matrix operator*(const Matrix& a, const Matrix& b); // friend function friend class MatrixSerializer; // friend class }; ``` Key points: - Friendship is declared *inside* the class granting access -- the granting class is in control. - It is not symmetric (A friends B does not mean B friends A) and not transitive. - Common use cases: operator overloading (especially `<<` for stream output), tightly coupled utility classes (serializers, test fixtures), factory patterns. --- **Part (c): C++ `static`** `static` has four distinct meanings depending on context: 1. **Static member variable**: shared across all instances of the class. Useful for counters, caches, constants. ```cpp class Foo { static int instance_count; // one copy, shared by all Foo objects }; ``` 2. **Static member function**: can be called without an object instance. Cannot access non-static members (no `this` pointer). ```cpp Foo::reset_counter(); // no object needed ``` 3. **Static local variable**: initialized once, persists across function calls. Classic use: Meyers singleton pattern. ```cpp Config& Config::get() { static Config instance; // created once, on first call return instance; } ``` 4. **Static at file scope (in a .cpp file)**: limits the symbol's linkage to the current translation unit -- other .cpp files cannot see it. Prefer anonymous namespaces in modern C++, but `static` still works. **Answer:** `yield` creates lazy generators in Python; `friend` grants private-member access to external functions/classes in C++; `static` in C++ means one of four things depending on context -- shared class state, no-instance method, persistent local variable, or translation-unit-local symbol. --- R-Squared Invariance Under Transformations [easy, Regression] --- https://quantvault.org/problem-1480-r-squared-invariance-under-transformations.html Q: Consider the simple linear regression $Y = \alpha + \beta X + \epsilon$. (a) If you multiply all $X$ values by a constant $c \neq 0$, what happens to $R^2$? (b) If you add a constant $k$ to all $Y$ values, what happens to $R^2$? (c) If you add a new predictor $Z$ that is perfectly collinear with $X$ (i.e., $Z = aX + b$ for constants $a, b$), can $R^2$ increase? A: **How to Think About It:** In simple linear regression, $R^2 = r_{XY}^2$, the square of the Pearson correlation between $X$ and $Y$. Correlation has a well-known invariance property: it is unchanged by any affine transformation $X \to aX + b$ (as long as $a \neq 0$). Since $R^2$ is just the square of the correlation, the answer to all three parts is the same: $R^2$ does not change. The intuition is that correlation measures the strength of the linear relationship, not the scale or location of the data. **Quick Sanity Checks:** - Multiplying $X$ by $c$ stretches the scatter plot horizontally but does not change how tightly the points cluster around the best-fit line. - Shifting $Y$ by $k$ moves the scatter plot vertically but does not change the spread or the fit quality. - Adding a perfectly collinear predictor gives the model no new information -- it is literally the same variable relabeled. **Formal Solution:** **(a) Multiplying $X$ by $c \neq 0$: $R^2$ is unchanged.** Let $X' = cX$. The correlation between $X'$ and $Y$ is: $$r_{X'Y} = \frac{\text{Cov}(cX, Y)}{\text{SD}(cX) \cdot \text{SD}(Y)} = \frac{c \cdot \text{Cov}(X, Y)}{|c| \cdot \text{SD}(X) \cdot \text{SD}(Y)} = \text{sign}(c) \cdot r_{XY}$$ Since $R^2 = r^2$, squaring removes the sign: $R^2$ is unchanged. Note that if $c < 0$, the sign of $\hat{\beta}$ flips, but $R^2$ does not care about direction. **(b) Adding $k$ to all $Y$: $R^2$ is unchanged.** Let $Y' = Y + k$. Then: $$\text{Cov}(X, Y') = \text{Cov}(X, Y + k) = \text{Cov}(X, Y)$$ $$\text{SD}(Y') = \text{SD}(Y + k) = \text{SD}(Y)$$ So $r_{XY'} = r_{XY}$ and $R^2$ is unchanged. Adding a constant shifts the intercept $\hat{\alpha}$ but affects nothing else. **(c) Adding a perfectly collinear predictor: $R^2$ cannot increase.** If $Z = aX + b$, then $Z$ lies in the column space of $[\mathbf{1}, X]$. The design matrix $[\mathbf{1}, X, Z]$ has the same column space as $[\mathbf{1}, X]$: $$\text{span}\{\mathbf{1}, X, Z\} = \text{span}\{\mathbf{1}, X\}$$ The OLS fitted values $\hat{Y}$ are the projection of $Y$ onto the column space, which is identical in both models. Therefore $R^2 = \|\hat{Y} - \bar{Y}\|^2 / \|Y - \bar{Y}\|^2$ is exactly the same. In practice, the design matrix is rank-deficient, and standard software (R, Python, etc.) will either drop $Z$ or report a warning about perfect multicollinearity. The model is not identifiable -- there are infinitely many coefficient combinations $(\hat{\beta}_X, \hat{\beta}_Z)$ that produce the same fitted values. **Answer:** $R^2$ is unchanged in all three cases. (a) Correlation is scale-invariant. (b) Correlation is shift-invariant. (c) A collinear predictor adds no new information to the column space. --- Range of Reverse Regression Slope Given Forward Slope [medium, Regression] --- https://quantvault.org/problem-1482-range-of-reverse-regression-slope-given-forward-slope.html Q: Consider two regressions on centered data (zero-intercept): - Regress $y$ on $x$: $y \sim bx$, with OLS slope $b = \text{Cov}(x,y) / \text{Var}(x)$ - Regress $x$ on $y$: $x \sim cy$, with OLS slope $c = \text{Cov}(x,y) / \text{Var}(y)$ Given that $b = 1$, determine the range of possible values for $c$. Include detailed steps and reasoning. A: **How to Think About It:** This is the classic forward-vs-reverse regression slope problem. The bridge between the two slopes is the identity $bc = r^2$, the squared correlation. The subtle part the careless version misses: the constraint $b=1$ is not just "$r^2 \le 1$"; it actually forces $\mathrm{Cov}(x,y)$ to be strictly positive (for a nondegenerate regressor), which rules out $c=0$. So the range is the half-open interval $(0,1]$, not the closed $[0,1]$. **Quick Estimate:** From $bc=r^2$ and $b=1$ we get $c=r^2$. Since $0 \le r^2 \le 1$, naively $c\in[0,1]$. But $b=1$ means $\mathrm{Cov}(x,y)=\mathrm{Var}(x)$, and for a non-constant $x$ that is strictly positive, so $r^2>0$ -- the value $0$ is only a limit, never attained. The ceiling $c=1$ is reached at perfect correlation. So $c\in(0,1]$. **Approach:** Derive $bc=r^2$ from the OLS formulas, then use the extra information in $b=1$ to pin the sign of the covariance. **Formal Solution:** The zero-intercept OLS slopes on centered data are $$b = \frac{\mathrm{Cov}(x,y)}{\mathrm{Var}(x)}, \qquad c = \frac{\mathrm{Cov}(x,y)}{\mathrm{Var}(y)}.$$ Multiplying, $$bc = \frac{\mathrm{Cov}(x,y)^2}{\mathrm{Var}(x)\,\mathrm{Var}(y)} = r^2,$$ where $r$ is the Pearson correlation. By Cauchy-Schwarz, $r^2 \in [0,1]$, with $r^2=1$ iff $x$ and $y$ are perfectly linearly related. Now use $b=1$. From $b = \mathrm{Cov}(x,y)/\mathrm{Var}(x) = 1$ we get $$\mathrm{Cov}(x,y) = \mathrm{Var}(x).$$ For a nondegenerate regressor ($\mathrm{Var}(x) > 0$, otherwise the slope $b$ is undefined), this makes $\mathrm{Cov}(x,y) = \mathrm{Var}(x) > 0$ -- **strictly positive**. Therefore $$c = \frac{r^2}{b} = r^2 = \frac{\mathrm{Cov}(x,y)^2}{\mathrm{Var}(x)\,\mathrm{Var}(y)} > 0.$$ **Boundary behaviour:** - **$c \to 0^{+}$ is a limit, never reached.** Since $\mathrm{Cov}(x,y)=\mathrm{Var}(x)$ is fixed and positive, $c = \mathrm{Cov}(x,y)/\mathrm{Var}(y) = \mathrm{Var}(x)/\mathrm{Var}(y)$ shrinks toward $0$ only as $\mathrm{Var}(y)\to\infty$; it is strictly positive for every finite configuration. So $c=0$ is excluded. - **$c = 1$ is attained** at perfect correlation $r^2=1$: then $\mathrm{Var}(y)=\mathrm{Var}(x)$, both regressions recover the same line $y=x$ (up to centering), and $c=1$. - **$c>1$ is impossible**, since $c=r^2 \le 1$. **Practical note:** The asymmetry between $b$ and $c$ trips people up -- regressing $y$ on $x$ minimizes vertical residuals while regressing $x$ on $y$ minimizes horizontal ones, so the slopes differ unless the fit is perfect. The identity $bc=r^2$ ties them together, and the constraint $b=1$ is what forbids the degenerate $c=0$ endpoint. **Answer:** $c \in (0,\,1]$ -- strictly positive (because $b=1$ forces $\mathrm{Cov}(x,y)=\mathrm{Var}(x)>0$, hence $r^2>0$), reaching $1$ only at perfect correlation. --- Smallest Multiple of 66 At Least 600,000 [easy, Combinatorics] --- https://quantvault.org/problem-1495-smallest-multiple-of-66-at-least-600-000.html Q: Find the smallest multiple of $66$ that is greater than or equal to $600{,}000$. A: **How to Think About It:** You want $\lceil 600{,}000 / 66 \rceil \times 66$. The cleanest approach is direct division: divide $600{,}000$ by $66$, round up to the nearest integer, then multiply back. Under interview pressure without a calculator, you can get there in a few steps of mental arithmetic using known multiples of $66$. **Quick Estimate:** $66 \times 10{,}000 = 660{,}000$. That is $60{,}000$ too high. $66 \times 9{,}000 = 594{,}000$. That is $6{,}000$ too low. So the answer is between $66 \times 9{,}000$ and $66 \times 10{,}000$, and we need to add enough multiples of $66$ to bridge the $6{,}000$ gap. **Formal Solution:** Divide directly: $$\frac{600{,}000}{66} = \frac{100{,}000}{11} = 9{,}090.909\ldots$$ Round up: $\lceil 9{,}090.909 \rceil = 9{,}091$. Multiply back: $$66 \times 9{,}091 = 66 \times 9{,}000 + 66 \times 91 = 594{,}000 + 6{,}006 = 600{,}006$$ Verify: $600{,}006 \geq 600{,}000$ and $600{,}006 - 66 = 599{,}940 < 600{,}000$, so $600{,}006$ is indeed the smallest such multiple. **Answer:** $600{,}006$. --- Theta-Gamma Relationship and Delta-Hedged P&L [hard, Options Pricing] --- https://quantvault.org/problem-1502-theta-gamma-relationship-and-delta-hedged-p-l.html Q: You are running a delta-hedged option position -- you own the option and continuously adjust your stock hedge to stay delta-neutral. Show that the daily P&L of this position is approximately: $$\Delta\Pi \approx \frac{1}{2}\Gamma S^2 \left[\left(\frac{\Delta S}{S}\right)^2 - \sigma_{\text{imp}}^2 \Delta t\right]$$ where $\Gamma$ is the option's gamma, $S$ is the stock price, $\Delta S$ is the stock's move over the interval $\Delta t$, and $\sigma_{\text{imp}}$ is the implied volatility used to price the option. Then explain: why does a long gamma position profit when realized volatility exceeds implied volatility? What is the economic intuition behind each term? A: **How to Think About It:** When you delta-hedge an option, you remove exposure to the stock's direction. What is left? Two competing forces: theta (time decay eating away at you) and gamma (the convexity payoff when the stock actually moves). Every day, you are paying rent via theta and collecting windfalls via gamma whenever the stock moves. The daily scorecard is literally realized variance versus implied variance. If the stock is moving more than the option's price assumed, you come out ahead. If not, theta grinds you down. This is the most important relationship in options trading. **Quick Sanity Checks:** Before deriving anything, check what should be true. A long gamma position should profit from large moves in either direction (gamma is non-directional). The breakeven should be where realized vol equals implied vol -- that is when theta and gamma exactly offset. And the P&L should scale with $\Gamma S^2$, which is the dollar gamma -- the actual dollar exposure to variance. **Derivation:** Start with the Taylor expansion of the option value $V(S, t)$ over a small interval $\Delta t$: $$\Delta V \approx \frac{\partial V}{\partial t}\Delta t + \frac{\partial V}{\partial S}\Delta S + \frac{1}{2}\frac{\partial^2 V}{\partial S^2}(\Delta S)^2 = \Theta\,\Delta t + \Delta_{\text{opt}}\,\Delta S + \frac{1}{2}\Gamma(\Delta S)^2$$ Your hedge is short $\Delta_{\text{opt}}$ shares. The P&L of the hedged portfolio is: $$\Delta\Pi = \Delta V - \Delta_{\text{opt}}\,\Delta S = \Theta\,\Delta t + \frac{1}{2}\Gamma(\Delta S)^2$$ The delta term cancels -- that is the whole point of delta hedging. You are left with theta plus the gamma P&L from the realized move. Now use the Black-Scholes PDE to eliminate $\Theta$. Under BSM, the option value satisfies: $$\Theta + \frac{1}{2}\sigma_{\text{imp}}^2 S^2 \Gamma + rS\Delta_{\text{opt}} = rV$$ The $rS\Delta_{\text{opt}}$ and $rV$ terms are small over short intervals (they are $O(r\,\Delta t)$ effects). Dropping them: $$\Theta \approx -\frac{1}{2}\sigma_{\text{imp}}^2 S^2 \Gamma$$ This is the theta-gamma trade-off in its purest form: theta is the cost you pay for owning convexity. Substitute back into the P&L expression: $$\Delta\Pi \approx -\frac{1}{2}\sigma_{\text{imp}}^2 S^2 \Gamma\,\Delta t + \frac{1}{2}\Gamma(\Delta S)^2$$ Factor out $\frac{1}{2}\Gamma S^2$: $$\boxed{\Delta\Pi \approx \frac{1}{2}\Gamma S^2 \left[\left(\frac{\Delta S}{S}\right)^2 - \sigma_{\text{imp}}^2 \Delta t\right]}$$ **Practical Interpretation:** The term $(\Delta S / S)^2$ is the realized variance over the interval. The term $\sigma_{\text{imp}}^2 \Delta t$ is the implied variance "budget" for that interval. The P&L is proportional to their difference, scaled by dollar gamma $\frac{1}{2}\Gamma S^2$. - **Long gamma** ($\Gamma > 0$): you profit when realized moves exceed implied. Every day the stock moves more than "expected," you collect. Every quiet day, you bleed theta. - **Short gamma** ($\Gamma < 0$): the mirror image. You collect theta every day and pray the stock does not move too much. - **Breakeven**: realized vol $=$ implied vol. Over a long enough horizon, if realized vol matches implied, the gamma gains and theta losses exactly cancel. This is why options market makers obsess over realized versus implied volatility. When you buy an option at 20 vol and the stock realizes 25 vol, you make money not from any directional bet, but from the gamma scalping -- each rebalance of the delta hedge locks in a small profit. Conversely, if you buy at 20 vol and the stock only realizes 15, theta eats you alive. **Answer:** The delta-hedged P&L is $\frac{1}{2}\Gamma S^2[(\Delta S/S)^2 - \sigma_{\text{imp}}^2 \Delta t]$, which equals dollar gamma times the difference between realized and implied variance. Long gamma profits when realized vol exceeds implied vol because the gamma gains from actual stock moves outweigh the theta cost baked into the option price. --- Vector Projection and Gram-Schmidt Orthogonalization [medium, Linear Algebra] --- https://quantvault.org/problem-1510-vector-projection-and-gram-schmidt-orthogonalization.html Q: Given vectors $\mathbf{a}$ and $\mathbf{b}$ in $\mathbb{R}^n$: 1. Derive the formula for the projection of $\mathbf{b}$ onto $\mathbf{a}$, and compute the residual (the component of $\mathbf{b}$ orthogonal to $\mathbf{a}$). Verify that the residual is indeed orthogonal to $\mathbf{a}$. 2. Explain how this projection extends to the Gram-Schmidt procedure: given a set of linearly independent vectors $\{\mathbf{v}_1, \ldots, \mathbf{v}_k\}$, produce an orthogonal set $\{\mathbf{u}_1, \ldots, \mathbf{u}_k\}$ spanning the same subspace. 3. How does Gram-Schmidt relate to the QR decomposition? A: **How to Think About It:** Projection is the most fundamental operation in linear algebra -- it decomposes a vector into "the part along a direction" and "the part perpendicular to it." If you think of $\mathbf{a}$ as a signal and $\mathbf{b}$ as noisy data, the projection extracts the signal component and the residual is the noise. Gram-Schmidt just applies this idea repeatedly: project out each previously found direction, and what remains is the new orthogonal component. This is how you build an orthonormal basis from any linearly independent set. **Quick Estimate:** The projection of $\mathbf{b}$ onto $\mathbf{a}$ should be a scalar multiple of $\mathbf{a}$. The scalar is the "shadow length" -- $\cos\theta \cdot \|\mathbf{b}\| / \|\mathbf{a}\|$ -- which works out to $\mathbf{a}^T \mathbf{b} / \mathbf{a}^T \mathbf{a}$. **Formal Solution:** **Part 1 -- Projection and residual:** We want the vector $\hat{\mathbf{b}}$ in the span of $\mathbf{a}$ that is closest to $\mathbf{b}$. Write $\hat{\mathbf{b}} = c \, \mathbf{a}$ and minimize $\|\mathbf{b} - c \, \mathbf{a}\|^2$: $$\frac{d}{dc} \|\mathbf{b} - c\mathbf{a}\|^2 = -2\mathbf{a}^T(\mathbf{b} - c\mathbf{a}) = 0$$ $$c = \frac{\mathbf{a}^T \mathbf{b}}{\mathbf{a}^T \mathbf{a}}$$ So the projection is: $$\text{proj}_{\mathbf{a}}(\mathbf{b}) = \frac{\mathbf{a}^T \mathbf{b}}{\mathbf{a}^T \mathbf{a}} \, \mathbf{a}$$ The residual is: $$\mathbf{b}_{\perp} = \mathbf{b} - \text{proj}_{\mathbf{a}}(\mathbf{b}) = \mathbf{b} - \frac{\mathbf{a}^T \mathbf{b}}{\mathbf{a}^T \mathbf{a}} \, \mathbf{a}$$ **Verification:** Check $\mathbf{a}^T \mathbf{b}_{\perp} = 0$: $$\mathbf{a}^T \mathbf{b}_{\perp} = \mathbf{a}^T \mathbf{b} - \frac{\mathbf{a}^T \mathbf{b}}{\mathbf{a}^T \mathbf{a}} \, \mathbf{a}^T \mathbf{a} = \mathbf{a}^T \mathbf{b} - \mathbf{a}^T \mathbf{b} = 0 \quad \checkmark$$ **Part 2 -- Gram-Schmidt orthogonalization:** Given linearly independent $\{\mathbf{v}_1, \ldots, \mathbf{v}_k\}$, produce orthogonal $\{\mathbf{u}_1, \ldots, \mathbf{u}_k\}$: $$\mathbf{u}_1 = \mathbf{v}_1$$ For $j = 2, \ldots, k$: $$\mathbf{u}_j = \mathbf{v}_j - \sum_{i=1}^{j-1} \frac{\mathbf{u}_i^T \mathbf{v}_j}{\mathbf{u}_i^T \mathbf{u}_i} \, \mathbf{u}_i$$ Each step subtracts from $\mathbf{v}_j$ its projections onto all previously computed orthogonal vectors. What remains is the component of $\mathbf{v}_j$ orthogonal to $\text{span}(\mathbf{u}_1, \ldots, \mathbf{u}_{j-1})$. **Why it works:** By induction. After step $j$, $\mathbf{u}_j$ is orthogonal to $\mathbf{u}_1, \ldots, \mathbf{u}_{j-1}$ by construction (we subtracted all the projections). And $\text{span}(\mathbf{u}_1, \ldots, \mathbf{u}_j) = \text{span}(\mathbf{v}_1, \ldots, \mathbf{v}_j)$ because each $\mathbf{u}_j$ is a linear combination of $\mathbf{v}_1, \ldots, \mathbf{v}_j$ and vice versa. To get an orthonormal set, normalize: $\mathbf{e}_j = \mathbf{u}_j / \|\mathbf{u}_j\|$. **Part 3 -- Connection to QR decomposition:** Gram-Schmidt on the columns of a matrix $V = [\mathbf{v}_1 | \cdots | \mathbf{v}_k]$ produces $Q = [\mathbf{e}_1 | \cdots | \mathbf{e}_k]$ with orthonormal columns, and the coefficients of the projection steps form an upper triangular matrix $R$: $$V = QR$$ where $R_{ij} = \mathbf{e}_i^T \mathbf{v}_j$ for $i \leq j$, and $R_{ij} = 0$ for $i > j$. The upper-triangular structure follows because $\mathbf{v}_j$ is expressed as a combination of $\mathbf{e}_1, \ldots, \mathbf{e}_j$ only (never using later basis vectors). In practice, the classical Gram-Schmidt algorithm can suffer from numerical instability. The modified Gram-Schmidt algorithm reorthogonalizes at each step, and Householder reflections provide an even more stable alternative for computing QR. **Answer:** $\text{proj}_{\mathbf{a}}(\mathbf{b}) = \frac{\mathbf{a}^T \mathbf{b}}{\mathbf{a}^T \mathbf{a}} \mathbf{a}$, with residual $\mathbf{b} - \text{proj}_{\mathbf{a}}(\mathbf{b})$ orthogonal to $\mathbf{a}$. Gram-Schmidt applies this iteratively to build an orthogonal basis, and the process is equivalent to computing the QR decomposition $V = QR$. --- Why Use Adjusted R-Squared? [easy, Regression] --- https://quantvault.org/problem-1513-why-use-adjusted-r-squared.html Q: You are fitting a linear regression and your colleague suggests adding more predictors to improve the model. They point out that $R^2$ keeps increasing as you add variables. What is wrong with using $R^2$ as the sole criterion for model selection, and how does adjusted $R^2$ address this? A: **How to Think About It:** $R^2$ measures the fraction of variance explained by the model. The problem is that it can only go up when you add a variable -- even if that variable is pure noise. This means an uncritical model builder can inflate $R^2$ arbitrarily by throwing in garbage predictors. Adjusted $R^2$ penalizes you for every extra parameter you add, so it only increases when a new variable earns its keep. **Key Insight:** $R^2$ rewards complexity for free; adjusted $R^2$ makes complexity pay its own way through the degrees-of-freedom penalty. **The Method:** Recall the two formulas. Standard $R^2$: $$R^2 = 1 - \frac{\text{SSE}}{\text{SST}}$$ Adjusted $R^2$: $$R^2_{\text{adj}} = 1 - \frac{\text{SSE}/(n - p - 1)}{\text{SST}/(n-1)}$$ where $n$ is the number of observations and $p$ is the number of predictors. The key difference: in $R^2_{\text{adj}}$, the residual variance is estimated as $\text{SSE}/(n-p-1)$ rather than raw $\text{SSE}$. As $p$ increases, the denominator $n-p-1$ shrinks, which inflates the SSE term and penalizes the $R^2_{\text{adj}}$ score. Adding a completely uninformative predictor reduces SSE by a tiny amount but costs one degree of freedom -- the net effect is a decrease in $R^2_{\text{adj}}$. Formally, adding predictor $j$ to the model increases $R^2_{\text{adj}}$ if and only if the $t$-statistic on $\hat{\beta}_j$ satisfies $|t_j| > 1$, which is a much lower bar than statistical significance but at least rules out pure noise. **Practical Considerations:** - Adjusted $R^2$ is still an in-sample metric and does not prevent overfitting as effectively as cross-validation. - For model selection among many predictors, prefer AIC, BIC, or out-of-sample validation. Adjusted $R^2$ is a quick check, not a complete solution. - In finance, adding lagged returns or correlated macro variables can inflate $R^2$ without adding genuine predictive power -- this is why out-of-sample $R^2$ is the standard in forecasting research. **Answer:** $R^2$ never decreases when predictors are added, even if they are noise. Adjusted $R^2$ corrects for this by deflating the residual sum of squares by its degrees of freedom $(n-p-1)$, penalizing model complexity. It increases only when a new predictor reduces residual variance by more than the baseline noise level. --- Winsorization Impact on OLS [hard, Statistics] --- https://quantvault.org/problem-1515-winsorization-impact-on-ols.html Q: A return $R$ is drawn from a two-component normal mixture: with probability $0.99$, $R \sim N(0, 1)$, and with probability $0.01$, $R \sim N(0, 25)$. The two components are independent. Define the winsorized return as $$R^{(c)} = \text{sign}(R) \cdot \min(|R|, c)$$ with threshold $c = 3$. (a) Express $E[R^{(c)}]$ and $\text{Var}(R^{(c)})$ in terms of normal CDFs, densities, and the mixture weights. You may leave standard normal tail integrals unevaluated (e.g., $\Phi(\cdot)$, $\phi(\cdot)$). (b) Qualitatively but precisely, explain how winsorization changes: - The estimated Sharpe ratio when $R$ is used as the return series. - The stability of OLS coefficient estimates when $R$ is the target (dependent) variable. A: **How to Think About It:** The mixture model here is designed to produce fat tails -- 99% of the time returns are well-behaved $N(0,1)$, but 1% of the time you get draws from $N(0,25)$, which has standard deviation $5$. Winsorization at $c = 3$ clips extreme values, replacing anything beyond $\pm 3$ with $\pm 3$. For the $N(0,1)$ component, $|R| > 3$ is a roughly $0.3\%$ event -- rare, barely affected. For the $N(0,25)$ component, $|R| > 3$ happens about $45\%$ of the time -- winsorization is doing heavy lifting on the fat tail. The key economic insight: the mixture is symmetric around zero, so winsorization does not change the mean (it stays zero), but it dramatically reduces the variance by chopping off the heavy tails. **Quick Estimate:** The unconditional variance of $R$ is $0.99 \times 1 + 0.01 \times 25 = 1.24$. After winsorization, the $N(0,1)$ component barely changes (its contribution stays near $1$). The $N(0,25)$ component, however, gets truncated hard at $\pm 3$. The truncated second moment of $N(0,25)$ inside $[-3,3]$ is roughly $E[R^2 \mid |R| \le 3] \approx 3.5$ (since $3/5 = 0.6$ in standardized units), and the mass beyond $\pm 3$ contributes $9 \cdot P(|R|>3) \approx 9 \times 0.45 = 4.05$. So the second moment from the heavy-tail component is roughly $3.5 \times 0.55 + 4.05 \approx 5.98$, contributing $0.01 \times 5.98 \approx 0.06$ to the total. Winsorized variance is approximately $0.99 \times 1.0 + 0.01 \times 5.98 \approx 1.05$, down from $1.24$ -- a meaningful reduction driven entirely by clipping the heavy-tail component. **Approach:** We compute moments by conditioning on each mixture component and splitting the integral at $\pm c$. **Formal Solution:** Let $\sigma_1 = 1$, $\sigma_2 = 5$, $w_1 = 0.99$, $w_2 = 0.01$, and $c = 3$. Write the density of $R$ as $$f_R(r) = w_1 \, \phi_1(r) + w_2 \, \phi_2(r)$$ where $\phi_j(r) = \frac{1}{\sigma_j} \phi\left(\frac{r}{\sigma_j}\right)$ and $\phi(\cdot)$ is the standard normal density. **Part (a): Mean and variance of $R^{(c)}$.** By symmetry of the mixture (both components have mean zero), $R$ is symmetric about zero. Winsorization preserves this symmetry: $R^{(c)}$ and $-R^{(c)}$ have the same distribution. Therefore $$E[R^{(c)}] = 0$$ For the variance, since the mean is zero, $\text{Var}(R^{(c)}) = E[(R^{(c)})^2]$. We compute the second moment by conditioning on each component. For component $j$: $$E[(R^{(c)})^2 \mid \text{component } j] = \int_{-c}^{c} r^2 \, \phi_j(r) \, dr \;+\; c^2 \, P_j(|R| > c)$$ where $P_j(|R| > c) = 2\left(1 - \Phi\left(\frac{c}{\sigma_j}\right)\right)$. For the integral, substituting $z = r/\sigma_j$: $$\int_{-c}^{c} r^2 \, \phi_j(r) \, dr = \sigma_j^2 \int_{-c/\sigma_j}^{c/\sigma_j} z^2 \, \phi(z) \, dz$$ Using the identity $\int_{-a}^{a} z^2 \phi(z) \, dz = 2\Phi(a) - 1 - 2a\phi(a)$ (from integration by parts), with $a_j = c/\sigma_j$: $$\int_{-c}^{c} r^2 \, \phi_j(r) \, dr = \sigma_j^2 \left[2\Phi(a_j) - 1 - 2a_j \phi(a_j)\right]$$ Putting it together: $$E[(R^{(c)})^2 \mid \text{comp } j] = \sigma_j^2 \left[2\Phi(a_j) - 1 - 2a_j \phi(a_j)\right] + c^2 \cdot 2\left[1 - \Phi(a_j)\right]$$ where $a_j = c / \sigma_j$. For $j=1$: $a_1 = 3$; for $j=2$: $a_2 = 3/5 = 0.6$. The total variance is: $$\text{Var}(R^{(c)}) = \sum_{j=1}^{2} w_j \left\{ \sigma_j^2 \left[2\Phi(a_j) - 1 - 2a_j\phi(a_j)\right] + 2c^2\left[1 - \Phi(a_j)\right] \right\}$$ **Part (b): Qualitative effects.** **Effect on estimated Sharpe ratio:** The Sharpe ratio is $\hat{\mu}/\hat{\sigma}$. Winsorization does not change the population mean (it is zero here by symmetry), so $\hat{\mu}$ remains centered at $0$ -- and therefore the estimated Sharpe **stays essentially zero and does not meaningfully rise**. Shrinking the denominator cannot systematically lift a ratio whose numerator is centered at zero; it merely rescales its sampling noise (for $\mu = 0$ the sampling variance of $\hat{\mu}/\hat{\sigma}$ is $\approx 1/n$ with or without winsorization). The real gain is elsewhere: the variance estimate itself becomes far more **stable** across subsamples -- without winsorization, a single $5\sigma$ draw from the heavy-tail component can blow up $\hat{\sigma}^2$ (in simulation, winsorizing cuts the sampling standard deviation of $\hat{\sigma}^2$ by roughly a factor of $3$), making the volatility estimate -- and hence any Sharpe built on it -- noisy and unreliable. **Effect on OLS stability:** When $R$ is the dependent variable, OLS estimates are $\hat{\beta} = (X^T X)^{-1} X^T R$. Extreme values of $R$ have high **leverage on the residuals** and inflate the residual variance, which makes $\hat{\beta}$ noisy. The heavy-tail component generates rare but extreme $R$ values that dominate the sum of squared residuals, producing large variance in $\hat{\beta}$ across samples. Winsorization clips these extremes, which: (1) reduces the variance of $\hat{\beta}$ (more stable coefficient estimates); (2) reduces the influence of outlying observations (robust-ifies the fit); and (3) shrinks confidence intervals for $\hat{\beta}$. The trade-off: winsorization introduces **bias** in $\hat{\beta}$ because it distorts the conditional mean $E[R^{(c)} \mid X] \neq E[R \mid X]$ when the true relationship generates extreme fitted values. In practice, for fat-tailed returns, this bias-variance trade-off almost always favors winsorization -- you lose a tiny bit of accuracy in modeling the tails but gain much more in estimation stability. **Answer:** (a) $E[R^{(c)}] = 0$ by symmetry. The variance is: $$\text{Var}(R^{(c)}) = \sum_{j=1}^{2} w_j \left\{ \sigma_j^2 \left[2\Phi(a_j) - 1 - 2a_j\phi(a_j)\right] + 2c^2\left[1 - \Phi(a_j)\right] \right\}$$ with $a_1 = 3$, $a_2 = 0.6$, $w_1 = 0.99$, $w_2 = 0.01$, $\sigma_1 = 1$, $\sigma_2 = 5$, $c = 3$. (b) Winsorization leaves the estimated Sharpe ratio essentially at zero -- by symmetry $\hat{\mu}$ stays centered at $0$, so $\hat{\mu}/\hat{\sigma}$ does not meaningfully rise; its real benefit is a much smaller and far more stable variance estimate (and hence a more reliable Sharpe estimate). It also stabilizes OLS coefficient estimates by clipping extreme residuals, at the cost of introducing mild bias from distorting the conditional mean in the tails. --- Bayesian ML, Overfitting, and Error Metrics [medium, Statistics] --- https://quantvault.org/problem-1526-bayesian-ml-overfitting-and-error-metrics.html Q: You're interviewing for a quant research role and the interviewer says: "Let's talk broad ML. Walk me through three things." (a) How do Bayesian methods apply in machine learning? What's the connection between Bayesian priors and regularization? When would you prefer a full Bayesian approach over a point estimate? (b) You've built a model that performs beautifully on the training set but falls apart out of sample. Walk me through your toolkit for preventing overfitting -- not just a list of techniques, but when and why you'd pick each one. (c) You need to choose an error metric for a new model. What are the main families of error measures for regression and classification, and how does the choice of metric change depending on the problem's cost structure? A: **How to Think About It:** This is a breadth question -- the interviewer wants to see that you understand the landscape of ML methodology, not just one narrow technique. The key is to connect the three parts: Bayesian methods are themselves a form of overfitting prevention (part a feeds into part b), and the choice of error metric (part c) determines what "good generalization" even means in practice. A strong answer weaves these threads together: what prior beliefs do you bring (Bayesian), how do you keep the model honest (regularization / overfitting control), and how do you measure success (error metrics matched to the cost structure). The unifying theme is: how do you build models that *generalize*, and how do you *measure* that they do? **Quick Estimate:** Before the detail, the one-line skeleton you want to land: (a) a regularization penalty *is* a log-prior, so MAP = regularized MLE, and you go full-Bayes when uncertainty/small-sample/cost-of-overconfidence matters; (b) overfitting = fitting noise, fought with a layered toolkit (validation splits, regularization, simpler models, early stopping, dropout, ensembling, more/augmented data, leakage control) chosen by model class and data structure; (c) error metrics split into regression (MAE, MSE/RMSE, MAPE/SMAPE, Huber, quantile, R^2, likelihood) and classification (accuracy, precision/recall/F1, ROC-AUC, PR-AUC, log loss, Brier, calibration), and you pick the one whose implied loss matches the *asymmetric business cost*. **Approach:** Answer each sub-question in turn, but explicitly cross-reference: the prior/penalty equivalence from (a) is the first tool in (b), and the cost structure in (c) is what tells you which errors the regularization in (b) should be tolerant of. Keep each part 'when and why', not just a list. **Formal Solution:** **(a) Bayesian Methods in ML** The Bayesian framework treats model parameters $\theta$ as random variables with a prior distribution $p(\theta)$. After observing data $D$, you update via Bayes' rule: $$p(\theta \mid D) = \frac{p(D \mid \theta)\,p(\theta)}{p(D)} \propto p(D \mid \theta)\, p(\theta).$$ Here $p(D\mid\theta)$ is the *likelihood* and $p(\theta\mid D)$ the *posterior*. Predictions use the full *posterior predictive*, integrating over parameter uncertainty rather than plugging in a single point estimate: $$p(y_{\text{new}} \mid D) = \int p(y_{\text{new}} \mid \theta)\, p(\theta \mid D)\, d\theta.$$ Practical connections: - **Regularization as a prior (the key equivalence):** maximizing the *log-posterior* $\log p(D\mid\theta) + \log p(\theta)$ is maximum-likelihood plus an additive penalty equal to the log-prior. A Gaussian prior $\theta_i \sim N(0,\sigma^2)$ has log-density $\propto -\theta_i^2/(2\sigma^2)$, i.e. a *squared-norm* penalty -- this is exactly **L2 / Ridge** regularization. A Laplace prior $\theta_i \sim \text{Laplace}(0,b)$ has log-density $\propto -|\theta_i|/b$, i.e. an *absolute-value* penalty -- this is **L1 / Lasso**, and the Laplace density's non-differentiable peak at zero is what drives exact-zero coefficients (sparsity). So every regularization penalty is implicitly a Bayesian prior; a flat/uniform prior recovers plain MLE (no penalty). - **MAP vs. full Bayes:** Maximum a posteriori (MAP) estimation finds the single most probable $\theta$ (the mode) -- this is what regularized MLE gives you, a *point estimate*. Full Bayesian inference instead retains the whole posterior, giving *calibrated uncertainty*. In quant finance this matters: "expected return 2% with a wide posterior" is a very different trade from "2% with a tight posterior." - **When to go full Bayesian:** (i) limited data / small-sample regimes (common in finance), where the prior does real work and point estimates are unstable; (ii) when you need uncertainty quantification for risk management or position sizing; (iii) hierarchical / multilevel structure (e.g. partial pooling across assets or regimes), where a hierarchical prior shares strength across groups; and (iv) when the cost of overconfidence is high (tail-risk decisions). When data is abundant and you only need a fast point prediction, MAP / regularized MLE is usually enough and far cheaper than sampling the posterior. **(b) Overfitting Prevention Toolkit** Overfitting happens when the model fits *noise* in the training data rather than the underlying signal -- training error keeps falling while validation/out-of-sample error rises. The core trade-off is bias vs. variance: overfit models are low-bias, high-variance. The toolkit, organized by *when and why* you reach for each: 1. **Proper data splitting / cross-validation (the diagnostic that comes first).** You cannot fight overfitting you cannot measure. Use a clean train/validation/test split, and K-fold CV for i.i.d. data. Crucially, for time series (most of quant finance), use **walk-forward / expanding-window** CV so the model is never trained on the future -- ordinary K-fold leaks look-ahead information and flatters the model. 2. **Regularization (L1/L2/Elastic Net) -- your first modeling line of defense.** L2 (Ridge) shrinks all coefficients toward zero: use when you believe most features contribute a little (the Gaussian-prior view from part a). L1 (Lasso) drives some coefficients exactly to zero: use when you suspect many features are irrelevant and want built-in feature selection (the Laplace-prior view). Elastic Net blends both when features are correlated. 3. **Simpler models / reduced complexity.** The cheapest fix is often a lower-capacity model: fewer features, lower polynomial degree, shallower trees, smaller networks. Prefer this when data is scarce relative to parameters -- a simpler model that generalizes beats a complex one that memorizes. 4. **Feature selection / dimensionality reduction.** Drop or combine features (filter/wrapper methods, PCA) to cut variance when you have many weakly-informative or collinear predictors. 5. **Early stopping.** For iterative learners (gradient boosting, neural nets), monitor validation loss and stop when it starts rising. It implicitly caps effective complexity and is essentially free. 6. **Dropout.** Specific to neural networks: randomly zero out units during training so the net cannot rely on any single neuron, forcing redundant representations. Mathematically akin to training an ensemble of sub-networks; use when a deep net overfits and you cannot easily get more data. 7. **Ensemble methods.** Bagging (e.g. Random Forest) averages over bootstrap samples to reduce *variance* -- good for high-variance base learners. Boosting (e.g. XGBoost) sequentially corrects errors to reduce *bias*, but its later rounds overfit, so it needs regularization (shrinkage, max depth, subsampling) and early stopping. 8. **More data / data augmentation.** The most reliable cure for variance is more data. When you cannot get it, synthesize examples via label-preserving transformations -- ubiquitous in images/NLP, less natural for tabular financial data (though resampling, jittering, or simulation can help). 9. **Leakage control.** Overfitting's evil twin: features that secretly encode the target (e.g. using future information, target-derived features, or fitting scalers on the full dataset before splitting). Audit the pipeline so all preprocessing is fit on training folds only -- otherwise every metric is a lie. 10. **Hyperparameter tuning.** Choose regularization strength, tree depth, learning rate, etc. on validation data (grid/random/Bayesian search), and report final performance on a held-out test set you never touched during tuning. The meta-point: there is no single fix. You diagnose with proper validation, then pick tools by *model class* (dropout for nets, depth/shrinkage for boosting, L1/L2 for linear) and *data structure* (walk-forward for time series, augmentation when data is scarce). **(c) Error Metrics and Cost Structure** The metric you optimize *is* the implicit definition of a good model, so it must match the problem's cost structure. Two families: *Regression metrics:* - **MAE** (mean absolute error): robust to outliers, treats all errors linearly; corresponds to predicting the conditional *median*. - **MSE / RMSE**: squares errors, so it penalizes large misses heavily and corresponds to predicting the conditional *mean*; sensitive to outliers (which can be a feature or a bug). RMSE is in the units of the target. - **MAPE / SMAPE** (percentage errors): scale-free, useful when relative error matters across different magnitudes; MAPE blows up near zero and is asymmetric, SMAPE partially fixes the symmetry. - **Huber loss**: quadratic for small residuals, linear for large ones -- a tunable compromise between MSE's sensitivity and MAE's robustness; good when you want mean-like behavior but with outlier protection. - **Quantile (pinball) loss**: targets a specific quantile, giving asymmetric penalties for over- vs. under-prediction -- exactly what you want when the cost of over- and under-shooting differ (e.g. inventory, VaR, demand). - **$R^2$**: variance explained, a unitless goodness-of-fit summary (not a loss to optimize directly); useful for communicating but can mislead with non-stationary data. - **Likelihood-based losses** (e.g. Gaussian/Poisson negative log-likelihood): the principled choice when you have a probabilistic model and care about the full predictive distribution. *Classification metrics:* - **Accuracy**: fraction correct -- fine for balanced classes, badly misleading under imbalance (99% accuracy is trivial if 99% of cases are one class). - **Precision / Recall / F1**: precision = of predicted positives, how many are right (cost of *false positives*); recall = of actual positives, how many you caught (cost of *false negatives*); F1 is their harmonic mean. Choose based on which error is costlier -- e.g. fraud/disease detection prioritizes recall, spam filtering may prioritize precision. - **ROC-AUC**: ranking quality across all thresholds, threshold-independent; can look optimistic under heavy class imbalance. - **PR-AUC** (precision-recall AUC): more informative than ROC-AUC when positives are rare, because it ignores the abundant true negatives. - **Log loss (cross-entropy)** and **Brier score**: *proper scoring rules* that reward well-*calibrated probabilities*, not just correct labels -- use when you act on the probability itself (position sizing, Kelly bets, expected-value decisions). - **Calibration** (reliability diagrams): a diagnostic that predicted probabilities match observed frequencies -- essential whenever downstream decisions multiply probability by payoff. - **Cost-weighted / confusion-matrix loss**: assign explicit dollar costs to each cell of the confusion matrix and minimize expected cost; this is the most direct way to encode an *asymmetric* cost structure and to set the decision *threshold* accordingly. *How cost structure changes the choice:* when errors are asymmetric (a false negative on fraud costs far more than a false positive), move from accuracy toward recall, cost-weighted loss, or quantile loss, and shift the decision threshold off 0.5. Under class imbalance, prefer PR-AUC, precision/recall, and resampling over raw accuracy. When you need *tail-risk* control, square the errors (MSE/RMSE) or use quantile loss at extreme quantiles. When downstream decisions consume the *probability* (sizing a bet), optimize and check a proper scoring rule (log loss, Brier) plus calibration, not just a hard-label metric. **Answer:** (a) Bayesian ML treats parameters as random with a prior, updates to a posterior via Bayes' rule, and predicts with the posterior predictive; a regularization penalty is exactly a log-prior, so **MAP = regularized MLE**, with **L2/Ridge $\Leftrightarrow$ Gaussian prior** and **L1/Lasso $\Leftrightarrow$ Laplace prior (sparsity)** -- go full Bayes when you need uncertainty, have small samples or hierarchy, or when overconfidence is costly. (b) Overfitting is fitting noise; fight it with proper (time-series-aware) validation, regularization, simpler models, feature selection, early stopping, dropout, ensembling, more/augmented data, leakage control, and tuning -- chosen by model class and data structure. (c) Regression metrics (MAE, MSE/RMSE, MAPE/SMAPE, Huber, quantile, $R^2$, likelihood) and classification metrics (accuracy, precision/recall/F1, ROC-AUC, PR-AUC, log loss, Brier, calibration, cost-weighted loss) should be selected so the implied loss matches the problem's asymmetric costs, class imbalance, tail risk, calibration needs, and decision thresholds. --- Bayesian Update from a No-Trade Event [medium, Statistics] --- https://quantvault.org/problem-1529-bayesian-update-from-a-no-trade-event.html Q: You are a market maker quoting a symmetric spread $[-s, +s]$ around zero for a one-shot game. The true value of the asset is $V \sim N(0, \sigma^2)$, unknown to you but observed perfectly by informed traders. The arrival structure is as follows. With probability $p_0$, nobody arrives and no trade occurs. Conditional on arrival, with probability $\mu$ an informed trader shows up -- she buys if $V > +s$ and sells if $V < -s$, and does nothing if $|V| \leq s$. With probability $1 - \mu$, a noise trader arrives -- she buys or sells with equal probability $\frac{1}{2}$, independently of $V$, but declines to trade at all with probability $q$. You observe that no trade occurs. **(a)** Write down $P(\text{no trade} \mid V = v)$ as an explicit function of $v$, $s$, $\mu$, $p_0$, and $q$. **(b)** Use Bayes' theorem to compute the posterior mean $E[V \mid \text{no trade}]$. Does observing no trade shift your mean estimate of $V$ away from zero? Why or why not? **(c)** Even though the posterior mean may be unchanged, the posterior distribution is not the same as the prior. Describe qualitatively how the posterior variance $\text{Var}(V \mid \text{no trade})$ compares to $\sigma^2$, and explain what this implies for how you should requote your spread. A: **How to Think About It:** You observed silence -- nobody traded. Who could have produced that silence? Either nobody arrived at all, or an informed trader arrived but found $V$ inside your spread (she trades if and only if $|V| > s$, so her silence is genuine information), or a noise trader arrived and happened to decline. The informed channel is the only one whose behavior depends on $V$, and it depends on $V$ only through $|V|$. So the no-trade likelihood is an *even* function of $v$: silence is exactly as consistent with $V = +v$ as with $V = -v$. Multiplying an even likelihood by a prior symmetric about zero gives a posterior symmetric about zero -- the mean cannot move. What does move is the spread of the distribution: large $|v|$ values would have triggered the informed trader, so silence downweights the tails and the posterior variance shrinks. One bookkeeping trap: the informed/noise split happens *conditional on arrival*, so those branches must carry the factor $(1 - p_0)$, otherwise the "likelihood" can exceed 1. **Quick Estimate:** Take $\sigma = 2$, $s = 1$, $\mu = 0.3$, $p_0 = 0.1$, $q = 0.2$. Inside the spread ($|v| \le s$) nobody who arrives ever hits you: likelihood $= 0.1 + 0.9(0.3 + 0.7 \times 0.2) = 0.496$. Outside the spread the informed trader always trades: likelihood $= 0.1 + 0.9 \times 0.7 \times 0.2 = 0.226$. The inside region is roughly $2.2\times$ as consistent with silence as the tails, so the posterior piles up symmetrically near zero: mean stays $0$, variance drops (simulation: $2.84$ vs. prior $4$). **Approach:** Condition on the arrival tree to get the likelihood, then exploit evenness in Bayes' rule. **Formal Solution:** **(a) Likelihood of no trade.** Three disjoint events produce no trade. With probability $p_0$, nobody arrives. With probability $(1-p_0)\mu$, an informed trader arrives and stays silent only when $|V| \le s$. With probability $(1-p_0)(1-\mu)$, a noise trader arrives and declines with probability $q$, independent of $V$. Summing: $$\boxed{P(\text{no trade} \mid V = v) = p_0 + (1-p_0)\,\mu\,\mathbf{1}[|v| \le s] + (1-p_0)(1-\mu)\,q}$$ Note the $(1-p_0)$ factors: the $\mu$ vs. $1-\mu$ split is *conditional on arrival*. Dropping them (i.e., writing $p_0 + \mu\mathbf{1}[|v|\le s] + (1-\mu)q$) is not a probability -- with $p_0 = 0.5$, $\mu = 0.8$, $q = 0.5$ it gives $1.4$. Write $L_{\text{in}} = p_0 + (1-p_0)[\mu + (1-\mu)q]$ for $|v| \le s$ and $L_{\text{out}} = p_0 + (1-p_0)(1-\mu)q$ for $|v| > s$; always $L_{\text{in}} > L_{\text{out}}$. **(b) Posterior mean.** By Bayes' theorem, with $f$ the $N(0, \sigma^2)$ density, $$f(v \mid \text{no trade}) \propto \Big[L_{\text{out}} + (1-p_0)\,\mu\,\mathbf{1}[|v| \le s]\Big] f(v).$$ Both $f(v)$ and $\mathbf{1}[|v| \le s]$ are even in $v$, so the posterior density is even, and therefore $$\boxed{E[V \mid \text{no trade}] = 0}$$ No trade does **not** shift your mean estimate: silence is directionless evidence. It tells you $V$ is more likely to be *small*, not more likely to be positive or negative. The normalizing constant is $$P(\text{no trade}) = L_{\text{out}} + (1-p_0)\,\mu\left[2\Phi\!\left(\tfrac{s}{\sigma}\right) - 1\right].$$ (Check with the Quick Estimate numbers: $0.226 + 0.9 \times 0.3 \times 0.383 = 0.329$, matching simulation.) **(c) Posterior variance.** Since the posterior mean is zero, $\text{Var}(V \mid \text{no trade}) = E[V^2 \mid \text{no trade}]$: $$\text{Var}(V \mid \text{no trade}) = \frac{L_{\text{out}}\,\sigma^2 + (1-p_0)\,\mu\,E\!\left[V^2\,\mathbf{1}[|V| \le s]\right]}{P(\text{no trade})},$$ where $E[V^2\mathbf{1}[|V| \le s]] = \sigma^2\left[2\Phi(\tfrac{s}{\sigma}) - 1\right] - 2s\sigma\,\phi(\tfrac{s}{\sigma})$. The no-trade posterior reweights mass toward the inside region $|v| \le s$ (weight boosted by the informed trader's silence), and values there have smaller $V^2$ than the prior average. Hence $$\boxed{\text{Var}(V \mid \text{no trade}) s$, so adverse-selection risk has fallen and you should **tighten your spread** on the next quote. **Answer:** $P(\text{no trade} \mid V = v) = p_0 + (1-p_0)\mu\,\mathbf{1}[|v| \le s] + (1-p_0)(1-\mu)q$. The posterior mean is $E[V \mid \text{no trade}] = 0$ by symmetry -- silence carries no directional information. The posterior variance satisfies $\text{Var}(V \mid \text{no trade}) < \sigma^2$: silence concentrates belief near zero, so you should tighten the spread. --- Bucket Sort for Uniform Samples [medium, Coding] --- https://quantvault.org/problem-1534-bucket-sort-for-uniform-samples.html Q: You have $n$ i.i.d. samples drawn from $\text{Unif}[0, 1)$. Design a bucket sort algorithm to sort them. 1. Write pseudocode for the algorithm. 2. Prove that the expected running time is $O(n)$ and that it uses $O(n)$ extra space. 3. What happens when the input distribution is not uniform -- for example, heavily skewed or heavy-tailed? How does performance degrade, and what could you do about it? A: **How to Think About It:** Bucket sort exploits the fact that when inputs are uniformly distributed, you can predict roughly where each element belongs. If you divide $[0, 1)$ into $n$ equal-width buckets, a uniform draw lands in bucket $\lfloor n \cdot x \rfloor$, and on average each bucket gets about one element. Sorting tiny buckets is nearly free, so the total work is dominated by the $O(n)$ cost of distributing elements -- not by comparison sorting. **Algorithm:** Create $n$ empty buckets indexed $0, 1, \ldots, n-1$. For each sample $x_i$, place it in bucket $\lfloor n \cdot x_i \rfloor$. Then sort each bucket individually using insertion sort. Finally, concatenate the sorted buckets in order. Why insertion sort? Each bucket has $O(1)$ elements in expectation, and insertion sort is optimal for tiny lists (no recursion overhead, good cache behavior, $O(1)$ for a single element). **Pseudocode:** ```python def bucket_sort(arr): n = len(arr) if n = 0 and lst[j] > key: lst[j + 1] = lst[j] j -= 1 lst[j + 1] = key ``` **Proof of $O(n)$ Expected Time:** Let $n_j$ be the number of elements in bucket $j$. The total work is: $$T(n) = O(n) + \sum_{j=0}^{n-1} O(n_j^2)$$ The first $O(n)$ term covers bucket creation and element distribution. The $O(n_j^2)$ term is the insertion sort cost for bucket $j$. We need $E\left[\sum_{j=0}^{n-1} n_j^2\right]$. Each element lands in bucket $j$ independently with probability $1/n$, so $n_j \sim \text{Binomial}(n, 1/n)$. For a $\text{Binomial}(n, 1/n)$ random variable: $$E[n_j^2] = \text{Var}(n_j) + (E[n_j])^2 = n \cdot \frac{1}{n} \cdot \left(1 - \frac{1}{n}\right) + 1 = 2 - \frac{1}{n}$$ Summing over all $n$ buckets: $$E\left[\sum_{j=0}^{n-1} n_j^2\right] = n \cdot \left(2 - \frac{1}{n}\right) = 2n - 1 = O(n)$$ So the total expected running time is $O(n) + O(n) = O(n)$. **Space Analysis:** We allocate $n$ bucket lists (each initially empty) and store $n$ elements total across all buckets. The output array also has $n$ elements. Total extra space: $O(n)$. **Edge Cases and Performance Degradation:** - **Non-uniform distribution (e.g., $\text{Beta}(5, 1)$ skewed toward 1):** Most elements cluster in a few buckets near the high end, while low-end buckets stay empty. The heavily loaded buckets have $O(n)$ elements, and sorting them takes $O(n^2)$ -- the algorithm degrades to quadratic. Fix: use quantile-based bucket boundaries instead of equal-width. If you know (or estimate) the CDF $F$, assign element $x$ to bucket $\lfloor n \cdot F(x) \rfloor$. This re-uniformizes the distribution across buckets. - **Heavy-tailed distribution (e.g., Pareto, Cauchy):** The support is unbounded, so fixed-width buckets on $[0,1)$ do not even cover the domain. Even if you extend the range, extreme values create a few buckets with massive counts. Fix: clip or transform the data first (e.g., apply the empirical CDF), or switch to a comparison-based sort like merge sort which guarantees $O(n \log n)$ worst case. - **Many duplicates (point masses):** All duplicates land in the same bucket, creating a large bucket regardless of the overall distribution. Insertion sort still handles this in $O(k^2)$ for $k$ duplicates. Fix: use a three-way partition or counting approach within each bucket. - **Adversarial input:** An adversary who knows the bucket structure can put all elements in one bucket, forcing $O(n^2)$. Bucket sort is not a good choice when you cannot trust the input distribution. **Answer:** Bucket sort achieves $O(n)$ expected time and $O(n)$ space for uniform inputs by assigning element $x$ to bucket $\lfloor n \cdot x \rfloor$ and sorting each bucket with insertion sort. The key insight is that uniform distribution guarantees each bucket has $O(1)$ expected elements. Performance degrades to $O(n^2)$ for skewed or heavy-tailed distributions because bucket loads become unbalanced; the fix is to use CDF-based bucket boundaries or fall back to a comparison sort. --- Computing OLS When the Design Matrix Is Too Large for Memory [medium, Linear Algebra] --- https://quantvault.org/problem-1543-computing-ols-when-the-design-matrix-is-too-large-for-memory.html Q: In Ordinary Least Squares, you need the matrix product $X^T X$ (where $X$ is $n \times p$) to compute the coefficient estimate $\hat{\beta} = (X^T X)^{-1} X^T Y$. But suppose $n$ is so large that you cannot fit the full matrix $X$ in memory. How would you compute $X^T X$ (and $X^T Y$) without storing the entire design matrix? Describe a specific, numerically accurate method -- not an approximate algorithm like SGD. A: **How to Think About It:** The key observation is a dimension mismatch that works in your favor. $X$ is $n \times p$ and might have billions of rows, but $X^T X$ is only $p \times p$. In most regression problems, $p$ is at most a few thousand features, so $X^T X$ fits comfortably in memory even when $X$ does not. The question is whether you can build $X^T X$ without loading all of $X$ at once. The answer is yes, because matrix multiplication can be decomposed into a sum over rows. **Key Insight:** The matrix product $X^T X$ is a sum of rank-1 outer products -- one per row of $X$. You can accumulate this sum one row (or one chunk) at a time. **The Method:** 1. **Initialize** a $p \times p$ accumulator matrix $S = 0$ and a $p \times 1$ vector $t = 0$. 2. **Stream through the data** one row (or chunk of rows) at a time. For each row $x_i \in \mathbb{R}^p$ with response $y_i$: - Update: $S \leftarrow S + x_i x_i^T$ (outer product, $p \times p$) - Update: $t \leftarrow t + x_i y_i$ (scaled vector, $p \times 1$) In chunk form, if you load a block $X_k$ of $m$ rows at a time: - $S \leftarrow S + X_k^T X_k$ - $t \leftarrow t + X_k^T Y_k$ 3. **After one pass** through all $n$ rows: $S = X^T X$ and $t = X^T Y$. 4. **Solve:** $\hat{\beta} = S^{-1} t$ (or better, use Cholesky decomposition of $S$). **Why this works:** Matrix multiplication is associative and can be decomposed: $$X^T X = \sum_{i=1}^{n} x_i x_i^T$$ This is just the definition of matrix multiplication written as a sum over rows. Each outer product $x_i x_i^T$ is a $p \times p$ matrix, and you accumulate them into $S$. Memory usage is $O(p^2)$ for the accumulator plus $O(mp)$ for the current chunk -- independent of $n$. **Practical Considerations:** - **Numerical stability:** Summing many outer products can accumulate floating-point error. For better accuracy, use compensated summation (Kahan summation) or process data in double precision. If features have very different scales, center and scale them first. - **Chunk size:** Larger chunks are faster (BLAS-optimized matrix multiply on blocks), but use more memory. A chunk of $m = 10{,}000$ rows with $p = 1{,}000$ features uses about 80 MB -- a good practical trade-off. - **Parallelization:** Each chunk's contribution $X_k^T X_k$ is independent, so you can compute them in parallel (e.g., MapReduce) and sum the results. This is the basis of distributed OLS in systems like Spark. - **Why not SGD?** SGD gives an approximate solution that depends on step sizes and number of passes. The chunked outer product method gives the exact OLS solution (up to floating-point precision) in a single pass. When you need the exact answer -- e.g., for inference, confidence intervals, or regulatory reporting -- this is the right approach. **Answer:** Compute $X^T X = \sum_i x_i x_i^T$ by streaming through the data one row (or chunk) at a time, accumulating the $p \times p$ outer product sum. This requires $O(p^2)$ memory regardless of $n$, gives the exact OLS solution, and parallelizes trivially. --- Correlation Range with Four Variables [hard, Linear Algebra] --- https://quantvault.org/problem-1548-correlation-range-with-four-variables.html Q: You have four random variables $a$, $b$, $c$, and $d$. Every pairwise correlation equals $\frac{1}{2}$, except for $\text{Corr}(a, d)$, which is unknown. Call it $\rho$. What is the range of valid values for $\rho$? In other words, for which values of $\rho$ does a valid joint distribution exist? A: **How to Think About It:** The constraint is that the $4 \times 4$ correlation matrix must be positive semi-definite (PSD). That is the necessary and sufficient condition for a set of correlations to be jointly realizable. The matrix has ones on the diagonal, $\rho$ in the $(1,4)$ and $(4,1)$ entries, and $1/2$ everywhere else off-diagonal. We need to find the range of $\rho$ that keeps all eigenvalues non-negative, which is equivalent to requiring all leading principal minors (or equivalently the determinant and all sub-determinants) to be non-negative. **Quick Estimate:** With three variables all at correlation $1/2$, the "average direction" picture is: each variable makes the same angle with the others. Adding a fourth variable $a$ that also correlates at $1/2$ with $b$ and $c$, the correlation with $d$ is constrained by geometry. The unconstrained range is $[-1, 1]$, but the existing network of $1/2$ correlations forces the lower bound upward. By symmetry the upper bound should still be $1$ (all four variables identical up to scaling). The lower bound will be somewhere around $-1/4$ to $-1/2$ -- let us compute. **Approach:** Write out the full $4 \times 4$ correlation matrix and enforce $\det(R) \geq 0$ along with all principal minor conditions. **Formal Solution:** The correlation matrix is: $$R = \begin{pmatrix} 1 & 1/2 & 1/2 & \rho \\ 1/2 & 1 & 1/2 & 1/2 \\ 1/2 & 1/2 & 1 & 1/2 \\ \rho & 1/2 & 1/2 & 1 \end{pmatrix}$$ **Step 1 -- Check sub-matrices.** All $1 \times 1$ minors are $1 > 0$. All $2 \times 2$ minors involving only the known correlations give $1 - 1/4 = 3/4 > 0$. The $2 \times 2$ minor for $(a, d)$ gives $1 - \rho^2 \geq 0$, so $\rho \in [-1, 1]$. The $3 \times 3$ sub-matrix for $(b, c, d)$ has all off-diagonal entries $1/2$, with determinant: $$\det = 1 - 3 \cdot \frac{1}{4} + 2 \cdot \frac{1}{8} = \frac{1}{2} > 0$$ The $3 \times 3$ sub-matrix for $(a, b, d)$ is: $$M = \begin{pmatrix} 1 & 1/2 & \rho \\ 1/2 & 1 & 1/2 \\ \rho & 1/2 & 1 \end{pmatrix}$$ $$\det(M) = \frac{1}{2} + \frac{\rho}{2} - \rho^2$$ Setting $\det(M) \geq 0$: the roots of $\rho^2 - \rho/2 - 1/2 = 0$ are $\rho = 1$ and $\rho = -1/2$, giving $\rho \in [-1/2, 1]$. By the same calculation, the $(a, c, d)$ minor gives the same constraint. **Step 2 -- The $4 \times 4$ determinant.** Expanding by cofactors along the first row: $$\det(R) = \frac{1}{4} + \frac{\rho}{2} - \frac{3\rho^2}{4}$$ Setting $\det(R) \geq 0$ and multiplying through by $-4$: $$3\rho^2 - 2\rho - 1 \leq 0$$ Factoring: $$(3\rho + 1)(\rho - 1) \leq 0$$ This gives $\rho \in [-1/3, 1]$. **Step 3 -- Combine all constraints.** The binding constraint comes from the $4 \times 4$ determinant: $\rho \in [-1/3, 1]$. The $3 \times 3$ constraint $\rho \geq -1/2$ is weaker and automatically satisfied. **Verification:** At $\rho = -1/3$, we get $\det(R) = 1/4 - 1/6 - 1/12 = 0$, confirming the matrix is PSD (singular) at the boundary. At $\rho = 1$, all four variables are perfectly correlated, which is trivially valid. **Answer:** The correlation between $a$ and $d$ must satisfy $\rho \in \left[-\dfrac{1}{3},\, 1\right]$. --- Correlation of Sum and Difference [easy, Random Variables] --- https://quantvault.org/problem-1549-correlation-of-sum-and-difference.html Q: Let $X$ and $Y$ be i.i.d. random variables, each with variance $\sigma^2$. Define $S = X + Y$ and $D = X - Y$. What is the correlation $\rho(S, D)$? As a follow-up: if $X$ and $Y$ are i.i.d. normal, can you say something stronger than just their correlation? A: **How to Think About It:** Before computing anything, think geometrically. Going from $(X, Y)$ to $(S, D) = (X+Y, X-Y)$ is a 45-degree rotation (scaled by $\sqrt{2}$). If $X$ and $Y$ are i.i.d., their joint distribution is symmetric around the $X = Y$ line. A rotation that aligns with that symmetry axis should decouple the two new coordinates -- so you'd expect zero correlation. That is the punchline, and the algebra just confirms the geometric intuition. **Quick Estimate:** Since $X$ and $Y$ are i.i.d., the covariance matrix of $(X, Y)$ is $\sigma^2 I$. Any linear transformation of a vector with a scalar covariance matrix produces outputs whose cross-covariances depend only on the dot products of the transformation rows. The rows of the transformation $\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}$ are orthogonal, so the covariance between $S$ and $D$ is zero. Done -- correlation is $0$. **Approach:** Use bilinearity of covariance to compute $\text{Cov}(S, D)$ directly. **Formal Solution:** Expand using bilinearity: $$\text{Cov}(S, D) = \text{Cov}(X + Y,\; X - Y)$$ $$= \text{Cov}(X, X) - \text{Cov}(X, Y) + \text{Cov}(Y, X) - \text{Cov}(Y, Y)$$ Since $X$ and $Y$ are i.i.d., we have $\text{Cov}(X, Y) = 0$, $\text{Var}(X) = \text{Var}(Y) = \sigma^2$. So: $$\text{Cov}(S, D) = \sigma^2 - 0 + 0 - \sigma^2 = 0$$ Since $\text{Cov}(S, D) = 0$: $$\rho(S, D) = \frac{\text{Cov}(S,D)}{\sqrt{\text{Var}(S)\,\text{Var}(D)}} = 0$$ Note that $\text{Var}(S) = \text{Var}(D) = 2\sigma^2$, so the denominator is well-defined and positive. **Follow-up -- the normal case:** If $X, Y$ are i.i.d. normal, then $(S, D)$ is jointly normal (any linear transformation of a multivariate normal is again multivariate normal). For jointly normal random variables, uncorrelated implies independent. So $S$ and $D$ are not just uncorrelated -- they are fully **independent**. This is strictly stronger. For non-normal i.i.d. variables, $S$ and $D$ are uncorrelated but may still be dependent. **Answer:** $\rho(S, D) = 0$. The sum and difference of i.i.d. random variables are always uncorrelated. If additionally they are normal, $S$ and $D$ are independent. --- Count Subarrays with Sum in a Range [hard, Coding] --- https://quantvault.org/problem-1551-count-subarrays-with-sum-in-a-range.html Q: Given an integer array $A_1, A_2, \dots, A_n$ (which may contain negative numbers) and integer bounds $L$ and $R$, count the number of contiguous subarrays whose sum falls in the range $[L, R]$. Design an $O(n \log n)$ algorithm. Your solution should handle negative numbers, 64-bit overflow, and use coordinate compression for stability. **Constraints:** - $1 \leq n \leq 10^5$ - $-10^9 \leq A_i \leq 10^9$ - $-10^{18} \leq L \leq R \leq 10^{18}$ **Example 1:** - Input: $A = [1, 2, -1, 3]$, $L = 2$, $R = 4$ - Output: $6$ - Explanation: The subarrays with sums in $[2, 4]$ are: $[1,2]$ (sum 3), $[1,2,-1]$ (sum 2), $[2]$ (sum 2), $[2,-1,3]$ (sum 4), $[-1,3]$ (sum 2), $[3]$ (sum 3). That gives 6 valid subarrays. **Example 2:** - Input: $A = [-1, -1, 1]$, $L = -1$, $R = 0$ - Output: $4$ - Explanation: The subarrays with sums in $[-1, 0]$ are: $[-1]$ (index 0, sum -1), $[-1]$ (index 1, sum -1), $[-1, 1]$ (sum 0), and $[-1, -1, 1]$ (sum -1). That gives 4 valid subarrays. A: **How to Think About It:** The brute force approach checks all $O(n^2)$ subarrays, which is too slow for $n = 10^5$. The key observation is that the sum of subarray $A[i+1..j]$ equals $P_j - P_i$ where $P$ is the prefix sum array. So counting subarrays with sum in $[L, R]$ reduces to counting pairs $(i, j)$ with $i 0: s += tree[i] i -= i & (-i) return s count = 0 for j in range(n + 1): lo = prefix[j] - R hi = prefix[j] - L lr = bisect.bisect_left(sorted_vals, lo) hr = bisect.bisect_right(sorted_vals, hi) if lr < hr: count += query(hr) - query(lr) update(rank[prefix[j]]) return count ``` **Handling edge cases:** - *64-bit overflow:* Prefix sums can reach $n \times 10^9 = 10^{14}$, which fits in a 64-bit integer. In C++/Java, use `long long` / `long`. - *Negative numbers:* The algorithm works unchanged because prefix sums are not monotone; coordinate compression handles arbitrary orderings. - *Coordinate compression stability:* Compressing only the actual prefix sum values (not the query bounds) avoids issues with bounds that fall outside the observed range; the binary search handles this gracefully. **Complexity:** - Time: $O(n \log n)$ -- one Fenwick update and one range query per prefix sum, each $O(\log n)$. - Space: $O(n)$. **Answer:** Use prefix sums to reduce the problem to counting pairs with differences in $[L, R]$, then sweep left-to-right with a Fenwick tree over coordinate-compressed ranks for $O(n \log n)$ total time. --- DAG Reachability Queries with Preprocessing [hard, Coding] --- https://quantvault.org/problem-1555-dag-reachability-queries-with-preprocessing.html Q: You have a directed acyclic graph (DAG) with $n$ nodes (labeled $0 \ldots n-1$) and $m$ edges. You need to answer $Q$ reachability queries of the form: "is there a directed path from node $u$ to node $v$?" A node always reaches itself ($u = v \to$ True). Implement the function: ```python def dag_reachability(n, edges, queries): ... ``` - `n`: number of nodes (labeled `0..n-1`). - `edges`: list of `[u, v]` directed edges (guaranteed acyclic). - `queries`: list of `[u, v]` pairs. Return a **list of booleans**, one per query, in order: `True` if `v` is reachable from `u` following directed edges, else `False`. The naive approach -- running a BFS/DFS per query at $O(n + m)$ each -- costs $O(Q \cdot (n + m))$ total, too slow when $Q$ is large. Design a preprocessing scheme (e.g. compute reachability once over the topological order) so queries are answered fast. **Constraints:** - $1 \leq n \leq 10^4$ - $1 \leq m \leq 10^5$ - $1 \leq Q \leq 10^5$ - The graph is guaranteed to be a DAG (no cycles). **Example** `dag_reachability(4, [[0,1],[1,2],[0,3]], [[0,2],[3,1],[0,3],[2,2]]) -> [True, False, True, True]` Because $0 \to 1 \to 2$ makes 2 reachable from 0 (True); there is no path from 3 to 1 (False); $0 \to 3$ is a direct edge (True); and a node always reaches itself, so $(2,2)$ is True. A: **How to Think About It:** The brute-force baseline is BFS/DFS per query at $O(n + m)$ each, so $Q$ queries cost $O(Q(n+m))$ -- far too slow when $Q$ is large. The insight is that a DAG has a topological order, which lets you propagate reachability information systematically during preprocessing rather than rediscovering it on every query. **Key Insight:** If node $u$ can reach $v$, and $v$ can reach $w$, then $u$ can reach $w$. Process nodes in **reverse topological order** and union each node's reachability set from its successors. Storing each set as a bitmask makes the union a single bitwise OR. **Algorithm (Bitmask Transitive Closure):** 1. Topologically sort the DAG with Kahn's algorithm: $O(n + m)$. 2. Initialize `reach[u]` with just the bit for $u$ (a node reaches itself). 3. For each node $u$ in **reverse** topological order, OR in `reach[v]` for every edge $u \to v$. 4. Answer each query $(u, v)$ in $O(1)$ by testing bit $v$ of `reach[u]`. Preprocessing is $O(nm/64)$ with bitmasks; each query is $O(1)$. **Contract note:** `dag_reachability(n, edges, queries)` must **return a list of booleans** (one per query, in order) -- not a reachability matrix and not a per-call boolean. **Code:** ```python from collections import defaultdict, deque def dag_reachability(n, edges, queries): # n nodes labeled 0..n-1. edges = list of [u, v] directed edges (a DAG). # queries = list of [u, v]; for each, answer whether v is reachable from u # following directed edges. A node reaches itself (u == v -> True). # Return a list of booleans, one per query, in order. adj = defaultdict(list) indegree = [0] * n for u, v in edges: adj[u].append(v) indegree[v] += 1 # Kahn's algorithm for topological sort queue = deque(i for i in range(n) if indegree[i] == 0) topo = [] while queue: node = queue.popleft() topo.append(node) for nb in adj[node]: indegree[nb] -= 1 if indegree[nb] == 0: queue.append(nb) # Bitmask DP in reverse topological order: reach[u] = set of nodes reachable from u reach = [1 << i for i in range(n)] # each node reaches itself for u in reversed(topo): for v in adj[u]: reach[u] |= reach[v] return [bool(reach[u] & (1 << v)) for u, v in queries] ``` **Complexity Summary:** | Approach | Preprocessing | Query | Space | |---|---|---|---| | Naive BFS per query | None | $O(n + m)$ | $O(n + m)$ | | Bitmask transitive closure | $O(nm / 64)$ | $O(1)$ | $O(n^2 / 64)$ | | Interval / DFS-timestamp labeling | $O(n + m)$ | $O(\log n)$ | $O(m)$ | For moderate $n$, bitmask DP on the topological order is the clean default: $O(1)$ queries after $O(nm/64)$ preprocessing. For very large sparse graphs where $O(n^2/64)$ space is prohibitive, interval/DFS-timestamp methods trade slightly slower queries for lower space. --- Detecting Multicollinearity in OLS [hard, Regression] --- https://quantvault.org/problem-1559-detecting-multicollinearity-in-ols.html Q: You are examining an OLS regression with design matrix $X$. Four methods have been proposed to detect multicollinearity among the predictor variables: 1. Inspect the eigenvalues of $X^T X$ for near-zero values. 2. Compute the condition number of $X^T X$ (ratio of largest to smallest eigenvalue) and flag large values. 3. Inspect the correlation matrix of the rows of $X$. 4. Inspect the correlation matrix of the columns of $X$. Which of these methods correctly detect multicollinearity? For each, explain why it works or why it fails. A: **How to Think About It:** Multicollinearity means some columns of $X$ are nearly linearly dependent -- one predictor can be approximately written as a linear combination of others. The OLS estimator $(X^T X)^{-1} X^T y$ breaks down when $X^T X$ is close to singular. So the right question to ask about any detection method is: does it measure something close to singularity of $X^T X$? Methods that look at pairwise correlations are too crude -- multicollinearity can be a multi-way relationship invisible in pairwise statistics. **Key Insight:** Multicollinearity is a property of the column space of $X$. Any reliable detection method needs to examine the joint linear structure of the columns, not just pairwise relationships. **The Method:** **Option 1 -- Eigenvalues of $X^T X$: Correct.** The matrix $X^T X$ is positive semi-definite. Its eigenvalues are all non-negative, and a zero eigenvalue means $X$ has a linearly dependent column (exact multicollinearity). Near-zero eigenvalues indicate near-multicollinearity. Since $\det(X^T X) = \prod_i \lambda_i$, a tiny determinant signals near-singularity. Inspecting small eigenvalues directly captures whether any linear combination of columns is nearly zero -- exactly what multicollinearity means. **Option 2 -- Condition number of $X^T X$: Correct.** The condition number $\kappa = \lambda_{\max} / \lambda_{\min}$ measures how sensitive the matrix inverse is to perturbations. A large $\kappa$ means $\lambda_{\min}$ is near zero relative to the matrix scale -- the same signal as option 1, just normalized. In practice, $\kappa > 30$ is often flagged as moderate multicollinearity, and $\kappa > 100$ as severe. **Option 3 -- Correlation matrix of the rows of $X$: Incorrect.** Rows of $X$ correspond to observations, not predictors. Correlations between observations tell you about your data sampling structure (are observations clustered?) but say nothing about whether any predictor can be written as a combination of others. This confuses two entirely different things. **Option 4 -- Correlation matrix of the columns of $X$: Partially useful, but insufficient.** Columns of $X$ are the predictors, so their pairwise correlations are at least the right object. A high pairwise correlation between two columns is indeed one form of multicollinearity. However, pairwise correlation matrices miss multi-way multicollinearity: three predictors $X_1, X_2, X_3$ can have moderate pairwise correlations but still satisfy $X_3 \approx X_1 + X_2$, making the system nearly singular. The correlation matrix would not flag this. So option 4 is only a weak screen, not a reliable diagnostic. **Answer:** Options 1 and 2 are correct. The eigenvalues (or condition number) of $X^T X$ are the right tools because they directly measure near-singularity of the Gram matrix, which is what multicollinearity causes. Options 3 and 4 are either wrong (rows) or insufficient (columns -- misses multi-way dependencies). --- Effects of Multicollinearity on Regression [medium, Regression] --- https://quantvault.org/problem-1569-effects-of-multicollinearity-on-regression.html Q: In standard linear regression, multicollinearity (high correlation among predictors) is present. Which of the following quantities are typically affected? 1. Variance of the fitted regression coefficients 2. $t$-statistics for individual coefficients 3. Sum of squared residuals (SSR) 4. $R^2$ For each, explain whether it is affected and why. A: **How to Think About It:** Multicollinearity means the columns of your design matrix $X$ are nearly linearly dependent, so $X^TX$ is close to singular. The key formula is the coefficient covariance: $\text{Var}(\hat{\beta}) = \sigma^2 (X^TX)^{-1}$. When a matrix is near-singular, its inverse has very large entries. That is where the damage shows up. But does multicollinearity affect how well the model fits? Think of it this way: if you duplicated a column of $X$, you'd have perfect multicollinearity, but the model can still fit the data equally well -- it just can't decide how to split the credit between the two copies. **Key Insight:** Multicollinearity inflates the variance of individual coefficient estimates but does not degrade the model's overall predictive power (fitted values and $R^2$ are unaffected). **The Method:** **1. Variance of fitted coefficients -- YES, affected.** $\text{Var}(\hat{\beta}) = \sigma^2(X^TX)^{-1}$. When $X^TX$ is nearly singular, $\det(X^TX) \approx 0$, so $(X^TX)^{-1}$ has very large entries. This means the estimated coefficients can swing wildly with small changes in the data. The variance inflation factor (VIF) quantifies this: VIF$_j = 1/(1 - R_j^2)$, where $R_j^2$ is the $R^2$ from regressing predictor $j$ on all other predictors. **2. $t$-statistics -- YES, affected.** The $t$-statistic for $\hat{\beta}_j$ is $t_j = \hat{\beta}_j / \text{SE}(\hat{\beta}_j)$. Since $\text{SE}(\hat{\beta}_j) = \sigma \sqrt{[(X^TX)^{-1}]_{jj}}$ is inflated by multicollinearity, the $t$-statistics become small even when the true coefficient is nonzero. This makes it hard to reject $H_0: \beta_j = 0$ for individual predictors, even though they may be jointly significant. **3. Sum of squared residuals -- NO, not affected.** SSR $= \|Y - X\hat{\beta}\|^2$. The fitted values $\hat{Y} = X(X^TX)^{-1}X^TY = H Y$ depend on the projection (hat) matrix $H$, which projects onto the column space of $X$. Multicollinearity does not change the column space of $X$ -- it only affects how the projection is decomposed across individual predictors. Think of it concretely: if you duplicate a column, the column space is unchanged, so the fitted values (and hence the residuals) are identical. **4. $R^2$ -- NO, not affected.** $R^2 = 1 - \text{SSR}/\text{SST}$, where SST $= \|Y - \bar{Y}\|^2$. Since SSR is unaffected by multicollinearity (as shown above) and SST depends only on $Y$, $R^2$ is also unaffected. **Practical Considerations:** - Multicollinearity is a problem for **interpretation** and **inference** (which variables matter?), not for **prediction** (how well does the model fit?). - Common fixes: remove redundant variables, use PCA/ridge regression, or just accept that individual coefficients are not identifiable and focus on predictions. - A classic trap in interviews: people think multicollinearity makes the model "worse." It doesn't -- it makes the coefficient estimates unstable, but the model's in-sample fit is fine. **Answer:** Quantities (1) and (2) are affected. Quantities (3) and (4) are not. Multicollinearity inflates coefficient variances and shrinks $t$-statistics, but leaves fitted values, residuals, and $R^2$ unchanged. --- Fisher Information for Exponential Samples [hard, Statistics] --- https://quantvault.org/problem-1580-fisher-information-for-exponential-samples.html Q: Suppose $X_1, \dots, X_n \mid \lambda$ are IID $\text{Exponential}(\lambda)$ random variables. Find the Fisher Information $I(\lambda)$ for the rate parameter $\lambda$. What is the value of the Fisher Information when $\lambda = 3$ and $n = 16$? A: **How to Think About It:** Fisher Information measures how much information a sample carries about a parameter -- specifically, how sharply the log-likelihood peaks around the true value. For an exponential distribution with rate $\lambda$, the PDF is simple ($\lambda e^{-\lambda x}$), so the log-likelihood is clean and the second derivative is easy to compute. Since the observations are IID, total Fisher Information is just $n$ times the single-observation information. **Quick Estimate:** For a single exponential observation, the MLE of $\lambda$ is $\hat{\lambda} = 1/X$, and by the Cramer-Rao bound, the variance of any unbiased estimator is at least $1/I(\lambda)$. Since $\text{Var}(\hat{\lambda})$ for the MLE scales like $\lambda^2/n$ (the exponential has coefficient of variation 1), we expect $I(\lambda) = n/\lambda^2$. For $\lambda = 3, n = 16$: $I = 16/9 \approx 1.78$. **Approach:** Compute the single-observation Fisher Information using the second derivative of the log-likelihood, then multiply by $n$. **Formal Solution:** The PDF of a single observation is: $$f(x \mid \lambda) = \lambda e^{-\lambda x}, \quad x > 0$$ The support does not depend on $\lambda$, so we can use the standard formula: $$I_1(\lambda) = -E\left[\frac{\partial^2}{\partial \lambda^2} \log f(X \mid \lambda)\right]$$ Step 1 -- Log-likelihood for one observation: $$\log f(x \mid \lambda) = \log \lambda - \lambda x$$ Step 2 -- First derivative (score function): $$\frac{\partial}{\partial \lambda} \log f(x \mid \lambda) = \frac{1}{\lambda} - x$$ Step 3 -- Second derivative: $$\frac{\partial^2}{\partial \lambda^2} \log f(x \mid \lambda) = -\frac{1}{\lambda^2}$$ This is a constant (does not depend on $x$), so the expectation is trivial: $$I_1(\lambda) = -\left(-\frac{1}{\lambda^2}\right) = \frac{1}{\lambda^2}$$ Step 4 -- For $n$ IID observations, Fisher Information is additive: $$I(\lambda) = n \cdot I_1(\lambda) = \frac{n}{\lambda^2}$$ Step 5 -- Plug in $\lambda = 3$, $n = 16$: $$I(3) = \frac{16}{9}$$ **Answer:** $I(\lambda) = \dfrac{n}{\lambda^2}$. At $\lambda = 3$ and $n = 16$, $I(\lambda) = \dfrac{16}{9}$. --- K Disjoint Maximum-Sum Subarrays [hard, Coding] --- https://quantvault.org/problem-1591-k-disjoint-maximum-sum-subarrays.html Q: Given an array $a_1, a_2, \ldots, a_n$ of integers (possibly negative) and an integer $K \geq 1$, consider all ways to choose $K$ non-overlapping non-empty contiguous subarrays. **Return only the maximum achievable total sum, as a single integer** -- you do not need to report which subarrays achieve it, and no list or set of intervals is expected. Implement the function with signature: ```python def max_k_subarray_sum(a, K): ... ``` where `a` is the list of integers and `K` is the number of subarrays to select. **Constraints:** - $1 \leq K \leq n$ - $-10^9 \leq a_i \leq 10^9$ - Subarrays must be non-empty and non-overlapping (disjoint) **Example 1:** Input: `a = [1, -2, 3, 5, -1, 4, -3, 2]`, `K = 2` Output: `13` Explanation: Take `[3, 5, -1, 4]` (sum 11) and `[2]` (sum 2), for a total of 13. **Example 2:** Input: `a = [-1, -2, -3]`, `K = 1` Output: `-1` Explanation: Must select at least one subarray; the maximum single element is -1. Design an $O(nK)$-time algorithm using rolling arrays, which needs only $O(K)$ extra space. Handle edge cases ($K$ larger than the number of positive segments, all-negative arrays). **Example** `max_k_subarray_sum([10, -1, 10], 2) -> 20` The two disjoint subarrays `[10]` and `[10]` (skipping the `-1` gap) sum to 20; merging them into one subarray would incur the `-1` and only give 19. A: **How to Think About It:** This extends Kadane's algorithm (maximum subarray, $K = 1$) to $K$ disjoint subarrays. At each position $i$ you are in one of two states: either the $j$-th subarray currently *ends* at $i$, or the $j$ completed subarrays all end at or before $i$. This gives a 2-state DP. **Algorithm:** Define two rolling arrays over positions: - `cur[j]` = max sum using exactly $j$ subarrays where the $j$-th subarray **ends exactly at the current position** - `best[j]` = max sum using exactly $j$ subarrays where all $j$ subarrays **end at or before the current position** **Transitions** (processing each element $x = a_i$, iterating $j$ from $K$ down to $1$ so that `best[j-1]` still holds the previous position's value): $$\texttt{cur}[j] = \max(\texttt{cur}[j],\; \texttt{best}[j-1]) + x$$ Either extend the current $j$-th subarray, or start a fresh $j$-th subarray here on top of $j-1$ finished ones. $$\texttt{best}[j] = \max(\texttt{best}[j],\; \texttt{cur}[j])$$ Close (or keep) the $j$-th subarray at the current position. **Base cases:** `best[0] = 0`, all other entries $-\infty$. **Final answer:** `best[K]` after processing all $n$ elements -- a single integer. **Code:** ```python def max_k_subarray_sum(a, K): # Max total sum of K non-overlapping non-empty contiguous subarrays. n = len(a) INF = float('inf') # cur[j] = best sum with j subarrays, j-th subarray ends exactly at current position # best[j] = best sum with j subarrays, all ending at or before current position cur = [-INF] * (K + 1) best = [-INF] * (K + 1) best[0] = 0 for x in a: # iterate j high->low so best[j-1] still refers to previous position for j in range(K, 0, -1): cur[j] = max(cur[j], best[j - 1]) + x best[j] = max(best[j], cur[j]) return best[K] ``` **Complexity:** Time $O(nK)$ (outer loop over $n$, inner over $K$); space $O(K)$ with rolling arrays. **Edge cases:** - **All-negative array:** works correctly — it selects the $K$ least-negative singletons, so the answer can be negative (e.g. `[-1,-2,-3], K=1 -> -1`). - **$K = n$:** every element becomes its own subarray, answer = sum of all elements. - **$K = 1$:** reduces to Kadane's algorithm. --- Longest Increasing Subsequence with Reconstruction [hard, Coding] --- https://quantvault.org/problem-1608-longest-increasing-subsequence-with-reconstruction.html Q: Given an array of $n$ integers, return the **length** of a **strictly** longest increasing subsequence (LIS). Implement the function: ```python def lis_length(nums): ... ``` - `nums` is a list of integers. - Return an **int**: the length of the strictly increasing subsequence of maximum length. Your solution must run in $O(n \log n)$ time and $O(n)$ space. **Constraints:** - $0 \le n \le 10^5$ - Array elements can be any 32-bit integer (including negatives and duplicates) - "Strictly increasing" means no two equal elements are allowed in the subsequence - The empty array has LIS length `0` **Example** ``` lis_length([10, 9, 2, 5, 3, 7, 101, 18]) -> 4 ``` A longest strictly increasing subsequence is `[2, 3, 7, 18]` (or `[2, 3, 7, 101]`), which has 4 elements, so the returned length is `4`. A: **How to Think About It:** The brute-force approach checks all $2^n$ subsequences -- hopeless. The classic DP gives $O(n^2)$, fine for small $n$ but too slow at $n = 10^5$. The key insight is patience sorting: maintain an array `tails` where `tails[i]` holds the smallest possible tail value of any strictly increasing subsequence of length $i+1$ found so far. This array is always sorted, so you can binary search it for each new element, giving $O(n \log n)$. This problem only asks for the **length**, so no predecessor tracking / reconstruction is needed -- the answer is simply `len(tails)` after processing every element. **Algorithm:** 1. Maintain a sorted array `tails` of the smallest tail values for each achievable subsequence length. 2. For each `x` in `nums`, binary search `tails` with `bisect_left` for the leftmost position `pos` where `tails[pos] >= x`. - If `pos == len(tails)`, `x` is larger than every tail, so it extends the longest subsequence: append `x`. - Otherwise, replace `tails[pos]` with `x` (a smaller tail for a subsequence of that length). 3. The final `len(tails)` is the LIS length. Using `bisect_left` (not `bisect_right`) enforces **strict** increase: an element equal to an existing tail overwrites it rather than extending, so duplicates never lengthen the subsequence. Why does `tails` stay sorted? The smallest tail of a length-$k$ subsequence must be strictly less than the smallest tail of a length-$(k+1)$ subsequence, otherwise you could extend the shorter one. Binary search exploits this invariant. **Code:** ```python from bisect import bisect_left def lis_length(nums): # Return the LENGTH of a strictly longest increasing subsequence of nums. # Patience sorting: tails[i] = smallest possible tail value of an # increasing subsequence of length i+1. tails stays sorted, so each new # element is placed via binary search in O(log n). tails = [] for x in nums: pos = bisect_left(tails, x) # leftmost tail >= x (strictly increasing) if pos == len(tails): tails.append(x) else: tails[pos] = x return len(tails) ``` **Edge Cases:** - **Empty array**: the loop never runs, `tails` stays empty, returns `0`. - **Single element**: `tails` becomes `[x]`, returns `1`. - **All equal elements**: `bisect_left` returns position `0` every time, so `tails` never grows beyond length 1. Returns `1` -- correct, since strict increase forbids equal elements. - **Already sorted**: each element extends `tails`, so the length equals `n`. - **Reverse sorted**: each element overwrites `tails[0]`, so the length is `1`. **Complexity:** - **Time**: $O(n \log n)$ -- $n$ elements, each doing one $O(\log n)$ binary search. - **Space**: $O(n)$ -- `tails` has at most $n$ entries. --- Maximum Non-Overlapping Interval Scheduling [medium, Coding] --- https://quantvault.org/problem-1618-maximum-non-overlapping-interval-scheduling.html Q: You have a list of $n$ tasks, each defined by a start time $s_i$ and an end time $e_i$. Two tasks overlap if their intervals intersect (one starts strictly before the other ends); back-to-back tasks $[a,b]$ and $[b,c]$ do NOT overlap. Implement the function: ``` schedule_counts(tasks, m) ``` - `tasks` is a list of `[start, end]` pairs (with `start [3, 3]` On a single machine the earliest-end greedy picks $(1,3)$, $(4,7)$, $(8,10)$ for 3 tasks; with $m = 1$ the multi-machine count is the same, so both entries are 3. A: **How to Think About It:** This is the classic interval scheduling problem. For part (a) the key greedy insight is: always pick the task that finishes earliest, because finishing early leaves the most room for future tasks. For part (b) the crucial observation is that a set of intervals is schedulable on $m$ machines **iff** at no point in time are more than $m$ of them active (for interval graphs, min machines needed = maximum overlap depth). So "maximize total tasks on $m$ machines" means "select the largest subset whose overlap depth never exceeds $m$." In particular, when $m = 1$ this reduces exactly to part (a). **Algorithm (Part a) -- Earliest Deadline First:** Sort tasks by end time; keep `last_end = -inf`; for each task, if its start $\ge$ `last_end`, select it and update `last_end`. This is optimal by the standard exchange argument (the greedy solution's $i$-th task finishes no later than any optimal solution's $i$-th task, so it never runs out of room first). **Algorithm (Part b) -- Multi-Machine, count-optimal:** Process tasks in **end-time** order. Maintain each machine's current free-time. For each task, assign it to a machine that is free by the task's start; among the free machines pick the one that freed up **latest** (best fit), keeping earlier-freeing machines available for later tasks. If no machine is free in time, skip the task. Processing in end order means each newly assigned end is the largest so far, which keeps the greedy exchange-argument optimality and correctly reduces to part (a) at $m = 1$. > Note: a common but WRONG approach is to sort by start time and assign to the earliest-free machine. That schedules a valid (feasible) assignment but does **not** maximize the number of tasks — it fails cases like $m=1$ where it can pick fewer than the single-machine optimum. **Code:** ```python import bisect def schedule_counts(tasks, m): # Part (a): single machine -- maximum number of non-overlapping tasks. # Classic earliest-deadline-first greedy (sort by end time). single = 0 last_end = float('-inf') for s, e in sorted(tasks, key=lambda x: x[1]): if s >= last_end: single += 1 last_end = e # Part (b): m identical machines -- maximize the TOTAL number of tasks # scheduled. A set of tasks is schedulable on m machines iff no point in # time is covered by more than m of them. Greedily process tasks by end # time and assign each to a machine that is free by the task's start; among # free machines pick the one that freed up latest (best fit), so machines # that freed earlier stay available for later tasks. If no machine is free # in time, the task is skipped. frees = [float('-inf')] * m # kept sorted ascending multi = 0 for s, e in sorted(tasks, key=lambda x: x[1]): # rightmost machine whose free time is = 0: frees.pop(i) bisect.insort(frees, e) multi += 1 return [single, multi] ``` **Complexity:** Both parts are $O(n \log n)$: sorting dominates, and each of the $n$ tasks in part (b) does an $O(\log m)$ search/insert into the machine free-time structure. --- Maximum Points in a Rotating Angle [medium, Coding] --- https://quantvault.org/problem-1620-maximum-points-in-a-rotating-angle.html Q: You are given a set of $n$ points in the 2D plane and a fixed angle $\alpha$ (in radians). An "angular wedge" of width $\alpha$ is centered at the origin and can be rotated freely. Find the rotation that maximizes the number of points contained within the wedge. **Constraints:** - $1 \le n \le 10^5$ - $0 < \alpha < 2\pi$ - Points may lie anywhere in the plane (excluding the origin) - Points on the boundary of the wedge count as contained **Examples:** Example 1: `points = [(1,0), (0,1), (-1,0), (0,-1)]`, `alpha = pi/2` Output: `2` -- a quarter-circle wedge can contain at most 2 of the 4 axis-aligned points. Example 2: `points = [(1,1), (1,2), (2,1)]`, `alpha = pi/2` Output: `3` -- rotate the wedge to face roughly 45 degrees and all 3 points fit inside. A: **How to Think About It:** The wedge is a pie slice of fixed angular width $\alpha$; only each point's *angle* from the origin matters, never its distance. Rotating the wedge = sliding a window of angular width $\alpha$ around the circle and counting how many angles fall inside. The key extremal fact (the "push-to-the-boundary" heuristic): the optimal window can always be slid until its trailing edge sits exactly on some point. So you never need to test infinitely many rotations — only the $n$ rotations that start each window at an existing point's angle. That turns a continuous optimization into a **sorted-array sliding window**. The one trap is the wrap-around at $\pm\pi$: a window can straddle the seam, which a naive linear scan misses. **Quick Estimate:** Check the logic on Example 2, `[(1,1),(1,2),(2,1)]`, $\alpha = \pi/2 = 90^\circ$, by eye. Angles: $(1,1)$ is $45^\circ$; $(2,1) = \arctan(1/2) \approx 27^\circ$; $(1,2) = \arctan 2 \approx 63^\circ$. All three sit inside $[27^\circ, 63^\circ]$, a spread of only $36^\circ < 90^\circ$, so a single $90^\circ$ wedge aimed near $45^\circ$ swallows all three — answer $3$, matching. For Example 1 the four axis points are $90^\circ$ apart; a *closed* $90^\circ$ wedge catches two adjacent ones (e.g. $0^\circ$ and $90^\circ$ on its boundaries) — answer $2$. Both fall straight out of "how many angles fit in a $\alpha$-wide arc." **Approach:** Polar-sort angles, duplicate with $+2\pi$, two-pointer window of width $\alpha$. **Formal Solution:** 1. Map each point to $\theta_i = \text{atan2}(y,x) \in (-\pi,\pi]$. 2. Sort the $n$ angles ascending. 3. **Un-wrap the circle:** append $\theta_i + 2\pi$ for every angle, yielding a sorted length-$2n$ array. A window that crosses the $\pm\pi$ seam now appears as a contiguous run in this doubled array, so no modular arithmetic is needed in the loop. 4. **Two-pointer sweep:** for each left index $i \in [0,n)$, advance a right pointer $j$ while $\theta_j - \theta_i \le \alpha$. The count $j - i$ is how many points a wedge whose trailing edge is at $\theta_i$ contains. Track the max. ```python import math def max_points_in_angle(points, alpha): angles = sorted(math.atan2(y, x) for x, y in points) n = len(angles) angles = angles + [a + 2 * math.pi for a in angles] # un-wrap max_count = 0 j = 0 for i in range(n): if j < i: j = i while j < 2 * n and angles[j] - angles[i] <= alpha: j += 1 max_count = max(max_count, j - i) return max_count ``` Each pointer only moves forward across $2n$ entries, so the sweep is $O(n)$; the sort dominates at $O(n\log n)$. **Answer:** $O(n\log n)$ via angular sort + $+2\pi$ un-wrapping + width-$\alpha$ sliding window; the max window size is the answer. Examples give $2$ and $3$. --- OLS with Correlated Errors [medium, Regression] --- https://quantvault.org/problem-1638-ols-with-correlated-errors.html Q: Consider the standard linear regression model $y = X\beta + \varepsilon$, where OLS assumes $\text{Cov}(\varepsilon) = \sigma^2 I$. Now suppose the errors are not i.i.d. but instead exhibit correlation. 1. If the errors are **positively correlated**, what happens to the OLS coefficient estimates $\hat{\beta}$? Are they still unbiased? What about the estimated standard errors and the resulting t-statistics? 2. If the errors are **negatively correlated**, how does the story change? 3. What would you do in practice to fix these issues? A: **How to Think About It:** The key insight is separating two things: the OLS point estimates themselves versus the uncertainty estimates around them. OLS computes $\hat{\beta} = (X^T X)^{-1} X^T y$. That formula does not depend on the error covariance structure at all -- as long as $E[\varepsilon | X] = 0$, the estimator is unbiased regardless of correlation. The problem is with the standard errors. OLS computes standard errors assuming $\text{Cov}(\varepsilon) = \sigma^2 I$, and when that is wrong, the standard errors are wrong, which means your t-stats and p-values are garbage. The direction in which they are wrong depends on how the error covariance lines up with the regressors, not on the sign of the correlation alone. **Key Insight:** OLS coefficients stay unbiased under correlated errors; it is the inference (standard errors, hypothesis tests, confidence intervals) that breaks. **The Method:** **Case 1: Positively correlated errors** - $\hat{\beta}$ remains **unbiased**. Unbiasedness only requires $E[\varepsilon | X] = 0$, which has nothing to do with the covariance structure. - $\hat{\beta}$ is **inefficient** -- it is no longer the minimum-variance linear unbiased estimator (the Gauss-Markov theorem fails when $\text{Cov}(\varepsilon) \neq \sigma^2 I$). - The true variance of $\hat{\beta}$ is $(X^T X)^{-1} X^T \Omega X (X^T X)^{-1}$, where $\Omega = \text{Cov}(\varepsilon)$. When errors are positively correlated, the off-diagonal terms of $\Omega$ are positive, and in the canonical time-series case (a persistent regressor) the true variance of $\hat{\beta}$ comes out **larger** than what OLS reports. That direction is not settled by the sign of the correlation alone -- it depends on how $\Omega$ lines up with $X$. A contrast regressor $X = (1, -1)^T$ reverses it: with two positively correlated errors the true variance is $\sigma^2(1 - \rho)/2$, below the OLS value $\sigma^2/2$. - Consequence in that typical case: OLS **underestimates** standard errors. Your t-statistics are inflated and p-values are too small, leading to spurious significance. You think you have found a signal when you have not. **Case 2: Negatively correlated errors** - $\hat{\beta}$ is still **unbiased** for the same reason. - Still **inefficient**. - With negative correlation, the off-diagonal terms of $\Omega$ are negative, and with a persistent regressor the true variance of $\hat{\beta}$ comes out **smaller** than what OLS reports. This too is the typical case rather than a theorem -- the same dependence on how $\Omega$ lines up with $X$ applies. - Consequence in that typical case: OLS **overestimates** standard errors. Your t-statistics are deflated and p-values are too large. You may fail to detect real effects -- a loss of power. **Practical Considerations:** 1. **GLS (Generalized Least Squares):** If you know $\Omega$, use $\hat{\beta}_{GLS} = (X^T \Omega^{-1} X)^{-1} X^T \Omega^{-1} y$. This is BLUE (best linear unbiased estimator) under the true covariance structure. 2. **Feasible GLS (FGLS):** In practice you estimate $\Omega$ from the residuals and plug it in. Works well with large samples. 3. **HAC Standard Errors:** If you just want correct inference without changing $\hat{\beta}$, use Newey-West (for time series) or clustered standard errors (for panel data). These give you the right standard errors without specifying the full covariance structure. 4. **Rule of thumb in finance:** Positive serial correlation in residuals is far more common (think trending macro variables, autocorrelated returns at low frequency). Always check for it with a Durbin-Watson test or by plotting residual autocorrelations. **Answer:** OLS coefficient estimates remain unbiased regardless of error correlation, but the OLS standard errors are no longer valid: the true variance is the sandwich $(X^T X)^{-1} X^T \Omega X (X^T X)^{-1}$, not $\sigma^2 (X^T X)^{-1}$. Which way they err depends on how $\Omega$ lines up with $X$, not on the sign of the correlation alone. In the canonical time-series case (positively autocorrelated errors, persistent regressor) OLS understates the standard errors and inflates t-stats; with negatively correlated errors it typically overstates them and costs power. Fix: use GLS or HAC/Newey-West robust standard errors. --- Online Mean and Variance for Streaming Returns [medium, Statistics] --- https://quantvault.org/problem-1641-online-mean-and-variance-for-streaming-returns.html Q: You receive a stream of returns for $N$ instruments over $T$ days, delivered in mini-batches of $B$ days at a time. $N$ can be up to $10^4$ and $T$ up to $10^6$. Design an algorithm that computes the exact sample mean vector $\hat{\mu} \in \mathbb{R}^N$ and diagonal **sample** variance vector $\hat{\sigma}^2 \in \mathbb{R}^N$ in a single pass using $O(N)$ memory, using Welford-style online updates and skipping missing values (`None`/NaN) per instrument. Implement: ```python def stream_mean_var(batches, n_instruments): ... ``` - `batches` is a list of mini-batches; each mini-batch is a list of rows; each row has length `n_instruments` and holds either a number (that instrument's return that day) or `None` (missing data). - For each non-`None` value, update per-instrument count $n_j$, mean $\bar{x}_j$, and sum of squared deviations $M_{2,j}$; the sample variance is $M_{2,j}/(n_j-1)$. Every instrument is guaranteed at least 2 valid observations. - **Return** `[means, variances]`: two lists of length `n_instruments` (means first, then sample variances). **Constraints:** - $1 \le N \le 10^4$, $1 \le T \le 10^6$, $1 \le B \le T$ - Returns may contain `None` (missing data) - Only $O(N)$ memory allowed -- you cannot store the full $N \times T$ matrix **Example** `stream_mean_var([[[1.0, 10.0], [3.0, None]], [[5.0, 20.0], [None, 30.0]]], 2) -> [[3.0, 20.0], [4.0, 100.0]]` Instrument 0 sees valid values 1, 3, 5 (mean 3, sample variance 4); instrument 1 sees 10, 20, 30 (mean 20, sample variance 100). The `None` entries are skipped so each instrument uses its own count. A: **How to Think About It:** You have more data than fits in memory, so you need incremental (online) statistics. The classic approach is Welford's algorithm, which tracks a running mean and a running sum of squared deviations (M2). The key subtlety is handling missing values (`None`) per-instrument: each instrument has its own valid count, so you maintain per-instrument counters. **Contract:** Define `stream_mean_var(batches, n_instruments)`. `batches` is a list of mini-batches; each mini-batch is a list of rows; each row has length `n_instruments` and holds either a number or `None` (missing). Process everything in a SINGLE pass with `O(N)` state, and **return `[means, variances]`** — two lists of length `n_instruments` (means first, then sample variances). **Algorithm:** For each instrument `j` maintain `n_j` (valid count), `mean_j`, and `M2_j` (sum of squared deviations). For each non-`None` value `x`: $$n_j \leftarrow n_j + 1,\quad \delta = x - \bar{x}_j,\quad \bar{x}_j \leftarrow \bar{x}_j + \frac{\delta}{n_j},\quad \delta_2 = x - \bar{x}_j,\quad M_{2,j} \leftarrow M_{2,j} + \delta\,\delta_2$$ `None` entries are skipped so different instruments can have different effective sample sizes (no imputation). The **sample** variance is `M2_j / (n_j - 1)` (Bessel-corrected); every instrument is guaranteed at least 2 valid observations. **Code:** ```python def stream_mean_var(batches, n_instruments): n = [0] * n_instruments mean = [0.0] * n_instruments m2 = [0.0] * n_instruments for batch in batches: for row in batch: for j in range(n_instruments): x = row[j] if x is None: continue n[j] += 1 delta = x - mean[j] mean[j] += delta / n[j] delta2 = x - mean[j] m2[j] += delta * delta2 means = [mean[j] for j in range(n_instruments)] variances = [m2[j] / (n[j] - 1) for j in range(n_instruments)] return [means, variances] ``` **Complexity:** Time `O(N*T)` — each of the `T` rows costs `O(N)` to update every instrument. Space `O(N)` — three arrays of length `N` (count, mean, M2). The mini-batch size `B` does not affect total work. --- Order Book Data Structure and Arbitrage Detection [hard, Coding] --- https://quantvault.org/problem-1645-order-book-data-structure-and-arbitrage-detection.html Q: Design and implement an order book data structure that supports the following operations: 1. **Add order** -- insert a new limit order (buy or sell) at a given price with a given quantity. 2. **Cancel order** -- remove an existing order by its ID. 3. **Match orders** -- when a new order arrives, match it against resting orders on the opposite side using price-time priority. 4. **Query best bid/ask** -- return the current top-of-book on each side. Your implementation should be optimized for low-latency trading. Discuss the time complexity of each operation and explain what data structures you would use. Then extend the design to handle **multi-venue arbitrage detection**. Suppose you maintain order books for the same instrument across $N$ exchanges. How do you efficiently detect when $\text{best\_ask}_i [[], [], [[1, 100, 3], [2, 100, 1]], 100]` The buy for 4 sweeps sell order 1 fully (3) then sell order 2 partially (1), producing the two fills; sell order 2 still rests with 1 unit left, so the best ask is 100. A: **How to Think About It:** An order book balances two needs: sorted-order queries (best bid/ask, matching by price-time priority) and fast random-access mutation (cancel by order ID). The production answer pairs a sorted price-level structure with a hash map from order ID to order. For this problem the tape is small, so a single order dictionary scanned per operation is clear and correct. **The contract (what `run_order_book(ops)` must return).** `ops` is a list of commands. The function returns a list with **one result per op**, in order: - `["add", id, side, price, qty]` -> a **list of fills**. Each fill is `[maker_id, maker_price, traded_qty]`, where `maker_id`/`maker_price` are the *resting* (maker) order that was hit. A buy crosses asks priced ` = price`. Matching uses price-time priority (best price first, earliest order first within a price) and trades execute at the **resting order's price**. Any unmatched incoming quantity rests on the book. No fills -> `[]`. - `["cancel", id]` -> `True` if a resting order with that id was removed, else `False` (unknown id, or an order that already fully filled). - `["best_bid"]` -> the highest resting buy price, or `None`. - `["best_ask"]` -> the lowest resting sell price, or `None`. **Code:** ```python def run_order_book(ops): # Single-venue limit order book. Replays a tape of operations and returns # a list with one result per op. # # Ops: # ["add", id, side, price, qty] -> list of fills [maker_id, maker_price, qty] # ["cancel", id] -> bool (True if a resting order was removed) # ["best_bid"] -> highest resting buy price or None # ["best_ask"] -> lowest resting sell price or None # # Matching: an incoming order crosses the opposite side using price-time # priority (best price first, earliest order first within a price). Trades # execute at the RESTING (maker) order's price. Leftover incoming qty rests. orders = {} # id -> {"side","price","qty","seq"} seq = 0 results = [] def best_bid(): prices = [o["price"] for o in orders.values() if o["side"] == "buy"] return max(prices) if prices else None def best_ask(): prices = [o["price"] for o in orders.values() if o["side"] == "sell"] return min(prices) if prices else None for op in ops: kind = op[0] if kind == "add": _, oid, side, price, qty = op seq += 1 fills = [] remaining = qty if side == "buy": # match against asks with price 0: candidates = [o for o in resting_asks() if o["price"] 0: candidates = [o for o in resting_bids() if o["price"] >= price] if not candidates: break maker = candidates[0] traded = min(remaining, maker["qty"]) fills.append([maker["id"], maker["price"], traded]) maker["qty"] -= traded remaining -= traded if maker["qty"] == 0: del orders[maker["id"]] if remaining > 0: orders[oid] = { "id": oid, "side": side, "price": price, "qty": remaining, "seq": seq, } results.append(fills) elif kind == "cancel": oid = op[1] if oid in orders: del orders[oid] results.append(True) else: results.append(False) elif kind == "best_bid": results.append(best_bid()) elif kind == "best_ask": results.append(best_ask()) else: raise ValueError("unknown op: %r" % (kind,)) return results ``` --- Reducing Portfolio Volatility [medium, Finance] --- https://quantvault.org/problem-1673-reducing-portfolio-volatility.html Q: You manage a large portfolio of stocks and want to reduce its overall volatility. Describe the main methods for constructing a lower-volatility portfolio, and explain why each works. You do not need to derive formulas -- focus on the intuition and practical trade-offs. A: **How to Think About It:** Portfolio volatility is not just about how volatile individual stocks are -- it is about how they move together. A portfolio of 100 highly correlated stocks is barely safer than holding one. The real lever is correlation, and any method that reduces effective correlation (or avoids stocks that concentrate idiosyncratic risk) reduces portfolio volatility. Frame your answer around three levels: diversification (the free lunch), optimization (the systematic approach), and factor hedging (the advanced layer). **Key Insight:** For a long-only portfolio of $n$ stocks with equal weights and pairwise correlation $\rho$, portfolio variance is: $$\sigma_p^2 = \frac{\sigma^2}{n} + \frac{(n-1)}{n} \rho \sigma^2 \xrightarrow{n \to \infty} \rho \sigma^2$$ The first term (idiosyncratic risk) goes to zero as $n$ grows; the second term (systematic risk proportional to average correlation) does not. This is why diversification has a floor. **The Method:** 1. **Naive Diversification.** Hold many stocks with roughly equal weights. This eliminates most idiosyncratic (stock-specific) risk. Works well up to $n \approx 20-30$ stocks; beyond that, the marginal benefit diminishes. Fast, cheap, but does not account for correlation structure. 2. **Mean-Variance Optimization (Markowitz).** Minimize portfolio variance subject to a target return: $$\min_{w} \ w^{T} \Sigma w \quad \text{s.t.} \quad w^{T} \mu = \mu_{\text{target}}, \ w^{T} \mathbf{1} = 1$$ The key input is the covariance matrix $\Sigma$. This finds the exact weights that minimize variance for a given return, exploiting the full correlation structure. The practical problem: $\Sigma$ must be estimated from data and is notoriously noisy, especially in high dimensions. Small errors in $\hat{\Sigma}$ lead to extreme, unstable weights. 3. **Minimum Variance Portfolio.** A special case of MVO: just minimize $w^{T} \Sigma w$ without a return constraint. This is the leftmost point on the efficient frontier. It tends to overweight low-volatility, defensive stocks (utilities, consumer staples) and underweight high-beta growth stocks. Empirically it often performs well on a risk-adjusted basis -- the "low-vol anomaly." 4. **Shrinkage / Regularized Covariance Estimation.** Instead of using the raw sample covariance matrix, shrink it toward a structured target (e.g., constant correlation or identity matrix) using methods like Ledoit-Wolf. This reduces estimation error and produces stabler weights. In practice this is almost always used in production -- raw sample covariance in high dimensions is garbage. 5. **Factor-Based Hedging.** Decompose returns into systematic factors (market, sector, momentum, value, etc.) and idiosyncratic residuals: $$R_i = \alpha_i + \sum_j \beta_{ij} F_j + \epsilon_i$$ Most portfolio volatility comes from a small number of factors. If you hedge out the dominant factor exposures (e.g., go market-neutral, or sector-neutral), you remove the correlated component of risk. What remains is mostly idiosyncratic, which diversifies away quickly. 6. **Risk Parity.** Instead of equal capital weights, assign equal risk contribution to each position. Each stock's contribution to portfolio variance is equalized. This naturally downweights volatile stocks and upweights low-vol stocks without requiring a return forecast. **Practical Considerations:** - The covariance matrix is always an estimate, never truth. Your portfolio optimization is only as good as your $\hat{\Sigma}$. - Transaction costs and turnover: MVO portfolios can rebalance aggressively in response to small covariance updates. Add a turnover penalty or use transaction-cost-aware optimization in production. - Long-only constraints restrict the diversification you can achieve. Short selling lets you hedge factor exposures more completely. - Beware of over-fitting: the minimum-variance portfolio on backtest data will always look better than out-of-sample. Use regularization and out-of-sample testing. **Answer:** The primary methods are (1) naive diversification to eliminate idiosyncratic risk, (2) mean-variance optimization with regularized covariance estimation to exploit the full correlation structure, (3) minimum-variance or risk parity weighting to avoid relying on return forecasts, and (4) factor hedging to remove exposure to systematic risk sources. All methods reduce volatility by reducing effective correlation across positions. --- Second Largest with Minimum Comparisons [hard, Coding] --- https://quantvault.org/problem-1681-second-largest-with-minimum-comparisons.html Q: You have an array of $n$ distinct numbers. Design an algorithm that finds the second largest element using at most $n + \lceil \log_2 n \rceil - 2$ comparisons in the worst case. 1. Describe the algorithm and prove it achieves this bound. 2. Prove that no comparison-based algorithm can do better -- i.e., $n + \lceil \log_2 n \rceil - 2$ is a lower bound on the worst-case number of comparisons needed to find the second largest. A: **How to Think About It:** Finding the max alone takes $n - 1$ comparisons (this is tight). The second largest must have lost directly to the max at some point -- it can only be eliminated by the best element. So the question reduces to: among all the elements that lost to the max, find the biggest. The trick is to organize the max-finding phase so the winner beats as few distinct elements as possible, which means structuring it as a balanced tournament. **Algorithm:** **Phase 1 -- Tournament to find the maximum ($n - 1$ comparisons):** Organize the $n$ elements into a balanced binary tournament bracket. In each round, pair up elements and keep the winner. After $\lceil \log_2 n \rceil$ rounds, the overall winner is the maximum. This uses exactly $n - 1$ comparisons (each comparison eliminates one element). Crucially, the maximum defeats exactly $\lceil \log_2 n \rceil$ elements during the tournament -- one per round. **Phase 2 -- Find the second largest ($\lceil \log_2 n \rceil - 1$ comparisons):** The second largest element must have lost directly to the maximum (since it can only be beaten by someone larger, and only the max is larger). The max defeated exactly $\lceil \log_2 n \rceil$ elements. Find the maximum among these $\lceil \log_2 n \rceil$ elements, which takes $\lceil \log_2 n \rceil - 1$ comparisons. **Total:** $(n - 1) + (\lceil \log_2 n \rceil - 1) = n + \lceil \log_2 n \rceil - 2$. **Example for $n = 8$:** - Round 1: 4 comparisons (8 elements $\to$ 4 winners) - Round 2: 2 comparisons (4 $\to$ 2) - Round 3: 1 comparison (2 $\to$ 1 champion) - Total Phase 1: 7 comparisons. The champion beat 3 elements (one per round). - Phase 2: Find the max of those 3 losers: 2 comparisons. - Total: $7 + 2 = 9 = 8 + 3 - 2$. **Lower Bound Proof:** Any algorithm finding the second largest must also identify the largest (otherwise it cannot be sure the element it outputs is second). Finding the largest requires at least $n - 1$ comparisons (each element except the max must lose at least once). Now, the second largest must have lost directly to the largest. Let $k$ be the number of elements the largest directly defeated. The algorithm must determine the max among these $k$ losers, requiring at least $k - 1$ additional comparisons. We need to minimize $k$ over all valid tournament structures. In any comparison-based scheme that identifies the max, the max must win at least $\lceil \log_2 n \rceil$ comparisons. This is because each comparison at most doubles the set of elements the current leader is known to beat. Starting from knowing it beats 0 others, after $j$ wins it is known to beat at most $2^j - 1$ others (accounting for transitivity). To certify it beats all $n - 1$ others requires $j \ge \lceil \log_2 n \rceil$ direct wins. So $k \ge \lceil \log_2 n \rceil$, and the total comparisons needed is at least: $$(n - 1) + (\lceil \log_2 n \rceil - 1) = n + \lceil \log_2 n \rceil - 2$$ Since the tournament algorithm achieves this bound, it is optimal. **Answer:** The tournament method finds the second largest in exactly $n + \lceil \log_2 n \rceil - 2$ comparisons, and this is provably optimal. Phase 1 uses a balanced knockout tournament ($n - 1$ comparisons) while tracking each element's direct losses. Phase 2 scans the $\lceil \log_2 n \rceil$ elements that lost to the champion ($\lceil \log_2 n \rceil - 1$ comparisons). --- Streaming Merge Join with Missing Keys [medium, Coding] --- https://quantvault.org/problem-1694-streaming-merge-join-with-missing-keys.html Q: Two streaming data sources are producing tuples in near-real-time: - **Stream A (temperature)**: $(t, \text{loc}, \text{temp})$ -- temperature readings tagged by timestamp and location. - **Stream B (humidity)**: $(t, \text{loc}, \text{humid})$ -- humidity readings tagged by timestamp and location. Both streams are *roughly* sorted by time, but events can arrive late or out of order, and either stream may be missing entries for certain $(t, \text{loc})$ pairs. Your job is to produce a joined output stream keyed by $(t, \text{loc})$ that contains the best-available temperature and humidity values for each key. Implement the function: ``` merge_join(temp_events, humid_events) ``` **Inputs.** `temp_events` and `humid_events` are each a list of `[t, loc, value]` events (temperature values and humidity values respectively). Events may repeat a `(t, loc)` key and may be unsorted. **Output.** Return a list of joined records `[t, loc, temp, humid]`, one per distinct `(t, loc)` key that appears in **either** stream, sorted by `(loc, t)`. **Value rule (last-value-carried-forward).** For a given key `(t, loc)`, each side's value is the value of the most recent event at that `loc` with `event_t [[1, "A", 20.0, 50.0], [2, "A", 20.0, 55.0], [3, "A", 22.0, 55.0]]` The key `(2, "A")` appears only in the humidity stream, so its temperature is carried forward from `t=1` (20.0); likewise `(3, "A")` carries humidity forward from `t=2` (55.0). A: **How to Think About It:** This is a classic stream-processing / merge-join problem -- the kind you would face building a real-time data pipeline for a trading floor (think merging a market-data feed with an order feed). The core tension is between *completeness* and *latency*: you want to join on $(t, \text{loc})$, but one side might be missing or late. The offline formulation here fixes the fill strategy: last-value-carried-forward (LVCF) per location. **The contract precisely.** The output has one record per distinct `(t, loc)` key appearing in **either** stream. For each key and each side, the value is the most recent event at that location with `event_t list of (event_t, order, value)` and sort each list by `(event_t, order)`. Recording the arrival `order` makes tie-breaking (last write wins at a timestamp) deterministic. 2. Collect the set of output keys as the union of `(loc, t)` across both streams. 3. For each key `(loc, t)`, scan that location's sorted list and take the value of the latest entry with `event_t `None`. 4. Emit `[t, loc, temp, humid]` for every key, sorted by `(loc, t)`. **Code:** ```python def merge_join(temp_events, humid_events): # Join two event streams on (t, loc) with last-value-carried-forward fill. # Output keys = union of (t, loc) across both streams (deduplicated). # For each key, each side's value is the most recent event at that loc with # event_t list of (event_t, order, value), sorted by (event_t, order) d = {} for order, (t, loc, val) in enumerate(events): d.setdefault(loc, []).append((t, order, val)) for loc in d: d[loc].sort() return d temp_d = build(temp_events) humid_d = build(humid_events) keys = set() for t, loc, _ in temp_events: keys.add((loc, t)) for t, loc, _ in humid_events: keys.add((loc, t)) def lvcf(d, loc, t): lst = d.get(loc) if not lst: return None best = None for event_t, _order, val in lst: # sorted ascending by (event_t, order) if event_t <= t: best = val else: break return best result = [] for loc, t in sorted(keys): tv = lvcf(temp_d, loc, t) hv = lvcf(humid_d, loc, t) result.append([t, loc, tv, hv]) return result ``` **Practical Considerations:** - **Watermarking (streaming variant):** in a true streaming system you would advance a watermark $W$ = min of the two streams' latest timestamps minus a slack factor, emit joins for keys with $t \le W$, and evict state older than $W - \text{grace}$. Flink and Kafka Streams call this "event-time processing with allowed lateness." The offline function above assumes all events are already in hand, so it can compute every LVCF fill exactly. - **Ties / duplicates:** multiple events at the same `(t, loc)` are resolved last-write-wins via the recorded input order. - **LVCF vs. interpolation:** LVCF is simple and conservative. For continuous signals, linear interpolation between the two nearest timestamps may be more accurate but adds complexity. **Complexity:** - Building and sorting the indexes: $O(N \log N)$ where $N$ is the number of events. - The LVCF scan shown is linear per key for clarity; with `bisect` on the sorted per-location list it is $O(\log n)$ per lookup, giving $O((K) \log n)$ over $K$ output keys. - **Space:** $O(N)$ for the buffered per-location state. --- Why We Divide by n-1 in Sample Variance [medium, Statistics] --- https://quantvault.org/problem-1714-why-we-divide-by-n-1-in-sample-variance.html Q: You have i.i.d. samples $X_1, \ldots, X_n$ from a distribution with mean $\mu$ and variance $\sigma^2$. The sample variance is defined as $$S^2 = \frac{1}{n-1}\sum_{i=1}^{n}(X_i - \bar{X})^2$$ Prove that $E[S^2] = \sigma^2$ -- that is, dividing by $n-1$ instead of $n$ makes $S^2$ an unbiased estimator of the population variance. As part of your proof, give a clear explanation of why the factor is $n-1$ and not $n$ in terms of degrees of freedom. A: **How to Think About It:** This is one of the most common interview questions in statistics, and it separates people who memorize from people who understand. The key tension is simple: we want to estimate $\sigma^2 = E[(X - \mu)^2]$, but we do not know $\mu$, so we substitute $\bar{X}$. The problem is that $\bar{X}$ is closer to the data than $\mu$ is -- it was literally chosen to minimize $\sum (X_i - \bar{X})^2$. So measuring deviations from $\bar{X}$ systematically underestimates the true spread. Dividing by $n-1$ instead of $n$ corrects for exactly that bias. **Quick Estimate:** Take $n = 2$ to see the effect starkly. Two draws $X_1, X_2$ with $\bar{X} = (X_1 + X_2)/2$. Then $\sum(X_i - \bar{X})^2 = 2 \cdot ((X_1 - X_2)/2)^2 = (X_1 - X_2)^2 / 2$. Now $E[(X_1 - X_2)^2] = 2\sigma^2$, so $E[\sum(X_i - \bar{X})^2] = \sigma^2 = (2-1)\sigma^2$. If you divided by $n = 2$, you would get $\sigma^2/2$ -- biased by a factor of 2. Dividing by $n-1 = 1$ gives $\sigma^2$. The bias is worst for small $n$ and vanishes as $n \to \infty$. **Approach:** The standard proof adds and subtracts $\mu$ inside the squared term, then uses linearity of expectation and the known variance of the sample mean. **Formal Solution:** Start by decomposing each deviation from $\bar{X}$ into a deviation from $\mu$: $$X_i - \bar{X} = (X_i - \mu) - (\bar{X} - \mu)$$ Square and sum over $i$: $$\sum_{i=1}^{n}(X_i - \bar{X})^2 = \sum_{i=1}^{n}\left[(X_i - \mu) - (\bar{X} - \mu)\right]^2$$ Expand the square: $$= \sum_{i=1}^{n}(X_i - \mu)^2 - 2(\bar{X} - \mu)\sum_{i=1}^{n}(X_i - \mu) + n(\bar{X} - \mu)^2$$ Notice that $\sum_{i=1}^{n}(X_i - \mu) = n(\bar{X} - \mu)$, so the cross term equals $-2n(\bar{X} - \mu)^2$. This gives: $$\sum_{i=1}^{n}(X_i - \bar{X})^2 = \sum_{i=1}^{n}(X_i - \mu)^2 - n(\bar{X} - \mu)^2$$ Now take expectations of both sides. Since $E[(X_i - \mu)^2] = \sigma^2$ for each $i$: $$E\left[\sum_{i=1}^{n}(X_i - \mu)^2\right] = n\sigma^2$$ And since $\text{Var}(\bar{X}) = \sigma^2/n$: $$E\left[n(\bar{X} - \mu)^2\right] = n \cdot \frac{\sigma^2}{n} = \sigma^2$$ Putting it together: $$E\left[\sum_{i=1}^{n}(X_i - \bar{X})^2\right] = n\sigma^2 - \sigma^2 = (n-1)\sigma^2$$ Therefore: $$E[S^2] = E\left[\frac{1}{n-1}\sum_{i=1}^{n}(X_i - \bar{X})^2\right] = \frac{(n-1)\sigma^2}{n-1} = \sigma^2$$ **Degrees of Freedom Interpretation:** The $n$ deviations $X_i - \bar{X}$ are not independent -- they satisfy the constraint $\sum_{i=1}^{n}(X_i - \bar{X}) = 0$. So there are only $n-1$ free pieces of information about spread. Equivalently, estimating $\mu$ by $\bar{X}$ "uses up" one degree of freedom. The factor $n-1$ in the denominator accounts for this lost degree of freedom. **Answer:** By expanding $\sum(X_i - \bar{X})^2 = \sum(X_i - \mu)^2 - n(\bar{X} - \mu)^2$ and taking expectations, the sum of squared deviations from the sample mean has expectation $(n-1)\sigma^2$, not $n\sigma^2$. The "missing" $\sigma^2$ comes from $E[n(\bar{X} - \mu)^2] = \sigma^2$ -- the cost of estimating the mean from the same data. Dividing by $n-1$ corrects for this, yielding $E[S^2] = \sigma^2$. --- Count Subarrays With Sum Less Than K [medium, Coding] --- https://quantvault.org/problem-1726-count-subarrays-with-sum-less-than-k.html Q: Given an array of positive integers `nums` and a positive integer `k`, return the number of contiguous subarrays whose sum is strictly less than $k$. **Constraints:** - $1 \le n \le 10^5$ where $n$ is the length of `nums` - $1 \le \text{nums}[i] \le 10^4$ - $1 \le k \le 10^9$ **Example 1:** - Input: `nums = [2, 1, 4, 3]`, `k = 5` - Output: `5` - Explanation: The valid subarrays are `[2]` (sum 2), `[1]` (sum 1), `[4]` (sum 4), `[3]` (sum 3), and `[2,1]` (sum 3). Subarrays like `[1,4]` (sum 5) and `[4,3]` (sum 7) are excluded because their sums are not strictly less than 5. **Example 2:** - Input: `nums = [1, 1, 1]`, `k = 3` - Output: `5` - Explanation: `[1]` appears 3 times (sums to 1 each), `[1,1]` appears 2 times (sums to 2 each). All five have sum < 3. The full array `[1,1,1]` sums to 3, which is not strictly less, so it is excluded. A: **How to Think About It:** Naively you enumerate all $O(n^2)$ subarrays and sum each -- $O(n^2)$ or worse. The structural fact that kills the redundancy is that **every element is positive**, so a subarray's sum is *monotone*: extend the right end, the sum only grows; shrink the left end, it only shrinks. That monotonicity is exactly the license for a sliding window with no backtracking -- the left pointer only ever moves forward. The heuristic being taught is the **two-pointer / monotone-window** pattern, plus the counting trick: once you know the smallest valid `left` for a given `right`, *all* $(\text{right}-\text{left}+1)$ subarrays ending at `right` are automatically valid, so you add them in one shot instead of looping. **Quick Estimate (order-of-magnitude sanity):** Give yourself a feel for the count before coding. If the values average $a$, a window stays under $k$ until it holds about $L\approx k/a$ elements, and there are $n$ possible right-endpoints, so the count is roughly $n\cdot\min(L, n/2)$ -- capped because a window can't be longer than the array. Test on Example 1: $\text{nums}=[2,1,4,3]$, average $a=2.5$, $k=5$, so $L\approx 5/2.5 = 2$ elements per window, $n=4$ endpoints $\Rightarrow \approx 4\times 2 = 8$ raw, but edge effects (windows near the start are short, and $[4]$ nearly saturates alone) trim it to the true $5$. Right order of magnitude, and the true answer $5$ is exactly the singleton count $4$ plus the one length-2 window $[2,1]$ -- consistent. Example 2: $[1,1,1]$, $k=3$: windows of length $\le 2$ qualify, giving $3$ singles $+ 2$ pairs $= 5$, matching. **Approach:** Monotone sliding window; add $(\text{right}-\text{left}+1)$ per right endpoint. **Formal Solution:** Maintain a window $[\text{left},\text{right}]$ with a running `window_sum`. For each new `right`: 1. Add `nums[right]` to `window_sum`. 2. While `window_sum` $\ge k$ (and `left` $\le$ `right`), subtract `nums[left]` and increment `left`. 3. Now every start in $\{\text{left},\ldots,\text{right}\}$ gives a subarray ending at `right` with sum $ = k and left <= right: window_sum -= nums[left] left += 1 count += (right - left + 1) return count ``` *Edge case:* if a single `nums[right]` $\ge k$, the while loop pushes `left` past `right`, so $\text{right}-\text{left}+1 = 0$ -- correctly counting no valid subarray ending there. **Complexity:** $\boxed{O(n)}$ time -- each element enters and leaves the window at most once, `left` never rewinds -- and $O(1)$ extra space. **Answer:** Sliding window with two pointers. For each right endpoint, shrink from the left until the sum drops below $k$, then add $\text{right}-\text{left}+1$. $O(n)$ time, $O(1)$ space. (Verified: Example 1 $\to 5$, Example 2 $\to 5$.) --- Eigenvalues of the All-Ones Matrix [easy, Linear Algebra] --- https://quantvault.org/problem-1733-eigenvalues-of-the-all-ones-matrix.html Q: Let $J$ be the $3 \times 3$ matrix where every entry is $1$. Find all eigenvalues of $J$ and their multiplicities. What are the corresponding eigenvectors? Then generalize: what are the eigenvalues of the $n \times n$ all-ones matrix? A: **How to Think About It:** Before computing anything, think about what this matrix does geometrically. The all-ones matrix $J$ takes any vector and projects it (with scaling) onto the direction $(1, 1, 1)^T$. Specifically, $Jv = (\mathbf{1}^T v)\,\mathbf{1}$, so it sums the components and puts that sum in every entry. This is a rank-1 matrix, which immediately tells you there is one nonzero eigenvalue and the rest are zero. In a quant interview, saying "it's rank 1, so there's one nonzero eigenvalue" before doing any algebra is exactly the right opening. **Quick Estimate:** The nonzero eigenvalue must equal the trace (since the other eigenvalues are zero). $\text{tr}(J) = 3$, so $\lambda_1 = 3$. Done. **Approach:** Confirm via direct computation and find eigenvectors. **Formal Solution:** Write $J = \mathbf{1}\mathbf{1}^T$ where $\mathbf{1} = (1, 1, 1)^T$. **Eigenvalue $\lambda_1 = 3$:** $$J\mathbf{1} = \mathbf{1}(\mathbf{1}^T \mathbf{1}) = 3\,\mathbf{1}$$ So $\mathbf{1} = (1, 1, 1)^T$ is an eigenvector with eigenvalue $3$. **Eigenvalue $\lambda_{2,3} = 0$:** Since $\text{rank}(J) = 1$, the null space has dimension $3 - 1 = 2$. Any vector orthogonal to $\mathbf{1}$ is in the null space: $$J v = \mathbf{1}(\mathbf{1}^T v) = \mathbf{0} \quad \text{whenever } \mathbf{1}^T v = 0$$ Two linearly independent null vectors: $v_2 = (1, -1, 0)^T$ and $v_3 = (1, 0, -1)^T$. **Generalization:** The $n \times n$ all-ones matrix $J_n = \mathbf{1}_n \mathbf{1}_n^T$ has: - Eigenvalue $n$ with multiplicity $1$ (eigenvector $\mathbf{1}_n$) - Eigenvalue $0$ with multiplicity $n - 1$ (any vector orthogonal to $\mathbf{1}_n$) This follows from the same rank-1 argument: $\text{rank}(J_n) = 1$, $\text{tr}(J_n) = n$. **Answer:** The eigenvalues of the $3 \times 3$ all-ones matrix are $\lambda = 3$ (multiplicity 1) and $\lambda = 0$ (multiplicity 2). For the $n \times n$ case: eigenvalue $n$ (multiplicity 1) and $0$ (multiplicity $n-1$). --- Gradient Boosting vs. Random Forests, Batch Normalization, and SGD Momentum [easy, Machine Learning] --- https://quantvault.org/problem-1743-gradient-boosting-vs-random-forests-batch-normalization-and-.html Q: Explain the following machine learning concepts at the level of a quant or engineer who understands the basics but wants the real intuition: **(a)** What is the key difference between Gradient Boosting and Random Forests? When would you choose one over the other? **(b)** What is batch normalization in deep learning, and why does it help training? **(c)** What is momentum in stochastic gradient descent, and why does it speed up convergence? A: **How to Think About It:** These are conceptual ML questions where the interviewer wants practical intuition, not a recitation of Wikipedia. For each, lead with the core distinction -- what problem does this technique solve, and what is the mechanism? Then give a concrete use case or failure mode that shows you understand it deeply. --- **Part (a): Gradient Boosting vs. Random Forests** Both are ensemble methods that combine many decision trees, but they differ fundamentally in *how* the trees are built and *what error* they address. **Random Forests (bagging):** - Build many trees *independently* on random bootstrap samples of the data, with random subsets of features. - Average (or majority-vote) predictions across all trees. - Each tree is fully grown -- high variance, low bias individually. The ensemble reduces variance through averaging. - Effect: reduces overfitting. A single deep tree wildly overfits; averaging 500 of them mostly cancels out the noise. - Robust, parallelizable, few hyperparameters to tune. Hard to seriously overfit with a large forest. **Gradient Boosting (boosting):** - Build trees *sequentially*. Each new tree fits the negative gradient of the loss function -- effectively the residual errors of the current ensemble. - Trees are shallow (depth 3-6) -- high bias, low variance individually. The ensemble reduces bias through accumulation. - Effect: reduces bias. It keeps correcting systematic errors, round by round. - More powerful but more fragile: learning rate, number of trees, and depth all need tuning. Can overfit badly with too many rounds or too high a learning rate. **When to use which:** - Random Forests: when you need something that works out of the box, when data is noisy, or when training speed matters (parallelizes trivially). - Gradient Boosting (XGBoost, LightGBM): when you need maximum predictive accuracy and have time to tune. Dominates tabular data competitions. --- **Part (b): Batch Normalization** Batch normalization (BN) normalizes each layer's pre-activation inputs to have zero mean and unit variance, computed over the current mini-batch. It then applies learnable scale and shift parameters $\gamma$ and $\beta$: $$\hat{x}_i = \frac{x_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}, \quad y_i = \gamma \hat{x}_i + \beta$$ Why does it help? Two mechanisms: 1. **Reduces internal covariate shift**: as the weights in earlier layers change during training, the distribution of inputs to later layers shifts. This forces later layers to constantly readjust, slowing learning. BN stabilizes these distributions, so each layer can assume its input distribution is roughly constant. 2. **Implicit regularization**: the mean and variance computed per mini-batch are noisy estimates of the true statistics. This noise acts like a regularizer and often reduces the need for dropout. Practical effect: allows much higher learning rates, faster convergence, and less sensitivity to initialization. Almost universally used in deep vision architectures (ResNets, etc.). --- **Part (c): Momentum in SGD** Standard SGD update: $$\theta \leftarrow \theta - \eta \nabla_{\theta} L$$ With momentum parameter $\gamma$ (typically 0.9): $$v \leftarrow \gamma v + \eta \nabla_{\theta} L$$ $$\theta \leftarrow \theta - v$$ The velocity $v$ is an exponentially weighted moving average of past gradients. This helps in two ways: 1. **Acceleration in consistent directions**: if the gradient consistently points the same way (say, left), the velocity builds up and steps get larger. You accelerate through long ravines in the loss surface. 2. **Dampening in oscillating directions**: in directions where the gradient flips sign (common in narrow, curved regions of the loss surface), positive and negative contributions cancel in the moving average, reducing oscillation. The physical analogy: a heavy ball rolling downhill builds momentum and rolls through small bumps and shallow local minima, rather than getting stuck or bouncing erratically. **Answer:** Random Forests reduce variance by averaging independent trees; Gradient Boosting reduces bias by sequentially correcting residuals. Batch normalization stabilizes layer input distributions during training, allowing faster learning. SGD momentum accumulates a gradient moving average, accelerating progress along consistent directions and dampening oscillations. --- Hyperparameter Selection in Machine Learning [easy, Machine Learning] --- https://quantvault.org/problem-1746-hyperparameter-selection-in-machine-learning.html Q: How would you choose hyperparameters in a machine learning model? Walk through the main strategies, their trade-offs, and practical guidelines for when to use each. A: **How to Think About It:** Hyperparameter selection is fundamentally an optimization problem with a noisy, expensive objective (model performance on held-out data). The core tension is efficiency vs. thoroughness: you want to find a good setting without wasting compute. In practice, most practitioners follow a two-stage approach -- a cheap, broad search to find promising regions, followed by a more targeted refinement. The other critical discipline is evaluation hygiene: your hyperparameter search must not touch the test set, or your reported performance is meaningless. **Key Insight:** Hyperparameter tuning is itself a search over a space. The right algorithm for that search depends on the dimensionality and cost of evaluation -- not on personal preference. **The Method:** **1. Grid Search** - Define a discrete grid of candidate values for each hyperparameter. - Train and evaluate the model at every combination on a validation set. - Choose the combination with the best validation score. - Cost: $O(n^k)$ evaluations for $k$ hyperparameters with $n$ values each. - When to use: small search spaces (1-2 hyperparameters, few candidate values). Exhaustive but impractical for $k \geq 3$. **2. Random Search** - Sample hyperparameter combinations randomly from specified distributions (e.g., log-uniform for learning rate). - Evaluate each sample on the validation set. - When to use: medium-dimensional search spaces. Bergstra and Bengio (2012) showed that random search outperforms grid search when only a few hyperparameters matter -- grid search wastes evaluations on irrelevant dimensions. **3. Bayesian Optimization** - Maintain a probabilistic surrogate model (typically a Gaussian Process) of the objective function. - Use an acquisition function (Expected Improvement, Upper Confidence Bound) to select the next point to evaluate. - Update the surrogate after each evaluation. - When to use: expensive model evaluations (deep learning, large datasets), moderate-dimensional search. Tools: Optuna, Hyperopt, scikit-optimize. - Advantage: most sample-efficient approach. Disadvantage: overhead from fitting the surrogate. **4. Cross-Validation for Evaluation** - For any search strategy, use $k$-fold cross-validation to estimate performance of each hyperparameter setting -- it reduces variance versus a single validation split. - For time series data: use expanding-window (walk-forward) CV, never standard $k$-fold. Standard $k$-fold leaks future data into training and gives optimistic, invalid estimates. **5. Early Stopping (for iterative models)** - During training, monitor validation loss after each epoch. - Stop when validation loss stops improving for a specified number of epochs (patience). - Acts as implicit regularization and automatically selects the number of training iterations. **Practical Considerations:** - Always log-scale the learning rate search range (e.g., sample uniformly from $[-5, -1]$ in $\log_{10}$ space). - Use a separate held-out test set that is never touched during hyperparameter search -- not even once. - Start with published defaults or prior work on similar tasks; refine from there rather than searching blindly. - Common high-impact hyperparameters: learning rate, regularization strength $\lambda$, tree depth, number of estimators, dropout rate. - Nested cross-validation is the statistically rigorous approach when both model selection and performance estimation are needed. **Answer:** Start with random search to identify promising regions of the hyperparameter space, then refine with Bayesian optimization if compute permits. Use $k$-fold cross-validation (or time-series CV for sequential data) to evaluate each candidate. Never tune on the test set. --- Longest Subarray With Sum K [medium, Coding] --- https://quantvault.org/problem-1759-longest-subarray-with-sum-k.html Q: Given an array $A_1, A_2, \ldots, A_n$ of integers (which may be negative) and a target value $K$, find the length of the longest contiguous subarray whose elements sum to exactly $K$. **Constraints:** - $1 \leq n \leq 10^5$ - $-10^9 \leq A_i \leq 10^9$ - $-10^{18} \leq K \leq 10^{18}$ **Example 1:** Input: $A = [1, -1, 5, -2, 3]$, $K = 3$ Output: $4$ Explanation: The subarray $[1, -1, 5, -2]$ sums to $3$ and has length $4$. **Example 2:** Input: $A = [-2, -1, 2, 1]$, $K = 1$ Output: $2$ Explanation: The subarray $[-1, 2]$ sums to $1$ and has length $2$. Design an algorithm that runs in $O(n)$ time and $O(n)$ space. Discuss overflow safeguards when element values can be large. A: **How to Think About It:** The classic trick for subarray-sum problems is prefix sums. If $P_t = A_1 + A_2 + \cdots + A_t$ (with $P_0 = 0$), then the subarray $A_{s+1} + \cdots + A_t = P_t - P_s$. So finding a subarray with sum $K$ reduces to finding indices $s earliest index prefix = 0 best = 0 for i, val in enumerate(A): prefix += val target = prefix - K if target in prefix_map: best = max(best, i - prefix_map[target]) if prefix not in prefix_map: prefix_map[prefix] = i return best ``` **Correctness argument:** Every contiguous subarray $A_{s+1}, \ldots, A_t$ with sum $K$ satisfies $P_s = P_t - K$. Our map records the earliest index for each prefix sum, so when we process index $t$ and find $P_t - K$ in the map at index $s$, the length $t - s$ is the longest subarray ending at $t$ with sum $K$. Taking the maximum over all $t$ gives the global answer. **Overflow safeguards:** - Prefix sums of $10^5$ elements each up to $10^9$ can reach $10^{14}$, which fits in a 64-bit integer but not a 32-bit one. Use `long long` in C++ or native Python integers (arbitrary precision). - The target $K$ can be $10^{18}$, so the subtraction $P_t - K$ must also use 64-bit arithmetic. - In languages like Java, use `long` instead of `int`. **Complexity:** $O(n)$ time and $O(n)$ space. **Answer:** Use a prefix sum hash map storing the earliest index for each prefix sum value. For each index $t$, look up $P_t - K$. This gives $O(n)$ time and $O(n)$ space. (Note: the problem statement mentions $O(n \log n)$ with a balanced BST, which also works but is unnecessary -- the hash map approach achieves $O(n)$.) --- OLS vs Ridge vs Lasso Regression [medium, Regression] --- https://quantvault.org/problem-1769-ols-vs-ridge-vs-lasso-regression.html Q: You are building a factor model for stock returns. You have $p = 200$ candidate factors and $n = 500$ daily observations. Let $X$ be the $n \times p$ design matrix and $Y$ the $n \times 1$ vector of returns. 1. Derive the OLS estimator $\hat{\beta}_{\text{OLS}}$ in closed form. Under what conditions does it break down, and why is $p = 200$, $n = 500$ already a warning sign? 2. Write down the Ridge regression objective (with penalty parameter $\lambda$) and derive its closed-form solution. Then show that Ridge is equivalent to the MAP estimator under Bayesian linear regression with a specific Gaussian prior on $\beta$. What is that prior? 3. Why might Lasso ($L^1$ penalty) be preferred over Ridge for this factor model? What practical advantage does it give you that Ridge cannot? A: **How to Think About It:** You have 200 factors and 500 observations -- that is a ratio of $n/p = 2.5$, which is uncomfortably close to the regime where OLS becomes unstable. In practice, many of those 200 factors will be correlated (value and earnings yield, momentum and reversal, etc.), so the effective degrees of freedom are even worse than the raw numbers suggest. The interviewer wants you to walk through the spectrum from "no regularization" (OLS) to "shrink everything" (Ridge) to "kill the irrelevant ones" (Lasso), and understand the Bayesian underpinnings. **Part (a): OLS Estimator** We minimize the squared residuals: $$\hat{\beta}_{\text{OLS}} = \arg\min_{\beta} \|Y - X\beta\|^2$$ Taking the gradient and setting it to zero gives the normal equations $X^T X \beta = X^T Y$, so: $$\hat{\beta}_{\text{OLS}} = (X^T X)^{-1} X^T Y$$ This requires $X^T X$ to be invertible. It fails when: - **$p > n$**: The matrix $X^T X$ is at most rank $n$, so it is singular when $p > n$. No unique solution exists. - **Multicollinearity**: Even when $p 0$. The matrix is always invertible. **Bayesian equivalence:** Consider the Bayesian linear model: - Likelihood: $Y \mid \beta \sim \mathcal{N}(X\beta, \, \sigma^2 I)$ - Prior: $\beta \sim \mathcal{N}\!\left(0, \, \frac{\sigma^2}{\lambda} I\right)$ The log-posterior is: $$\log p(\beta \mid Y) = -\frac{1}{2\sigma^2} \|Y - X\beta\|^2 - \frac{\lambda}{2\sigma^2} \|\beta\|^2 + \text{const}$$ Maximizing this (the MAP estimate) is equivalent to minimizing $\|Y - X\beta\|^2 + \lambda\|\beta\|^2$, which is exactly the Ridge objective. So Ridge regression is MAP estimation under a zero-mean isotropic Gaussian prior on $\beta$ with variance $\sigma^2 / \lambda$ per component. Larger $\lambda$ means a tighter prior -- you believe the coefficients are small. **Part (c): Why Lasso over Ridge?** The Lasso objective uses an $L^1$ penalty: $$\hat{\beta}_{\text{Lasso}} = \arg\min_{\beta} \left\{ \|Y - X\beta\|^2 + \lambda \|\beta\|_1 \right\}$$ The critical difference: Lasso produces **sparse** solutions -- it drives irrelevant coefficients to exactly zero. Ridge shrinks everything toward zero but never reaches it. For a 200-factor model, you almost certainly believe that only a handful of factors (maybe 10-30) truly drive returns. Ridge keeps all 200 in the model with small but nonzero weights. Lasso automatically selects the relevant subset. This gives you: - **Interpretability**: You can see which factors matter and which are noise. - **Out-of-sample robustness**: Fewer parameters means less overfitting. Estimating 20 nonzero coefficients is much more stable than estimating 200 small ones. - **Execution simplicity**: A sparse model is cheaper to trade -- you only need exposure to a few factors, not all 200. The Bayesian analogy: Lasso corresponds to a Laplace (double-exponential) prior on $\beta$, which has a sharp peak at zero -- encoding the belief that most coefficients are exactly zero. **Answer:** OLS is $(X^T X)^{-1} X^T Y$ but is unstable when $p/n$ is not small. Ridge adds $\lambda I$ to guarantee invertibility and is equivalent to MAP estimation with a $\mathcal{N}(0, \sigma^2/\lambda \cdot I)$ prior. Lasso replaces the $L^2$ penalty with $L^1$, producing sparse solutions that select the few factors that actually matter -- a critical advantage when most of your 200 candidates are noise. --- Optimal Fibonacci Computation [medium, Coding] --- https://quantvault.org/problem-1773-optimal-fibonacci-computation.html Q: Implement a function that computes the $n$-th Fibonacci number, where $F(0) = 0$, $F(1) = 1$, and $F(n) = F(n-1) + F(n-2)$ for $n \geq 2$. Your solution should achieve $O(\log n)$ time complexity. **Constraints:** - $0 \leq n \leq 10^{18}$ - Return the exact value of $F(n)$ (assume arbitrary-precision integers) **Examples:** - Input: `n = 0` -> Output: `0` - Input: `n = 1` -> Output: `1` - Input: `n = 10` -> Output: `55` - Input: `n = 50` -> Output: `12586269025` A: **How to Think About It:** The Fibonacci recurrence $F(n) = F(n-1) + F(n-2)$ screams "dynamic programming," and the naive DP solution runs in $O(n)$ time with $O(1)$ space -- which is fine for small $n$. But when $n$ can be $10^{18}$, linear time is way too slow. The key observation is that the Fibonacci recurrence is a *linear* recurrence, which means it can be expressed as a $2 \times 2$ matrix multiplication. Once you see that, you can use repeated squaring (fast exponentiation) to compute the $n$-th power of that matrix in $O(\log n)$ multiplications. **Algorithm:** The Fibonacci recurrence can be written in matrix form: $$\begin{pmatrix} F(n+1) \\\\ F(n) \end{pmatrix} = \begin{pmatrix} 1 & 1 \\\\ 1 & 0 \end{pmatrix} \begin{pmatrix} F(n) \\\\ F(n-1) \end{pmatrix}$$ Applying this repeatedly gives: $$\begin{pmatrix} F(n+1) \\\\ F(n) \end{pmatrix} = \begin{pmatrix} 1 & 1 \\\\ 1 & 0 \end{pmatrix}^{n} \begin{pmatrix} 1 \\\\ 0 \end{pmatrix}$$ So $F(n)$ is the bottom-left (or top-right) entry of the matrix $M^n$ where $M = \begin{pmatrix} 1 & 1 \\\\ 1 & 0 \end{pmatrix}$. To compute $M^n$ in $O(\log n)$ steps, use exponentiation by squaring: 1. Start with `result = I` (identity matrix) and `base = M`. 2. While $n > 0$: - If $n$ is odd, multiply `result = result * base`. - Square the base: `base = base * base`. - Integer-divide $n$ by 2. 3. Return `result[1][0]` (which is $F(n)$). Each step involves a constant number of $2 \times 2$ matrix multiplications (4 multiplies and 2 adds each), so the total work is $O(\log n)$ multiplications. **Code:** ```python def fib(n: int) -> int: """Compute F(n) in O(log n) time via matrix exponentiation.""" if n == 0: return 0 def mat_mul(A, B): return [ [A[0][0] * B[0][0] + A[0][1] * B[1][0], A[0][0] * B[0][1] + A[0][1] * B[1][1]], [A[1][0] * B[0][0] + A[1][1] * B[1][0], A[1][0] * B[0][1] + A[1][1] * B[1][1]] ] result = [[1, 0], [0, 1]] # identity matrix base = [[1, 1], [1, 0]] # Fibonacci matrix while n > 0: if n % 2 == 1: result = mat_mul(result, base) base = mat_mul(base, base) n //= 2 return result[0][1] ``` For comparison, here are the slower approaches: ```python # O(n) time, O(1) space -- iterative DP def fib_linear(n: int) -> int: a, b = 0, 1 for _ in range(n): a, b = b, a + b return a # O(2^n) time, O(n) space -- naive recursion (never use this) def fib_naive(n: int) -> int: if n <= 1: return n return fib_naive(n - 1) + fib_naive(n - 2) ``` **Complexity:** | Method | Time | Space | |---|---|---| | Naive recursion | $O(2^n)$ | $O(n)$ | | Memoized recursion | $O(n)$ | $O(n)$ | | Iterative DP | $O(n)$ | $O(1)$ | | Matrix exponentiation | $O(\log n)$ | $O(1)$ | Note: the $O(\log n)$ bound counts matrix multiplications. If you account for big-integer arithmetic (since $F(n)$ has $O(n)$ digits), the true cost is $O(n \log n)$ bit operations, but this is still dramatically faster than the $O(n^2)$ bit operations of the iterative approach for large $n$. **Answer:** The optimal approach uses matrix exponentiation: express the Fibonacci recurrence as $M^n$ where $M$ is the $2 \times 2$ transition matrix, then compute $M^n$ via repeated squaring in $O(\log n)$ matrix multiplications. --- Optimal Tender Price for a Block Trade [medium, Optimization] --- https://quantvault.org/problem-1779-optimal-tender-price-for-a-block-trade.html Q: A seller will accept your tender price $P$ for a block of shares (size $Q$) with probability $F(P)$, where $F$ is a strictly increasing CDF on $\mathbb{R}$ with density $f$. If the tender is accepted, your mark-to-market gain is $V - P$, where $V \sim N(\mu, \sigma^2)$ is the true value of the block, independent of $F$. You want to choose $P$ to maximize your expected profit. 1. Write down the expected gain as a function of $P$ and derive the first-order condition (FOC). 2. Rewrite the FOC in terms of the hazard rate $h(P) = \frac{f(P)}{1 - F(P)}$. Interpret the optimality condition economically. 3. Solve explicitly for $P^{*}$ when $F$ is the logistic CDF with location $\mu_0$, scale $1/\lambda$, i.e., $F(P) = \frac{1}{1 + e^{-\lambda(P - \mu_0)}}$. A: **How to Think About It:** This is a classic adverse-selection bidding problem. You are quoting a price to buy a block. Bid too low and the seller rejects you; bid too high and you win but overpay. The optimal bid balances the probability of execution against the expected profit per trade. Before any math: your profit is $(\mu - P)$ per accepted trade, and raising $P$ increases acceptance probability $F(P)$ but shrinks the margin. The optimum trades off marginal acceptance probability against marginal profit -- exactly the logic behind any market-making spread. **Quick Estimate:** Suppose $\mu = 100$, $\mu_0 = 98$ (seller's location), $\lambda = 1$. Intuitively the optimal bid is somewhere between the seller's midpoint 98 and the true value 100. At $P = 99$, acceptance probability is $F(99) = 1/(1 + e^{-1}) \approx 0.73$, expected profit is $0.73 \times (100 - 99) = 0.73$. At $P = 98$, acceptance is $0.5$, profit is $0.5 \times 2 = 1.0$. At $P = 97$, acceptance is about $0.27$, profit is $0.27 \times 3 = 0.81$. So the optimum is near $P \approx 98$, i.e., close to the seller's location parameter. We will confirm this. **Approach:** Write the expected gain, differentiate, and express via the hazard rate. Then specialize to the logistic distribution, whose hazard rate has a particularly clean form. **Formal Solution:** **Part 1 -- Expected Gain and FOC** Since $V$ is independent of whether the tender is accepted, the expected gain is: $$G(P) = F(P) \cdot (\mu - P)$$ Differentiating with respect to $P$: $$G'(P) = f(P)(\mu - P) - F(P) = 0$$ This gives the FOC: $$f(P)(\mu - P) = F(P)$$ **Part 2 -- Hazard Rate Form** Divide both sides of the FOC by $1 - F(P)$: $$\frac{f(P)}{1 - F(P)} \cdot (\mu - P) = \frac{F(P)}{1 - F(P)}$$ The left side contains the hazard rate $h(P) = f(P)/(1 - F(P))$, and the right side is the odds ratio. So the FOC becomes: $$h(P)(\mu - P) = \frac{F(P)}{1 - F(P)}$$ Alternatively, rearranging the original FOC by dividing both sides by $F(P)$: $$\frac{f(P)}{F(P)} (\mu - P) = 1$$ But the hazard-rate form is more natural. The economic interpretation: at the optimum, the marginal gain from raising $P$ by $dP$ (which increases acceptance probability by $f(P) \, dP$ on a deal worth $\mu - P$) exactly equals the marginal cost (paying $dP$ more on the $F(P)$ fraction of deals that would have been accepted anyway). **Part 3 -- Logistic CDF** For the logistic distribution with $F(P) = \frac{1}{1 + e^{-\lambda(P - \mu_0)}}$, we have: $$f(P) = \lambda F(P)(1 - F(P))$$ This is the key property of the logistic -- the density factors as the CDF times the survival function times $\lambda$. The hazard rate is: $$h(P) = \frac{f(P)}{1 - F(P)} = \lambda F(P)$$ Substituting into the FOC $f(P)(\mu - P) = F(P)$: $$\lambda F(P)(1 - F(P))(\mu - P) = F(P)$$ Since $F(P) > 0$ at any interior optimum, divide by $F(P)$: $$\lambda(1 - F(P))(\mu - P) = 1$$ Now $1 - F(P) = \frac{e^{-\lambda(P - \mu_0)}}{1 + e^{-\lambda(P - \mu_0)}} = \frac{1}{1 + e^{\lambda(P - \mu_0)}}$. Let $x = \lambda(P - \mu_0)$. Then: $$\frac{\lambda(\mu - P)}{1 + e^{x}} = 1$$ $$\lambda(\mu - P) = 1 + e^{\lambda(P - \mu_0)}$$ Let $\Delta = \mu - \mu_0$ (the edge -- how much more the block is worth to you than the seller's location). Then $\mu - P = \Delta - (P - \mu_0)$ and writing $u = \lambda(P - \mu_0)$: $$\lambda \Delta - u = 1 + e^{u}$$ This is a transcendental equation for $u$ (equivalently for $P^{*}$). It does not have a closed form in elementary functions, but can be expressed using the Lambert $W$ function. Rearranging: $$e^{u} = \lambda \Delta - 1 - u$$ For the special case $\lambda \Delta = 2$, we can verify $u = 0$ works: $e^{0} = 1 = 2 - 1 - 0$. This gives $P^{*} = \mu_0$, meaning you bid at the seller's location when $\mu - \mu_0 = 2/\lambda$. More generally, the solution satisfies $P^{*} = \mu - \frac{1 + e^{\lambda(P^{*} - \mu_0)}}{\lambda}$, which is easily solved numerically for any given parameters. Checking our earlier estimate: with $\mu = 100$, $\mu_0 = 98$, $\lambda = 1$, we get $\lambda \Delta = 2$, so $P^{*} = \mu_0 = 98$, confirming our back-of-envelope calculation that the optimum was near 98. **Answer:** The expected gain is $G(P) = F(P)(\mu - P)$. The FOC is $f(P)(\mu - P) = F(P)$, or in hazard-rate form: $h(P)(\mu - P) = F(P)/(1 - F(P))$. For the logistic CDF, the optimal bid satisfies $\lambda(1 - F(P^{*}))(\mu - P^{*}) = 1$, which reduces to the transcendental equation $e^{\lambda(P^{*} - \mu_0)} = \lambda(\mu - P^{*}) - 1$. --- Optimal Tick Improvement for a Market Maker [medium, Optimization] --- https://quantvault.org/problem-1780-optimal-tick-improvement-for-a-market-maker.html Q: A market maker can improve their quote by $k \in \{0, 1, 2, \ldots\}$ ticks (each tick has size $\tau$). Improving by $k$ ticks multiplies the conditional-on-queue fill intensity by $e^{\lambda k}$ for some $\lambda > 0$, but reduces the per-fill spread capture from $s$ to $s - k\tau$. Ignoring adverse selection differences across $k$, choose the optimal tick improvement $k^{*}$ that maximizes expected profit per unit time. Under what condition on $s$, $\lambda$, and $\tau$ is it optimal to improve by at least one tick? A: **How to Think About It:** This is a classic rate-times-margin optimization. Improving your quote makes you more likely to get filled (higher intensity), but each fill is worth less (smaller spread capture). The expected profit per unit time is the product of the fill rate and the per-fill profit. You want to find the sweet spot where the marginal gain in fill rate exactly offsets the marginal loss in spread. This is the same tradeoff a retailer faces between price and volume. **Quick Estimate:** The profit function is $\Pi(k) = e^{\lambda k}(s - k\tau)$. At $k = 0$, profit is $s$ (times the base intensity, which we can normalize to $1$). At $k = 1$, profit is $e^{\lambda}(s - \tau)$. Improving by one tick is worthwhile if $e^{\lambda}(s - \tau) > s$, i.e., $e^{\lambda} > s/(s - \tau)$. For small $\lambda$ and $\tau$, this approximates to $\lambda > \tau/s$ -- the fill-rate elasticity must exceed the fractional spread loss. **Approach:** Treat $k$ as continuous, optimize with calculus, then round to the nearest integer. **Formal Solution:** The expected profit per unit time (up to the base intensity constant) is: $$\Pi(k) = e^{\lambda k}(s - k\tau)$$ where $k \geq 0$ and $s - k\tau > 0$ (i.e., $k 0$, we need: $$\lambda(s - k\tau) - \tau = 0$$ $$k^{*} = \frac{s}{\tau} - \frac{1}{\lambda}$$ Since $k$ must be a non-negative integer, the optimal discrete solution is: $$k^{*} = \max\left(0, \left\lfloor \frac{s}{\tau} - \frac{1}{\lambda} + 1 \right\rfloor \text{ or } \left\lceil \frac{s}{\tau} - \frac{1}{\lambda} \right\rceil\right)$$ In practice, evaluate $\Pi$ at $\lfloor k^{*}_{\text{cont}} \rfloor$ and $\lceil k^{*}_{\text{cont}} \rceil$ and pick the better one. *Second-order check:* $$\Pi''(k) = e^{\lambda k}[\lambda^2(s - k\tau) - 2\lambda\tau]$$ At the optimum $s - k^{*}\tau = \tau/\lambda$, so $\Pi''(k^{*}) = e^{\lambda k^{*}}[\lambda^2 \cdot \tau/\lambda - 2\lambda\tau] = e^{\lambda k^{*}}[-\lambda\tau] \Pi(0)$: $$e^{\lambda}(s - \tau) > s \implies e^{\lambda} > \frac{s}{s - \tau}$$ **Answer:** The continuous optimum is $k^{*} = s/\tau - 1/\lambda$. It is optimal to improve by at least one tick when $e^{\lambda} > s/(s - \tau)$, i.e., the exponential boost in fill rate from one tick improvement exceeds the ratio of current spread to reduced spread. Equivalently, for small $\lambda$ and $\tau/s$, the condition is approximately $\lambda > \tau/(s - \tau)$. --- Option Pricing on a Dice Product [easy, Options Pricing] --- https://quantvault.org/problem-1784-option-pricing-on-a-dice-product.html Q: The value of an underlying asset $S$ is determined by the product of two independent rolls of a fair 100-sided die (faces $1$ through $100$). You want to price a European call option with strike $K = 9700$ that expires immediately. What is the fair price of this call? A: **How to Think About It:** This is a discrete expected-value calculation. The option pays $\max(S - 9700, 0)$ where $S = D_1 \cdot D_2$ and each $D_i$ is uniform on $\{1, \ldots, 100\}$. The maximum possible product is $100 \times 100 = 10000$, so the option is deep out of the money and it only pays when both dice roll very high. The key is to enumerate the small number of $(D_1, D_2)$ pairs where $D_1 \cdot D_2 > 9700$. **Quick Estimate:** We need $D_1 \cdot D_2 > 9700$ out of $10000$ equally likely outcomes. The product only exceeds $9700$ when both dice are in the high 90s. For $D_1 = 100$, we need $D_2 > 97$, giving 3 outcomes. For $D_1 = 99$, we need $D_2 > 98.0$, giving 2 outcomes. For $D_1 = 98$, we need $D_2 > 98.98$, giving 2 outcomes ($D_2 = 99$ and $D_2 = 100$, since $98 \times 99 = 9702 > 9700$). By symmetry, each of these also counts when the roles of $D_1$ and $D_2$ are swapped. The payoffs are small (at most $300$), and there are only about 8 favorable pairs. Rough guess: total payoff around $1000$, divided by $10000$, so the price is around $0.10$. **Approach:** Enumerate all pairs $(i, j)$ with $ij > 9700$. **Formal Solution:** The fair price is: $$C = E[\max(S - 9700, 0)] = \frac{1}{10000} \sum_{i=1}^{100} \sum_{j=1}^{100} \max(ij - 9700, 0)$$ We need $ij > 9700$. For a given $i$, we need $j > 9700/i$. - If $i \leq 97$: $9700/i \geq 100$, so no valid $j$. No contribution. - $i = 98$: $9700/98 = 98.98\ldots$, so $j \in \{99, 100\}$. - $j = 99$: payoff $= 98 \times 99 - 9700 = 9702 - 9700 = 2$ - $j = 100$: payoff $= 9800 - 9700 = 100$ - Subtotal: $102$ - $i = 99$: $9700/99 = 97.98\ldots$, so $j \in \{98, 99, 100\}$. - $j = 98$: payoff $= 9702 - 9700 = 2$ - $j = 99$: payoff $= 9801 - 9700 = 101$ - $j = 100$: payoff $= 9900 - 9700 = 200$ - Subtotal: $303$ - $i = 100$: $9700/100 = 97$, so $j \in \{98, 99, 100\}$ (since we need $j > 97$, i.e., $j \geq 98$). - $j = 98$: payoff $= 9800 - 9700 = 100$ - $j = 99$: payoff $= 9900 - 9700 = 200$ - $j = 100$: payoff $= 10000 - 9700 = 300$ - Subtotal: $600$ Total payoff sum: $102 + 303 + 600 = 1005$. $$C = \frac{1005}{10000} = 0.1005$$ **Answer:** The fair price of the call is $\$0.1005$. --- Quantile Regression: Check Loss, KKT Conditions, and Scalable Optimization [hard, Regression] --- https://quantvault.org/problem-1794-quantile-regression-check-loss-kkt-conditions-and-scalable-o.html Q: Fix a quantile level $\tau \in (0, 1)$. The quantile regression estimator $\hat{\beta}$ minimizes the check-loss objective: $$\hat{\beta} = \arg\min_{\beta} \sum_{i=1}^{n} \rho_\tau(y_i - x_i^\top \beta), \quad \rho_\tau(u) = u(\tau - \mathbf{1}\{u < 0\})$$ **(i)** Show that $\hat{\beta}$ defined above is indeed the sample $\tau$-quantile regression estimator. Derive the first-order (subgradient) optimality conditions. **(ii)** Reformulate the minimization as a linear program (LP). Define the auxiliary variables and write the LP explicitly. **(iii)** For large-scale problems where the LP is too slow, describe coordinate descent with a Huberized approximation of the check loss. Explain why the Huberized approximation is needed and how to choose the smoothing parameter. A: **How to Think About It:** Quantile regression generalizes median regression ($\tau = 0.5$) to arbitrary quantile levels. The check loss $\rho_\tau(u)$ is an asymmetric absolute-value function: it penalizes positive residuals with weight $\tau$ and negative residuals with weight $(1-\tau)$. When $\tau = 0.5$, this reduces to $|u|/2$ (least absolute deviations). The function is convex but not differentiable at $u=0$, which makes subgradient calculus the right tool. The LP reformulation is the classic trick for turning piecewise-linear convex problems into linear programs by splitting residuals into positive and negative parts. **Key Insight:** The check loss is piecewise linear, so its subgradient conditions have a natural interpretation: at the solution, the fraction of observations with negative residuals (fitted values above $y_i$) must equal exactly $\tau$, weighted by the design. **Part (i): Subgradient optimality conditions.** The check loss can be written: $$\rho_\tau(u) = \tau u^+ + (1-\tau)u^-, \quad u^+ = \max(u,0), \quad u^- = \max(-u,0)$$ So the objective is $Q(\beta) = \tau \sum_i r_i^+ + (1-\tau) \sum_i r_i^-$ where $r_i = y_i - x_i^\top\beta$. The subgradient of $\rho_\tau$ with respect to $u$ is: $$\partial_u \rho_\tau(u) = \begin{cases} \tau & \text{if } u > 0 \\ -(1-\tau) & \text{if } u 0} \tau x_i - \sum_{i: r_i h \\ u^2 / (2h) + (\tau - 1/2) u + h/8 & \text{if } |u| \leq h \\ -(1-\tau) u - h/2 & \text{if } u 0$. As $h \to 0$, $\rho_{\tau,h} \to \rho_\tau$. The key properties: $\rho_{\tau,h}$ is convex, continuously differentiable, and its gradient is Lipschitz with constant $1/h$. **Coordinate descent update:** With a smooth objective, the 1D update for coordinate $j$ is: $$\hat{t} = \arg\min_t \sum_i \rho_{\tau,h}(r_i - t x_{ij})$$ This has a closed-form solution (or near-closed-form via a simple Newton step) because the smoothed loss is locally quadratic. Cycle through all coordinates until convergence. **Choosing $h$:** A standard data-driven choice is $h \propto n^{-1/3}$ (the minimax-optimal smoothing bandwidth for quantile problems). In practice, start with $h = \hat{\sigma} / \sqrt{n}$ where $\hat{\sigma}$ is the residual standard deviation from an initial OLS fit, then decrease $h$ across iterations (continuation method) to tighten the approximation. **Answer:** - **(i)** $\hat{\beta}$ satisfies $\sum_i x_i(\tau - \mathbf{1}\{y_i < x_i^\top \hat{\beta}\}) = 0$, the quantile regression score equation. - **(ii)** LP: minimize $\tau \mathbf{1}^\top u + (1-\tau)\mathbf{1}^\top v$ subject to $X\beta + u - v = y$, $u, v \geq 0$. - **(iii)** Smooth the check loss near zero using a quadratic Huber kernel with bandwidth $h \to 0$; run coordinate descent on the smoothed objective; decrease $h$ via a continuation schedule for convergence to the exact quantile solution. --- Sampling Multivariate Normals via Spectral Decomposition [medium, Linear Algebra] --- https://quantvault.org/problem-1804-sampling-multivariate-normals-via-spectral-decomposition.html Q: You need to generate samples from a multivariate normal distribution $X \sim N(0, \Sigma)$ where $\Sigma$ is an $N \times N$ positive semi-definite covariance matrix. The standard approach is Cholesky decomposition, but suppose Cholesky is off the table -- maybe $\Sigma$ is singular or nearly singular, or you just want to understand the alternative. 1. Explain how to use the spectral (eigenvalue) decomposition of $\Sigma$ to generate these samples. Give the step-by-step procedure. 2. Prove that your method produces the correct covariance structure, i.e., that $\text{Cov}(X) = \Sigma$. 3. When would you prefer spectral decomposition over Cholesky in practice? A: **How to Think About It:** The core idea is simple: if you can write $\Sigma = A A^\top$ for any matrix $A$, then $X = A Z$ where $Z \sim N(0, I)$ gives you $X \sim N(0, \Sigma)$. Cholesky gives you one such $A$ (lower triangular), but spectral decomposition gives you another one that is sometimes better. The spectral version factors $\Sigma$ through its eigenvectors and eigenvalues, which has a nice geometric interpretation: you are generating independent noise along the principal axes of the ellipsoid defined by $\Sigma$, then rotating into the original coordinate system. **Key Insight:** Any symmetric PSD matrix $\Sigma$ can be written as $\Sigma = V \Lambda V^\top$ where $V$ is orthogonal and $\Lambda$ is diagonal with non-negative entries. Taking elementwise square roots of $\Lambda$ gives you a "square root" of $\Sigma$: set $A = V \Lambda^{1/2}$, and $A A^\top = V \Lambda^{1/2} (V \Lambda^{1/2})^\top = V \Lambda V^\top = \Sigma$. **The Method:** 1. Compute the eigendecomposition $\Sigma = V \Lambda V^\top$, where $V = [v_1 \mid \cdots \mid v_N]$ is the matrix of eigenvectors and $\Lambda = \text{diag}(\lambda_1, \ldots, \lambda_N)$. 2. Form the matrix square root: $\Lambda^{1/2} = \text{diag}(\sqrt{\lambda_1}, \ldots, \sqrt{\lambda_N})$. If any $\lambda_i$ is slightly negative due to numerical noise, clip it to zero. 3. Generate $Z \sim N(0, I_N)$ -- a vector of $N$ independent standard normals. 4. Compute $X = V \Lambda^{1/2} Z$. **Proof of Correctness:** $$\text{Cov}(X) = E[X X^\top] = V \Lambda^{1/2} E[Z Z^\top] \Lambda^{1/2} V^\top = V \Lambda^{1/2} I \Lambda^{1/2} V^\top = V \Lambda V^\top = \Sigma$$ Since $X$ is a linear transformation of a Gaussian vector $Z$, $X$ is also Gaussian, so matching the mean (zero) and covariance ($\Sigma$) fully determines the distribution. **Implementation:** ```python import numpy as np def sample_mvn_spectral(Sigma, n_samples=1): eigenvalues, V = np.linalg.eigh(Sigma) eigenvalues = np.maximum(eigenvalues, 0) # clip numerical noise A = V * np.sqrt(eigenvalues) # broadcasting: V @ diag(sqrt(eig)) Z = np.random.randn(len(Sigma), n_samples) return (A @ Z).T # shape: (n_samples, N) ``` **Practical Considerations:** - **Singular covariance:** Cholesky requires $\Sigma$ to be strictly positive definite. Spectral decomposition handles singular $\Sigma$ gracefully -- zero eigenvalues just mean you don't generate noise along those directions. - **Near-singular matrices:** Eigendecomposition is more numerically stable when $\Sigma$ has a large condition number. - **Speed:** Cholesky is faster, $O(N^3/3)$ vs $O(N^3)$ for full eigendecomposition. If $\Sigma$ is well-conditioned and you just need samples, Cholesky wins on speed. - **Dimensionality reduction:** If only $k \ll N$ eigenvalues are meaningfully large, you can truncate: use only the top $k$ eigenvectors and generate $k$-dimensional $Z$. This gives you approximate sampling in $O(kN)$ per sample, which is useful in high-dimensional settings like PCA-based risk models. **Answer:** Generate $Z \sim N(0, I_N)$ and compute $X = V \Lambda^{1/2} Z$ where $\Sigma = V \Lambda V^\top$ is the eigendecomposition. This works because $\text{Cov}(X) = V \Lambda V^\top = \Sigma$. Prefer spectral over Cholesky when $\Sigma$ is singular, nearly singular, or when you want to exploit low-rank structure for dimensionality reduction. --- Shortest Path on a Grid with Alternating Move Directions [medium, Coding] --- https://quantvault.org/problem-1805-shortest-path-on-a-grid-with-alternating-move-directions.html Q: You have an $n \times n$ grid where some cells are obstacles. Movement rules alternate by time step: - At **even** time steps ($t = 0, 2, 4, \ldots$), you may only move **vertically** (up or down by one cell). - At **odd** time steps ($t = 1, 3, 5, \ldots$), you may only move **horizontally** (left or right by one cell). You may also choose to **stay in place** at any time step (this still advances the clock by 1). You start at cell $(1, 1)$ at time $t = 0$ and want to reach cell $(n, n)$. 1. Define a state space that accounts for position and time parity. How large is this state space? 2. Design an $O(n^2)$ algorithm to compute the minimum time to reach $(n, n)$, or report that it is unreachable. 3. Prove that BFS on your state space returns the correct shortest-path answer. **Constraints:** - $2 \leq n \leq 1000$ - The grid is given as an $n \times n$ binary matrix where $0$ = free cell and $1$ = obstacle. - $(1,1)$ and $(n,n)$ are always free. **Example 1:** ``` Input: grid = [[0,0,0], [0,1,0], [0,0,0]] Output: 6 Explanation (0-indexed cells; each move advances the clock by 1): t=0 (even, vertical): (0,0) -> (1,0) t=1 (odd, horizontal): (1,0) -> (1,0) [stay; obstacle at (1,1)] t=2 (even, vertical): (1,0) -> (2,0) t=3 (odd, horizontal): (2,0) -> (2,1) t=4 (even, vertical): (2,1) -> (2,1) [stay] t=5 (odd, horizontal): (2,1) -> (2,2) The target (2,2) is first occupied after 6 moves, so the minimum time is 6. ``` **Example 2:** ``` Input: grid = [[0,0], [0,0]] Output: 2 Explanation (0-indexed): t=0 (even, vertical): (0,0) -> (1,0) t=1 (odd, horizontal): (1,0) -> (1,1) (1,1) is (n,n) for n=2, reached after 2 moves. Minimum time = 2. ``` A: **How to Think About It:** The twist here is that the allowed move direction depends on whether the current time step is even or odd. A standard BFS on grid coordinates alone loses track of which directions are available right now. The fix is simple: expand the state to include the time parity. Since parity only has two values (even or odd), this only doubles the state space -- still $O(n^2)$. Think of it as two copies of the grid layered on top of each other: the "even layer" (where you can move vertically) and the "odd layer" (where you can move horizontally). Every transition moves you from one layer to the other. BFS on this layered graph gives shortest paths because all edges have weight 1. **Algorithm:** 1. **State space.** Define $S = \{(r, c, p) : 1 \leq r, c \leq n, \; p \in \{0, 1\}\}$ where $p$ is the time parity ($0$ = even, $1$ = odd). The size of $S$ is $2n^2$. 2. **Transitions from state $(r, c, p)$:** - If $p = 0$ (even step -- vertical moves allowed): neighbors are $(r-1, c, 1)$ and $(r+1, c, 1)$, plus $(r, c, 1)$ for staying in place. - If $p = 1$ (odd step -- horizontal moves allowed): neighbors are $(r, c-1, 0)$ and $(r, c+1, 0)$, plus $(r, c, 0)$ for staying in place. - A neighbor is valid only if it is within bounds and the target cell is not an obstacle. 3. **BFS.** Initialize a queue with the start state $(1, 1, 0)$ at distance $0$. Run standard BFS. The answer is $\min(\text{dist}(n, n, 0), \text{dist}(n, n, 1))$ -- we accept reaching $(n, n)$ on either parity. **Code:** ```python from collections import deque def min_time_to_reach(grid): n = len(grid) # dist[r][c][p] = minimum time to reach (r,c) with parity p INF = float('inf') dist = [[[INF, INF] for _ in range(n)] for _ in range(n)] dist[0][0][0] = 0 # start at (0,0), parity 0, time 0 queue = deque() queue.append((0, 0, 0)) # (row, col, parity) while queue: r, c, p = queue.popleft() t = dist[r][c][p] np = 1 - p # next parity if p == 0: # Even step: vertical moves (up, down) or stay moves = [(r - 1, c), (r + 1, c), (r, c)] else: # Odd step: horizontal moves (left, right) or stay moves = [(r, c - 1), (r, c + 1), (r, c)] for nr, nc in moves: if 0 t + 1: dist[nr][nc][np] = t + 1 queue.append((nr, nc, np)) ans = min(dist[n - 1][n - 1][0], dist[n - 1][n - 1][1]) return ans if ans < INF else -1 ``` **Correctness Proof:** We need to show BFS on the state graph $G = (S, E)$ returns the shortest path. 1. **Unweighted graph.** Every edge in $G$ has weight 1 (each transition takes exactly one time step). BFS on an unweighted graph finds shortest paths -- this is a standard result. 2. **State space captures all information.** At any point during traversal, the next set of allowed moves depends only on the current cell $(r, c)$ and the parity $p$ of the current time step. The state $(r, c, p)$ is a sufficient statistic for the future -- there is no hidden state. Therefore any path in the original problem corresponds to a path in $G$, and vice versa. 3. **Optimality.** Suppose there is a path in the original problem reaching $(n, n)$ in $T$ steps. This path corresponds to a sequence of states in $G$ of length $T$. BFS finds the shortest such sequence. Conversely, any path in $G$ corresponds to a valid sequence of moves in the original problem. So the BFS distance equals the minimum time. 4. **Complexity.** The state space has $|S| = 2n^2$ nodes. Each node has at most 3 outgoing edges. BFS visits each node at most once. Total work: $O(n^2)$ time and $O(n^2)$ space. **Answer:** Define the state space $S = \{(r, c, p)\}$ with $|S| = 2n^2$. Run BFS from $(1, 1, 0)$ on the layered graph where even-parity states connect vertically and odd-parity states connect horizontally. BFS returns the shortest path in $O(n^2)$ time because the graph is unweighted and the state fully determines available transitions. --- Take-It-or-Leave-It Pricing with Adverse Selection [medium, Game Theory] --- https://quantvault.org/problem-1816-take-it-or-leave-it-pricing-with-adverse-selection.html Q: A seller holds a box whose value $V$ is drawn uniformly from $[0, 100]$. You are the buyer. You post a single take-it-or-leave-it price $x$ -- the seller either accepts or walks away. If a sale occurs, the seller also receives an exogenous rebate of $\$10$ on top of the price. Two scenarios: 1. **(Uninformed seller)** The seller does not observe $V$ before deciding. They accept if their expected gain from selling is non-negative, i.e., $x + 10 \geq E[V]$. 2. **(Informed seller)** The seller observes $V$ before deciding. They accept if $V \leq x + 10$ (their value is covered by price plus rebate). For each scenario: (a) derive your expected profit $\pi(x)$ as a function of $x$, (b) find the price $x^{*}$ that maximizes $\pi(x)$, and (c) compare the two optima -- what does the difference tell you about the effect of seller information on buyer profitability? A: **How to Think About It:** This is an adverse selection problem in the style of Akerlof's lemons market. When the seller is uninformed, they accept or reject without conditioning on $V$, so you buy a random box with $E[V] = 50$. The rebate shifts the seller's participation constraint -- they need $x + 10 \geq 50$, so you must pay at least $40$. Your profit is $50 - x$, maximized at $x = 40$. When the seller is informed, they only sell when $V$ is low -- specifically when $V \leq x + 10$. You face the classic lemons problem: the average box you actually buy is worth far less than 50, because the seller screens out the high-$V$ boxes. This kills your profit margin dramatically. **Quick Estimate:** Before any algebra: in the informed case, if you post $x = 20$, the seller sells whenever $V \leq 30$. You buy a box worth $E[V | V \leq 30] = 15$ on average, and you pay 20 for it -- that is a loss of 5 per trade. Clearly a high price is bad. At $x = 0$, seller sells if $V \leq 10$: you get a box worth $E[V | V \leq 10] = 5$ and pay nothing, netting 5 per trade, but only with probability $10/100 = 0.1$. Expected profit $\approx 0.5$. In the uninformed case at $x = 40$: seller always sells (since $40 + 10 = 50 = E[V]$), you buy a box worth $50$ and pay $40$ -- profit of 10 per trade, with certainty. The informed case is much worse for you. **Approach:** Compute the probability of trade and the conditional expectation of $V$ given a sale, then optimize over $x$. **Formal Solution:** *Scenario A -- Uninformed Seller:* The uninformed seller does not know $V$, so their expected value of the box is $E[V] = 50$. They accept if the price plus rebate covers this expected value: $$x + 10 \geq 50 \implies x \geq 40.$$ If $x \geq 40$: the seller always accepts. A sale occurs with probability 1, and $V$ is drawn unconditionally from $\text{Uniform}[0, 100]$. Your expected profit is: $$\pi_A(x) = E[V] - x = 50 - x.$$ This is decreasing in $x$, so the optimal price is $x_A^{*} = 40$, giving: $$\pi_A(40) = 50 - 40 = 10.$$ If $x < 40$: the seller rejects (since $x + 10 < 50$, selling is a net loss for them). No trade occurs, $\pi_A(x) = 0$. So $x_A^{*} = 40$ is confirmed. *Scenario B -- Informed Seller:* The informed seller observes $V$ and accepts iff their net gain is non-negative: $$x + 10 \geq V \implies V \leq x + 10.$$ Assume $0 \leq x \leq 90$ (so $x + 10 \in [10, 100]$ and some trade occurs). The probability of a sale is: $$P(\text{sale}) = P(V \leq x + 10) = \frac{x + 10}{100}.$$ Given a sale, $V$ is uniform on $[0, x + 10]$, so: $$E[V \mid \text{sale}] = \frac{x + 10}{2}.$$ Your expected profit (per round, not per trade) is: $$\pi_B(x) = P(\text{sale}) \cdot \left(E[V \mid \text{sale}] - x\right) = \frac{x + 10}{100} \cdot \left(\frac{x + 10}{2} - x\right).$$ Simplify the per-trade profit: $$\frac{x + 10}{2} - x = \frac{10 - x}{2}.$$ So: $$\pi_B(x) = \frac{(x + 10)(10 - x)}{200} = \frac{100 - x^2}{200}.$$ This is a downward-opening parabola in $x$, maximized at $x = 0$: $$x_B^{*} = 0, \quad \pi_B(0) = \frac{100 - 0}{200} = 0.5.$$ Sanity check: at $x = 0$, the seller sells only when $V \leq 10$. Trade probability is $10\%$, average box value given sale is $5$, you pay $0$ -- profit of $5$ per trade, $0.5$ expected. Correct. *Comparison:* | Scenario | $x^{*}$ | $\pi(x^{*})$ | |---|---|---| | A (Uninformed seller) | $40$ | $10$ | | B (Informed seller) | $0$ | $0.5$ | **Answer:** Seller information reduces buyer profit by a factor of 20 -- from $\pi = 10$ to $\pi = 0.5$. The optimal price also collapses from $40$ to $0$. In the informed case, the buyer can only profit by offering a very low price and accepting that trades are rare; any higher price attracts sellers with low-$V$ boxes and the adverse selection wipes out the margin. --- Basket Return and Dollar P&L Calculation [easy, Finance] --- https://quantvault.org/problem-1827-basket-return-and-dollar-p-l-calculation.html Q: You are running a 3-name basket with weights $(0.50, 0.30, 0.20)$. Over the last minute, the three names moved by $+1.20\%$, $-0.60\%$, and $+0.15\%$ respectively. The basket notional is $\$2{,}000{,}000$. (a) What is the basket return in basis points? (b) What is the dollar P&L on the basket? A: **How to Think About It:** This is pure mental math -- a trader should be able to do this in under 30 seconds. The basket return is just a weighted average of the individual returns. Then multiply by the notional to get the dollar move. Do the weights times returns in your head: the 50% weight on the +1.20% name is the dominant driver. **Quick Estimate:** $0.5 \times 1.20 = 0.60$. The other two are small corrections: $0.3 \times (-0.60) = -0.18$ and $0.2 \times 0.15 = 0.03$. Total: $0.60 - 0.18 + 0.03 = 0.45\%$. That is 45 bps. Dollar P&L: $0.0045 \times 2{,}000{,}000 = \$9{,}000$. **Approach:** Weighted sum of individual returns gives the portfolio return. Dollar P&L = portfolio return times notional. **Formal Solution:** **(a) Basket return:** $$R_{\text{basket}} = \sum_{i=1}^{3} w_i R_i = 0.50 \times (+1.20\%) + 0.30 \times (-0.60\%) + 0.20 \times (+0.15\%)$$ $$= 0.60\% - 0.18\% + 0.03\% = 0.45\%$$ Converting to basis points (1 bp = 0.01%): $$R_{\text{basket}} = \boxed{45 \text{ bps}}$$ **(b) Dollar P&L:** $$\Delta V = R_{\text{basket}} \times \text{Notional} = 0.0045 \times \$2{,}000{,}000 = \boxed{\$9{,}000}$$ **Answer:** The basket returned **45 bps** and the dollar P&L was **$9,000**. --- Breeden-Litzenberger Formula [hard, Options Pricing] --- https://quantvault.org/problem-1832-breeden-litzenberger-formula.html Q: Assume the call price surface $C(K, T)$ is twice differentiable in strike $K$ for a fixed maturity $T$, and that the market is free of static arbitrage. 1. Derive the Breeden-Litzenberger result: show that the risk-neutral probability density $f_{\mathbb{Q}}(K)$ of the terminal stock price $S_T$ is related to call prices by $$f_{\mathbb{Q}}(K) = e^{rT} \frac{\partial^2 C}{\partial K^2}$$ where $r$ is the continuously compounded risk-free rate. Also state what the first derivative $\partial C/\partial K$ alone tells you. 2. Explain how you would estimate this density in practice from a discrete set of quoted option prices. For example, with strike spacing $\Delta K = 5$, calls quoted at $C(K - 5) = 10.00$, $C(K) = 7.00$, $C(K + 5) = 4.50$ and a tenor short enough that $e^{rT} \approx 1$, estimate $f_{\mathbb{Q}}(K)$ and the risk-neutral probability $P^{\mathbb{Q}}(S_T > K)$. 3. Give the economic interpretation: what option position isolates the risk-neutral density at a specific strike? A: **How to Think About It:** The Breeden-Litzenberger formula is one of the most elegant results in derivatives theory -- it says that option markets implicitly contain the full risk-neutral distribution of the underlying. Before writing any math, think about it this way: a call option's value depends on the entire right tail of the distribution above the strike. If you have call prices for every possible strike, you have encoded every tail probability. Taking derivatives just peels off the layers. The first derivative gives you the survival function (tail probability), and the second derivative gives you the density itself. This is not just a theoretical curiosity -- traders use this daily to extract implied distributions from option chains. **Quick Sanity Checks:** - The density $f_{\mathbb{Q}}(K)$ must be non-negative everywhere. Since the market is arbitrage-free, call prices are convex in strike ($\partial^2 C / \partial K^2 \geq 0$), so this checks out. - As $K \to \infty$, both $C(K) \to 0$ and $f_{\mathbb{Q}}(K) \to 0$. Consistent. - As $K \to 0$, the call approaches its intrinsic value $S_0 e^{-qT} - K e^{-rT}$ (for a non-dividend-paying stock, $C \to S_0 - K e^{-rT}$), so $\partial C / \partial K \to -e^{-rT}$ and $\partial^2 C / \partial K^2 \to 0$, meaning the density vanishes at zero. That makes sense for a lognormal-like distribution. - The factor $e^{rT}$ is just undoing the discounting baked into call prices. - Plugging Black-Scholes prices in ($S_0 = 100$, $r = 5\%$, $\sigma = 20\%$, $T = 1$) gives $e^{rT}\,\partial^2 C/\partial K^2 = 0.0197$ at $K = 100$, exactly the lognormal density of $S_T$ there. The formula is a model-free generalization, not a new model. **Derivation:** Start from the risk-neutral pricing formula for a European call: $$C(K) = e^{-rT} \int_K^{\infty} (S_T - K) \, f_{\mathbb{Q}}(S_T) \, dS_T$$ Split the integrand: $$C(K) = e^{-rT} \left[ \int_K^{\infty} S_T \, f_{\mathbb{Q}}(S_T) \, dS_T - K \int_K^{\infty} f_{\mathbb{Q}}(S_T) \, dS_T \right]$$ Differentiate with respect to $K$. Using Leibniz's rule (the boundary terms from the first integral cancel since $S_T = K$ at the boundary gives $(K - K) f_{\mathbb{Q}}(K) = 0$): $$\frac{\partial C}{\partial K} = e^{-rT} \left[ -\int_K^{\infty} f_{\mathbb{Q}}(S_T) \, dS_T \right] = -e^{-rT} \left[1 - F_{\mathbb{Q}}(K)\right]$$ where $F_{\mathbb{Q}}(K) = \int_0^K f_{\mathbb{Q}}(S_T) \, dS_T$ is the risk-neutral CDF. This already tells us something useful: the slope of the call price curve with respect to strike gives us the (discounted) risk-neutral survival probability. So $-e^{rT}\,\partial C/\partial K = P^{\mathbb{Q}}(S_T > K)$ is the risk-neutral probability of finishing in the money, and $-\partial C/\partial K$ itself is the price of a digital call paying $1$ if $S_T > K$. Nothing here depends on a model: every model-specific detail lives inside $f_{\mathbb{Q}}$. Differentiate again: $$\frac{\partial^2 C}{\partial K^2} = e^{-rT} f_{\mathbb{Q}}(K)$$ Rearranging: $$\boxed{f_{\mathbb{Q}}(K) = e^{rT} \frac{\partial^2 C}{\partial K^2}}$$ **Practical Estimation (Butterfly Spread):** In practice, you do not have a continuous call price function -- you have discrete strikes. Approximate the second derivative with the standard finite-difference formula: $$f_{\mathbb{Q}}(K) \approx e^{rT} \frac{C(K + \Delta K) - 2C(K) + C(K - \Delta K)}{(\Delta K)^2}$$ Notice that the numerator $C(K + \Delta K) - 2C(K) + C(K - \Delta K)$ is exactly the cost of a butterfly spread centered at $K$ with wing width $\Delta K$. So the economic interpretation is: - A butterfly spread is a bet on the stock landing near $K$ at expiry. - Its price, properly scaled, gives you the risk-neutral probability of $S_T$ being near $K$. - As $\Delta K \to 0$, this converges to the density. **Worked example.** With $\Delta K = 5$, $C(K-5) = 10.00$, $C(K) = 7.00$, $C(K+5) = 4.50$ and $e^{rT} \approx 1$, the butterfly costs $10.00 - 2(7.00) + 4.50 = 0.50$, so $$f_{\mathbb{Q}}(K) \approx \frac{0.50}{5^2} = 0.02 \text{ per unit of strike.}$$ The first derivative comes from a call spread. The central difference across $K$ gives $$P^{\mathbb{Q}}(S_T > K) \approx -e^{rT}\,\frac{C(K+5) - C(K-5)}{2\Delta K} = \frac{10.00 - 4.50}{10} = 0.55.$$ The one-sided spreads give $0.60$ (from $K-5$ to $K$) and $0.50$ (from $K$ to $K+5$); those estimate the survival probability at the midpoints $K - 2.5$ and $K + 2.5$, and the central difference is the estimate at $K$ itself. The quotes pass the static-arbitrage checks: both slopes lie in $[-1, 0]$ and the prices are convex in strike. In practice, you take your option chain, compute butterfly prices at each available strike, normalize by $(\Delta K)^2$, and multiply by $e^{rT}$. This gives you a discrete approximation to the implied risk-neutral distribution. Smoothing (e.g., fitting a spline to implied vols first) helps avoid noisy density estimates from bid-ask spreads. Puts work too: $\partial^2 P/\partial K^2 = e^{-rT} f_{\mathbb{Q}}(K)$ as well (put-call parity makes $C - P$ linear in $K$), so in practice you use the more liquid out-of-the-money side at each strike. **Practical Interpretation:** Traders use extracted risk-neutral densities constantly. Comparing the implied density to a lognormal benchmark tells you where the market is pricing extra probability mass -- fat tails, skewness, or bimodality around events. Before earnings, you often see bimodal implied densities reflecting the market's view of a big move in either direction. Risk managers use the implied density to stress-test portfolios under the market's own probability measure rather than relying on historical distributions. **Answer:** The Breeden-Litzenberger formula states $f_{\mathbb{Q}}(K) = e^{rT} \, \partial^2 C / \partial K^2$. The risk-neutral density is recovered from the convexity of call prices in strike, and the first derivative gives the in-the-money probability, $-e^{rT}\,\partial C/\partial K = P^{\mathbb{Q}}(S_T > K)$ (a digital call). Practically, this is estimated via butterfly spread prices: $f_{\mathbb{Q}}(K) \approx e^{rT} [C(K+\Delta K) - 2C(K) + C(K-\Delta K)] / (\Delta K)^2$. A butterfly spread centered at $K$ is a pure bet on the underlying landing near $K$, making its price a direct readout of the implied probability density at that point. For the example quotes, $f_{\mathbb{Q}}(K) \approx 0.02$ and $P^{\mathbb{Q}}(S_T > K) \approx 0.55$. --- Delta of a Digital Call Option [medium, Options Pricing] --- https://quantvault.org/problem-1839-delta-of-a-digital-call-option.html Q: A digital (binary) call option pays $\$1$ if $S_T > K$ and $\$0$ otherwise. Under standard Black-Scholes assumptions (constant volatility $\sigma$, risk-free rate $r$, log-normal stock price), the price of this option is: $$V = e^{-rT} N(d_2)$$ where $d_2 = \dfrac{\ln(S/K) + (r - \sigma^2/2)T}{\sigma\sqrt{T}}$. 1. Derive the delta $\Delta = \partial V / \partial S$ of this digital call. 2. What happens to the delta as $T \to 0$ when the option is at-the-money ($S \approx K$)? What does this imply for hedging? A: **How to Think About It:** A digital call is a discounted bet on the event $\{S_T > K\}$, so its price is $e^{-rT}$ times the risk-neutral probability of that event, $N(d_2)$. Delta asks: how fast does that probability move when the stock moves? The heuristic is a **chain-rule + dimensional check**. The only place $S$ enters is through $d_2$, and $d_2$ shifts by $\partial d_2/\partial S = 1/(S\sigma\sqrt{T})$ per dollar. The probability responds to that shift at the rate of the normal *density* $\phi(d_2)$. So $\Delta = e^{-rT}\phi(d_2)\cdot\tfrac{1}{S\sigma\sqrt{T}}$ almost writes itself. The trap is to reach for the vanilla-call delta $N(d_1)$ — that is the *derivative of a payoff that grows linearly past $K$*, not of an indicator. A digital's payoff is flat except for a jump, so its sensitivity lives entirely in the density at the boundary. **Quick Estimate:** The interview payoff here is the *scaling law*, and you can get it dimensionally without a single BSM table lookup. At the money $d_2\approx 0$, so $\phi(d_2)\approx\phi(0)=1/\sqrt{2\pi}\approx 0.4$. Everything else in $\Delta=\tfrac{e^{-rT}\phi(d_2)}{S\sigma\sqrt{T}}$ is order-1 except the lone $\sqrt{T}$ in the denominator, so **$\Delta$ scales like $1/\sqrt{T}$**. Put numbers on it: take $S=K=100$, $\sigma=0.2$, $r=0$. At $T=1$: $\Delta\approx \tfrac{0.4}{100\cdot 0.2\cdot 1}=\tfrac{0.4}{20}=0.02$. Now shrink the horizon to $T=0.01$ (about 2–3 trading days): $\sqrt{T}$ drops from $1$ to $0.1$, a factor of $10$, so $\Delta\approx 0.02\times 10 = 0.2$. Shrink again to $T=10^{-4}$: another factor of $10$, $\Delta\approx 2$. The number has no ceiling — halving the time to expiry multiplies delta by $\sqrt2$ forever. That runaway *is* the answer to part 2: the hedge ratio diverges. **Approach:** Chain rule through $d_2$; then read off the $T\to 0$ behaviour from the $1/\sqrt{T}$ factor with $\phi(d_2)\to\phi(0)$. **Formal Solution:** **(a) Delta.** With $V=e^{-rT}N(d_2)$ and $d_2=\dfrac{\ln(S/K)+(r-\sigma^2/2)T}{\sigma\sqrt{T}}$, only $\ln S$ carries the $S$-dependence, so $$\frac{\partial d_2}{\partial S}=\frac{1}{S\sigma\sqrt{T}}.$$ By the chain rule, using $N'(x)=\phi(x)$, $$\boxed{\;\Delta=\frac{\partial V}{\partial S}=e^{-rT}\phi(d_2)\,\frac{\partial d_2}{\partial S}=\frac{e^{-rT}\,\phi(d_2)}{S\sigma\sqrt{T}}\;}$$ Sanity: $\Delta>0$ always (higher $S$ raises the ITM probability). Deep ITM ($d_2\to+\infty$) or deep OTM ($d_2\to-\infty$) both send $\phi(d_2)\to 0$, so $\Delta\to 0$ in the tails; the sensitivity peaks near ATM. **(b) The limit $T\to 0$ at the money.** Set $S=K$, so $\ln(S/K)=0$ and $d_2=\dfrac{(r-\sigma^2/2)\sqrt{T}}{\sigma}\to 0$; hence $\phi(d_2)\to\phi(0)=\tfrac{1}{\sqrt{2\pi}}$ and $e^{-rT}\to 1$. The denominator $S\sigma\sqrt{T}\to 0$, so $$\Delta\sim\frac{1}{S\sigma\sqrt{2\pi}}\cdot\frac{1}{\sqrt{T}}\;\longrightarrow\;+\infty.$$ Delta **blows up without bound** — it concentrates into a Dirac spike at $S=K$ as the payoff sharpens into a step. A tiny stock move flips the whole \$1 payoff, so no finite position in the underlying can hedge it; the required share count explodes. **Practical note:** desks never hedge a naked digital near expiry with stock. They replicate it with a tight call spread — long $C(K)$, short $C(K+\epsilon)$, scaled by $1/\epsilon$ — whose delta stays bounded. Tighter $\epsilon$ means better replication but more gamma to carry. **Answer:** $\Delta=\dfrac{e^{-rT}\,\phi(d_2)}{S\sigma\sqrt{T}}$. At the money it grows like $1/\sqrt{T}$ and diverges as $T\to 0$, so the underlying alone essentially cannot hedge the digital right near expiry. --- Implied Probabilities and Bookmaker Overround [easy, Finance] --- https://quantvault.org/problem-1855-implied-probabilities-and-bookmaker-overround.html Q: A sportsbook quotes decimal odds of $O_A = 1.72$ on Team A and $O_B = 2.35$ on Team B for a game where exactly one team wins. 1. Compute the implied probability of each outcome from the posted odds. 2. Calculate the bookmaker's overround (also called the "vig" or "juice") -- the margin baked into these quotes. 3. Determine the no-arbitrage range of "fair" win probabilities for Team A that are consistent with these two quotes. A: **How to Think About It:** Decimal odds tell you the payout per unit staked, including stake return. So odds of $O_A = 1.72$ mean: bet \$1, get back \$1.72 if Team A wins. The implied probability is just $1/O_A$ -- the break-even probability if the book had zero margin. But if you sum up $1/O_A + 1/O_B$ and get a number greater than 1, the book is extracting that excess as profit. That excess is the overround. The no-arbitrage range is then the interval of true Team A probabilities where neither betting on A nor betting on B at these odds has positive expected value for the bettor. **Quick Estimate:** Let's ballpark. $1/1.72 \approx 0.58$ and $1/2.35 \approx 0.43$. Sum is about $0.58 + 0.43 = 1.01$ -- so the overround is roughly 1%, or about 1 cent on the dollar. That is a lean book. Typical sportsbooks run 3-10%, so this is a tighter-than-average market. Quick sanity: the fair probability for A must be somewhere below 0.58 (otherwise betting A is a freeroll for bettors). Similarly it must be above $1 - 0.43 = 0.57$. So the fair probability for A is pinned in a very narrow band around 57-58%. **Approach:** Work directly from the definition of decimal odds. No arbitrage requires that the expected profit from betting either side at the quoted odds is non-positive. **Formal Solution:** **Step 1 -- Implied Probabilities.** Decimal odds $O_i$ imply a break-even probability (from the book's perspective): $$p_A^{\text{impl}} = \frac{1}{O_A} = \frac{1}{1.72} \approx 0.5814$$ $$p_B^{\text{impl}} = \frac{1}{O_B} = \frac{1}{2.35} \approx 0.4255$$ **Step 2 -- Overround.** The overround $\Omega$ is the sum of implied probabilities minus 1: $$\Omega = \frac{1}{O_A} + \frac{1}{O_B} - 1 = 0.5814 + 0.4255 - 1 = 0.0069$$ So the book is running roughly a **0.69% margin** on this game. Equivalently, for every \$100 of balanced two-sided action, the book expects to keep about \$0.69. Alternatively expressed as a percentage of total implied probability: the overround is $\Omega / (1 + \Omega) \approx 0.68\%$ of handle. Both conventions appear in practice -- know which one your interviewer means. **Step 3 -- No-Arbitrage Range for $p_A$.** Let $p$ be the true probability that Team A wins. The bettor's expected profit from a \$1 bet on A is: $$\pi_A(p) = p \cdot (O_A - 1) - (1 - p) \cdot 1 = p \cdot O_A - 1$$ No-arbitrage on the A side requires $\pi_A(p) \leq 0$, i.e.: $$p \leq \frac{1}{O_A} = 0.5814$$ Similarly, a \$1 bet on B has expected profit $\pi_B(p) = (1-p) \cdot O_B - 1$. No-arbitrage on the B side requires: $$(1 - p) \leq \frac{1}{O_B} \implies p \geq 1 - \frac{1}{O_B} = 1 - 0.4255 = 0.5745$$ So the no-arbitrage range for the true Team A win probability is: $$0.5745 \leq p_A \leq 0.5814$$ This interval has width exactly equal to the overround $\Omega \approx 0.0069$. The book can only be arbitrage-free if the true probability falls inside this band. **Answer:** - Implied probability of A: $p_A^{\text{impl}} \approx 58.1\%$ - Implied probability of B: $p_B^{\text{impl}} \approx 42.6\%$ - Overround: $\Omega \approx 0.69\%$ - No-arbitrage range for Team A: $p_A \in [0.5745,\ 0.5814]$, a band of width equal to $\Omega$ --- Knowledge Gradient for Two Bernoulli Arms [hard, Optimization] --- https://quantvault.org/problem-1858-knowledge-gradient-for-two-bernoulli-arms.html Q: You face a two-armed bandit problem with a one-step lookahead. Arm $i$ ($i = 1, 2$) has an unknown success probability $p_i$, with prior $p_i \sim \text{Beta}(\alpha_i, \beta_i)$. You may collect one Bernoulli sample from one arm, then must play the empirically best arm once for a payoff of 1 on success and 0 on failure. 1. Derive a closed-form expression for the expected value of information (EVOI) of sampling arm $i$. 2. State the decision rule for which arm to sample. 3. Simplify the EVOI formula for the case of symmetric priors ($\alpha_1 = \beta_1 = \alpha_2 = \beta_2$). A: **How to Think About It:** This is a Bayesian decision problem where you get one free "look" before committing. Without any sample, you would just play the arm with the higher prior mean. The value of sampling comes from the chance that the sample changes your mind -- you might discover the arm you thought was worse is actually better. The knowledge gradient (KG) quantifies exactly this: the expected improvement in your final decision quality from taking one sample. **Quick Estimate:** Suppose arm 1 has prior mean $\mu_1 = \alpha_1 / (\alpha_1 + \beta_1) = 0.6$ and arm 2 has $\mu_2 = 0.5$. Without sampling, you play arm 1 and expect payoff 0.6. If you sample arm 1 and see a success (which updates $\mu_1$ upward), you still play arm 1 -- no change. If you see a failure (updating $\mu_1$ downward), you might switch to arm 2 if the updated $\mu_1$ drops below 0.5. The EVOI of sampling arm 1 is the probability of this switch times the improvement when it happens. For arm 2, a success might push $\mu_2$ above $\mu_1$, causing a switch. The EVOI is larger when the two arms have similar prior means (more chance of a switch) and when priors are weak (each observation shifts the mean more). **Formal Derivation:** Let $\mu_i = \alpha_i / (\alpha_i + \beta_i)$ denote the prior mean of arm $i$, and let $n_i = \alpha_i + \beta_i$. **Baseline value (no sampling):** $$V_0 = \max(\mu_1, \mu_2)$$ **Value of sampling arm $i$:** If we sample arm $i$ and observe success ($S$) or failure ($F$): - $P(S) = \mu_i$ (prior predictive) - $P(F) = 1 - \mu_i$ After observing success on arm $i$, the posterior mean updates to: $$\mu_i^{+} = \frac{\alpha_i + 1}{n_i + 1}$$ After failure: $$\mu_i^{-} = \frac{\alpha_i}{n_i + 1} = \frac{\mu_i n_i}{n_i + 1}$$ The arm $j \neq i$ is not sampled, so $\mu_j$ stays the same. After sampling arm $i$, you play whichever arm has the higher posterior mean. The expected payoff is: $$V_i = \mu_i \cdot \max(\mu_i^{+}, \mu_j) + (1 - \mu_i) \cdot \max(\mu_i^{-}, \mu_j)$$ The EVOI of sampling arm $i$ is: $$\text{EVOI}_i = V_i - V_0$$ **Expanding the formula:** Without loss of generality, suppose $\mu_1 \geq \mu_2$ (arm 1 is currently favored). *Case: Sample arm 1 (the leading arm):* - After success: $\mu_1^{+} = (\alpha_1 + 1)/(n_1 + 1)$. Since $\mu_1 \geq \mu_2$ and success only increases $\mu_1$, we still play arm 1. $\max(\mu_1^{+}, \mu_2) = \mu_1^{+}$. - After failure: $\mu_1^{-} = \alpha_1/(n_1 + 1)$. If $\mu_1^{-} \mu_1$, switch to arm 2. - After failure: $\mu_2^{-} = \alpha_2/(n_2 + 1) \mu_1$: $$\text{EVOI}_2 = \mu_2 \cdot \mu_2^{+} + (1 - \mu_2) \cdot \mu_1 - \mu_1 = \mu_2(\mu_2^{+} - \mu_1)$$ Similarly, $\mu_2^{+} - \mu_2 = \frac{1 - \mu_2}{n_2+1}$, so $\mu_2^{+} = \mu_2 + \frac{1 - \mu_2}{n_2+1}$. $$\text{EVOI}_2 = \mu_2\left(\mu_2 + \frac{1 - \mu_2}{n_2+1} - \mu_1\right) = \mu_2(\mu_2^{+} - \mu_1)$$ Valid when $\mu_2^{+} > \mu_1$. Otherwise $\text{EVOI}_2 = 0$. **Closed-form summary (assuming $\mu_1 \geq \mu_2$):** $$\text{EVOI}_1 = (1 - \mu_1) \cdot \max(\mu_2 - \mu_1^{-}, 0)$$ $$\text{EVOI}_2 = \mu_2 \cdot \max(\mu_2^{+} - \mu_1, 0)$$ where $\mu_i^{+} = (\alpha_i + 1)/(n_i + 1)$ and $\mu_i^{-} = \alpha_i/(n_i + 1)$. **Decision Rule:** Sample the arm with the higher EVOI: - If $\text{EVOI}_1 > \text{EVOI}_2$, sample arm 1. - If $\text{EVOI}_2 > \text{EVOI}_1$, sample arm 2. - If $\text{EVOI}_1 = \text{EVOI}_2 = 0$, sampling is worthless; just play the current best arm. **Symmetric priors ($\alpha_1 = \beta_1 = \alpha_2 = \beta_2 = a$):** Both arms have $\mu_1 = \mu_2 = 1/2$ and $n_1 = n_2 = 2a$. Since the arms are tied: $$\mu_i^{+} = \frac{a + 1}{2a + 1}, \quad \mu_i^{-} = \frac{a}{2a + 1}$$ By symmetry, sampling either arm has the same EVOI. After sampling arm $i$: - Success (prob $1/2$): posterior mean $(a+1)/(2a+1) > 1/2$, so play arm $i$. Expected payoff $(a+1)/(2a+1)$. - Failure (prob $1/2$): posterior mean $a/(2a+1) < 1/2$, so play the other arm. Expected payoff $1/2$. $$V_{\text{sample}} = \frac{1}{2} \cdot \frac{a+1}{2a+1} + \frac{1}{2} \cdot \frac{1}{2} = \frac{a+1}{2(2a+1)} + \frac{1}{4}$$ $$= \frac{2(a+1) + 2a+1}{4(2a+1)} = \frac{4a + 3}{4(2a+1)}$$ Baseline: $V_0 = 1/2$. $$\text{EVOI} = \frac{4a+3}{4(2a+1)} - \frac{1}{2} = \frac{4a+3 - 2(2a+1)}{4(2a+1)} = \frac{1}{4(2a+1)}$$ As $a \to \infty$ (strong prior), $\text{EVOI} \to 0$ -- you already know the arms are fair, sampling teaches nothing. As $a \to 0$ (weak prior), $\text{EVOI} \to 1/4$ -- maximum value of information. **Answer:** For $\mu_1 \geq \mu_2$: $$\text{EVOI}_i = P(\text{switch} \mid \text{sample } i) \times (\text{improvement from switch})$$ $$\text{EVOI}_1 = (1 - \mu_1)\max\!\left(\mu_2 - \frac{\alpha_1}{n_1+1},\, 0\right)$$ $$\text{EVOI}_2 = \mu_2 \cdot \max\!\left(\frac{\alpha_2+1}{n_2+1} - \mu_1,\, 0\right)$$ Sample the arm with higher EVOI. For symmetric priors $\text{Beta}(a, a)$, $\text{EVOI} = 1/[4(2a+1)]$, and you are indifferent between arms. --- OLS Assumptions, Violations, and Diagnostics [medium, Regression] --- https://quantvault.org/problem-1866-ols-assumptions-violations-and-diagnostics.html Q: Consider the linear regression model $Y = X\beta + \epsilon$, estimated by ordinary least squares (OLS). 1. State the five classical OLS assumptions (Gauss-Markov plus normality). 2. For each assumption, explain what goes wrong when it is violated -- specifically, does it affect bias, efficiency, or the validity of standard errors and hypothesis tests? 3. For each violation, name a diagnostic test to detect it and a practical remedy. A: **How to Think About It:** The Gauss-Markov theorem says OLS is BLUE -- Best Linear Unbiased Estimator -- under its assumptions. But 'best' and 'unbiased' can break independently. Violations that bias $\hat{\beta}$ are the most dangerous (you get the wrong answer). Violations that only inflate standard errors are less severe (your point estimates are fine, but your t-stats are wrong). Know which is which. **Key Insight:** Bias in $\hat{\beta}$ comes almost exclusively from Assumption 3 (exogeneity) being violated. All other violations mainly affect efficiency or inference, not the coefficient estimates themselves. **The Assumptions and Their Violations:** **1. Linearity: $E[Y|X] = X\beta$** - *What it says:* The conditional mean of $Y$ is a linear function of $X$. - *When violated:* The model is misspecified. $\hat{\beta}$ is biased and inconsistent -- it is fitting a line through a nonlinear cloud. - *Detect:* Plot residuals vs. fitted values; look for systematic curvature. RESET test. - *Fix:* Add polynomial terms, interactions, or use a nonlinear model. **2. No perfect multicollinearity: $\text{rank}(X) = p$** - *What it says:* No column of $X$ is an exact linear combination of the others. $(X^T X)^{-1}$ must exist. - *When violated:* $(X^T X)$ is singular -- OLS has no unique solution. Near-multicollinearity inflates standard errors severely. - *Detect:* Variance Inflation Factor (VIF). Rule of thumb: VIF $> 10$ is a concern. - *Fix:* Drop a collinear variable, combine them (e.g., PCA), or use ridge regression. **3. Exogeneity: $E[\epsilon | X] = 0$** - *What it says:* The error is uncorrelated with the regressors on average. No omitted variable correlated with $X$, no reverse causality, no measurement error in $X$. - *When violated:* $\hat{\beta}$ is biased and inconsistent. This is the most serious violation -- you cannot fix it by getting more data. - *Detect:* Hausman test (compare OLS to IV). Qualitative reasoning about the data-generating process. - *Fix:* Instrumental variables (IV/2SLS), control for omitted variables, or use a natural experiment. **4. Homoscedasticity: $\text{Var}(\epsilon_i | X) = \sigma^2$ (constant)** - *What it says:* All residuals have the same variance regardless of $X$. - *When violated (heteroscedasticity):* $\hat{\beta}$ is still unbiased, but OLS is no longer efficient (GLS dominates). More importantly, standard errors are wrong, so t-stats and confidence intervals are invalid. - *Detect:* Plot $|\hat{\epsilon}_i|$ vs. $\hat{y}_i$; Breusch-Pagan test; White test. - *Fix:* Use White (heteroscedasticity-robust) standard errors, or WLS if the variance structure is known. **5. No autocorrelation: $\text{Cov}(\epsilon_i, \epsilon_j) = 0$ for $i \neq j$** - *What it says:* Residuals are uncorrelated across observations. - *When violated:* Similar to heteroscedasticity -- $\hat{\beta}$ is unbiased but standard errors are wrong (usually understated), making inference anti-conservative. - *Detect:* Durbin-Watson test; ACF plot of residuals. - *Fix:* Newey-West (HAC) standard errors for time series; include lagged variables; GLS/FGLS. **Bonus -- Normality of $\epsilon$ (for finite-sample inference)** - *What it says:* $\epsilon \sim N(0, \sigma^2 I)$. Required for exact $t$ and $F$ distributions in small samples. - *When violated:* Large-sample inference still works by CLT. Small-sample t-tests are only approximate. - *Detect:* Q-Q plot; Jarque-Bera test. - *Fix:* Increase sample size; bootstrap standard errors. **Answer:** The Gauss-Markov assumptions are (1) linearity, (2) full rank, (3) exogeneity, (4) homoscedasticity, (5) no autocorrelation. Exogeneity violations bias $\hat{\beta}$; all others leave $\hat{\beta}$ unbiased but compromise efficiency or inference. Robust standard errors (White, Newey-West) are the workhorse fix for assumptions 4-5 in practice. --- OLS Coefficient Confidence Intervals [medium, Regression] --- https://quantvault.org/problem-1867-ols-coefficient-confidence-intervals.html Q: Consider the simple linear regression model $Y_i = \beta_0 + \beta_1 x_i + \varepsilon_i$ for $i = 1, \ldots, n$, where the errors $\varepsilon_i$ are i.i.d. $N(0, \sigma^2)$. 1. Derive the distribution of the OLS estimator $\hat{\beta}_1$ and use it to construct a confidence interval for $\beta_1$. 2. What assumptions are required for the confidence interval to be valid? What happens if they are violated? A: **How to Think About It:** The OLS estimator $\hat{\beta}_1$ is a linear combination of the $Y_i$'s, which are normal under our assumptions. So $\hat{\beta}_1$ is itself normal. The confidence interval comes from pivoting: take $\hat{\beta}_1$, subtract the true $\beta_1$, divide by the standard error, and you get a $t$-distribution (because we estimate $\sigma^2$ from the residuals). The only real work is computing the variance of $\hat{\beta}_1$. **Key Insight:** The width of the confidence interval is driven by two things: the noise level $\sigma^2$ and the spread of the $x_i$'s. More spread in $x$ means a tighter estimate of the slope -- this is why designed experiments space out their treatment levels. **The Method:** **Step 1: OLS estimator.** In simple linear regression: $$\hat{\beta}_1 = \frac{\sum_{i=1}^n (x_i - \bar{x})(Y_i - \bar{Y})}{\sum_{i=1}^n (x_i - \bar{x})^2} = \frac{S_{xY}}{S_{xx}}$$ where $S_{xx} = \sum (x_i - \bar{x})^2$. **Step 2: Distribution of $\hat{\beta}_1$.** Since $\hat{\beta}_1$ is a linear combination of the $Y_i$'s and each $Y_i \sim N(\beta_0 + \beta_1 x_i, \sigma^2)$: $$\hat{\beta}_1 \sim N\left(\beta_1, \frac{\sigma^2}{S_{xx}}\right)$$ The variance $\sigma^2 / S_{xx}$ makes intuitive sense: more noise ($\sigma^2$ large) or less spread in $x$ ($S_{xx}$ small) means more uncertainty in the slope estimate. **Step 3: Pivot to a $t$-statistic.** We don't know $\sigma^2$, so we estimate it with the residual variance: $$\hat{\sigma}^2 = \frac{1}{n-2} \sum_{i=1}^n (Y_i - \hat{Y}_i)^2 = \frac{\text{RSS}}{n-2}$$ Under the model assumptions, $\hat{\sigma}^2$ is independent of $\hat{\beta}_1$ and $(n-2)\hat{\sigma}^2/\sigma^2 \sim \chi^2_{n-2}$. Therefore: $$t = \frac{\hat{\beta}_1 - \beta_1}{\hat{\sigma}/\sqrt{S_{xx}}} \sim t_{n-2}$$ **Step 4: Confidence interval.** Inverting the pivot: $$\hat{\beta}_1 \pm t_{n-2, \alpha/2} \cdot \frac{\hat{\sigma}}{\sqrt{S_{xx}}}$$ This is the $(1-\alpha)$ confidence interval for $\beta_1$. **Assumptions required:** 1. **Linearity**: $E[Y_i \mid x_i] = \beta_0 + \beta_1 x_i$. If violated, the estimator is biased. 2. **Independence**: The $\varepsilon_i$ are independent. Serial correlation (common in time series) invalidates the standard error formula -- the actual variance of $\hat{\beta}_1$ could be much larger than $\sigma^2/S_{xx}$. 3. **Homoscedasticity**: $\text{Var}(\varepsilon_i) = \sigma^2$ for all $i$. Heteroscedasticity doesn't bias $\hat{\beta}_1$ but makes the standard error wrong. Use heteroscedasticity-robust (White) standard errors in practice. 4. **Normality**: $\varepsilon_i \sim N(0, \sigma^2)$. This is needed for the exact $t$-distribution in finite samples. For large $n$, the CLT gives approximate normality of $\hat{\beta}_1$ even without normal errors. **Practical Considerations:** In finance, assumptions 2 and 3 are almost always violated. Time series residuals are serially correlated and heteroscedastic (volatility clustering). Use Newey-West standard errors for serial correlation and White standard errors for heteroscedasticity. The confidence interval formula stays the same -- only the standard error computation changes. **Answer:** The $(1-\alpha)$ confidence interval for $\beta_1$ is $\hat{\beta}_1 \pm t_{n-2, \alpha/2} \cdot \hat{\sigma}/\sqrt{S_{xx}}$, valid under linearity, independence, homoscedasticity, and normality (the last can be relaxed for large $n$ via CLT). --- Suppressor Variables in Multiple Regression [medium, Regression] --- https://quantvault.org/problem-1884-suppressor-variables-in-multiple-regression.html Q: In a multiple regression $y \sim x_1 + x_2$, both $\hat{\beta}_1$ and $\hat{\beta}_2$ have significant t-statistics. But when you run the simple regression $y \sim x_1$ alone, $\hat{\beta}_1$ is no longer significant. What could cause this? Explain the mechanism and provide a concrete example. A: **How to Think About It:** This is a classic trap in applied regression. Your first instinct might be that something is wrong -- how can a variable become significant only after you add a second predictor? The answer is that $x_2$ is acting as a *suppressor*: it is soaking up variance in $x_1$ that is noise with respect to $y$. Once you strip that noise out by conditioning on $x_2$, the signal in $x_1$ becomes legible. The key is that $x_2$ does not need to correlate with $y$ on its own -- it just needs to correlate with the junk in $x_1$. **Key Insight:** OLS partial coefficients measure the effect of each predictor *after removing* the linear influence of the others. When $x_2$ is correlated with $x_1$, including it re-partitions the variance in $x_1$, and if the piece it removes is noise, the signal-to-noise ratio of $\hat{\beta}_1$ improves. **The Mechanism:** 1. **Simple regression:** $\hat{\beta}_1 = \text{Cov}(y, x_1) / \text{Var}(x_1)$. This estimate uses all variance in $x_1$ -- signal and noise alike -- in its denominator. If $x_1$ contains noise that is correlated with $x_2$ but not with $y$, that noise inflates $\text{Var}(x_1)$ without helping $\text{Cov}(y, x_1)$. The t-statistic is small. 2. **Multiple regression:** The partial coefficient is $$\hat{\beta}_1 = \frac{\text{Cov}(y, x_1) - \hat{\beta}_2 \, \text{Cov}(x_2, x_1)}{\text{Var}(x_1)(1 - r_{12}^2)}$$ where $r_{12}$ is the correlation between $x_1$ and $x_2$. Think of this as: project $x_1$ onto the space orthogonal to $x_2$, and regress $y$ on that residual. The $x_2$-correlated noise has been stripped out. If that noise was the main source of variance in $x_1$, both the numerator and denominator shrink -- but the numerator holds its value while the denominator drops faster, so the t-statistic rises. 3. **Standard error:** The SE of $\hat{\beta}_1$ scales with $1/\sqrt{\text{Var}(x_1)(1-r_{12}^2) \cdot n}$. Removing noise variance reduces the effective denominator, tightening the SE and pushing the t-statistic up. **Concrete Example:** Let $y$ = job performance, $x_1$ = aptitude test score, $x_2$ = test-day anxiety level. - Anxiety is essentially uncorrelated with actual job performance (anxious people are not worse employees). - But anxiety is negatively correlated with test scores: it depresses scores for high-ability candidates. So $x_1$ is a noisy signal of true ability -- the noise comes from anxiety. In the simple regression $y \sim x_1$, the noise blurs the ability-performance relationship and the coefficient is weak. Add $x_2$ (anxiety), and the regression holds anxiety constant, isolating true ability. Now $\hat{\beta}_1$ measures the clean ability effect and becomes significant. Meanwhile $\hat{\beta}_2$ picks up the anxiety-score link, which also shows up as significant because conditioning on ability exposes the anxiety channel. **Practical Considerations:** - Suppression is not a pathology to fix -- it reveals that $x_1$ contains mixed signals. The multiple regression is giving you a more precise answer, not a wrong one. - Watch for this when variables are proxies or composites (test scores, survey indices, PCA components). Any proxy carries noise from its construction process. - In finance: a factor loading on a portfolio might look insignificant in isolation but become significant once a correlated risk factor is controlled for. For example, a value factor might appear insignificant until you control for a quality factor that shares variance. - Suppression can also flip signs (not just significance), which is the more alarming case. If $\hat{\beta}_1$ changes sign when you add $x_2$, check your correlations carefully before trusting any interpretation. **Answer:** $x_2$ is a suppressor variable -- it correlates with $x_1$ but not with $y$, so it absorbs noise in $x_1$. In the multiple regression, the partial coefficient of $x_1$ isolates the signal component, increasing the t-statistic. This is not a statistical artifact; it reflects a genuine improvement in precision from conditioning on a relevant nuisance variable. --- Systematic vs. Idiosyncratic Risk [easy, Finance] --- https://quantvault.org/problem-1885-systematic-vs-idiosyncratic-risk.html Q: Explain the difference between systematic risk and non-systematic (idiosyncratic) risk. How does diversification affect each? What does this imply about which type of risk is priced by the market? A: **How to Think About It:** Think about owning a single stock versus a 500-stock portfolio. The single stock has two kinds of risk: stuff that hits every company (market crashes, rate hikes, recessions) and stuff that is specific to that company (a bad earnings report, a CEO resignation, a product recall). When you add more stocks to the portfolio, the company-specific shocks tend to cancel out -- your neighbor's bad quarter is uncorrelated with your other holdings' bad quarters, so they average away. But the market-wide shocks hit everything simultaneously and cannot be diversified away. That is the essential distinction. **Key Insight:** A well-diversified portfolio eliminates idiosyncratic risk, leaving only systematic risk. Since any investor can diversify at low cost, the market will not pay you extra for bearing idiosyncratic risk -- you chose not to diversify, so you bear that risk for free. **The Method:** **Systematic Risk (Market Risk)** - Affects the entire market or broad asset classes - Cannot be diversified away -- it affects all assets simultaneously - Examples: interest rate changes, recessions, inflation, geopolitical shocks - Measured by $\beta$ in CAPM: $\beta_i = \text{Cov}(R_i, R_m) / \text{Var}(R_m)$ - Priced by the market: investors demand a risk premium $\beta_i (E[R_m] - R_f)$ for bearing it **Non-Systematic Risk (Idiosyncratic / Specific Risk)** - Affects a single firm or narrow sector - Can be diversified away by holding a broad portfolio - Examples: CEO departure, product recall, earnings surprise, litigation - Not priced in equilibrium: since it can be eliminated for free, investors receive no compensation for bearing it **Risk Decomposition:** Total variance for asset $i$ decomposes as: $$\sigma_i^2 = \beta_i^2 \sigma_m^2 + \sigma_{\varepsilon_i}^2$$ where $\beta_i^2 \sigma_m^2$ is the systematic component and $\sigma_{\varepsilon_i}^2$ is the idiosyncratic component. As you build a diversified portfolio, the idiosyncratic terms average toward zero while the systematic terms accumulate. **Expected return (CAPM):** $$E[R_i] = R_f + \beta_i (E[R_m] - R_f)$$ Only $\beta_i$ -- the systematic risk loading -- enters the pricing equation. Idiosyncratic risk $\sigma_{\varepsilon_i}$ does not. **Answer:** Systematic risk is market-wide and cannot be diversified away; it is priced (investors earn a beta-scaled premium for bearing it). Idiosyncratic risk is firm-specific, diversifies away in a large portfolio, and is therefore not priced in equilibrium. --- ACF and PACF of an ARMA(1,1) Process [hard, Time Series] --- https://quantvault.org/problem-1893-acf-and-pacf-of-an-arma-1-1-process.html Q: Consider the ARMA(1,1) process $$X_t - \phi X_{t-1} = \varepsilon_t + \theta \varepsilon_{t-1},$$ where $|\phi| < 1$, $|\theta| < 1$, and $\varepsilon_t \overset{\text{i.i.d.}}{\sim} N(0, \sigma^2)$. 1. Derive $\gamma(h) = \text{Cov}(X_t, X_{t+h})$ for $h \ge 0$ and the autocorrelation function $\rho(h) = \gamma(h)/\gamma(0)$. 2. Show that $\rho(1)$ can be positive or negative depending on $\phi$ and $\theta$, and describe when each case occurs. 3. Sketch how the PACF behaves for an ARMA(1,1) and explain how the signs of $\phi$ and $\theta$ affect short-lag correlations. A: **How to Think About It:** An ARMA(1,1) is the minimal model that captures both momentum (AR) and mean-reversion-after-shock (MA) in a single process. The ACF decays geometrically starting from lag 1 -- that is the AR(1) signature -- but the lag-1 value itself is distorted by the MA term. The PACF, by contrast, does not cut off cleanly like a pure MA; it also decays geometrically. Neither the ACF nor PACF cuts off to zero at any finite lag, which is the diagnostic fingerprint of an ARMA model (versus a pure AR where the PACF cuts off, or a pure MA where the ACF cuts off). **Quick Sanity Checks:** - If $\theta = 0$: reduces to AR(1), so $\rho(h) = \phi^h$ and PACF cuts off after lag 1. - If $\phi = 0$: reduces to MA(1), so $\rho(1) = \theta/(1+\theta^2)$ and $\rho(h) = 0$ for $h \ge 2$. - For $\phi > 0$, $\theta > 0$: both components add positive autocorrelation, so $\rho(1) > 0$. - For $\phi > 0$, $\theta 0$ always. - The factor $(1+\phi\theta) > 0$ when $|\phi\theta| 0$ when $\phi + \theta > 0$ - $\rho(1) 0$ and keeps one sign when $\theta < 0$. Check with $\phi = 0.5,\ \theta = 0.4$: $\rho(1) = 0.692$ and the PACF is $0.692,\ -0.256,\ 0.101,\ -0.040, \dots$, each term about $-0.4 = -\theta$ times the one before. So the two signs play different roles at short lags: $\phi + \theta$ sets the sign of $\rho(1) = \alpha(1)$, $\phi$ sets how slowly the ACF decays after lag 1, and $\theta$ sets how the PACF tails off. Unlike a pure AR(1) (where PACF is zero for $h \ge 2$), the PACF of ARMA(1,1) tails off slowly. Neither the ACF nor PACF has a clean cutoff -- this is how you distinguish an ARMA from a pure AR or MA in practice. **Answer:** $$\gamma(0) = \frac{(1+2\phi\theta+\theta^2)\sigma^2}{1-\phi^2}, \quad \gamma(h) = \phi^{h-1}\gamma(1)\text{ for }h\ge 1, \quad \rho(1) = \frac{(\phi+\theta)(1+\phi\theta)}{1+2\phi\theta+\theta^2}.$$ Sign of $\rho(1)$ is determined by the sign of $\phi + \theta$. Both ACF and PACF tail off geometrically -- no clean cutoff at any lag. --- Bond Duration and Convexity Approximation [medium, Finance] --- https://quantvault.org/problem-1915-bond-duration-and-convexity-approximation.html Q: You hold a fixed-rate bond with face value $F$, annual coupon rate $c$, maturity $T$ years, and yield-to-maturity $y$ (annual compounding). 1. Write the bond price $P(y)$ as a discounted cash-flow sum. 2. Define Macaulay duration $D_{\text{Mac}}$, modified duration $D_{\text{mod}}$, and convexity $C$ in terms of the derivatives $\frac{dP}{dy}$ and $\frac{d^{2}P}{dy^{2}}$. 3. Derive the duration-convexity approximation for the relative price change $\frac{\Delta P}{P}$ under a small yield shock $\Delta y$. When do you expect this approximation to be accurate, and when does it break down or become biased? A: **How to Think About It:** This is the bread and butter of fixed-income risk. A bond is just a stream of known cash flows, so its price is a present-value sum. Duration tells you how sensitive that price is to yield changes -- it is the first-order (linear) effect. Convexity captures the curvature -- the second-order correction. Before writing any formulas, you should know: duration always makes the price move look worse than it really is for a plain vanilla bond, because convexity is positive and works in the bondholder's favor. A senior trader would say "duration is the hedge ratio, convexity is the P&L you pick up when yields move big." **Quick Sanity Checks:** - A zero-coupon bond has Macaulay duration equal to $T$ (all cash flow arrives at maturity). - Modified duration is just Macaulay duration discounted by one period: $D_{\text{mod}} = D_{\text{Mac}} / (1 + y)$. - Convexity is always positive for a standard fixed-rate bond (the price-yield curve is convex downward toward the yield axis). - For small $\Delta y$, the duration term dominates. For large $\Delta y$, ignoring convexity leads to significant error. **Derivation:** **(i) Bond price as a discounted cash-flow sum:** The bond pays coupon $cF$ at times $t = 1, 2, \ldots, T$ and returns the face $F$ at time $T$. Discounting each cash flow at yield $y$: $$P(y) = \sum_{t=1}^{T} \frac{cF}{(1+y)^{t}} + \frac{F}{(1+y)^{T}}$$ This can also be written as: $$P(y) = cF \cdot \frac{1 - (1+y)^{-T}}{y} + \frac{F}{(1+y)^{T}}$$ The first term is the annuity value of coupons; the second is the present value of the principal repayment. **(ii) Duration and convexity from derivatives of $P(y)$:** Take the first derivative: $$\frac{dP}{dy} = -\sum_{t=1}^{T} \frac{t \cdot cF}{(1+y)^{t+1}} - \frac{T \cdot F}{(1+y)^{T+1}}$$ **Macaulay duration** is the weighted-average time to cash flows, with weights proportional to PV of each cash flow: $$D_{\text{Mac}} = \frac{1}{P} \sum_{t=1}^{T} t \cdot \frac{CF_t}{(1+y)^{t}}$$ where $CF_t = cF$ for $t 200$ bps), third- and higher-order terms become non-negligible and the approximation degrades. The direction of the bias depends on the sign of the move: the leading omitted term is $\frac{1}{6}\frac{d^{3}P}{dy^{3}}(\Delta y)^{3}$ with $\frac{d^{3}P}{dy^{3}} < 0$ for a vanilla bond, so for large yield *increases* the parabola **overstates** the true price, while for large yield *decreases* it **understates** the true price. (Check with $P(y) = (1+y)^{-10}$ at $y = 5\%$: for $\Delta y = +500$ bps the quadratic approximation gives $0.398$ vs true $0.386$ -- overstates; for $\Delta y = -500$ bps it gives $0.983$ vs true $1.000$ -- understates.) A common misstatement is that the approximation "always underestimates the true price": that is a property of the duration-only (linear) approximation, which positive convexity corrects, and it does not carry over to the duration-convexity (quadratic) approximation. - For bonds with embedded options (callables, putables), the price-yield relationship is not smoothly convex -- convexity can turn negative, and the Taylor expansion around the current yield can be a poor local approximation. - At very low yields near zero, a given basis-point shock is a larger *relative* move in yield, so higher-order terms matter more. **Practical Interpretation:** On a trading desk, you hedge the duration risk (first order) and then think about convexity as the residual P&L driver. If you are long convexity, you benefit from large moves in either direction -- the convexity term $\frac{1}{2} C (\Delta y)^{2}$ is always positive. That is why traders say "long convexity = long gamma" -- it is the bond analogue of being long options. **Answer:** $$P(y) = \sum_{t=1}^{T} \frac{CF_t}{(1+y)^{t}}, \quad D_{\text{mod}} = -\frac{1}{P}\frac{dP}{dy}, \quad C = \frac{1}{P}\frac{d^{2}P}{dy^{2}}$$ $$\frac{\Delta P}{P} \approx -D_{\text{mod}} \cdot \Delta y + \frac{1}{2} C \cdot (\Delta y)^{2}$$ The approximation is accurate for small yield shocks and degrades on large moves as the omitted higher-order terms grow: on a plain vanilla bond the quadratic understates the true price for large yield falls and overstates it for large yield rises. --- Breusch-Pagan Heteroskedasticity Test [hard, Statistics] --- https://quantvault.org/problem-1920-breusch-pagan-heteroskedasticity-test.html Q: Consider the OLS regression $y_t = x_t' \beta + u_t$, where the error variance may depend on observable variables: $\text{Var}(u_t) = \sigma^2 h(z_t' \gamma)$ with $h(z_t' \gamma) = 1 + z_t' \gamma$. 1. Derive the Breusch-Pagan Lagrange multiplier (LM) test statistic using the auxiliary regression of squared OLS residuals $\hat{u}_t^2$ on $z_t$. Show that the statistic can be written in terms of the $R^2$ of the auxiliary regression. 2. State the asymptotic distribution of the test statistic under $H_0: \gamma = 0$ (homoskedasticity). 3. Discuss finite-sample adjustments and when the standard BP test may perform poorly. A: **How to Think About It:** OLS gives you consistent coefficient estimates even with heteroskedasticity, but your standard errors and test statistics are wrong. Before you reach for robust standard errors, it is worth testing whether heteroskedasticity is actually present. The Breusch-Pagan test is the simplest diagnostic: regress the squared residuals on the suspected drivers of variance and check if there is any explanatory power. If squared residuals are just noise, the $R^2$ of that regression should be near zero. The LM test formalizes this intuition. **Key Insight:** Under homoskedasticity, $\hat{u}_t^2$ should be unrelated to any observable $z_t$. The BP statistic is simply $nR^2$ from the auxiliary regression of $\hat{u}_t^2$ on $z_t$, which follows $\chi^2(q)$ under the null. **The Method:** **Step 1: Run the primary OLS regression.** Estimate $y_t = x_t' \beta + u_t$ by OLS and obtain residuals $\hat{u}_t$. **Step 2: Construct the auxiliary regression.** Under $H_0: \gamma = 0$, the variance is constant at $\sigma^2$. Breusch and Pagan consider the auxiliary regression: $$\frac{\hat{u}_t^2}{\hat{\sigma}^2} = 1 + z_t' \gamma + \text{error}$$ where $\hat{\sigma}^2 = \frac{1}{n} \sum_{t=1}^n \hat{u}_t^2$. Equivalently, regress $\hat{u}_t^2$ on a constant and $z_t$. **Step 3: Derive the LM statistic.** The score (Lagrange multiplier) test for $H_0: \gamma = 0$ in the normal linear heteroskedasticity model reduces to half the explained sum of squares from the auxiliary regression. This simplifies to: $$\text{BP} = \frac{1}{2} \text{ESS}_{\text{aux}} = \frac{n \cdot R_{\text{aux}}^2}{1}$$ More precisely, let $R_{\text{aux}}^2$ be the $R^2$ from regressing $\hat{u}_t^2$ on $z_t$ (with intercept). Then: $$\text{BP} = \frac{n \cdot R_{\text{aux}}^2}{1} \quad \text{(original BP form: } \text{ESS}_{\text{aux}} / 2\text{)}$$ The Koenker (1981) studentized version, which is more robust to non-normality, uses: $$\text{BP}^{*} = n \cdot R_{\text{aux}}^2$$ where the auxiliary regression is simply $\hat{u}_t^2$ on a constant and $z_t$, with no division by $\hat{\sigma}^2$. **Step 4: Asymptotic distribution.** Under $H_0$ and standard regularity conditions: $$\text{BP} \xrightarrow{d} \chi^2(q)$$ where $q = \dim(z_t)$ is the number of variance regressors. Reject $H_0$ at level $\alpha$ if $\text{BP} > \chi^2_{1-\alpha}(q)$. **Practical Considerations:** - **Non-normality.** The original BP test assumes normally distributed errors. If errors are heavy-tailed, the original form can over-reject. Use the Koenker studentized version ($n R^2$ without dividing residuals by $\hat{\sigma}^2$), which is robust to non-normality. - **Functional form.** BP tests for a specific linear form of heteroskedasticity ($h = 1 + z'\gamma$). White's test is a more general alternative that includes squares and cross-products of regressors. - **Small samples.** The $\chi^2$ approximation can be poor for small $n$. An $F$-test version of the auxiliary regression is often preferred: $F = \frac{R_{\text{aux}}^2 / q}{(1 - R_{\text{aux}}^2)/(n - q - 1)}$. - **Choice of $z_t$.** Typically $z_t$ includes the fitted values $\hat{y}_t$ or the original regressors $x_t$. Using $\hat{y}_t$ tests whether variance scales with the level of $y$; using $x_t$ tests which specific variables drive heteroskedasticity. **Answer:** The BP test statistic is $nR^2$ from the auxiliary regression of squared OLS residuals on the suspected heteroskedasticity drivers $z_t$. Under $H_0$ (homoskedasticity), it follows $\chi^2(q)$ where $q = \dim(z_t)$. For robustness to non-normality, use the Koenker studentized version. In small samples, prefer the $F$-test form of the auxiliary regression. --- Cross-Sectional Factor Model Estimation and Diagnostics [medium, Regression] --- https://quantvault.org/problem-1937-cross-sectional-factor-model-estimation-and-diagnostics.html Q: Daily stock excess returns $r_t \in \mathbb{R}^N$ follow a linear factor model: $$r_t = X_t \beta_t + \varepsilon_t$$ where $X_t$ is an $N \times K$ matrix of standardized exposures to style factors (including Value), $\beta_t$ is the $K \times 1$ vector of factor returns on day $t$, and $\varepsilon_t$ is the vector of idiosyncratic errors. 1. Describe how to estimate $\beta_t$ via cross-sectional OLS each day and how to compute $t$-statistics for the factor return estimates over a multi-day horizon. 2. Propose a concrete procedure to detect multicollinearity among the factor exposures and to identify unstable factor loadings over time. 3. Identify one structural drawback of a purely linear factor model and describe how you would test for the presence of nonlinearity in the return-factor relationship. A: **How to Think About It:** This is the bread and butter of quantitative equity research -- the Fama-MacBeth two-pass approach and its diagnostics. The key idea: each day you run a cross-sectional regression of that day's stock returns on their factor exposures to extract implied factor returns. Then you treat the resulting time series of factor returns as data and ask whether each factor earns a statistically significant premium. The subtlety is that a single day's cross-sectional regression is noisy (hundreds of stocks, but the $R^2$ is typically low), so you need to aggregate across many days and be careful about how you compute standard errors. **Key Insight:** The cross-sectional regression gives you one $\hat{\beta}_t$ per day. Statistical significance comes from the time-series behavior of the $\hat{\beta}_t$ sequence, not from any single day's regression. **The Method:** *Part (i): Estimation and $t$-statistics* 1. Each day $t$, run the cross-sectional OLS regression of $r_t$ on $X_t$: $$\hat{\beta}_t = (X_t^\top X_t)^{-1} X_t^\top r_t$$ This gives you a $K$-vector of estimated factor returns for that day. 2. Collect the time series $\{\hat{\beta}_t\}_{t=1}^T$ over $T$ trading days. 3. For each factor $k$, compute the sample mean and standard error: $$\bar{\beta}_k = \frac{1}{T} \sum_{t=1}^T \hat{\beta}_{k,t}, \quad \text{SE}(\bar{\beta}_k) = \frac{\hat{\sigma}_k}{\sqrt{T}}$$ where $\hat{\sigma}_k$ is the sample standard deviation of $\{\hat{\beta}_{k,t}\}$. 4. The $t$-statistic is: $$t_k = \frac{\bar{\beta}_k}{\text{SE}(\bar{\beta}_k)} = \frac{\bar{\beta}_k \sqrt{T}}{\hat{\sigma}_k}$$ 5. If the daily factor returns are serially correlated, use Newey-West standard errors with an appropriate lag window instead of the plain $\hat{\sigma}_k / \sqrt{T}$. *Part (ii): Multicollinearity and instability diagnostics* - **Multicollinearity detection:** - Compute the condition number of $X_t^\top X_t$ each day. A condition number above 30 signals problematic collinearity. - Equivalently, compute variance inflation factors (VIFs) for each factor. A VIF above 5-10 indicates that the factor's exposure is nearly a linear combination of others. - Track the eigenvalue spectrum of $X_t^\top X_t$ over time. If the smallest eigenvalue is close to zero relative to the largest, certain factor return estimates will be unstable. - **Unstable loadings detection:** - Run rolling-window regressions (e.g., 60-day windows) and plot the time series of each $\hat{\beta}_{k,t}$. Large swings or regime changes in the factor return series indicate instability. - Apply a formal structural break test (e.g., CUSUM or Bai-Perron) to each factor's return series. - Compute the rolling correlation between factors. If correlations between factors change substantially over time, the model may be misspecified or the factors may be redundant in certain regimes. *Part (iii): Nonlinearity* - **Structural drawback:** A linear model assumes that a one-unit increase in, say, Value exposure always has the same marginal effect on returns, regardless of the level of Value or interactions with other factors. In practice, factor returns often exhibit threshold effects (e.g., Value works differently in the top decile vs. bottom decile) and interaction effects (e.g., cheap stocks with strong momentum behave differently from cheap stocks with weak momentum). - **Testing for nonlinearity:** - Add squared factor exposure terms $X_{k,t}^2$ or interaction terms $X_{j,t} \cdot X_{k,t}$ to the cross-sectional regression and test whether these additional coefficients are jointly significant using an $F$-test. - Alternatively, sort stocks into quantile buckets by factor exposure and compare the actual mean return per bucket to the linear model's prediction. If the relationship is linear, the residuals across buckets should be roughly zero; systematic curvature indicates nonlinearity. - A Ramsey RESET test applied to the cross-sectional regression is another standard diagnostic: regress residuals on powers of the fitted values and test for significance. **Practical Considerations:** - Because exposures $X_t$ are standardized, the estimated $\hat{\beta}_{k,t}$ has a natural interpretation: it is the return spread (in the same units as $r_t$) between a stock one standard deviation above the mean on factor $k$ and a stock at the mean, holding other factors constant. - Outlier stocks can dominate a single day's cross-sectional regression. Winsorizing returns or exposures at the 1st/99th percentile each day is standard practice. - In production, GLS (using an estimated residual covariance) often replaces OLS to improve efficiency, but OLS is the starting point. **Answer:** Estimate factor returns daily via cross-sectional OLS, then compute Fama-MacBeth $t$-statistics from the time series of daily estimates (with Newey-West corrections if needed). Diagnose multicollinearity with VIFs or condition numbers and instability with rolling-window analysis or structural break tests. The main linear limitation is the assumption of constant marginal effects; test for nonlinearity by adding polynomial or interaction terms and checking their joint significance. --- Detecting the Direction of Time in a Price Series [medium, Time Series] --- https://quantvault.org/problem-1945-detecting-the-direction-of-time-in-a-price-series.html Q: You are handed a time series of a stock's daily closing prices, but someone may have reversed the time axis -- flipping the series end-to-end so the last day appears first. Your job is to figure out whether the series is running forward or backward. Here is the catch: if the price followed a pure Brownian motion (i.e., increments are i.i.d. Gaussian), the distribution of the path looks identical forward and backward. So you cannot rely on anything that holds for a symmetric random walk. What statistical properties of real financial data break time-reversibility, and how would you use them to detect whether the series has been flipped? A: **How to Think About It:** Pure Brownian motion is time-reversible -- if you reverse the path, the joint distribution of increments is unchanged. So there is no hope of detecting a reversal from a true GBM. But real markets are not GBM. They have several well-documented asymmetries that break time-symmetry. The question is really asking: what features of real price data look different when you play the tape backward? If you can name one such feature and describe a concrete test, you pass. If you can name three and explain why each works, you ace it. **Key Insight:** The leverage effect -- the empirical fact that negative returns increase future volatility more than positive returns of the same magnitude -- is the cleanest time-asymmetric signature in real equity data. In reversed time, this relationship flips sign, giving you a simple statistical test. **The Method:** 1. **Compute log returns** $r_t = \log(P_t / P_{t-1})$ from the given series. 2. **Leverage effect test (primary).** Compute the cross-correlation between current returns and future squared returns: $$\rho = \text{Corr}(r_t, r_{t+1}^2)$$ In real forward-time equity data, this correlation is negative (roughly $-0.05$ to $-0.15$ for daily data). A negative return today predicts higher volatility tomorrow. If the series is reversed, the relationship flips: you would see $\text{Corr}(r_t, r_{t+1}^2) > 0$ because what was "future volatility responding to past returns" becomes "past volatility predicting future returns." Decision rule: $\rho 0$ suggests reversed. 3. **Volatility clustering asymmetry (supporting).** Compute the autocorrelation of squared returns $\text{Corr}(r_t^2, r_{t+k}^2)$ for several lags $k$. Volatility clustering itself (positive autocorrelation of $r_t^2$) is symmetric -- it looks the same forward or backward. But the decay profile can be asymmetric: volatility tends to spike suddenly and decay slowly (fast rise, slow fall). In reversed time, the pattern becomes slow rise, fast fall. Measure the skewness of the changes in realized volatility: $\Delta \sigma_t = \sigma_t - \sigma_{t-1}$. In forward time, $\text{Skew}(\Delta \sigma_t)$ is typically positive (sharp upward jumps); reversed, it becomes negative. 4. **Bid-ask bounce (if tick data).** At the tick level, prices tend to bounce between bid and ask, creating negative first-order autocorrelation in returns. This autocorrelation structure is time-symmetric by itself, but the pattern of trades hitting bid vs. ask is not: order flow has serial correlation (buy trades cluster, sell trades cluster). In reversed time, the lead-lag relationship between order flow and price changes flips. 5. **Lead-lag across assets (if multi-asset data).** Large-cap stocks lead small-cap stocks. If you reverse time, the lagging asset appears to lead. Compute cross-correlations at positive and negative lags: in forward time, the large-cap return at $t$ predicts the small-cap return at $t+1$ but not vice versa. **Practical Considerations:** - The leverage effect test is the strongest single test for daily equity data. It requires a few hundred observations to get a statistically significant signal. - For very short series (under 100 observations), these tests may lack power. You may need to combine multiple tests. - These tests assume equity-like dynamics. For currencies or commodities, the leverage effect may be weaker or reversed (some commodities show an inverse leverage effect where positive shocks increase volatility). - Volatility clustering alone does not help -- it is time-symmetric. You need an asymmetric feature. - All of these tests fail on pure GBM data, by construction. The question is specifically about real financial data. **Answer:** The primary test is to compute $\text{Corr}(r_t, r_{t+1}^2)$. If this is negative, the series is likely in forward time (leverage effect: negative returns predict higher future volatility). If positive, the series has probably been reversed. Supporting evidence comes from the asymmetric volatility dynamics (sharp spikes, slow decay) and, if available, lead-lag relationships across assets. None of these tests work on pure Brownian motion -- the question hinges on stylized facts of real markets. --- Estimating the Hurst Exponent via Rescaled Range Analysis [hard, Time Series] --- https://quantvault.org/problem-1956-estimating-the-hurst-exponent-via-rescaled-range-analysis.html Q: You observe a time series $x_1, x_2, \ldots, x_T$ and want to determine whether it exhibits mean-reversion ($H 0.5$). 1. Define the rescaled range statistic $R/S$ for a block of $n$ observations. Show how the scaling relationship $E[R/S] \sim c \, n^H$ gives you the Hurst exponent $H$. 2. Provide a step-by-step algorithm to estimate $H$ from data. Be specific about how you choose block sizes, how you compute $R/S$ for each block, and how you extract $H$ from the log-log regression. 3. Give two concrete reasons -- with mathematical intuition -- why heavy tails or volatility clustering can bias $\hat{H}$ upward, making an uncorrelated series look persistent. A: **How to Think About It:** The Hurst exponent measures how the range of partial sums scales with window length. For an i.i.d. process, the range of a random walk of $n$ steps grows like $\sqrt{n}$, so $H = 0.5$. If the series is persistent (positive autocorrelation at long lags), the walk drifts further than a random walk and $H > 0.5$. If it mean-reverts, the walk stays compressed and $H 0.5$: persistence (trending behavior). - $\hat{H} 0.5$. The bias can be substantial: simulations with $t_3$-distributed i.i.d. increments routinely produce $\hat{H} \approx 0.6\text{-}0.65$. *Reason 2: Volatility clustering creates pseudo-persistence in ranges.* Consider a GARCH(1,1) process where the increments $x_t = \sigma_t \epsilon_t$ are uncorrelated but $\sigma_t$ is serially dependent. Within a block that happens to fall in a high-volatility regime, both $R$ and $S$ are large. Within a low-volatility block, both are small. The problem is that $R/S$ is not regime-invariant: within a single block, if volatility trends upward, the early low-volatility observations contribute little to $S$ but the late high-volatility observations drive $R$ far from zero. The rescaled range of a block spanning a volatility regime change is larger than for a block with constant volatility -- even though the standardized increments $\epsilon_t$ are i.i.d. Across block sizes, larger blocks are more likely to span regime changes, which inflates $R/S$ at larger $n$ and steepens the log-log slope. The result: $\hat{H} > 0.5$ for data that has zero autocorrelation in levels. **Answer:** Estimate $H$ by computing the average rescaled range $\overline{(R/S)}$ at geometrically spaced block sizes, then regressing $\log \overline{(R/S)}$ on $\log n$ -- the slope is $\hat{H}$. Heavy tails bias $\hat{H}$ upward because extreme observations inflate $R$ faster than $S$. Volatility clustering biases $\hat{H}$ upward because blocks spanning regime changes have inflated $R/S$ ratios, steepening the log-log slope. Both effects make uncorrelated series appear persistent, which is why raw $R/S$ analysis must be supplemented with controls (e.g., shuffled-data benchmarks or DFA) when applied to financial data. --- Estimation with Equicorrelated Normal Samples [hard, Statistics] --- https://quantvault.org/problem-1957-estimation-with-equicorrelated-normal-samples.html Q: You have $n$ observations $X_1, \ldots, X_n$ drawn from a $N(\mu, 1)$ distribution, but they are not independent -- every pair has the same correlation $r$, so the covariance matrix is: $$\Sigma = (1-r)I + r\mathbf{1}\mathbf{1}^\top$$ where $\mathbf{1}$ is the all-ones vector. (a) How would you estimate $\mu$? Is the sample mean $\bar{X}$ still the best linear unbiased estimator? (b) Derive the test statistic for testing $H_0: \mu = \mu_0$ against $H_1: \mu \neq \mu_0$. How does correlation affect the power of this test? (c) What is the valid range of $r$ for this covariance structure to be well-defined? A: **How to Think About It:** Equicorrelated samples come up constantly in finance -- think of returns from stocks in the same sector, or repeated measurements on the same day from a mean-reverting process. The key question is: does correlation destroy the unbiasedness of the sample mean, or just inflate its variance? The answer to (a) is encouraging: $\bar{X}$ is still unbiased. But the effective sample size shrinks dramatically when $r$ is large -- 1000 highly correlated observations can be worth less than 10 independent ones. The test statistic in (b) must account for this or you will massively overstate your significance. **Quick Estimate for (b):** Suppose $n = 100$, $r = 0.5$. Effective sample size: $n_{\text{eff}} = 100/(1 + 99 \times 0.5) \approx 100/50.5 \approx 2$. If you naively use $\sqrt{n} = 10$ in your z-statistic, you overstate it by a factor of $\sqrt{50} \approx 7$. This is why ignoring correlation in clustered data leads to spurious significance. **Approach:** Use linearity of expectation for unbiasedness, compute $\text{Var}(\bar{X})$ directly from the covariance structure, then standardize. **Formal Solution:** **(a) Estimating $\mu$:** Unbiasedness of $\bar{X}$: $$E[\bar{X}] = \frac{1}{n}\sum_{i=1}^n E[X_i] = \mu$$ Correlation does not affect the mean, only the variance. So $\bar{X}$ is unbiased regardless of $r$. Variance of $\bar{X}$: $$\text{Var}(\bar{X}) = \frac{1}{n^2} \sum_{i,j} \text{Cov}(X_i, X_j) = \frac{1}{n^2}\left[n \cdot 1 + n(n-1)r\right] = \frac{1 + (n-1)r}{n}$$ Is $\bar{X}$ the best linear unbiased estimator (BLUE)? By symmetry of the equicorrelated structure, the Gauss-Markov theorem applied to $\Sigma$ yields that the GLS estimator is $\hat{\mu}_{\text{GLS}} = (\mathbf{1}^\top \Sigma^{-1} \mathbf{1})^{-1} \mathbf{1}^\top \Sigma^{-1} X$. For the equicorrelated structure, $\Sigma^{-1} \mathbf{1} \propto \mathbf{1}$, so GLS reduces to OLS, meaning $\bar{X}$ is in fact the BLUE. **(b) Test statistic for $H_0: \mu = \mu_0$:** Since $\bar{X}$ is a linear combination of jointly normal variables, $\bar{X} \sim N(\mu, (1+(n-1)r)/n)$ exactly. Under $H_0$: $$T = \frac{\bar{X} - \mu_0}{\sqrt{(1+(n-1)r)/n}} \sim N(0,1)$$ The effective sample size is: $$n_{\text{eff}} = \frac{n}{1 + (n-1)r}$$ When $r > 0$: $n_{\text{eff}} n$ -- negative correlation adds information. This is the benefit of diversification. **(c) Valid range of $r$:** The matrix $\Sigma = (1-r)I + r\mathbf{1}\mathbf{1}^\top$ must be positive definite (PD). Its eigenvalues are: - $1 - r$ with multiplicity $n - 1$ (eigenvectors orthogonal to $\mathbf{1}$) - $1 + (n-1)r$ with multiplicity $1$ (eigenvector $\mathbf{1}/\sqrt{n}$) For PD, both must be positive: $$1 - r > 0 \implies r 0 \implies r > -\frac{1}{n-1}$$ So the valid range is: $$r \in \left(-\frac{1}{n-1},\; 1\right)$$ At $r = -1/(n-1)$, the matrix is singular -- all observations sum to a constant almost surely, so you learn nothing by taking more draws. **Answer:** - (a) $\bar{X}$ is the BLUE for $\mu$ with variance $(1+(n-1)r)/n$. - (b) $T = (\bar{X} - \mu_0)/\sqrt{(1+(n-1)r)/n} \sim N(0,1)$ under $H_0$. - (c) $r \in (-1/(n-1),\; 1)$. --- From ML Signals to Live Trades [medium, Finance] --- https://quantvault.org/problem-1967-from-ml-signals-to-live-trades.html Q: An ML model outputs discrete trading signals: $-1$ (short), $0$ (neutral), and $+1$ (long). 1. Describe how you would convert these signals into actual trade executions -- including position sizing, order type, and execution timing. 2. What risk management controls would you layer on top? Discuss at least four distinct risk dimensions. A: **How to Think About It:** The signal is just the starting point -- the hard part is everything between "the model says buy" and "you actually have a position on." In practice, you need a position management layer that translates discrete signals into target positions, an execution layer that gets you there efficiently, and a risk layer that overrides everything when things go wrong. An interviewer asking this wants to see that you understand the full pipeline, not just the model. **Key Insight:** Never conflate the signal with the trade. The signal tells you direction; position sizing, execution, and risk controls determine how much you actually put on and whether you survive to trade tomorrow. **The Method:** **Step 1: Signal to Target Position** - Map each signal to a target notional: $\text{target} = \text{signal} \times \text{base\_size}$ - The base size can be fixed or scaled by conviction. If the model outputs continuous scores internally (before discretizing to $\{-1, 0, 1\}$), use those raw scores for finer sizing - Volatility-scale the base size: $\text{base\_size} = \text{target\_risk} / \sigma_{\text{realized}}$, where $\sigma_{\text{realized}}$ is the recent realized volatility of the instrument. This keeps your dollar risk roughly constant across regimes **Step 2: Compute the Trade Delta** - $\Delta = \text{target\_position} - \text{current\_position}$ - Only trade if $|\Delta|$ exceeds a minimum threshold (to avoid churning on noise) **Step 3: Execution** - Use limit orders pegged to the mid or near the touch to minimize market impact - For larger deltas, slice the order across time (e.g., TWAP or VWAP over a window) - Set a maximum participation rate (e.g., no more than 5-10% of volume) - If the signal decays quickly, lean toward aggressive execution; if it is slow-decay, be patient **Step 4: Risk Management Controls** 1. **Position limits:** Hard cap on notional per instrument and gross/net portfolio exposure. If any limit is hit, new signals in that direction are ignored until exposure decreases 2. **Stop losses:** Per-position stop (e.g., exit if unrealized loss exceeds 2% of notional) and portfolio-level drawdown stop (e.g., flatten everything if portfolio drops 5% intraday) 3. **Volatility scaling:** Continuously adjust position sizes as realized vol changes. In a vol spike, your existing positions auto-shrink in target terms, prompting partial exits. Formula: $\text{adjusted\_size} = \text{base\_size} \times (\sigma_{\text{target}} / \sigma_{\text{current}})$ 4. **Correlation monitoring:** Track rolling pairwise correlations across positions. If correlations spike (e.g., during a risk-off event), your diversification benefit collapses. Trigger position reduction when portfolio concentration (e.g., top eigenvalue of the correlation matrix) exceeds a threshold 5. **Signal staleness:** If the model has not refreshed its signal for an unexpectedly long time (data feed issue, model crash), begin unwinding toward neutral. Never hold a position on a stale signal 6. **Execution quality monitoring:** Track slippage relative to arrival price. If execution costs are eating a large fraction of expected alpha, throttle trading or widen the minimum trade threshold **Practical Considerations:** - Transaction costs are the silent killer. A signal with $2$ bps of alpha per trade is worthless if you pay $3$ bps in spread and fees. Always model net-of-cost PnL - The risk controls should be hierarchical: position-level stops first, then portfolio-level, then firm-level. Each layer acts independently - In live trading, latency matters: the risk system must be able to kill positions faster than the alpha system can open them **Answer:** Convert signals to target positions using volatility-scaled sizing, execute via limit orders with participation constraints, and layer on position limits, stop losses, vol scaling, correlation monitoring, signal staleness checks, and execution quality tracking. The risk framework should be hierarchical and able to override the alpha signal at every level. --- Ljung-Box Test for Serial Correlation in Returns [hard, Statistics] --- https://quantvault.org/problem-1997-ljung-box-test-for-serial-correlation-in-returns.html Q: Consider a zero-mean return series $\{r_t\}_{t=1}^{n}$. Define the sample autocorrelations: $$\hat{\rho}(h) = \frac{\sum_{t=h+1}^{n} r_t \, r_{t-h}}{\sum_{t=1}^{n} r_t^2}$$ (i) Write down the Ljung-Box portmanteau statistic $Q(m)$ and state its asymptotic null distribution under the assumption that $\{r_t\}$ is i.i.d. noise. (ii) Discuss the bias-variance tradeoff in choosing the number of lags $m$. How does conditional heteroskedasticity (e.g., GARCH effects) affect the validity of the standard Ljung-Box test? Propose a HAC-adjusted variant that remains valid under heteroskedasticity. A: **How to Think About It:** The Ljung-Box test is the workhorse for checking whether a return series has any predictable structure -- serial correlation. On a trading desk, this is the first thing you run on residuals from any model: if the autocorrelations are jointly significant, your model is missing something. The test aggregates autocorrelations across multiple lags into a single statistic, which is more powerful than checking each lag individually. **Key Insight:** The standard Ljung-Box test assumes i.i.d. returns under the null. But financial returns are almost never i.i.d. -- they have volatility clustering (GARCH effects). This heteroskedasticity inflates the variance of sample autocorrelations, causing the standard test to over-reject. You need a HAC correction to get reliable p-values. **The Method:** **(i) The Ljung-Box Statistic:** The statistic is: $$Q(m) = n(n+2) \sum_{h=1}^{m} \frac{\hat{\rho}(h)^2}{n - h}$$ Under $H_0$: $\{r_t\}$ is i.i.d. with finite variance, as $n \to \infty$: $$Q(m) \xrightarrow{d} \chi^2(m)$$ This is an improvement over the original Box-Pierce statistic $Q_{BP} = n \sum_{h=1}^{m} \hat{\rho}(h)^2$, which has worse finite-sample properties. The Ljung-Box correction factor $(n+2)/(n-h)$ accounts for the downward bias in $\hat{\rho}(h)^2$ as an estimator of $\rho(h)^2$. Reject $H_0$ at level $\alpha$ if $Q(m) > \chi^2_{1-\alpha}(m)$. **(ii) Choosing $m$ -- Bias-Variance Tradeoff:** - **Too small $m$:** The test has good power against low-order serial correlation but misses higher-order patterns. If the true autocorrelation is at lag $h > m$, the test is blind to it. - **Too large $m$:** Including many lags where $\rho(h) = 0$ dilutes the signal. Each zero-autocorrelation lag adds noise ($\hat{\rho}(h)^2$ has expectation $\approx 1/n$ under the null), reducing power against concentrated alternatives. - **Rules of thumb:** Common choices are $m \approx \ln(n)$ (parsimonious) or $m \approx n^{1/3}$ (data-driven rate). For daily returns with $n \approx 250$, $m = 5$ to $20$ is typical. For intraday data, look at more lags. **Effect of Heteroskedasticity:** Under conditional heteroskedasticity (e.g., $r_t = \sigma_t \epsilon_t$ with $\epsilon_t$ i.i.d. but $\sigma_t$ time-varying), the returns are uncorrelated but not independent. The problem: under GARCH-type dynamics, $$\text{Var}(\hat{\rho}(h)) \neq \frac{1}{n}$$ Specifically, $\text{Var}(\hat{\rho}(h))$ depends on the fourth-moment structure (kurtosis) of the returns. Since financial returns are heavy-tailed, $\text{Var}(\hat{\rho}(h)) > 1/n$, and the standard $\chi^2(m)$ critical values are too small. The test over-rejects, producing spurious evidence of serial correlation. **HAC-Adjusted Variant:** Replace the standard normalization with a heteroskedasticity-consistent variance estimate. Define: $$Q^{*}(m) = n \sum_{h=1}^{m} \frac{\hat{\rho}(h)^2}{\hat{v}(h)}$$ where $\hat{v}(h)$ is a consistent estimator of $n \cdot \text{Var}(\hat{\rho}(h))$ under heteroskedasticity: $$\hat{v}(h) = \frac{\sum_{t=h+1}^{n} r_t^2 \, r_{t-h}^2}{\left(\sum_{t=1}^{n} r_t^2\right)^2 / n}$$ This is the approach of Lobato, Nankervis, and Savin (2001). Under GARCH-type heteroskedasticity and the null of no serial correlation, $Q^{*}(m) \xrightarrow{d} \chi^2(m)$. An alternative is the approach of Fisher and Gallagher, who use a wild bootstrap to obtain critical values: resample $r_t^{*} = r_t \cdot \eta_t$ where $\eta_t$ are i.i.d. Rademacher ($\pm 1$), preserving the heteroskedastic structure while destroying serial correlation. **Practical Considerations:** - Always plot the sample ACF before running the test -- visual inspection catches patterns the test might miss. - For model residuals (e.g., after fitting ARMA), the degrees of freedom adjust: $Q(m) \sim \chi^2(m - p - q)$. - In practice, run both the standard and HAC-adjusted tests. If they disagree, trust the HAC version. **Answer:** The Ljung-Box statistic is $Q(m) = n(n+2)\sum_{h=1}^{m} \hat{\rho}(h)^2/(n-h) \sim \chi^2(m)$ under i.i.d. null. Choose $m$ balancing detection of high-lag patterns (large $m$) against power dilution (small $m$). Heteroskedasticity inflates autocorrelation variance, causing over-rejection; fix this with a HAC-adjusted variant that scales each $\hat{\rho}(h)^2$ by its heteroskedasticity-robust variance estimate. --- Lookahead Bias From Universe Membership Leakage [medium, Machine Learning] --- https://quantvault.org/problem-2001-lookahead-bias-from-universe-membership-leakage.html Q: You are building an alpha signal using a feature $x_i(t)$ to predict the next-day return $r_i(t+1)$. The feature itself never directly uses $r_i(t+1)$, so at first glance it looks clean. But you discover that $x_i(t)$ was constructed using a cross-sectional normalization that references the day-$(t+1)$ universe membership list $U(t+1)$ -- for example, it excludes stocks that were halted on day $t+1$, information that is only known at $t+1$. 1. Explain why this creates lookahead bias even though $r_i(t+1)$ never appears in the formula for $x_i(t)$. 2. Design an explicit, reproducible audit test that would detect this leakage using only logged feature values and timestamps. Describe the test procedure step by step. 3. Describe how to rebuild $x_i(t)$ to eliminate the bias while preserving the spirit of the cross-sectional normalization. A: **How to Think About It:** This is one of the sneakiest forms of data leakage in production alpha research. The feature formula looks clean -- no future returns anywhere in sight. But the normalization denominator is computed over a set of stocks that was determined using tomorrow's information. That means the feature value you compute today is different from the value you would have computed if you only knew what you knew at time $t$. The resulting alpha will look great in backtest and fall apart in live trading, because in production you cannot know which stocks will be halted tomorrow. Any time a preprocessing step touches future information -- even indirectly through universe membership, index reconstitution, or survivorship -- you have leakage. **Key Insight:** Lookahead bias does not require that future returns enter the feature formula. It only requires that any information unavailable at decision time $t$ influences the feature value. Universe membership $U(t+1)$ is future information, and it changes the normalization base, which changes every feature value in the cross section. **The Method:** *Part 1 -- Why this is lookahead bias:* Cross-sectional normalization typically looks like: $$x_i(t) = \frac{\tilde{x}_i(t) - \mu(t)}{\sigma(t)}$$ where $\mu(t)$ and $\sigma(t)$ are the mean and standard deviation computed over the universe $U$. If you use $U(t+1)$ instead of $U(t)$, the mean and standard deviation shift. Stocks halted on day $t+1$ are often halted because of extreme events -- earnings surprises, regulatory actions, M&A announcements -- that are correlated with returns. Removing these stocks from the normalization base systematically alters $\mu(t)$ and $\sigma(t)$ in a direction that is correlated with the target $r_i(t+1)$. So while $r_i(t+1)$ never appears explicitly, it leaks in through the conditioning set. In a live setting, you would not know $U(t+1)$ at time $t$, so the feature values you actually compute would differ from the backtest values. The backtest alpha is an illusion. *Part 2 -- Audit test to detect the leakage:* 1. Pull the logged feature values $x_i(t)$ and timestamps from the production feature store. 2. Independently reconstruct the feature using only information available at or before time $t$: use universe $U(t)$ (the membership list known at end of day $t$) and the same raw inputs $\tilde{x}_i(t)$. Call this $x_i^{\text{clean}}(t)$. 3. Compute the discrepancy $\delta_i(t) = x_i(t) - x_i^{\text{clean}}(t)$ for each stock and date. 4. Run a panel regression of $\delta_i(t)$ on $r_i(t+1)$, controlling for time fixed effects. If the coefficient is statistically significant, the logged feature contains future information. 5. As an additional check, compute the rank correlation between $\delta_i(t)$ and $r_i(t+1)$ on each date, then test whether the time-series average of these correlations is significantly different from zero. 6. Finally, compare the Sharpe ratio of the alpha built on the logged $x_i(t)$ versus the one built on $x_i^{\text{clean}}(t)$. A material drop in the clean version confirms the original alpha was inflated by leakage. The key property of this test is that it is reproducible: it uses only logged data and timestamps, requires no judgment calls, and produces a p-value. *Part 3 -- Rebuilding the feature:* - Replace $U(t+1)$ with $U(t)$ in all cross-sectional computations. The universe used for normalization on day $t$ should be determined solely by information available at end of day $t$. - If the normalization requires a notion of "tradeable universe," use the tradeable list as of the market close on day $t$, not the open on day $t+1$. - If index reconstitutions or corporate actions create ambiguity, use the most recently confirmed membership list before the decision time. - After rebuilding, re-run the audit test above to confirm that $\delta_i(t)$ is now identically zero (or negligibly small due to floating-point differences). - Document the universe determination rule with an explicit timestamp cutoff so that future researchers can verify point-in-time correctness. **Practical Considerations:** - This type of leakage is common in academic papers that use CRSP or Compustat data with ex-post delisting information baked into the universe. - Similar issues arise with index reconstitution (e.g., using the current S&P 500 list to build historical cross-sections), survivorship bias in hedge fund databases, and corporate action adjustments that use future effective dates. - In production systems, the best defense is a strict point-in-time data architecture where every table is keyed by both event date and knowledge date, and all queries filter on knowledge date $\leq t$. **Answer:** The bias arises because $U(t+1)$ leaks future information into the normalization, changing every feature value in a return-correlated way. Detect it by reconstructing features with $U(t)$, computing the discrepancy, and regressing discrepancies on future returns. Fix it by strictly using $U(t)$ for all cross-sectional operations and enforcing a point-in-time data architecture. --- Maximizing a Quadratic Form [medium, Optimization] --- https://quantvault.org/problem-2007-maximizing-a-quadratic-form.html Q: Let $A$ be an $n \times n$ square matrix and $x \in \mathbb{R}^n$. You want to find the maximum of the quadratic form $x^\top A x$. 1. Assume $A$ is symmetric and $\|x\|_2 = 1$. What is the maximum value of $x^\top A x$, and where is it achieved? 2. What if $A$ is not symmetric? Show how to reduce this case to the symmetric one. 3. What happens if you drop the unit-norm constraint and optimize over all $x \in \mathbb{R}^n$? A: **How to Think About It:** A quadratic form $x^\top A x$ on the unit sphere is a classic eigenvalue problem. The key insight is that any symmetric matrix can be diagonalized in an orthonormal basis -- so in that basis the quadratic form becomes a weighted sum of squares of the coordinates, and the maximum is just the largest weight, i.e., the largest eigenvalue. Before writing any formulas, you should know: the answer is $\lambda_{\max}$, achieved at the corresponding eigenvector. If $A$ is not symmetric, the skew-symmetric part $\frac{A - A^\top}{2}$ contributes zero to any quadratic form (since $x^\top B x = 0$ for any skew-symmetric $B$), so only the symmetric part matters. **Quick Sanity Check:** Take $A = \text{diag}(3, 1)$ and $x = (\cos\theta, \sin\theta)^\top$. Then $x^\top A x = 3\cos^2\theta + \sin^2\theta$, maximized at $\theta = 0$ with value $3 = \lambda_{\max}$. That is exactly what the theorem says. **Formal Solution:** **Part 1 -- Symmetric case.** Since $A$ is symmetric, by the spectral theorem it has an orthonormal eigenbasis $\{v_1, \ldots, v_n\}$ with $Av_i = \lambda_i v_i$ and eigenvalues $\lambda_1 \geq \lambda_2 \geq \cdots \geq \lambda_n$. Write $x = \sum_{i=1}^n c_i v_i$. The unit-norm constraint gives $\sum_i c_i^2 = 1$. Then: $$x^\top A x = \left(\sum_i c_i v_i\right)^\top A \left(\sum_j c_j v_j\right) = \sum_i c_i^2 \lambda_i \leq \lambda_1 \sum_i c_i^2 = \lambda_1$$ Equality holds when $c_1 = 1$ and $c_i = 0$ for $i > 1$, i.e., $x = v_1$. So $\max_{\|x\|=1} x^\top A x = \lambda_{\max}$, achieved at the eigenvector corresponding to the largest eigenvalue. This ratio $\frac{x^\top A x}{x^\top x}$ is called the **Rayleigh quotient**. **Part 2 -- Non-symmetric case.** Decompose $A = S + K$ where $S = \frac{A + A^\top}{2}$ is symmetric and $K = \frac{A - A^\top}{2}$ is skew-symmetric. For any $x$: $$x^\top K x = (x^\top K x)^\top = x^\top K^\top x = -x^\top K x \implies x^\top K x = 0$$ So $x^\top A x = x^\top S x$ for all $x$. The quadratic form depends only on the symmetric part of $A$. Apply Part 1 to $S$. **Part 3 -- Unconstrained case.** If $A$ (or its symmetric part) has any strictly positive eigenvalue $\lambda > 0$ with eigenvector $v$, then $x = tv$ gives $x^\top A x = t^2 \lambda \to \infty$ as $t \to \infty$. No finite maximum exists. A finite maximum over all of $\mathbb{R}^n$ only exists if $A$ is negative semi-definite (all eigenvalues $\leq 0$), in which case the maximum is $0$, achieved at $x = 0$. **Answer:** - Symmetric $A$, $\|x\| = 1$: $\max x^\top A x = \lambda_{\max}(A)$, achieved at the corresponding eigenvector. - Non-symmetric $A$: replace $A$ with $\frac{A+A^\top}{2}$ and apply the above. - Unconstrained: no finite maximum unless $A$ is negative semi-definite. --- Maximum Ones in a Nilpotent Binary Matrix [hard, Linear Algebra] --- https://quantvault.org/problem-2010-maximum-ones-in-a-nilpotent-binary-matrix.html Q: Let $A$ be an $n \times n$ matrix whose entries are all $0$ or $1$, and suppose $A^2 = 0$ (i.e., $A$ is nilpotent of order 2). What is the maximum number of $1$s that $A$ can contain? Prove your answer and give an explicit construction that achieves the maximum. A: **How to Think About It:** The condition $A^2 = 0$ is a constraint on *paths*, not just on rank. Read $A$ as the adjacency matrix of a directed graph on $n$ vertices: $A_{ik} = 1$ means there is an edge $i \to k$. The $(i,j)$ entry of $A^2$ counts directed paths $i \to k \to j$ of length two. Because every entry of $A$ is $0$ or $1$ -- so the products $A_{ik}A_{kj}$ are nonnegative and *cannot cancel* -- $A^2 = 0$ is equivalent to the purely combinatorial statement: **there is no directed path of length two anywhere in the graph.** The whole problem reduces to: how many edges can a directed graph on $n$ vertices have if it contains no path of length two? That count is the number of $1$s. **Quick Estimate:** "No length-two path" means no vertex can be both the head of some edge and the tail of some edge -- otherwise those two edges would chain into a length-two path. So the vertices that *emit* edges and the vertices that *receive* edges form two disjoint groups. If $a$ vertices emit and $b$ vertices receive (with $a + b \le n$), the most edges you can have is the complete bipartite $a \times b$ block, $a\cdot b$ edges. Maximizing $a\cdot b$ subject to $a + b \le n$ gives $a = \lfloor n/2 \rfloor$, $b = \lceil n/2 \rceil$, so the answer should be $\lfloor n/2 \rfloor \lceil n/2 \rceil = \lfloor n^2/4 \rfloor$. For $n = 6$: $9$. For $n = 7$: $12$. **Approach:** Translate $A^2 = 0$ into "no length-two directed path," prove that this forces the emitting and receiving vertex sets to be disjoint (yielding the upper bound $\lfloor n^2/4\rfloor$ by AM-GM), then exhibit a construction that attains it. **Formal Solution:** *Step 1 -- Reformulate the constraint combinatorially.* For any $i, j$, $$\bigl(A^2\bigr)_{ij} = \sum_{k=1}^{n} A_{ik} A_{kj}.$$ Every term $A_{ik}A_{kj}$ is $0$ or $1$, so the sum is zero **iff every term is zero**, i.e. for all $i,j,k$ it is never the case that $A_{ik} = 1$ and $A_{kj} = 1$. Equivalently: there is no index $k$ that is simultaneously the head of some edge ($A_{ik}=1$ for some $i$) and the tail of some edge ($A_{kj}=1$ for some $j$). (Beware the trap of arguing only through $\mathrm{rank}(A)$: it is true that $\mathrm{Col}(A)\subseteq\mathrm{Null}(A)$ forces $\mathrm{rank}(A)\le\lfloor n/2\rfloor$, but rank alone does **not** bound the number of $1$s -- a single rank-$1$ all-ones $2\times 3$ off-diagonal block already has $6$ ones, far more than its rank. The sharp constraint is the path/disjointness condition below, not the rank.) *Step 2 -- The emitting and receiving sets are disjoint.* Define $$S = \{\,k : \text{row } k \text{ is nonzero}\,\} = \{k : \exists\, j,\ A_{kj} = 1\} \quad(\text{vertices that emit an edge}),$$ $$T = \{\,k : \text{column } k \text{ is nonzero}\,\} = \{k : \exists\, i,\ A_{ik} = 1\} \quad(\text{vertices that receive an edge}).$$ If some $k \in S \cap T$, then there exist $i$ with $A_{ik}=1$ and $j$ with $A_{kj}=1$, giving the term $A_{ik}A_{kj}=1$ in $(A^2)_{ij}$ -- contradicting $A^2=0$. Hence $$S \cap T = \varnothing, \qquad\text{so}\qquad |S| + |T| \le n.$$ *Step 3 -- Upper bound on the number of $1$s.* Every $1$ of $A$ sits in some row $k\in S$ (its row is nonzero) and some column $\ell\in T$ (its column is nonzero); that is, all $1$s lie inside the $S\times T$ submatrix, which has exactly $|S|\,|T|$ entries. Therefore $$\#\{1\text{s in }A\} \;\le\; |S|\,|T| \;\le\; \left(\frac{|S|+|T|}{2}\right)^{2} \le \left(\frac{n}{2}\right)^2,$$ by AM-GM. Since the count is an integer and $|S|+|T|\le n$, the maximum of $|S|\,|T|$ over integers with $|S|+|T|\le n$ is attained at $|S|=\lfloor n/2\rfloor,\ |T|=\lceil n/2\rceil$, giving $$\#\{1\text{s}\} \;\le\; \left\lfloor \frac{n}{2} \right\rfloor \left\lceil \frac{n}{2} \right\rceil = \left\lfloor \frac{n^2}{4} \right\rfloor.$$ *Step 4 -- Matching construction.* Let $m = \lfloor n/2 \rfloor$. Define the $n \times n$ matrix $$A = \begin{pmatrix} 0 & J \\ 0 & 0 \end{pmatrix},$$ where $J$ is the $m \times (n - m)$ all-ones matrix and the zero blocks have the appropriate sizes. Then: - $A$ has entries in $\{0,1\}$ and exactly $m(n - m) = \lfloor n^2/4 \rfloor$ ones. - $A^2 = 0$: the only nonzero block is the top-right $J$, and multiplying it by the corresponding (bottom) rows of $A$, which are all zero, gives $J\cdot 0 = 0$; all other block products vanish. (Combinatorially: $S = \{1,\dots,m\}$ emit only into $T = \{m+1,\dots,n\}$, and the vertices in $T$ emit nothing, so no edge can be continued -- no length-two path exists.) Thus the bound is tight. For $n = 6$, $m = 3$: a $3\times 3$ all-ones block in the top-right corner gives $9$ ones with $A^2 = 0$. **Answer:** The maximum number of $1$s is $\lfloor n^2 / 4 \rfloor$, achieved by an all-ones $\lfloor n/2\rfloor \times \lceil n/2\rceil$ block placed off-diagonal (emitting rows mapping into disjoint receiving columns) in an otherwise zero matrix. For $n=6$ this maximum is $\lfloor 36/4\rfloor = 9$. --- Minimum of x^x on (0, 1] [medium, Optimization] --- https://quantvault.org/problem-2016-minimum-of-x-x-on-0-1.html Q: Consider the function $f(x) = x^x$ defined on the interval $(0, 1]$. 1. Find the minimum value of $f(x)$ on this interval. 2. What is $\lim_{x \to 0^+} f(x)$? A: **How to Think About It:** The function $x^x$ looks exotic, but the standard move is to rewrite it as $e^{x \ln x}$, which converts it into a composition you can differentiate cleanly. Before doing any calculus, think about boundary behavior: at $x = 1$ we get $f(1) = 1$, and as $x$ gets small (say $x = 0.1$), we get $0.1^{0.1} \approx e^{0.1 \cdot (-2.3)} \approx e^{-0.23} \approx 0.79$. So the function dips below 1 somewhere in the interior. The limit as $x \to 0^+$ is a classic -- it hinges on the fact that $x \ln x \to 0$. **Quick Estimate:** At $x = 1/e \approx 0.368$, we get $f(1/e) = (1/e)^{1/e} = e^{-1/e} \approx e^{-0.368} \approx 0.692$. That is lower than $f(0.1) \approx 0.79$ and $f(1) = 1$, so $x = 1/e$ looks like the minimum. For the limit, $x \ln x$ at $x = 0.01$ gives $0.01 \cdot (-4.6) = -0.046$, which is close to zero, so $f(x) \approx e^{-0.046} \approx 0.955$, heading toward 1. **Approach:** Rewrite as an exponential and differentiate. **Formal Solution:** Write $f(x) = e^{x \ln x}$. Differentiating: $$f'(x) = e^{x \ln x}(\ln x + 1) = x^x(\ln x + 1)$$ Since $x^x > 0$ for all $x > 0$, the sign of $f'(x)$ depends entirely on $\ln x + 1$: - $f'(x) = 0$ when $\ln x = -1$, i.e., $x = 1/e$ - $f'(x) 0$ for $x > 1/e$ (function is increasing) So $x = 1/e$ is a global minimum on $(0, 1]$: $$f(1/e) = (1/e)^{1/e} = e^{-1/e} \approx 0.6922$$ For the limit as $x \to 0^+$, we need $\lim_{x \to 0^+} x \ln x$. This is a $0 \cdot (-\infty)$ form. Rewrite as: $$\lim_{x \to 0^+} \frac{\ln x}{1/x}$$ This is $-\infty / \infty$, so apply L'Hopital's rule: $$\lim_{x \to 0^+} \frac{1/x}{-1/x^2} = \lim_{x \to 0^+} (-x) = 0$$ Therefore $\lim_{x \to 0^+} x^x = e^0 = 1$. **Answer:** The minimum value is $e^{-1/e} \approx 0.6922$, attained at $x = 1/e$. The limit as $x \to 0^+$ is $1$. --- Missing Data Imputation and Regression Pipeline [medium, Machine Learning] --- https://quantvault.org/problem-2017-missing-data-imputation-and-regression-pipeline.html Q: You are given a feature matrix `X` and a target vector `y`. Some entries of `X` are missing (represented as `None`). Build a small modeling pipeline that cleans the data and fits a linear model. Implement a function with the signature: ```python def impute_and_fit(X, y): ... ``` - `X` is an `n x p` matrix given as a list of rows; each entry is a float or `None` (missing). - `y` is the length-`n` target vector. **Requirements:** 1. **Impute** each column's missing (`None`) entries with that column's **mean over the observed (non-`None`) values**. If an entire column is missing, fill it with `0.0`. 2. **Fit ordinary least squares WITH an intercept** on the cleaned data. 3. **Return** the coefficient vector `[intercept, b1, b2, ...]` as a plain list of floats (intercept first, then one slope per feature column). **Constraints:** - Handle edge cases: all-`None` columns, single-feature inputs, and columns with several missing entries. **Example** ```python impute_and_fit( [[1.0, None], [None, 3.0], [3.0, 5.0], [4.0, 6.0]], [2.1, 4.0, 6.2, 7.9], ) -> [-2.218474576271187, 1.7166101694915263, 0.5766101694915252] ``` Column 1's missing value is filled with the mean of its observed entries `(1+3+4)/3`, column 2's with `(3+5+6)/3`; then OLS with an intercept is fit and the returned list is `[intercept, b_x1, b_x2]`. A: **How to Think About It:** This is a two-stage pipeline: impute, then fit. The imputation rule is *mean over observed values*, computed **per column** (each column is filled independently with the average of its own non-missing entries; a fully missing column becomes all zeros). The fit is plain ordinary least squares **with an intercept**, and the required return value is the flat coefficient list `[intercept, b1, b2, ...]` -- intercept first, then one slope per feature. **Algorithm:** - **Impute:** Load `X` into a float array, mapping `None` to `NaN`. For each column, take the mean of the observed (non-NaN) entries and write it into the missing slots. If every entry in a column is missing, set the whole column to `0.0`. - **Fit OLS with intercept:** Prepend a column of ones to the imputed matrix and solve the least-squares system `A @ beta = y`. The first entry of `beta` is the intercept; the rest are the per-feature slopes. **Why MSE / OLS:** Minimizing squared error is exactly maximum-likelihood estimation of `beta` under i.i.d. Gaussian noise `y_i = x_i^T beta + eps_i`: the Gaussian log-likelihood's `beta`-dependent term is `-1/(2 sigma^2) * sum_i (y_i - x_i^T beta)^2`, so maximizing likelihood is minimizing the sum of squared residuals. That is what `np.linalg.lstsq` returns. **Code:** ```python import numpy as np def impute_and_fit(X, y): # X is an n x p matrix (list of rows) of feature values; some entries are None # (missing). y is the length-n target. # Step 1: impute each column's missing entries with that column's MEAN over the # OBSERVED (non-None) values (if a whole column is missing, use 0.0). # Step 2: fit ordinary least squares WITH an intercept on the cleaned data. # Return the coefficient vector [intercept, b1, b2, ...] as a list of floats. n = len(X) p = len(X[0]) if n > 0 else 0 # Build design matrix with NaN for missing entries. Xf = np.array( [[np.nan if X[i][j] is None else float(X[i][j]) for j in range(p)] for i in range(n)], dtype=float, ) # Impute each column with the mean of its OBSERVED values (0.0 if all missing). for j in range(p): col = Xf[:, j] mask = np.isnan(col) if mask.all(): Xf[:, j] = 0.0 else: col[mask] = np.mean(col[~mask]) yv = np.asarray(y, dtype=float) # OLS with intercept: prepend a column of ones and solve least squares. A = np.hstack([np.ones((n, 1)), Xf]) beta, *_ = np.linalg.lstsq(A, yv, rcond=None) return [float(b) for b in beta] ``` **Complexity:** Imputation is `O(n * p)`. The least-squares solve on the `n x (p+1)` design matrix is `O(n * p^2 + p^3)`. The returned list has length `p + 1` (intercept followed by one coefficient per feature column). --- Nonparametric Walk-Forward Backtest Protocol [hard, Finance] --- https://quantvault.org/problem-2028-nonparametric-walk-forward-backtest-protocol.html Q: You have daily returns for $N$ candidate trading signals $\{f_i\}_{i=1}^{N}$. For each signal, you form a dollar-neutral portfolio by ranking stocks on the signal and going long the top decile, short the bottom decile. 1. Propose a rolling walk-forward scheme that produces an out-of-sample Sharpe ratio estimate for each signal. Specify how you choose the training window length, the rebalance frequency, and the test window length. 2. With $N$ signals tested, how do you account for multiple testing? Describe a nonparametric procedure to control the false discovery rate (FDR) so that you can identify which signals, if any, have genuinely positive out-of-sample Sharpe ratios. 3. What are the key pitfalls of this approach, and how would you mitigate them in practice? A: **How to Think About It:** The core challenge is building a backtest that does not fool you. Every quant has seen strategies that look great in-sample and die in production. The two main enemies are look-ahead bias (using future information to make today's decision) and multiple testing (testing $N$ signals and cherry-picking the winner). A walk-forward protocol handles the first; a multiplicity correction handles the second. The nonparametric requirement means we cannot lean on normal distribution assumptions for returns -- we need procedures that work for heavy-tailed, autocorrelated, skewed data. **Key Insight:** The walk-forward structure must strictly respect the arrow of time, and the multiple testing correction must account for the dependence structure across signals -- standard Bonferroni is too conservative because signal returns are correlated. **The Method:** *Part 1 -- Walk-Forward Scheme:* 1. Fix a training window of length $W$ days (e.g., $W = 252$ for one year) and a test window of length $T$ days (e.g., $T = 21$ for one month). 2. At each rebalance date $t$, use only data from $[t - W, t - 1]$ to rank stocks on signal $f_i$ and form the long-short portfolio. 3. Record the portfolio return over $[t, t + T - 1]$. This is one out-of-sample observation. 4. Slide the window forward by $T$ days and repeat, producing a time series of non-overlapping OOS returns $\{r_{i,1}, r_{i,2}, \ldots, r_{i,K}\}$ where $K = \lfloor (\text{total days} - W) / T \rfloor$. 5. The out-of-sample Sharpe ratio for signal $i$ is: $$\widehat{SR}_i = \frac{\bar{r}_i}{s_i} \cdot \sqrt{252 / T}$$ where $\bar{r}_i$ and $s_i$ are the sample mean and standard deviation of the $K$ OOS return blocks. 6. Choosing $W$: too short and your ranking is noisy; too long and you miss regime changes. A practical default is 1-2 years. You can run the protocol for multiple $W$ values and check stability. 7. Choosing $T$: must be long enough that portfolio turnover costs are amortized. Monthly ($T = 21$) is standard for cross-sectional equity signals. *Part 2 -- Multiple Testing Correction:* 1. For each signal $i$, compute a p-value for the null $H_0^{(i)}: SR_i \leq 0$. Under the nonparametric requirement, use a block bootstrap or a circular bootstrap on the OOS return blocks to estimate the null distribution of $\widehat{SR}_i$. Specifically: - Resample the $K$ OOS return blocks with replacement (use stationary block bootstrap with expected block length $\ell$ to preserve any residual autocorrelation). - Recompute $\widehat{SR}_i^{*}$ on each bootstrap sample. - The p-value is the fraction of bootstrap samples where $\widehat{SR}_i^{*} \geq \widehat{SR}_i$, centered under the null (shift the mean to zero before resampling). 2. Collect the $N$ p-values $\{p_1, \ldots, p_N\}$. 3. Apply the Benjamini-Hochberg (BH) procedure at level $q$ (e.g., $q = 0.05$): - Sort: $p_{(1)} \leq p_{(2)} \leq \cdots \leq p_{(N)}$. - Find the largest $k$ such that $p_{(k)} \leq k \cdot q / N$. - Reject $H_0$ for signals with $p_{(i)} \leq p_{(k)}$. 4. BH controls the expected FDR at level $q$ and is less conservative than Bonferroni, especially when signals are correlated. *Part 3 -- Pitfalls and Mitigations:* - **Look-ahead in signal construction:** If the signals themselves were designed using the full dataset (e.g., you picked which factors to test after seeing the data), the walk-forward only protects the portfolio formation step, not the signal selection step. Mitigation: treat the signal universe as given before the backtest begins, or use a separate holdout period for signal discovery vs. signal evaluation. - **Transaction costs:** Decile sorts can produce high turnover. Always deduct realistic transaction costs (bid-ask spread, market impact) from returns before computing Sharpe. - **Survivorship and universe bias:** Make sure the stock universe at each rebalance date only includes stocks that were tradeable at that time. No future knowledge of delistings. - **Non-stationarity:** A signal might work in one regime and fail in another. Check whether the OOS Sharpe is stable across sub-periods, not just positive on average. - **Dependence across signals:** If many signals are highly correlated, you are not really testing $N$ independent hypotheses. BH still controls FDR, but you lose statistical power. Consider clustering signals first and testing one representative per cluster. **Answer:** Use a rolling walk-forward with non-overlapping OOS windows, compute per-signal Sharpe ratios from the OOS returns, obtain p-values via stationary block bootstrap under the null of zero Sharpe, and apply Benjamini-Hochberg to control the false discovery rate across all $N$ signals. Always deduct realistic trading costs, avoid look-ahead in signal construction, and verify stability across time. --- Numerical Function Minimization via Gradient Descent [medium, Optimization] --- https://quantvault.org/problem-2029-numerical-function-minimization-via-gradient-descent.html Q: Implement gradient descent to minimize a polynomial given by its coefficient list. Write a function with signature: ```python def gd_minimize(coeffs, x0, lr, max_iter): ``` where `f(x) = sum(coeffs[i] * x**i)` (so `coeffs = [10, -6, 1]` means $f(x) = 10 - 6x + x^2$). You do NOT have an analytic derivative — approximate $f'(x)$ with a **central difference**. Follow this pinned deterministic protocol so the result is reproducible: - finite-difference step $h = 10^{-5}$ - convergence tolerance $\text{tol} = 10^{-10}$ on $|x_{new} - x|$ - update $x \leftarrow x - \text{lr} \cdot f'(x)$; stop when $|x_{new} - x| 2.9999999999308358` Here $f(x) = x^2 - 6x + 10 = (x-3)^2 + 1$, whose minimum is at $x = 3$; gradient descent from $x_0 = 0$ converges to essentially $3$ (up to the finite-difference/tolerance protocol). A: **How to Think About It:** Gradient descent repeatedly steps in the direction that decreases $f$. Here $f$ is a polynomial given by its coefficient list, `coeffs`, so $f(x) = \sum_i \text{coeffs}[i]\, x^i$. We do NOT get an analytic derivative, so we approximate $f'(x)$ from function evaluations. This problem pins a fully deterministic protocol so the answer is reproducible: **central difference** with finite-difference step $h = 10^{-5}$, convergence tolerance $\text{tol} = 10^{-10}$ on $|x_{new} - x|$, update rule $x \leftarrow x - \text{lr} \cdot f'(x)$, starting at $x_0$, stopping when $|x_{new} - x| < \text{tol}$ or `max_iter` is reached. The function returns the final $x$ (the approximate argmin). **Algorithm:** 1. Build $f(x) = \sum_i \text{coeffs}[i]\, x^i$ from the coefficient list. 2. At each step approximate $f'(x) \approx \dfrac{f(x+h) - f(x-h)}{2h}$ (central difference, $O(h^2)$ accurate). 3. Update $x \leftarrow x - \text{lr} \cdot f'(x)$. 4. Stop when $|x_{new} - x| < \text{tol}$ or `max_iter` iterations elapse; return the final $x$. **Code:** ```python def gd_minimize(coeffs, x0, lr, max_iter): # Minimize f(x) = sum(coeffs[i] * x**i) by gradient descent, approximating # f'(x) with a CENTRAL difference. Pinned deterministic protocol: # h = 1e-5, tol = 1e-10, update x <- x - lr * f'(x), # stop when |x_new - x| < tol OR max_iter reached. Start at x0. h = 1e-5 tol = 1e-10 def f(x): total = 0.0 for i, c in enumerate(coeffs): total += c * x**i return total x = x0 for _ in range(max_iter): grad = (f(x + h) - f(x - h)) / (2 * h) x_new = x - lr * grad if abs(x_new - x) < tol: x = x_new break x = x_new return x ``` **Why central difference:** The forward difference $\frac{f(x+h)-f(x)}{h}$ is $O(h)$ accurate and costs one extra evaluation, while the central difference $\frac{f(x+h)-f(x-h)}{2h}$ is $O(h^2)$ accurate at two evaluations — the first-order error term cancels by symmetry. The optimal $h$ balances truncation error against floating-point roundoff, giving $h^\star \approx \epsilon^{1/3} \approx 10^{-5}$, which is exactly the pinned value. **Enhancements that improve convergence:** 1. **Adaptive step size (backtracking line search):** instead of a fixed `lr`, shrink the step until the Armijo sufficient-decrease condition $f(x - \alpha g) \le f(x) - c\,\alpha g^2$ holds. 2. **Newton's method:** use the numerical second derivative $f''(x) \approx \frac{f(x+h) - 2f(x) + f(x-h)}{h^2}$ and step $x \leftarrow x - f'(x)/f''(x)$ for quadratic convergence near the minimum (at the risk of diverging when $f''$ is small or negative). 3. **Momentum:** accumulate a running average of past gradients to damp oscillations in narrow valleys. **Note:** the function signature is `gd_minimize(coeffs, x0, lr, max_iter)` and it must **return** the final $x$ (a float), not print it. --- Order Book Dynamics and Price Formation [easy, Market Microstructure] --- https://quantvault.org/problem-2035-order-book-dynamics-and-price-formation.html Q: Walk through the mechanics of a limit order book. 1. What is the difference between a passive (limit) order and an active (market) order? How does each interact with the order book? 2. Explain how price-time priority works. If two limit orders rest at the same price, which one gets filled first? 3. Suppose the order book shows bids at $\$100.00$ (500 shares), $\$99.99$ (300 shares) and asks at $\$100.02$ (200 shares), $\$100.03$ (400 shares). A market buy order for 350 shares arrives. Walk through exactly what happens -- which resting orders get filled, what is the resulting book state, and what is the new mid-price? 4. Distinguish between temporary and permanent price impact. Why does the distinction matter for someone executing a large order? A: **How to Think About It:** The order book is the central data structure of modern electronic markets. Everything a market maker or execution trader does revolves around understanding how orders enter the book, match, and move prices. The key mental model: passive orders are the supply of liquidity sitting on the shelf; active orders are the demand that grabs it off the shelf. When demand exceeds the supply at a given price level, the price moves. If you think of it that way, most microstructure results follow naturally. **Key Insight:** Price moves happen when incoming order flow consumes the available depth at a price level, forcing the next fill to occur at a worse price. Whether that move is permanent (reflects new information) or temporary (reflects a short-lived liquidity imbalance) is the central question in execution and market making. **The Method:** **Part 1 -- Passive vs. Active Orders** - A **passive order** (limit order) specifies a price and quantity and rests in the book until matched or cancelled. A limit buy at $\$100$ says "I will buy up to $N$ shares, but only at $\$100$ or lower." It provides liquidity. - An **active order** (market order) demands immediate execution at the best available price. It takes liquidity. A marketable limit order (e.g., a limit buy at $\$100.05$ when the best ask is $\$100.02$) behaves like a market order until its price limit is reached. **Part 2 -- Price-Time Priority (FIFO)** Most exchanges use strict price-time priority: 1. **Price priority**: an order at a better price always fills first. A bid at $\$100.01$ fills before a bid at $\$100.00$. 2. **Time priority**: among orders at the same price, the one that arrived earlier fills first (first-in, first-out). This means queue position matters enormously. A market maker who is first in the queue at the best bid has a significant edge -- they get filled before later arrivals. Losing your queue spot (e.g., by cancelling and re-submitting) is costly. **Part 3 -- Walking Through the Example** Initial book: | Side | Price | Size | |------|-------|------| | Ask | $\$100.03$ | 400 | | Ask | $\$100.02$ | 200 | | Bid | $\$100.00$ | 500 | | Bid | $\$99.99$ | 300 | Spread: $\$100.02 - \$100.00 = \$0.02$. Mid-price: $\$100.01$. A market buy for 350 shares arrives: 1. First, it matches against the best ask at $\$100.02$. There are 200 shares there, so all 200 fill at $\$100.02$. Remaining buy quantity: 150 shares. 2. The $\$100.02$ level is now empty. The order walks up to the next ask at $\$100.03$. It fills 150 of the 400 shares resting there at $\$100.03$. Remaining ask at $\$100.03$: 250 shares. Resulting book: | Side | Price | Size | |------|-------|------| | Ask | $\$100.03$ | 250 | | Bid | $\$100.00$ | 500 | | Bid | $\$99.99$ | 300 | New best ask: $\$100.03$. Best bid: $\$100.00$. New spread: $\$0.03$. New mid-price: $\$100.015$. The mid-price moved up by $\$0.005$ -- this is the price impact of the 350-share market buy. **Part 4 -- Temporary vs. Permanent Impact** - **Temporary impact** is the price displacement caused by consuming liquidity that gets replenished shortly after. Limit orders flow back in, the spread narrows, and the mid-price partially reverts. This component is essentially a transaction cost. - **Permanent impact** is the portion of the price move that does not revert because the trade conveyed information. If a large buy signals that an informed trader believes the stock is worth more, the book will not fully replenish at the old levels. For execution traders, this distinction is critical. If you are executing a large parent order (say, buying 100,000 shares), you want to minimize temporary impact by slicing the order over time (TWAP, VWAP, etc.). But the permanent impact is unavoidable -- it is the cost of your information leaking into the market. The optimal execution strategy balances urgency (information decay, risk) against market impact. **Answer:** The order book is a sorted collection of resting limit orders. Market orders consume liquidity starting at the best price and walk through successive levels if their size exceeds available depth, causing price impact. Price-time priority governs fill order. The resulting price movement decomposes into temporary impact (liquidity displacement, mean-reverting) and permanent impact (information content, non-reverting). Understanding this decomposition is essential for market making, execution algorithms, and transaction cost analysis. --- Problems With L0 Regularization [medium, Optimization] --- https://quantvault.org/problem-2050-problems-with-l0-regularization.html Q: In regression, we sometimes want to penalize the number of non-zero coefficients directly. The $L_0$ "norm" of a coefficient vector $\beta$ counts how many entries are non-zero: $$\|\beta\|_0 = \#\{j : \beta_j \neq 0\}$$ The $L_0$-regularized regression problem is: $$\min_{\beta} \|Y - X\beta\|_2^2 + \lambda \|\beta\|_0$$ 1. Why is this problem fundamentally harder to solve than $L_1$ or $L_2$ regularized regression? 2. What specific mathematical properties make $L_0$ regularization intractable for standard optimization algorithms? 3. Why is $L_1$ (Lasso) used as a practical substitute, and in what sense is it the "best" convex substitute for $L_0$? A: **How to Think About It:** The appeal of $L_0$ regularization is obvious -- you want a sparse model, so why not directly penalize the number of non-zero coefficients? The problem is that "count the non-zeros" is a combinatorial objective, not a smooth one. Flipping a coefficient from $0.0001$ to $0$ changes the penalty by exactly $\lambda$, but flipping it from $0.0001$ to $0.0002$ changes the penalty by nothing. This discontinuity is the root of all the trouble. In practice, anyone who has tried best-subset selection on more than about 30-40 features knows it becomes computationally infeasible. **Key Insight:** The $L_0$ penalty is non-convex and discontinuous, which means there is no gradient to follow, no efficient descent direction, and no polynomial-time algorithm guaranteed to find the global minimum. **The Method:** The problems with $L_0$ regularization break down into three categories: 1. **NP-hardness (computational intractability):** Solving the $L_0$ problem exactly requires searching over all possible subsets of features. With $p$ features, there are $2^p$ subsets. For $p = 50$, that is over $10^{15}$ subsets -- far beyond any brute-force search. This is equivalent to the classical best-subset selection problem, which is known to be NP-hard. No clever algorithm can avoid the exponential blowup in the worst case. 2. **Non-convexity and discontinuity:** The function $f(\beta_j) = \mathbb{1}(\beta_j \neq 0)$ is discontinuous at zero. It jumps from 0 to 1 with no smooth transition. This means: - The overall objective $\|Y - X\beta\|_2^2 + \lambda\|\beta\|_0$ is non-convex - Gradient-based optimizers (gradient descent, Newton's method, coordinate descent) cannot be applied because the penalty has no well-defined gradient - Local minima are abundant, so even heuristic search methods may get stuck far from the global optimum 3. **No efficient relaxation to the same problem:** Unlike $L_2$ regularization (Ridge), which gives a closed-form solution $\beta = (X^TX + \lambda I)^{-1}X^TY$, and $L_1$ (Lasso), which is a convex program solvable in polynomial time, $L_0$ has no tractable reformulation that preserves the exact same solution. **Practical Considerations:** - $L_1$ (Lasso) is the tightest convex relaxation of $L_0$. Geometrically, the $L_1$ unit ball is the convex hull of the $L_0$ "ball" (the set of vectors with at most $k$ non-zero entries, intersected with the right constraints). This is why Lasso produces sparse solutions -- it is the best convex approximation to the sparsity-inducing objective. - In low dimensions ($p \leq 30$ or so), modern mixed-integer programming solvers can actually solve the $L_0$ problem exactly. Recent work has pushed this boundary, but for high-dimensional problems typical in quant finance (thousands of features), $L_1$ remains the go-to. - Greedy algorithms like forward stepwise selection and matching pursuit are heuristics for the $L_0$ problem. They are fast but offer no global optimality guarantee. - The SCAD and MCP penalties are non-convex alternatives that sit between $L_0$ and $L_1$ -- they reduce bias on large coefficients while maintaining some computational tractability. **Answer:** $L_0$ regularization is intractable because (1) the optimization is NP-hard, requiring a search over $2^p$ subsets, (2) the penalty is non-convex and discontinuous, ruling out gradient-based methods, and (3) there is no polynomial-time algorithm for the global optimum. $L_1$ (Lasso) is preferred because it is the tightest convex relaxation of $L_0$, producing sparse solutions while remaining solvable in polynomial time. --- Ridge vs. Lasso Regression [medium, Regression] --- https://quantvault.org/problem-2066-ridge-vs-lasso-regression.html Q: You are comparing two common regularization approaches for linear regression: Ridge ($L_2$) and Lasso ($L_1$). 1. Write down the optimization problem each method solves. What penalty term does each add to the ordinary least squares objective? 2. Ridge regression has a closed-form solution. Derive it, and show that the matrix you invert is invertible for every $\lambda > 0$, even when $X^TX$ is singular (collinear features, or more features than observations). Why does Lasso not have one? 3. When would you prefer Ridge over Lasso, and vice versa? What does each method actually do to the coefficient vector $\hat{\beta}$? A: **How to Think About It:** The core question is what each penalty actually does geometrically to the coefficients. Ridge shrinks all $\hat{\beta}_j$ smoothly toward zero -- like a spring pulling every coefficient back, hardest along the directions the data pin down least. Lasso shrinks too, but it also has a kink at zero, which means it can push coefficients all the way to exactly zero and drop features entirely. That difference -- continuous shrinkage vs. hard zeroing -- drives every practical trade-off. A quant interviewer asking this wants to see you understand the geometry, the closed form, and the real-world selection logic, not just recite the formulas. **Key Insight:** The $L_2$ penalty is smooth (differentiable everywhere), which is why Ridge has a closed form. The $L_1$ penalty has a kink at $\beta_j = 0$, which breaks differentiability and forces iterative solvers -- but that same kink is exactly what causes exact sparsity. **The Method:** *Part 1: Optimization Problems* Both methods minimize the usual residual sum of squares plus a penalty on $\beta$: $$\hat{\beta}^{\text{Ridge}} = \arg\min_{\beta} \|Y - X\beta\|_2^2 + \lambda\|\beta\|_2^2$$ $$\hat{\beta}^{\text{Lasso}} = \arg\min_{\beta} \|Y - X\beta\|_2^2 + \lambda\|\beta\|_1$$ where $\|\beta\|_2^2 = \sum_j \beta_j^2$ and $\|\beta\|_1 = \sum_j |\beta_j|$. The tuning parameter $\lambda \geq 0$ controls how hard the penalty bites -- larger $\lambda$ means more shrinkage. *Part 2: Closed Form for Ridge* For Ridge, expand the objective and take the derivative with respect to $\beta$: $$\frac{\partial}{\partial \beta}\left[\|Y - X\beta\|_2^2 + \lambda\|\beta\|_2^2\right] = -2X^T(Y - X\beta) + 2\lambda\beta = 0$$ Rearranging: $$X^TX\hat{\beta} + \lambda\hat{\beta} = X^TY$$ $$(X^TX + \lambda I)\hat{\beta} = X^TY$$ $$\hat{\beta}^{\text{Ridge}} = (X^TX + \lambda I)^{-1}X^TY$$ **Why the inverse always exists.** $X^TX$ is symmetric positive semidefinite: for any $v$, $v^TX^TXv = \|Xv\|_2^2 \geq 0$, so its eigenvalues satisfy $\mu_1 \geq \cdots \geq \mu_p \geq 0$. Write $X^TX = Q\,\text{diag}(\mu_i)\,Q^T$ with $Q$ orthogonal; since $I = QQ^T$, $$X^TX + \lambda I = Q\,\text{diag}(\mu_i + \lambda)\,Q^T,$$ and every eigenvalue is $\mu_i + \lambda \geq \lambda > 0$. So $X^TX + \lambda I$ is positive definite and invertible for every $\lambda > 0$, with no assumption on the rank of $X$: collinear columns or more features than observations ($p > n$, where $X^TX$ is singular and OLS has no unique solution) are fine. The Hessian of the objective is $2(X^TX + \lambda I) \succ 0$, so the objective is strictly convex and this stationary point is the unique minimizer. **What $\lambda$ buys numerically and statistically.** The condition number drops from $\mu_1/\mu_p$ (infinite when $\mu_p = 0$) to $(\mu_1 + \lambda)/(\mu_p + \lambda) \leq (\mu_1 + \lambda)/\lambda$, finite whatever the data. When $X^TX$ is invertible, each component of $\hat{\beta}$ along an eigenvector is the OLS component multiplied by $\mu_i/(\mu_i + \lambda)$: high-variance directions pass almost untouched, while the fragile low-variance directions that OLS would fit to noise are shrunk hard. That is the bias-for-variance trade. (A random $5 \times 8$ design with $\lambda = 0.3$ makes this concrete: $X^TX$ has rank $5$, yet $X^TX + 0.3I$ has smallest eigenvalue exactly $0.3$ and the closed form matches a numerical minimizer.) Lasso has no closed form because $\|\beta\|_1$ is not differentiable at $\beta_j = 0$. The first-order condition involves the subgradient of the $L_1$ norm, and coordinate descent (or LARS) must be used. The one exception is an orthonormal design ($X^TX = I$): there the problem separates by coordinate and Lasso is soft-thresholding of the OLS coefficients, $\hat{\beta}_j = \text{sign}(z_j)\max(|z_j| - \lambda/2,\, 0)$ with $z = X^TY$, which shows the exact zeros directly. *Part 3: When to Use Each* - **Use Lasso** when you believe the true model is sparse -- only a handful of features actually matter. Lasso performs feature selection automatically by driving irrelevant coefficients exactly to zero. Especially useful when $p \gg n$ (many more features than observations). - **Use Ridge** when you expect all (or most) features to contribute and multicollinearity is present. Ridge keeps all features in the model and shrinks them smoothly, most strongly along low-variance directions. The closed form is also computationally cheap -- important when $p$ and $n$ are both large. - **Elastic Net** ($\lambda_1\|\beta\|_1 + \lambda_2\|\beta\|_2^2$) combines both: it does feature selection like Lasso but handles correlated predictors more gracefully (Lasso tends to pick one arbitrarily among a group of correlated features and zero out the others). **Practical Considerations:** In financial applications, Ridge is often preferred in factor models where all factors plausibly contribute (e.g., PCA-based factor returns). Lasso shows up in sparse signal detection -- e.g., identifying which of 500 macro variables actually predict returns. One common mistake is forgetting to standardize features before applying either method: both penalties treat all $\beta_j$ symmetrically, so a feature measured in dollars will be penalized very differently from one measured in basis points unless you standardize first. **Answer:** Ridge ($L_2$) shrinks all coefficients toward zero without zeroing them and has closed form $\hat{\beta} = (X^TX + \lambda I)^{-1}X^TY$, always well defined because $\lambda I$ lifts every eigenvalue of $X^TX$ to at least $\lambda > 0$; use it for multicollinear problems where all features contribute. Lasso ($L_1$) produces exact zeros and performs feature selection; use it when the model is sparse. Lasso lacks a closed form because the $L_1$ norm is non-differentiable at zero. --- Ridge vs. Lasso Regression: Theory and Practice [medium, Regression] --- https://quantvault.org/problem-2067-ridge-vs-lasso-regression-theory-and-practice.html Q: Consider a linear model $y = X\beta + \varepsilon$ with design matrix $X \in \mathbb{R}^{n \times d}$ and response $y \in \mathbb{R}^n$. 1. Write the optimization problems for ridge regression and lasso, including how the intercept is typically handled. 2. Derive the closed-form solution for ridge regression when $X^\top X$ is invertible. 3. Explain mathematically (not just heuristically) why lasso can produce exact zeros in the coefficient vector while ridge typically does not. 4. Describe a cross-validation scheme to select the regularization hyperparameter $\lambda$, and recommend a metric you would optimize when building a trading signal. A: **How to Think About It:** Ridge and lasso are the two canonical regularized regression methods, and understanding their differences is essential for any quant who builds predictive models. The core tension is bias vs. variance: both methods shrink coefficients toward zero to reduce overfitting, but they do it in geometrically different ways. Ridge uses an $L_2$ penalty (a sphere), which shrinks everything proportionally. Lasso uses an $L_1$ penalty (a diamond), whose corners sit on the axes -- and that geometry is exactly why lasso can zero out coefficients while ridge cannot. **Key Insight:** The reason lasso produces exact zeros is not heuristic -- it comes from the subdifferential of the absolute value function. At $\beta_j = 0$, the $L_1$ penalty has a kink, and the subgradient condition can be satisfied at zero. The smooth $L_2$ penalty has no such kink, so the gradient condition at zero is generically not satisfied. **The Method:** **Part (i): Optimization problems** Assume columns of $X$ are centered (mean-subtracted) and the intercept $\beta_0$ is fit separately as $\bar{y}$. Then regularize only the slope coefficients $\beta \in \mathbb{R}^d$. **Ridge:** $$\hat{\beta}_{\text{ridge}} = \arg\min_{\beta} \left\{ \frac{1}{2n} \|y - X\beta\|_2^2 + \lambda \|\beta\|_2^2 \right\}$$ **Lasso:** $$\hat{\beta}_{\text{lasso}} = \arg\min_{\beta} \left\{ \frac{1}{2n} \|y - X\beta\|_2^2 + \lambda \|\beta\|_1 \right\}$$ The intercept is handled by centering: set $\hat{\beta}_0 = \bar{y} - \bar{X}^\top \hat{\beta}$, where $\bar{X}$ is the vector of column means. This way the intercept is never penalized. **Part (ii): Ridge closed-form** The ridge objective is differentiable. Setting the gradient to zero: $$-\frac{1}{n} X^\top(y - X\beta) + 2\lambda \beta = 0$$ $$X^\top X \beta + 2n\lambda \beta = X^\top y$$ $$(X^\top X + 2n\lambda I)\beta = X^\top y$$ With the common convention $\lambda' = 2n\lambda$: $$\hat{\beta}_{\text{ridge}} = (X^\top X + \lambda' I)^{-1} X^\top y$$ Note: $X^\top X + \lambda' I$ is always invertible for $\lambda' > 0$, even when $X^\top X$ is singular. This is one of ridge's key practical benefits. **Part (iii): Why lasso zeros out coefficients** Consider the lasso subproblem for a single coefficient $\beta_j$, holding all others fixed. The optimality condition uses the subdifferential of $|\beta_j|$: $$\frac{\partial}{\partial \beta_j} \left[ \frac{1}{2n}\|y - X\beta\|_2^2 \right] + \lambda \cdot \partial |\beta_j| \ni 0$$ Let $r_j = y - X_{-j}\beta_{-j}$ be the partial residual. The condition becomes: - If $\beta_j > 0$: $-\frac{1}{n} X_j^\top r_j + \frac{1}{n} \|X_j\|^2 \beta_j + \lambda = 0$ - If $\beta_j < 0$: $-\frac{1}{n} X_j^\top r_j + \frac{1}{n} \|X_j\|^2 \beta_j - \lambda = 0$ - If $\beta_j = 0$: the subgradient condition requires $\left|\frac{1}{n} X_j^\top r_j\right| \leq \lambda$ So $\hat{\beta}_j = 0$ whenever the correlation between feature $j$ and the residual is small enough (below $\lambda$ in magnitude). This is the soft-thresholding operator: $$\hat{\beta}_j = \text{sign}(z_j) \max(|z_j| - \lambda, 0) / (\|X_j\|^2/n)$$ where $z_j = X_j^\top r_j / n$. For **ridge**, the optimality condition at $\beta_j = 0$ is $-X_j^\top r_j / n + 2\lambda \cdot 0 = 0$, which requires $X_j^\top r_j = 0$ exactly. This happens with probability zero for continuous data. So ridge generically never produces exact zeros. **Part (iv): Cross-validation for trading signals** Use **time-series cross-validation** (walk-forward or expanding window), never standard K-fold, because financial data is serially correlated. 1. Define a grid of $\lambda$ values (log-spaced, e.g., $10^{-4}$ to $10^{2}$). 2. For each $\lambda$, run walk-forward CV: train on data up to time $t$, predict on period $[t+1, t+h]$, slide forward, repeat. 3. Optionally add a **purge/embargo** gap between train and test to prevent leakage from overlapping labels. **Metric choice:** For a trading signal, do not optimize $R^2$ or MSE -- these reward accurate forecasts of magnitude, which is not the same as profitability. Instead, optimize: - **Information coefficient (IC):** Spearman rank correlation between predicted and realized returns. This measures whether the signal ranks assets correctly. - Alternatively, **risk-adjusted PnL** (Sharpe ratio of a simple long/short portfolio formed from the signal), though this is noisier. The key is that a trading signal needs to get the direction and ranking right, not the exact magnitude. **Answer:** Ridge has a closed-form $(X^\top X + \lambda I)^{-1} X^\top y$ and shrinks coefficients toward zero but never exactly to zero. Lasso produces exact zeros via soft-thresholding, driven by the non-differentiability of $|\beta_j|$ at zero. For trading signals, use walk-forward CV with an information coefficient metric. --- Ridge vs. Lasso Shrinkage in the SVD Basis [medium, Regression] --- https://quantvault.org/problem-2068-ridge-vs-lasso-shrinkage-in-the-svd-basis.html Q: You have standardized features $X \in \mathbb{R}^{n \times p}$ and a return vector $y \in \mathbb{R}^n$. Ridge regression solves $$\min_{\beta} \|y - X\beta\|_2^2 + \lambda \|\beta\|_2^2$$ and Lasso solves $$\min_{\beta} \|y - X\beta\|_2^2 + \lambda \|\beta\|_1$$ (i) Derive the closed-form solution $\hat{\beta}^{\text{ridge}}$ and express it in the SVD basis of $X$. What are the shrinkage factors applied to each singular direction, and how do they depend on $\lambda$? (ii) Contrast this with Lasso's shrinkage behavior. Why does Lasso produce sparse solutions while Ridge does not? In the orthonormal design case, write down the explicit shrinkage operator for each method. (iii) Explain how cross-validation should be structured to select $\lambda$ and produce an honest out-of-sample $R^2$ estimate. Why is naive cross-validation (using the same CV loop for both tuning and evaluation) problematic, and how do you fix it? (iv) In a typical quant setting with $p \gg n$ and many weak, correlated signals, which method would you default to and why? A: **How to Think About It:** Ridge and Lasso are the two fundamental regularization tools for regression, and understanding how they shrink coefficients is essential for any quant working with predictive signals. The key insight comes from looking at these estimators in the SVD basis, where the geometry becomes transparent: Ridge smoothly shrinks all directions, while Lasso hard-thresholds weak directions to zero. This difference has enormous practical consequences when you are building trading signals from noisy, correlated features. **Key Insight:** In the SVD basis, Ridge applies a smooth multiplicative shrinkage factor $d_j^2 / (d_j^2 + \lambda)$ to each principal direction, while Lasso applies soft-thresholding $\text{sign}(z_j)(|z_j| - \lambda/2)_+$ in the orthonormal case. Ridge keeps every direction alive (just dampened); Lasso kills weak directions entirely. **Derivation:** **(i) Ridge closed form and SVD shrinkage.** Take the gradient of the ridge objective and set it to zero: $$\nabla_{\beta} \left[ \|y - X\beta\|_2^2 + \lambda \|\beta\|_2^2 \right] = -2X^T(y - X\beta) + 2\lambda \beta = 0$$ Solving: $$\hat{\beta}^{\text{ridge}} = (X^TX + \lambda I)^{-1} X^T y$$ Now write the SVD of $X = UDV^T$, where $U \in \mathbb{R}^{n \times r}$, $D = \text{diag}(d_1, \ldots, d_r)$, $V \in \mathbb{R}^{p \times r}$, and $r = \text{rank}(X)$. Then $X^TX = VD^2V^T$, and: $$(X^TX + \lambda I)^{-1} = V(D^2 + \lambda I)^{-1}V^T$$ So: $$\hat{\beta}^{\text{ridge}} = V(D^2 + \lambda I)^{-1}DU^Ty = \sum_{j=1}^{r} \frac{d_j}{d_j^2 + \lambda} (u_j^T y) \, v_j$$ Compare with OLS, which gives $\hat{\beta}^{\text{OLS}} = \sum_j \frac{1}{d_j}(u_j^T y) \, v_j$. Ridge multiplies the $j$-th OLS component by the shrinkage factor: $$s_j = \frac{d_j^2}{d_j^2 + \lambda}$$ When $d_j^2 \gg \lambda$, $s_j \approx 1$ (strong directions are barely touched). When $d_j^2 \ll \lambda$, $s_j \approx 0$ (weak/noisy directions are crushed). The transition happens around $d_j^2 \approx \lambda$. **(ii) Lasso shrinkage contrast.** Lasso has no closed form in general, but in the orthonormal design case ($X^TX = I$), the OLS solution is $\hat{\beta}^{\text{OLS}}_j = X_j^Ty$, and the shrinkage operators are: - Ridge: $\hat{\beta}^{\text{ridge}}_j = \frac{1}{1+\lambda} \hat{\beta}^{\text{OLS}}_j$ (proportional shrinkage) - Lasso: $\hat{\beta}^{\text{lasso}}_j = \text{sign}(\hat{\beta}^{\text{OLS}}_j)(|\hat{\beta}^{\text{OLS}}_j| - \lambda/2)_+$ (soft-thresholding) The critical difference: Ridge scales every coefficient toward zero but never to exactly zero. Lasso sets coefficients to exactly zero when $|\hat{\beta}^{\text{OLS}}_j| < \lambda/2$. This is why Lasso produces sparse models -- it performs automatic variable selection. Geometrically, the $L_1$ ball has corners aligned with the coordinate axes, so the constrained optimum tends to land on a corner (some coordinates exactly zero). The $L_2$ ball is smooth, so the optimum is never exactly on an axis unless the unconstrained solution already is. **(iii) Cross-validation structure.** The correct procedure uses nested cross-validation (double CV): 1. **Outer loop** (evaluation): Split the data into $K$ outer folds. Each outer fold provides an honest out-of-sample prediction. 2. **Inner loop** (tuning): Within each outer training set, run a second $K$-fold CV over a grid of $\lambda$ values. Select the $\lambda$ that minimizes inner CV error. 3. **Fit and predict**: Using the selected $\lambda$, fit the model on the full outer training set and predict on the held-out outer test fold. 4. **Aggregate**: The out-of-sample $R^2$ is computed from the outer fold predictions. Why naive CV fails: if you use a single CV loop to both pick $\lambda$ and estimate out-of-sample performance, you are evaluating performance at the $\lambda$ that looked best on the test folds. This introduces selection bias -- you picked the $\lambda$ that got lucky on the test data. The resulting $R^2$ is systematically inflated. Nested CV breaks this circularity by ensuring the data used to evaluate performance was never involved in any tuning decision. For time-series returns, use walk-forward (expanding or rolling window) CV rather than random folds, to respect temporal ordering and avoid lookahead bias. **(iv) Ridge vs. Lasso in practice for quant signals.** In a typical quant setting with $p \gg n$ and many weak, correlated predictors, Ridge is usually the better default. Here is why: - **Correlated features**: Lasso tends to pick one feature from a correlated group and zero out the rest, which is unstable -- small data perturbations change which feature survives. Ridge averages across correlated features, giving more stable coefficient estimates. - **Dense weak signals**: If predictive power is spread across many features (each contributing a small amount), Lasso's hard thresholding kills too many useful signals. Ridge keeps them all, appropriately dampened. - **Stability**: Ridge coefficients are smooth functions of the data; Lasso coefficients can jump discontinuously. For a trading strategy, this means Ridge produces more stable portfolio weights over time. Lasso is preferred when you believe the true signal is genuinely sparse (only a handful of features matter) and interpretability is important. **Answer:** The ridge estimator is $\hat{\beta}^{\text{ridge}} = (X^TX + \lambda I)^{-1}X^Ty$, which in the SVD basis applies shrinkage factors $d_j^2/(d_j^2 + \lambda)$ to each principal direction. Lasso applies soft-thresholding, producing exact zeros and thus sparse solutions. Honest out-of-sample evaluation requires nested CV (inner loop for $\lambda$ selection, outer loop for performance estimation). In the $p \gg n$ regime with correlated weak signals, Ridge is generally preferred for its stability and ability to aggregate diffuse signal across many features. --- Straddle Gamma Across Price Movement [medium, Options Pricing] --- https://quantvault.org/problem-2087-straddle-gamma-across-price-movement.html Q: You are long a straddle (long call + long put at the same strike $K$ and expiry $T$) under Black-Scholes assumptions. Explain how the portfolio gamma varies as the underlying spot price $S$ moves. Specifically: 1. What is the gamma at-the-money ($S = K$)? 2. How does gamma change as $S$ moves away from $K$ in either direction? 3. How does time to expiry affect the gamma profile? A: **How to Think About It:** Before writing any formulas, think about what gamma means economically. Gamma measures how fast your delta changes as the stock moves. A straddle is a bet on realized volatility -- you want the stock to move a lot. Gamma tells you how much you profit from those moves (through delta-hedging or rebalancing). Intuitively, your gamma should be highest when the stock is near the strike (where both options are most sensitive to price changes) and should fall off as the stock moves far away (where the options become deep ITM or deep OTM and behave like linear instruments). **Quick Sanity Checks:** - At $S = K$: Both options are ATM, maximum uncertainty about whether they expire ITM. Delta is changing rapidly. Gamma should be at its peak. - Deep ITM call / deep OTM put ($S \gg K$): Call delta is near 1, put delta is near 0. Neither is changing much. Gamma should be near zero. - Deep OTM call / deep ITM put ($S \ll K$): Call delta is near 0, put delta is near $-1$. Again, deltas are barely moving. Gamma near zero. - The gamma profile should be symmetric and bell-shaped around $S = K$. **Derivation:** A straddle consists of a long call and a long put at the same strike and expiry. Since put-call parity implies that calls and puts have the same gamma: $$\Gamma_{\text{call}} = \Gamma_{\text{put}} = \frac{N'(d_1)}{S \sigma \sqrt{T}}$$ where $N'(d_1) = \frac{1}{\sqrt{2\pi}} e^{-d_1^2 / 2}$ is the standard normal density evaluated at $d_1$. The straddle gamma is: $$\Gamma_{\text{straddle}} = \Gamma_{\text{call}} + \Gamma_{\text{put}} = \frac{2 N'(d_1)}{S \sigma \sqrt{T}}$$ **1. At-the-money ($S = K$):** When $S = K$ (and ignoring rates/dividends for simplicity), $d_1 \approx \frac{\sigma \sqrt{T}}{2}$, which is close to 0 for short-dated options. The normal density $N'(d_1)$ is near its maximum value of $1/\sqrt{2\pi} \approx 0.399$. So: $$\Gamma_{\text{straddle}}^{\text{ATM}} \approx \frac{2 \times 0.399}{K \sigma \sqrt{T}} = \frac{0.798}{K \sigma \sqrt{T}}$$ This is the peak gamma. **2. As $S$ moves away from $K$:** As $S$ moves away from $K$ in either direction, $|d_1|$ increases, and $N'(d_1) = \frac{1}{\sqrt{2\pi}} e^{-d_1^2/2}$ decreases (the Gaussian density has its peak at 0 and decays exponentially). Therefore, $\Gamma_{\text{straddle}}$ decreases. The decay is roughly Gaussian in shape: gamma falls off like $e^{-d_1^2/2}$. Deep ITM or deep OTM, gamma approaches zero -- the options behave approximately like linear instruments (delta is pinned near $\pm 1$ or 0) and there is no curvature left. The profile is symmetric around $S = K$ (approximately, ignoring the $1/S$ factor which creates slight asymmetry). **3. Effect of time to expiry:** - **Far from expiry (large $T$):** The gamma peak at ATM is lower (divided by $\sqrt{T}$) but broader. The options are sensitive over a wider range of spot prices. - **Near expiry (small $T$):** The gamma peak becomes very tall (proportional to $1/\sqrt{T} \to \infty$) but very narrow. Gamma is concentrated in a tight band around the strike. This is the "gamma spike" that traders talk about near expiry -- if you are ATM, your delta is swinging wildly; if you are even slightly away from the strike, gamma is negligible. - **At expiry ($T \to 0$):** Gamma approaches a Dirac delta function centered at $S = K$. The option payoff becomes a hockey stick with an infinite kink at the strike. **Practical Interpretation:** A straddle holder profits from realized moves through gamma: each day, the gamma P&L is approximately $\frac{1}{2} \Gamma (\Delta S)^2$. This profit is maximized when gamma is highest, i.e., when the stock is near the strike. If the stock drifts far from the strike, the straddle becomes a directional position with low gamma and low theta -- it is no longer a good volatility bet. Traders managing straddle books near expiry are acutely aware of the gamma spike. If you are ATM with hours to go, a 1% move creates a huge delta change that must be hedged immediately. This is why gamma risk near expiry dominates the risk management conversation. **Answer:** Straddle gamma is $\frac{2 N'(d_1)}{S \sigma \sqrt{T}}$, which is bell-shaped and centered at $S = K$. It peaks ATM (where $N'(d_1)$ is maximized) and decays toward zero as $S$ moves deep ITM or deep OTM. Near expiry, the peak becomes taller and narrower; far from expiry, it is shorter and broader. --- The Roll Spread Estimator [medium, Market Microstructure] --- https://quantvault.org/problem-2092-the-roll-spread-estimator.html Q: You observe only transaction prices $\{P_t\}$ at equally spaced times. Under the Roll model: - The efficient ("true") price $M_t$ follows a random walk: $M_t = M_{t-1} + u_t$, where $u_t$ are i.i.d. mean-zero innovations. - Each trade occurs at either the bid or the ask with equal probability, so the observed transaction price is $P_t = M_t + c \cdot Q_t$, where $Q_t \in \{-1, +1\}$ is an i.i.d. sign (each with probability $1/2$) and $c > 0$ is the half-spread. - The trade direction $Q_t$ is independent of the efficient price innovations $u_t$. Let $\Delta P_t = P_t - P_{t-1}$ denote the price change. 1. Show that $\text{Cov}(\Delta P_t, \Delta P_{t-1}) = -c^2$. 2. Derive the Roll estimator $\hat{c} = \sqrt{-\widehat{\text{Cov}}(\Delta P_t, \Delta P_{t-1})}$, where the sample autocovariance replaces the population quantity. 3. Explain why this estimator can fail in practice and what modifications are used. A: **How to Think About It:** The Roll model captures a simple but powerful idea: even if the true price is a pure random walk (no predictability), observed transaction prices will exhibit negative serial correlation because of bid-ask bounce. Every time you trade, you are randomly hitting the bid or the ask, adding noise of $\pm c$ to the true price. Consecutive price changes tend to reverse because a buy at the ask is likely followed by a move back toward the bid (or vice versa). The magnitude of this reversal tells you the spread. **Quick Estimate:** Suppose the half-spread is 5 cents and the efficient price has zero drift. The bid-ask bounce adds variance $c^2 = 0.0025$ to each price. The first-order autocovariance of returns should be about $-0.0025$. So if you compute $\widehat{\text{Cov}}(\Delta P_t, \Delta P_{t-1})$ from data and get $-0.0025$, you estimate $\hat{c} = \sqrt{0.0025} = 0.05$, giving a full spread of 10 cents. **Approach:** Decompose the price change into efficient-price and bounce components, then compute the autocovariance. **Formal Solution:** Since $P_t = M_t + cQ_t$, the price change is: $$\Delta P_t = P_t - P_{t-1} = (M_t - M_{t-1}) + c(Q_t - Q_{t-1}) = u_t + c(Q_t - Q_{t-1})$$ **Step 1: Compute $\text{Cov}(\Delta P_t, \Delta P_{t-1})$.** Expand: $$\text{Cov}(\Delta P_t, \Delta P_{t-1}) = \text{Cov}(u_t + c(Q_t - Q_{t-1}),\; u_{t-1} + c(Q_{t-1} - Q_{t-2}))$$ Since $u_t$ and $Q_t$ are all mutually independent across time (and independent of each other), the only cross-term that survives involves $Q_{t-1}$, which appears in both $\Delta P_t$ and $\Delta P_{t-1}$: $$= \text{Cov}(-cQ_{t-1},\; cQ_{t-1}) = -c^2 \text{Var}(Q_{t-1})$$ Since $Q_{t-1} \in \{-1, +1\}$ with equal probability, $\text{Var}(Q_{t-1}) = E[Q_{t-1}^2] - (E[Q_{t-1}])^2 = 1 - 0 = 1$. Therefore: $$\text{Cov}(\Delta P_t, \Delta P_{t-1}) = -c^2$$ **Step 2: The Roll estimator.** Solving for $c$: $$c = \sqrt{-\text{Cov}(\Delta P_t, \Delta P_{t-1})}$$ Replace the population autocovariance with its sample analog: $$\hat{c} = \sqrt{-\widehat{\text{Cov}}(\Delta P_t, \Delta P_{t-1})}$$ The estimated full spread is $2\hat{c}$. **Step 3: Practical issues.** - **Positive autocovariance:** In practice, the sample autocovariance of price changes is sometimes positive (e.g., due to momentum, stale quotes, or autocorrelated order flow). Then $\hat{c} = \sqrt{-\text{negative number}}$ is undefined. The common fix is to set $\hat{c} = 0$ when the autocovariance is positive, or to use modifications like the Corwin-Schultz (2012) high-low estimator. - **Autocorrelated trade direction:** The Roll model assumes $Q_t$ is i.i.d., but real order flow is persistent (buy orders cluster). This violates the model and biases the estimator. - **Time-varying spreads:** The model assumes constant $c$, but spreads widen during volatility and narrow during calm periods. **Answer:** Under the Roll model, bid-ask bounce creates negative first-order autocovariance $\text{Cov}(\Delta P_t, \Delta P_{t-1}) = -c^2$, giving the estimator $\hat{c} = \sqrt{-\widehat{\text{Cov}}}$. The full spread estimate is $2\hat{c}$. --- CDS Par Spread Derivation Under Constant Hazard Rate [hard, Options Pricing] --- https://quantvault.org/problem-2122-cds-par-spread-derivation-under-constant-hazard-rate.html Q: Consider a par Credit Default Swap (CDS) with maturity $T$. The premium leg pays continuously at rate $s$ until default or maturity. The protection leg pays $1 - R$ at the time of default. Assume a constant hazard rate $h$, constant recovery rate $R$, and constant risk-free rate $r$, all under the risk-neutral measure. (a) Derive the par spread $s$ in closed form as a function of $r$, $h$, $R$, and $T$. (b) Show that as $T \to \infty$, the par spread satisfies $s \to (1 - R)h$. State any assumptions you use. (c) Given $R = 40\%$ and an observed par spread $s = 180$ bps for a long-maturity CDS, estimate the hazard rate $h$. A: **How to Think About It:** A CDS is just a swap where one leg compensates for credit losses and the other pays a running premium for that protection. At par, the present value of premiums paid equals the present value of the protection received. The math simplifies enormously under a constant hazard rate because the survival probability is just $e^{-ht}$ -- an exponential decay. So both legs reduce to integrals of exponentials, which are elementary. Before diving in, note the limiting intuition: if you extend the maturity to infinity, boundary effects vanish, and the spread must just reflect the instantaneous expected loss rate, which is $(1-R)h$. **Quick Sanity Checks:** The par spread should increase with $h$ (more default risk means higher premium), increase with $1-R$ (lower recovery means bigger losses to insure), and be independent of notional. For long maturities, the spread should flatten out -- it should not blow up or go to zero. The answer $s = (1-R)h$ in the limit is dimensionally correct (both sides are rates in units of per-year). **Derivation:** **(a) Par Spread in Closed Form** The survival probability to time $t$ is $Q(t) = e^{-ht}$, so the default density is $f(t) = h e^{-ht}$. **Premium leg PV:** The protection buyer pays $s$ continuously, but only while alive. Discounting at rate $r$: $$PV_{\text{prem}} = s \int_0^T e^{-rt} Q(t) \, dt = s \int_0^T e^{-(r+h)t} \, dt = s \cdot \frac{1 - e^{-(r+h)T}}{r + h}$$ **Protection leg PV:** The protection seller pays $1 - R$ at default (if default occurs before $T$). The default density at time $t$ is $h e^{-ht}$, so: $$PV_{\text{prot}} = (1-R) \int_0^T e^{-rt} h e^{-ht} \, dt = (1-R) h \int_0^T e^{-(r+h)t} \, dt = (1-R) h \cdot \frac{1 - e^{-(r+h)T}}{r+h}$$ Setting $PV_{\text{prem}} = PV_{\text{prot}}$ for the par condition: $$s \cdot \frac{1 - e^{-(r+h)T}}{r+h} = (1-R) h \cdot \frac{1 - e^{-(r+h)T}}{r+h}$$ The annuity factors $\frac{1 - e^{-(r+h)T}}{r+h}$ cancel (they are strictly positive for $T > 0$), giving: $$\boxed{s = (1-R)h}$$ Notice something striking: under continuous premium payment and constant parameters, the par spread does not depend on $r$ or $T$ at all. The discounting hits both legs identically, so it cancels. **(b) Asymptotic Behavior as $T \to \infty$** From part (a), we already have $s = (1-R)h$ exactly for all $T > 0$, so the limit is immediate: $$\lim_{T \to \infty} s = (1-R)h$$ **Assumptions used:** - Constant hazard rate $h$ (time-homogeneous Poisson default process) - Constant risk-free rate $r > 0$ - Continuous premium payments (no accrual adjustment at default) - No counterparty risk - Recovery $R$ is a fixed fraction of notional, paid at default time If any of these assumptions are relaxed (e.g., term structure of hazard rates, discrete premium payments with accrued interest), then $s$ would depend on $T$ and $r$, and the relationship $s = (1-R)h$ would only hold approximately for large $T$. **(c) Estimating $h$ from Market Data** For a long-maturity CDS, we use the asymptotic relationship: $$s \approx (1-R)h \implies h \approx \frac{s}{1-R}$$ Plugging in $s = 180$ bps $= 0.0180$ and $R = 0.40$: $$h \approx \frac{0.0180}{1 - 0.40} = \frac{0.0180}{0.60} = 0.03$$ So the implied hazard rate is $h \approx 3\%$ per year, or equivalently about 300 bps. This means the risk-neutral default intensity is 3% annually. **Practical Interpretation:** The formula $s = (1-R)h$ is the single most important relationship in credit derivatives. Traders use it constantly to convert between spreads and implied default probabilities. If you see a 5-year CDS at 180 bps with standard 40% recovery, you immediately know the market is pricing about 3% annual default probability. The one-year survival probability is $e^{-0.03} \approx 97\%$, and the 5-year survival probability is $e^{-0.15} \approx 86\%$. **Answer:** (a) $s = (1-R)h$ -- under constant hazard rate with continuous premium payments, the par spread is independent of both $r$ and $T$. (b) The limit $s \to (1-R)h$ as $T \to \infty$ is immediate from (a); with non-constant parameters, it requires $r > 0$ and bounded hazard rates so boundary terms vanish. (c) $h \approx 0.03 = 3\%$ per year. --- Comparing Forecasting Models for Daily Asset Returns [medium, Machine Learning] --- https://quantvault.org/problem-2126-comparing-forecasting-models-for-daily-asset-returns.html Q: You are comparing two forecasting models for daily asset returns: Model A, a simple linear factor model, and Model B, a regularized machine-learning model. Both output next-day predicted returns $\hat{r}^{(A)}_t$ and $\hat{r}^{(B)}_t$ for a universe of assets. Each day, you form a zero-cost long-short portfolio by going long the top-predicted assets and short the bottom-predicted assets, scaled so that each portfolio has unit ex-ante volatility. **(a)** Describe how you would construct a rolling cross-validation scheme on this time-series data to fairly compare the out-of-sample Sharpe ratios of the two models. Be specific about the fold structure, what is trained on what, and how you prevent look-ahead bias. **(b)** Propose a statistical test to assess whether Model B's Sharpe ratio is significantly higher than Model A's. State the null and alternative hypotheses clearly, define the test statistic, and discuss its distributional assumptions. **(c)** You are evaluating not just two models but an entire library of candidate models. Discuss how transaction costs, turnover constraints, and multiple comparisons should factor into the evaluation. A: **How to Think About It:** This is a model evaluation design problem, not a calculation. The central difficulty is that financial time series are not i.i.d. -- there is autocorrelation in returns, volatility clustering, and regime shifts. Standard K-fold CV is broken here because it randomly assigns observations to folds, which means you can train on tomorrow and test on yesterday. The other trap is multiple comparisons: if you test 100 models and pick the best, you are almost guaranteed to find a spurious winner even if none of them work. A good answer to this question shows you have felt the pain of these issues in practice. **Key Insight:** The organizing principle for all three parts is the same: never use future information when making a decision, and account for the cost of every choice you make. **The Method:** **(a) Rolling Walk-Forward Cross-Validation** 1. **Partition the timeline** into $K$ consecutive non-overlapping test windows, each of length $T_{\text{test}}$ (e.g., 6-12 months). Walk them forward chronologically: fold 1 tests on months 1-6, fold 2 on months 7-12, etc. 2. **Training set for fold $k$**: all data strictly before the test window. Use an expanding window (all history) or a fixed rolling window (e.g., last 3-5 years) to avoid stale data dominating later folds. 3. **Embargo period**: insert a gap of $\tau$ days between the end of training and start of testing (e.g., $\tau = 5$ to 20 trading days). This prevents feature leakage from overlapping return windows or slow-updating signals. 4. **Purging**: if features for day $t$ use data up to day $t+k$ (e.g., forward returns in labels), remove observations within $k$ days of the test boundary from training entirely. 5. **Compute out-of-sample returns** for each model's portfolio in each test fold, then concatenate across folds to get a full out-of-sample PnL series. Compute Sharpe ratio on this concatenated series -- not an average of fold-level Sharpes, which ignores serial structure. **(b) Testing Sharpe Ratio Differences** Let $d_t = r^{(B)}_t - r^{(A)}_t$ be the daily difference in portfolio returns (already scaled to unit vol). Define: $$\text{SR}_\Delta = \frac{\bar{d}}{\hat{\sigma}_d}$$ where $\bar{d}$ is the sample mean and $\hat{\sigma}_d$ is an autocorrelation-robust standard error (use Newey-West with $L$ lags, e.g., $L = \lfloor 4(T/100)^{2/9} \rfloor$). - **$H_0$**: $E[d_t] \leq 0$ (Model B Sharpe $\leq$ Model A Sharpe) - **$H_1$**: $E[d_t] > 0$ (Model B Sharpe $>$ Model A Sharpe) Under $H_0$, by CLT (with HAC correction), $\sqrt{T} \cdot \bar{d} / \hat{\sigma}_d \xrightarrow{d} N(0,1)$, so use a one-sided z-test. Reject $H_0$ at level $\alpha$ if the z-statistic exceeds $z_\alpha$ (e.g., 1.645 for $\alpha = 5\%$). Alternatively, use the **Diebold-Mariano test** which is designed for this exact setting -- it directly tests equal predictive accuracy between two models using a loss differential series, and is robust to autocorrelation. For robustness, complement with a bootstrap: resample the $d_t$ series using a block bootstrap (block length matching the autocorrelation structure), compute the Sharpe differential on each resample, and read off the empirical p-value. **(c) Transaction Costs, Turnover, and Multiple Comparisons** **Transaction costs and turnover:** Compute a net-of-cost Sharpe by deducting estimated transaction costs. For each model, track daily turnover $\tau_t$ (fraction of portfolio rebalanced). Net daily return is: $$r^{\text{net}}_t = r^{\text{gross}}_t - c \cdot \tau_t$$ where $c$ is the one-way cost in basis points (e.g., 5-10 bps for liquid equities). A model with a slightly higher gross Sharpe but 3x the turnover may lose to Model A after costs. Turnover constraints can be imposed directly: penalize or disqualify models exceeding a turnover budget. **Multiple comparisons:** If you test $M$ models, the probability of finding at least one spurious winner under the null of no skill is $1 - (1-\alpha)^M$. Fix this by: - **Bonferroni correction**: use significance level $\alpha/M$ for each individual test. Conservative but simple. - **Benjamini-Hochberg (BH) procedure**: controls the false discovery rate (FDR) rather than the family-wise error rate. More powerful when $M$ is large. - **The Sharpe ratio haircut (Harvey-Liu)**: adjust the minimum required Sharpe upward based on the number of strategies tested. A commonly cited rule: require $\text{SR} \geq 1.0$ after a Bonferroni-type adjustment, not just $\text{SR} \geq 0.5$. - **Out-of-sample holdout**: reserve a final holdout period (never touched during model selection) and evaluate the winning model on it exactly once. **Answer:** Use walk-forward CV with an embargo gap for out-of-sample evaluation; test Sharpe differences with a HAC-robust z-test or Diebold-Mariano; correct for multiple comparisons with BH or a dedicated holdout; always report net-of-cost Sharpe with turnover disclosed. --- MDP Formulation for Stock Trading [medium, Optimization] --- https://quantvault.org/problem-2151-mdp-formulation-for-stock-trading.html Q: You want to trade a single stock over a finite horizon of $T$ time steps to maximize your expected total profit. At each step you observe the current stock price $S_t$ and whether you currently hold the stock or not. At each time step, you can take one of three actions: **buy** one share (if you have no position), **sell** your share (if you are holding), or **hold** (do nothing). Buying costs you $S_t$ and selling earns you $S_t$. Assume you can only hold at most one share at a time and there are no transaction costs beyond the price itself. The stock price follows a discrete-time Markov process -- that is, $S_{t+1}$ depends only on $S_t$, not on the full history. 1. Formulate this problem as a Markov Decision Process: define the state space, action space, transition probabilities, and reward function. 2. Write down the Bellman equation for the value function $V(t, s, h)$ where $t$ is time, $s$ is the stock price, and $h \in \{0, 1\}$ indicates whether you hold the stock. 3. How would you solve this MDP in practice? Discuss the approach and any complications that arise when the price space is large or continuous. A: **How to Think About It:** This is a classic setup question that every quant should be able to rattle off. The key tension is simple: you want to buy low and sell high, but you do not know the future. The MDP framework lets you think about this rigorously -- at every point in time, given what you know right now (price and position), what is the optimal action? The entire problem reduces to defining four things cleanly: states, actions, transitions, and rewards. Once you have those, the Bellman equation writes itself. **Key Insight:** The state must capture everything you need to make an optimal decision. Here that means the current time $t$, the current price $S_t$, and whether you are holding the stock $h \in \{0, 1\}$. If you forget to include the holding indicator, you cannot distinguish "should I buy?" from "should I sell?" -- the action space depends on the state. **The Method:** **Part 1 -- MDP Formulation:** - **State space:** $\mathcal{S} = \{(t, s, h) : t \in \{0, 1, \ldots, T\},\; s \in \mathcal{P},\; h \in \{0, 1\}\}$, where $\mathcal{P}$ is the set of possible prices and $h = 1$ means you hold the stock. - **Action space:** At state $(t, s, 0)$ (no position): $\mathcal{A} = \{\text{buy}, \text{hold}\}$. At state $(t, s, 1)$ (holding): $\mathcal{A} = \{\text{sell}, \text{hold}\}$. This enforces the constraint that you cannot sell what you do not own or buy a second share. - **Transition probabilities:** Since the price is Markov, $P(S_{t+1} = s' \mid S_t = s) = p(s' \mid s)$, independent of the action taken (actions affect your portfolio, not the stock price). The holding state transitions deterministically: buy flips $h$ from 0 to 1, sell flips $h$ from 1 to 0, hold keeps $h$ unchanged. - **Reward function:** $R(t, s, h, a)$ captures the immediate cash flow: - Buy: $R = -s$ (you pay the price) - Sell: $R = +s$ (you receive the price) - Hold: $R = 0$ - At terminal time $T$, if $h = 1$, you can either force a liquidation at $S_T$ or assign $R = S_T$ as a terminal reward to close out the position. **Part 2 -- Bellman Equation:** For $t < T$: $$V(t, s, 0) = \max\left\{-s + \sum_{s'} p(s' \mid s)\, V(t+1, s', 1),\;\; \sum_{s'} p(s' \mid s)\, V(t+1, s', 0)\right\}$$ $$V(t, s, 1) = \max\left\{+s + \sum_{s'} p(s' \mid s)\, V(t+1, s', 0),\;\; \sum_{s'} p(s' \mid s)\, V(t+1, s', 1)\right\}$$ The first term in each max corresponds to acting (buy or sell), the second to holding. Terminal conditions: $V(T, s, 0) = 0$ and $V(T, s, 1) = s$ (forced liquidation at horizon). **Part 3 -- Practical Solution:** 1. **Discretize the price space.** If $S_t$ takes on $M$ possible values, the state space has size $2 \times M \times T$. For a coarse grid ($M \sim 50$-$100$, $T \sim 250$ trading days), this is easily solvable by backward induction in $O(T \cdot M^{2})$ time. 2. **Backward induction.** Start at $t = T$ with the terminal values. At each step $t = T-1, T-2, \ldots, 0$, compute $V(t, s, h)$ for all $(s, h)$ by evaluating both actions and taking the max. Store the optimal action as the policy. 3. **Complications with large/continuous price spaces:** - The curse of dimensionality hits when you add features to the state (e.g., volume, volatility regime, multiple assets). The state space grows exponentially. - For continuous prices, you need function approximation -- fit $V(t, s, h)$ with regression (Longstaff-Schwartz style) or a neural network. - Model risk is the elephant in the room: your transition probabilities $p(s' \mid s)$ are estimated, not known. The optimal policy is only as good as the model. In practice, traders add constraints (position limits, maximum holding periods) to make the policy more robust to model misspecification. **Practical Considerations:** - Adding transaction costs is straightforward: replace $R(\text{buy}) = -s - c$ and $R(\text{sell}) = s - c$ for some cost $c$. This creates a no-trade region around the current position, which is economically sensible. - A discount factor $\gamma < 1$ can be included if you want to penalize delayed profits, though for short-horizon trading this is often set to 1. - If the Markov assumption is too restrictive, you can expand the state to include lagged prices or technical indicators, but this increases the state space. **Answer:** The MDP has states $(t, s, h)$, actions $\{\text{buy}, \text{sell}, \text{hold}\}$ with feasibility constraints based on $h$, Markov transition probabilities for the price, and immediate rewards equal to the cash flow from each trade. The value function satisfies the Bellman equations above and is solved by backward induction over the finite horizon. For large or continuous state spaces, use function approximation methods like fitted value iteration or Longstaff-Schwartz regression. --- Optimal Bid-Ask Quotes Against an Informed Trader [medium, Market Microstructure] --- https://quantvault.org/problem-2163-optimal-bid-ask-quotes-against-an-informed-trader.html Q: You are making a market in a stock whose true value $V$ is uniformly distributed on $[0, 100]$. You must quote a bid $b$ and an ask $a$, with $b a$, and they sell to you at your bid $b$ if $V < b$. No trade occurs when $b \leq V \leq a$. What bid and ask should you quote to maximize your expected profit? What does the optimal solution tell you about market making against a fully informed counterparty? A: **How to Think About It:** Before writing any integrals, ask yourself: when does a trade happen, and who benefits? The trader only buys from you when the asset is worth MORE than your ask -- so every trade on the ask side loses you money. Similarly, they only sell to you when the asset is worth LESS than your bid -- so every trade on the bid side also loses you money. Every single trade goes against you. Your expected profit from any trade is negative by construction. The only way to avoid losing money is to avoid trading entirely, which means quoting a spread so wide that the trader never finds a favorable price. That gut check already tells you where this is heading: the only "optimal" strategy in this adversarial setting is $b = 0$, $a = 100$ -- i.e., post no meaningful quotes at all. **Quick Estimate:** Take $b = 25$ and $a = 75$. The ask side: if $V > 75$, the trader buys at 75. The asset is worth on average $87.5$ in that region, so you lose $87.5 - 75 = 12.5$ per trade, and this happens with probability $25/100 = 0.25$. Ask-side loss: $0.25 \times (-12.5) = -3.125$. The bid side: if $V a$. You sell an asset worth $V$ for $a$, so your profit is $a - V < 0$. $$\Pi_a(a) = \int_a^{100} (a - V) \cdot \frac{1}{100}\, dV = \frac{1}{100}\left[aV - \frac{V^2}{2}\right]_a^{100} = \frac{1}{100}\left(100a - 5000 - a^2 + \frac{a^2}{2}\right) = \frac{100a - 5000 - a^2/2}{100}$$ *Bid side:* The trader sells at $b$ when $V < b$. You buy an asset worth $V$ for $b$, so your profit is $V - b < 0$. $$\Pi_b(b) = \int_0^{b} (V - b) \cdot \frac{1}{100}\, dV = \frac{1}{100}\left[\frac{V^2}{2} - bV\right]_0^b = \frac{-b^2/2}{100}$$ *Total:* $$\Pi(b, a) = \frac{1}{100}\left(-\frac{b^2}{2} + 100a - 5000 - \frac{a^2}{2}\right)$$ Optimize over $a$: $$\frac{\partial \Pi}{\partial a} = \frac{1}{100}(100 - a) = 0 \implies a^{*} = 100$$ Optimize over $b$: $$\frac{\partial \Pi}{\partial b} = \frac{1}{100}(-b) = 0 \implies b^{*} = 0$$ Substituting back: $\Pi(0, 100) = \frac{1}{100}(0 + 10000 - 5000 - 5000) = 0$. **Answer:** The optimal quotes are $b^{*} = 0$, $a^{*} = 100$, yielding expected profit of exactly $\$0$. In a world with only perfectly informed traders, there is no profitable market to make. --- Revenue Optimization with Poisson Demand [medium, Optimization] --- https://quantvault.org/problem-2172-revenue-optimization-with-poisson-demand.html Q: Daily rental demand for a single apartment is modeled as $Q \sim \text{Poisson}(\lambda_0 e^{-\beta p})$, where $p \geq 0$ is the nightly price, and $\lambda_0 > 0$, $\beta > 0$ are parameters. 1. Choose $p$ to maximize expected revenue $E[pQ]$. Find the optimal price $p^{*}$. 2. Now suppose you must also keep the expected vacancy rate below $v_{\max} \in (0, 1)$, meaning $P(Q = 0) \leq v_{\max}$. Add this constraint and solve for the optimal price. Characterize when the vacancy constraint binds. A: **How to Think About It:** This is a classic price-quantity tradeoff. Raising the price increases revenue per unit but decreases demand. With Poisson demand, the expected quantity equals the rate parameter, so expected revenue is $p \cdot \lambda_0 e^{-\beta p}$ -- a hump-shaped function of $p$. The unconstrained optimum is where the marginal revenue from a price increase exactly offsets the lost demand. The vacancy constraint adds a floor on demand: if you price too high, the apartment sits empty too often. **Quick Estimate:** Expected revenue is $R(p) = p \lambda_0 e^{-\beta p}$. Setting the derivative to zero: $\lambda_0 e^{-\beta p}(1 - \beta p) = 0$, giving $p^{*} = 1/\beta$. For example, if $\beta = 0.1$ (each dollar of price reduces log-demand by 0.1), the optimal price is $\$10$. Revenue at the optimum is $\lambda_0 \cdot 10 \cdot e^{-1} \approx 3.68 \lambda_0$. **Approach:** Differentiate expected revenue for part (i). For part (ii), use Lagrangian optimization with the vacancy constraint. **Formal Solution:** **Part (i): Unconstrained optimization.** Since $Q \sim \text{Poisson}(\lambda)$ with $\lambda = \lambda_0 e^{-\beta p}$, we have $E[Q] = \lambda = \lambda_0 e^{-\beta p}$. Expected revenue: $$R(p) = p \cdot E[Q] = p \lambda_0 e^{-\beta p}$$ Take the derivative: $$R'(p) = \lambda_0 e^{-\beta p} - \beta p \lambda_0 e^{-\beta p} = \lambda_0 e^{-\beta p}(1 - \beta p)$$ Setting $R'(p) = 0$: since $\lambda_0 e^{-\beta p} > 0$ for all $p$, we need $1 - \beta p = 0$, so: $$p^{*} = \frac{1}{\beta}$$ Second derivative check: $R''(p) = \lambda_0 e^{-\beta p}(-\beta - \beta + \beta^2 p) = \lambda_0 e^{-\beta p}(\beta^2 p - 2\beta)$. At $p = 1/\beta$: $R''(1/\beta) = \lambda_0 e^{-1}(\beta - 2\beta) = -\beta \lambda_0 e^{-1} \ln(1/v_{\max})$ for $\bar{p} > 0$ (otherwise the constraint is infeasible at any positive price). The constraint binds when $p^{*} > \bar{p}$, i.e., when: $$\frac{1}{\beta} > \frac{1}{\beta} \ln\left(\frac{\lambda_0}{\ln(1/v_{\max})}\right)$$ $$1 > \ln\left(\frac{\lambda_0}{\ln(1/v_{\max})}\right)$$ $$e > \frac{\lambda_0}{\ln(1/v_{\max})}$$ $$\lambda_0 < e \cdot \ln(1/v_{\max})$$ So the constrained solution is: $$p^{*}_{\text{constrained}} = \begin{cases} 1/\beta & \text{if } \lambda_0 \geq e \cdot \ln(1/v_{\max}) \\ \bar{p} = \frac{1}{\beta}\ln\left(\frac{\lambda_0}{\ln(1/v_{\max})}\right) & \text{if } \lambda_0 < e \cdot \ln(1/v_{\max}) \end{cases}$$ When $\lambda_0$ is large (high base demand), the unconstrained optimum already keeps vacancy low enough. When $\lambda_0$ is small, you must price lower than the revenue-optimal level to maintain sufficient occupancy. **Answer:** (i) $p^{*} = 1/\beta$. (ii) The vacancy constraint binds when $\lambda_0 < e \cdot \ln(1/v_{\max})$, in which case the optimal price is $\bar{p} = \frac{1}{\beta}\ln\left(\frac{\lambda_0}{\ln(1/v_{\max})}\right)$. --- Two-Sided Market Making With Overround [hard, Finance] --- https://quantvault.org/problem-2191-two-sided-market-making-with-overround.html Q: You are making a two-sided market on a binary event. The true probability that the event occurs is $p$. You quote decimal odds $O_{\text{yes}}$ and $O_{\text{no}}$ (payout includes stake, so a customer who bets $\$1$ on "yes" receives $O_{\text{yes}}$ if the event occurs and loses the dollar otherwise). A customer arrives with a private belief $p_c$ about the event probability, drawn uniformly on $[0, 1]$. The customer computes their expected value on each side: - Bet "yes": $\text{EV} = p_c \cdot O_{\text{yes}} - 1$ - Bet "no": $\text{EV} = (1 - p_c) \cdot O_{\text{no}} - 1$ If exactly one side has positive expected value, the customer takes that side. If both sides have positive expected value, the customer takes the side with higher EV. If neither side has positive EV, the customer does not trade. The customer always stakes $\$1$. 1. Derive your expected profit per arriving customer as a function of $(O_{\text{yes}}, O_{\text{no}})$ and $p$. 2. Find the pair of odds $(O_{\text{yes}}^{*}, O_{\text{no}}^{*})$ that maximizes your expected profit, and verify that the maximum is strictly positive for all $p \in (0,1)$. A: **How to Think About It:** You are a market maker quoting odds on a yes/no event. Your edge comes from the overround -- the implied probabilities you quote sum to more than 1, so on average you collect more than you pay out. But you are not trading against a single counterparty; customers arrive with random beliefs and only trade when they think they have an edge. The question is: how does this adverse selection eat into your vig? The key observation is that customers with extreme beliefs (very high $p_c$ or very low $p_c$) are the ones who trade. Customers in the middle see no edge on either side and walk away. Your profit depends on the fraction of customers who trade and how much you make (or lose) on each trade on average. **Quick Estimate:** Take $p = 0.5$ and symmetric odds $O_{\text{yes}} = O_{\text{no}} = 1.8$ (implied overround $2/1.8 \approx 1.11$). The breakeven belief for "yes" is $p_c > 1/1.8 \approx 0.556$, and for "no" is $p_c a$ (EV of yes is positive) and bets "no" when $p_c 1$, $O_{\text{no}} > 1$, and $1/O_{\text{yes}} + 1/O_{\text{no}} > 1$, which gives $a > b$. In this regime, customers with $p_c \in [b, a]$ see no positive EV on either side and do not trade. Since $p_c \sim \text{Uniform}[0,1]$: - Customer bets "no" (probability $b$): market maker's expected profit per trade is $1 - (1-p) O_{\text{no}}$. - Customer bets "yes" (probability $1 - a$): market maker's expected profit per trade is $1 - p \, O_{\text{yes}}$. - Customer walks away (probability $a - b$): profit is $0$. Total expected profit: $$E[\pi] = b \bigl(1 - (1-p)\, O_{\text{no}}\bigr) + (1 - a)\bigl(1 - p\, O_{\text{yes}}\bigr)$$ Substituting $a = 1/O_{\text{yes}}$ and $b = 1 - 1/O_{\text{no}}$: $$E[\pi] = \left(1 - \frac{1}{O_{\text{no}}}\right)\!\left(1 - (1-p)\, O_{\text{no}}\right) + \left(1 - \frac{1}{O_{\text{yes}}}\right)\!\left(1 - p\, O_{\text{yes}}\right)$$ Expanding and simplifying: $$E[\pi] = 3 - p\, O_{\text{yes}} - (1-p)\, O_{\text{no}} - \frac{1}{O_{\text{yes}}} - \frac{1}{O_{\text{no}}}$$ This is the answer to part 1. **Optimization (Part 2):** To maximize over $(O_{\text{yes}}, O_{\text{no}})$, take partial derivatives: $$\frac{\partial E[\pi]}{\partial O_{\text{yes}}} = -p + \frac{1}{O_{\text{yes}}^2} = 0 \quad \Longrightarrow \quad O_{\text{yes}}^{*} = \frac{1}{\sqrt{p}}$$ $$\frac{\partial E[\pi]}{\partial O_{\text{no}}} = -(1-p) + \frac{1}{O_{\text{no}}^2} = 0 \quad \Longrightarrow \quad O_{\text{no}}^{*} = \frac{1}{\sqrt{1-p}}$$ The second derivatives are $-2/O^3 0$$ The minimum profit occurs at $p = 1/2$ with symmetric optimal odds $O_{\text{yes}}^{*} = O_{\text{no}}^{*} = \sqrt{2} \approx 1.414$ (implied overround $2\sqrt{2}/2 \cdot 100\% \approx 141\%$). **Sanity Check:** The optimal overround is $1/O_{\text{yes}}^{*} + 1/O_{\text{no}}^{*} = \sqrt{p} + \sqrt{1-p}$. This exceeds 1 for all $p \in (0,1)$ (since each term is positive and their sum at $p = 0$ or $p = 1$ equals 1, and it is concave with interior maximum $\sqrt{2} > 1$). The implied probabilities are $\sqrt{p}$ and $\sqrt{1-p}$, which compress probabilities toward $1/2$ -- exactly the "shading" a market maker applies to protect against adverse selection. **Answer:** Expected profit per customer: $$E[\pi] = 3 - p\, O_{\text{yes}} - (1-p)\, O_{\text{no}} - \frac{1}{O_{\text{yes}}} - \frac{1}{O_{\text{no}}}$$ Optimal odds: $O_{\text{yes}}^{*} = 1/\sqrt{p}$, $O_{\text{no}}^{*} = 1/\sqrt{1-p}$. Maximum profit: $3 - 2\sqrt{p} - 2\sqrt{1-p} > 0$ for all $p \in (0,1)$. --- VaR and ES via Peaks-Over-Threshold [hard, Statistics] --- https://quantvault.org/problem-2193-var-and-es-via-peaks-over-threshold.html Q: You are modeling extreme daily losses using Extreme Value Theory. Suppose daily losses $L_t$ above a high threshold $u$ follow a Generalized Pareto Distribution (GPD) with shape parameter $\xi$ and scale parameter $\sigma$: $$P(L_t - u > y \mid L_t > u) = \begin{cases} \left(1 + \xi \frac{y}{\sigma}\right)^{-1/\xi} & \xi \neq 0 \\ e^{-y/\sigma} & \xi = 0 \end{cases}$$ for $y > 0$ (with $y < -\sigma / \xi$ when $\xi < 0$). You observe $T$ total daily losses, of which $N_u$ exceed the threshold $u$. 1. Derive closed-form expressions for VaR at confidence level $\alpha$ (close to 1) and Expected Shortfall (ES) at level $\alpha$, in terms of $\xi$, $\sigma$, $u$, $N_u$, and $T$. 2. Explain how you would estimate $\xi$ and $\sigma$ from the $N_u$ exceedances, and how those estimates plug into your VaR and ES formulas. 3. State the regularity conditions on the threshold $u$ and the shape parameter $\xi$ that are required for these formulas to be valid. A: **How to Think About It:** The whole point of POT is that you do not need to model the entire loss distribution -- you only need to get the tail right. The Pickands-Balkema-de Haan theorem tells you that for a broad class of distributions, the excess losses above a high threshold converge to a GPD. So the game is: pick a high threshold $u$, fit a GPD to the exceedances, then extrapolate into the far tail to get VaR and ES. This is the standard approach for risk management when you care about the 99th or 99.5th percentile and your data has fat tails. **Quick Estimate:** Suppose you have $T = 1000$ daily observations, $N_u = 50$ exceed the threshold $u = 2\%$, and you fit $\xi = 0.25$, $\sigma = 0.8\%$. For $\alpha = 0.99$: the quantile $(T/N_u)(1-\alpha) = (1000/50)(0.01) = 0.2$. Then VaR $= 2\% + (0.8\%/0.25)(0.2^{-0.25} - 1) = 2\% + 3.2\%(0.2^{-0.25} - 1)$. Now $0.2^{-0.25} = (1/0.2)^{0.25} = 5^{0.25} \approx 1.495$. So VaR $\approx 2\% + 3.2\% \times 0.495 \approx 3.58\%$. ES is VaR/(1 - $\xi$) plus a correction: $\text{ES} \approx 3.58\%/(1-0.25) + (\sigma - \xi u)/(1-\xi) = 4.77\% + (0.8\% - 0.25 \times 2\%)/0.75 = 4.77\% + 0.40\% = 5.17\%$. Fat tails ($\xi > 0$) push ES well above VaR. **Approach:** We combine the semi-parametric tail estimator $\bar{F}(x) = (N_u/T)(1 + \xi(x-u)/\sigma)^{-1/\xi}$ with standard quantile inversion for VaR, and then integrate the tail for ES. **Formal Solution:** *Step 1: Tail probability.* For $x > u$, the unconditional survival function is: $$\bar{F}(x) = P(L_t > u) \cdot P(L_t > x \mid L_t > u) = \frac{N_u}{T}\left(1 + \xi\frac{x - u}{\sigma}\right)^{-1/\xi}$$ where we estimate $P(L_t > u) \approx N_u / T$. *Step 2: VaR derivation.* VaR at level $\alpha$ is the quantile $q_\alpha$ such that $P(L_t > q_\alpha) = 1 - \alpha$. Setting $\bar{F}(q_\alpha) = 1 - \alpha$ and solving: $$\frac{N_u}{T}\left(1 + \xi\frac{q_\alpha - u}{\sigma}\right)^{-1/\xi} = 1 - \alpha$$ $$1 + \xi\frac{q_\alpha - u}{\sigma} = \left(\frac{N_u}{T(1 - \alpha)}\right)^{\xi}$$ $$\boxed{\text{VaR}_\alpha = u + \frac{\sigma}{\xi}\left[\left(\frac{N_u}{T(1 - \alpha)}\right)^{\xi} - 1\right]}$$ This is the POT-GPD VaR formula. *Step 3: ES derivation.* Expected Shortfall is the conditional expectation of losses given they exceed VaR: $$\text{ES}_\alpha = E[L_t \mid L_t > \text{VaR}_\alpha] = \text{VaR}_\alpha + E[L_t - \text{VaR}_\alpha \mid L_t > \text{VaR}_\alpha]$$ The mean excess function of the GPD with shape $\xi \text{VaR}_\alpha] = \frac{\sigma + \xi(\text{VaR}_\alpha - u)}{1 - \xi}$$ Substituting $\text{VaR}_\alpha - u = (\sigma/\xi)[(N_u/(T(1-\alpha)))^\xi - 1]$: $$\text{ES}_\alpha = \text{VaR}_\alpha + \frac{\sigma + \xi \cdot \frac{\sigma}{\xi}\left[\left(\frac{N_u}{T(1-\alpha)}\right)^\xi - 1\right]}{1 - \xi}$$ $$= \text{VaR}_\alpha + \frac{\sigma\left(\frac{N_u}{T(1-\alpha)}\right)^\xi}{1 - \xi}$$ This simplifies to: $$\boxed{\text{ES}_\alpha = \frac{\text{VaR}_\alpha}{1 - \xi} + \frac{\sigma - \xi u}{1 - \xi}}$$ *Step 4: Estimation.* Given the $N_u$ exceedances $y_1 = L_{(1)} - u, \ldots, y_{N_u} = L_{(N_u)} - u$, estimate $\xi$ and $\sigma$ by maximizing the GPD log-likelihood: $$\ell(\xi, \sigma) = -N_u \ln \sigma - \left(1 + \frac{1}{\xi}\right)\sum_{i=1}^{N_u}\ln\left(1 + \xi\frac{y_i}{\sigma}\right)$$ This is solved numerically (e.g., Newton-Raphson or profile likelihood). The MLEs $\hat{\xi}$ and $\hat{\sigma}$ are then plugged directly into the VaR and ES formulas above. Confidence intervals can be obtained via the delta method or profile likelihood. *Step 5: Regularity conditions.* - **Threshold $u$**: Must be high enough that the GPD approximation is accurate (bias-variance trade-off). In practice, use a mean residual life plot or the Hill plot to select $u$. As $u \to \infty$, the GPD approximation improves but $N_u$ shrinks, increasing variance. - **Shape parameter $\xi$**: For ES to be finite, we need $\xi -1/2$. - **Support constraint**: When $\xi -1/2$ for MLE consistency. --- Designing a City Rent Prediction System [medium, Machine Learning] --- https://quantvault.org/problem-2202-designing-a-city-rent-prediction-system.html Q: You are designing an end-to-end machine learning system to predict rental prices across a city, leveraging structured listing data, text descriptions, images, and geospatial features. Address the following: (a) **Data collection and cleaning** -- What data sources would you use? What cleaning steps matter most? (b) **Feature engineering** -- How would you extract useful signals from text descriptions, listing photos, and geospatial data? (c) **Model selection** -- What model architectures would you consider, and how would you combine structured and unstructured features? (d) **Validation** -- How should you split data to avoid leakage, given that listings have both temporal and geographic structure? (e) **Fairness and robustness** -- What biases are you worried about, and how do you detect and mitigate them? A: **How to Think About It:** Rent prediction is a hedonic pricing problem -- you are trying to decompose rental prices into contributions from a property's observable attributes. The core challenge is that you have multiple modalities (text, images, numbers, location) that need to be combined, and several types of leakage to guard against: temporal leakage (training on future data), spatial leakage (testing on neighborhoods geographically adjacent to training data), and feature leakage (embedding information that is only available at listing time, not at prediction time). A senior ML practitioner thinks about these failure modes before thinking about model architecture. **Key Insight:** The biggest practical risk is leakage -- a model that looks great in validation but fails in deployment because it learned a spurious signal. Every design decision should be evaluated with the question: would this feature be available at the time of prediction in production? **The Method:** **(a) Data Collection and Cleaning.** Primary sources: - Rental listings (Zillow, Apartments.com, Craigslist scrapes): price, sqft, bedrooms, bathrooms, amenities, listing text, photos, listing date. - Public records: property tax assessments, deed transfers, building permits, zoning maps. - Geospatial: OpenStreetMap (transit stops, parks, schools, restaurants), walk scores, crime statistics by census tract. - Temporal: days on market, relisting history. Cleaning priorities: - Deduplicate relisted properties (same unit listed multiple times at different prices). - Cap outliers in price and sqft at reasonable percentiles -- do not remove them silently. - Handle missing photos or descriptions with explicit missingness indicators (do not impute with zeros). - Normalize price to dollars per square foot as an alternative target. **(b) Feature Engineering.** **Numeric/structured:** sqft, bedrooms, bathrooms, floor number, building age, parking included (binary), pet policy. **Geospatial:** - Distance to nearest subway station, bus stop, highway. - Walk score, bike score, transit score. - Neighborhood-level aggregates: median household income, school ratings, crime rate -- all from census tract or zip code, lagged by at least 1 year to avoid leakage. - Spatial lag features: average rent of nearby listings in the past 6 months. **Text features (listing descriptions):** - Bag-of-words or TF-IDF as a fast baseline. - Fine-tuned transformer embeddings (e.g., a lightweight BERT variant) for richer semantic features. - Explicit keyword extraction: doorman, laundry, renovated, exposed brick, dishwasher. These amenity indicators are often more useful than dense embeddings for tabular models. - Sentiment score as a meta-feature. **Image features (listing photos):** - CNN-extracted embeddings from a pretrained model (ResNet, EfficientNet). - Structured attributes from a fine-tuned classifier: natural light score, finish quality (low/medium/high), presence of outdoor space. - Be careful: photo quality and count are correlated with price and listing quality, so include them as explicit features. **(c) Model Selection.** - **Hedonic regression** (interpretable baseline): $\log(\text{price}) \sim \beta^\top x$ with regularization. Use this to understand feature importance and establish a benchmark. Interpretable coefficients are valuable for stakeholder communication. - **Gradient Boosted Trees (XGBoost/LightGBM):** Best for structured/tabular features with nonlinear interactions. Fast, robust to outliers, handles missing values natively. This is typically the production workhorse. - **Neural network fusion model:** For incorporating text and image embeddings, use a multi-input architecture: embed text via transformer, embed images via CNN, concatenate with structured features, pass through a small MLP. This captures cross-modal interactions. - **Ensemble:** Stack GBT and neural network predictions with a linear meta-learner. **(d) Validation Design.** Do not use random K-fold CV -- it leaks future information and spatial autocorrelation. - **Temporal split:** Train on listings before date $T$, validate on listings in $[T, T + \Delta]$, test on listings after $T + \Delta$. This mirrors production deployment. - **Purging:** Remove listings from the validation set that were active during the training period (to avoid using stale information from the same listing). - **Spatial cross-validation:** Hold out entire neighborhoods or census tracts for the validation set, not individual listings. This tests whether the model generalizes to new areas, not just to new listings in known areas. - **Leakage audit:** For every feature, ask: is this derived from data that postdates the listing? Common culprits -- using the final closed price as a feature, using aggregate statistics computed on the full dataset before splitting. **(e) Fairness and Robustness.** - **Protected attribute proxies:** Geospatial features (neighborhood, zip code) can encode race/ethnicity historically. Monitor model predictions for disparate impact across demographic groups using census data as a proxy. - **Disparate impact testing:** Check whether the model systematically over- or under-predicts prices in majority-minority neighborhoods. Use demographic parity or equalized odds metrics. - **Distribution shift:** Rental markets can shift rapidly (COVID, interest rate changes). Monitor feature distributions and prediction errors over time. Retrain on rolling windows rather than full historical data. - **Outlier robustness:** Luxury listings and social housing are in the same dataset but follow different pricing mechanisms. Consider separate models or explicit regime indicators. **Answer:** A robust city rent prediction system requires: multi-source data collection with careful deduplication; structured + text + image features with explicit leakage checks; GBT as the primary model with optional neural fusion for unstructured features; time-based and geographically-stratified validation splits; and ongoing monitoring for disparate impact and distribution shift. --- European Call Option With Infinite Maturity [medium, Options Pricing] --- https://quantvault.org/problem-2204-european-call-option-with-infinite-maturity.html Q: What is the value of a vanilla European call option as the time to maturity $T \to \infty$? Assume the Black-Scholes framework with constant volatility $\sigma$, constant risk-free rate $r > 0$, spot price $S$, and strike $K$. Consider two cases: 1. The underlying pays **no dividends** ($q = 0$). 2. The underlying pays a **continuous dividend yield** $q > 0$. A: **How to Think About It:** Don't reach for the Black-Scholes formula first — probe the *limit* with economics. A call is the right to buy at $K$ forever. Two forces race as $T\to\infty$: (i) the present value of the strike payment, $Ke^{-rT}$, vanishes because $r>0$ — the strike becomes free; (ii) the *deliverable* stock is worth $Se^{-qT}$ today, because a call holder receives price appreciation but **not** the dividends the stock leaks out at rate $q$. The whole answer hinges on which of these two survives. This is a **limit/extreme-case probe**: replace the messy formula with 'what does each term do at the boundary?' The naive trap is to say 'longer maturity = more optionality = more value' and expect the call to keep rising — false once dividends bleed the underlying dry. **Quick Estimate:** Use $C = Se^{-qT}\Phi(d_1) - Ke^{-rT}\Phi(d_2)$ and price each piece as $T$ grows. - The drift inside $d_1$ is $(r-q+\sigma^2/2)T$, which grows like $T$ while the denominator grows like $\sqrt T$, so $d_1\to+\infty$ whenever $r-q+\sigma^2/2>0$ — true here — giving $\Phi(d_1)\to 1$ and likewise $\Phi(d_2)\to 1$. So both $\Phi$ factors saturate to $1$; the limit is decided entirely by the two exponential prefactors. - **No dividends ($q=0$):** prefactor on the stock is $Se^{0}=S$ (doesn't decay), prefactor on the strike is $Ke^{-rT}\to 0$. So $C\to S\cdot 1 - 0 = S$. Sanity plug: $S=100,K=90,r=3\%,\sigma=20\%$ gives $C\approx 96.3$ at $T=100$ — already $96\%$ of the way to $S=100$, and $\approx 100.0$ by $T=500$. Lands on $S$. - **With dividends ($q>0$):** now the stock prefactor $Se^{-qT}\to 0$ *and* $Ke^{-rT}\to 0$ — both legs decay, so $C\to 0$. Plug $q=2\%$: the same option is $\approx 27$ at $T=10$ but has collapsed to $\approx 11$ at $T=100$ and $\approx 0$ by $T=500$. It rises then falls — the dividend drag eventually wins. **Approach:** Take $T\to\infty$ in the (dividend-adjusted) Black-Scholes formula, tracking each $\Phi$ factor and each discount prefactor separately. **Formal Solution:** **(a) No dividends ($q=0$).** The price is $$C = S\,\Phi(d_1) - Ke^{-rT}\,\Phi(d_2),\qquad d_{1}=\frac{\ln(S/K)+(r+\sigma^2/2)T}{\sigma\sqrt T},\ \ d_2=d_1-\sigma\sqrt T.$$ As $T\to\infty$: the numerator of $d_1$ grows $\propto T$, denominator $\propto\sqrt T$, so $d_1\to+\infty$ and $\Phi(d_1)\to 1$. Also $d_2=d_1-\sigma\sqrt T$; since $d_1$ grows like $\sigma\sqrt T\cdot(\tfrac{r+\sigma^2/2}{\sigma^2})\sqrt T \to$ faster than $\sigma\sqrt T$, $d_2\to+\infty$ too, so $\Phi(d_2)\to 1$. And $Ke^{-rT}\to 0$ since $r>0$. Hence $$\boxed{\lim_{T\to\infty}C = S\cdot 1 - 0\cdot 1 = S.}$$ Economically: over an infinite horizon the strike is free (its PV is zero) and the stock is almost surely far in the money, so the call *is* the stock. **(b) Continuous dividend yield $q>0$.** Now $$C = Se^{-qT}\,\Phi(d_1) - Ke^{-rT}\,\Phi(d_2),\qquad d_1=\frac{\ln(S/K)+(r-q+\sigma^2/2)T}{\sigma\sqrt T}.$$ Both $\Phi$ factors still tend to a finite limit in $[0,1]$, but the prefactors both vanish: $Se^{-qT}\to 0$ and $Ke^{-rT}\to 0$. Therefore $$\boxed{\lim_{T\to\infty}C = 0.}$$ The forgone dividends, in present value, accumulate to the entire stock price over an infinite horizon, wiping out the option. (This is exactly why American calls on dividend payers can be worth early-exercising — the European version is heading to zero.) **Answer:** With no dividends, $C\to S$. With continuous dividend yield $q>0$, $C\to 0$. --- Newton's Method: Quadratic Convergence Proof and Stopping Rule [medium, Optimization] --- https://quantvault.org/problem-2206-newton-s-method-quadratic-convergence-proof-and-stopping-rul.html Q: Let $f : \mathbb{R} \to \mathbb{R}$ be twice continuously differentiable, and suppose $f(\alpha) = 0$ with $f'(\alpha) \neq 0$ (i.e., $\alpha$ is a simple root). 1. Write down Newton's method for approximating $\alpha$. 2. Prove that Newton's method converges locally and quadratically: if $x_0$ is sufficiently close to $\alpha$, then $|x_{n+1} - \alpha| \leq C |x_n - \alpha|^2$ for some constant $C$. 3. State a practical stopping rule that controls the absolute error in the root estimate. A: **How to Think About It:** Newton's method is the workhorse of numerical root-finding. The idea is simple: at each step, approximate $f$ by its tangent line and solve for where the tangent crosses zero. Geometrically, you are linearizing $f$ around your current guess. The reason it converges so fast (quadratically) is that a smooth function is well-approximated by its tangent near the root, and the approximation error is second-order in the distance to the root. The key assumption is that $f'(\alpha) \neq 0$ -- if the derivative vanishes at the root, convergence degrades to linear. **Part 1: Newton's Method.** Given an initial guess $x_0$, the iteration is: $$x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}$$ This is obtained by setting the tangent line $f(x_n) + f'(x_n)(x - x_n) = 0$ and solving for $x$. **Part 2: Proof of Local Quadratic Convergence.** Define the error $e_n = x_n - \alpha$. We want to show $|e_{n+1}| \leq C |e_n|^2$. By Taylor's theorem, expand $f(x_n)$ around $\alpha$: $$f(x_n) = f(\alpha) + f'(\alpha) e_n + \frac{1}{2} f''(\xi_n) e_n^2$$ where $\xi_n$ is between $x_n$ and $\alpha$. Since $f(\alpha) = 0$: $$f(x_n) = f'(\alpha) e_n + \frac{1}{2} f''(\xi_n) e_n^2$$ Similarly, expand $f'(x_n)$: $$f'(x_n) = f'(\alpha) + f''(\eta_n) e_n$$ for some $\eta_n$ between $x_n$ and $\alpha$. Now compute the error at step $n+1$: $$e_{n+1} = x_{n+1} - \alpha = x_n - \frac{f(x_n)}{f'(x_n)} - \alpha = e_n - \frac{f(x_n)}{f'(x_n)}$$ $$= \frac{e_n f'(x_n) - f(x_n)}{f'(x_n)}$$ Substituting the Taylor expansions into the numerator: $$e_n f'(x_n) - f(x_n) = e_n [f'(\alpha) + f''(\eta_n) e_n] - [f'(\alpha) e_n + \tfrac{1}{2} f''(\xi_n) e_n^2]$$ $$= f'(\alpha) e_n + f''(\eta_n) e_n^2 - f'(\alpha) e_n - \tfrac{1}{2} f''(\xi_n) e_n^2$$ $$= e_n^2 \left[ f''(\eta_n) - \tfrac{1}{2} f''(\xi_n) \right]$$ As $x_n \to \alpha$, both $\eta_n, \xi_n \to \alpha$, so $f''(\eta_n) - \frac{1}{2}f''(\xi_n) \to \frac{1}{2} f''(\alpha)$. Thus: $$e_{n+1} = \frac{e_n^2 [f''(\eta_n) - \frac{1}{2}f''(\xi_n)]}{f'(x_n)}$$ For $x_0$ sufficiently close to $\alpha$, $f'(x_n)$ is bounded away from zero (since $f'(\alpha) \neq 0$ and $f'$ is continuous), and the bracketed term is bounded. Therefore there exists $C > 0$ such that: $$|e_{n+1}| \leq C |e_n|^2$$ More precisely, the asymptotic error constant is: $$\lim_{n \to \infty} \frac{|e_{n+1}|}{|e_n|^2} = \frac{|f''(\alpha)|}{2|f'(\alpha)|}$$ This confirms quadratic convergence: each iteration roughly squares the error (and scales by the constant $|f''(\alpha)|/(2|f'(\alpha)|)$). **Part 3: Practical Stopping Rule.** The standard practical stopping rule uses the step size as a proxy for the error. By the mean value theorem: $$f(x_n) = f(x_n) - f(\alpha) = f'(\zeta_n)(x_n - \alpha)$$ so $|x_n - \alpha| = |f(x_n)|/|f'(\zeta_n)| \approx |f(x_n)|/|f'(x_n)| = |x_n - x_{n+1}|$. Therefore, a practical stopping rule is: $$|x_{n+1} - x_n| < \epsilon$$ where $\epsilon$ is the desired absolute error tolerance. This works because in the regime of quadratic convergence, $|x_{n+1} - x_n| \approx |x_n - \alpha|$, so the step size is a good proxy for the actual error. An alternative (or additional) criterion is $|f(x_n)| < \delta$ for some tolerance $\delta$, but this can be misleading when $|f'(\alpha)|$ is very large or very small. For robust implementations, use both criteria: $$|x_{n+1} - x_n| < \epsilon \quad \text{and} \quad |f(x_n)| < \delta$$ **Answer:** Newton's iteration is $x_{n+1} = x_n - f(x_n)/f'(x_n)$. It converges quadratically with asymptotic constant $|f''(\alpha)|/(2|f'(\alpha)|)$, proven via Taylor expansion of the error recurrence. A practical stopping rule is $|x_{n+1} - x_n| < \epsilon$. --- Onsite Data Analysis Project [medium, Machine Learning] --- https://quantvault.org/problem-2208-onsite-data-analysis-project.html Q: You are given an unfamiliar dataset during an onsite interview and asked to perform a complete data analysis in roughly one hour. The dataset could be anything -- customer transactions, sensor readings, financial returns. You have access to Python (pandas, sklearn, matplotlib) and a Jupyter notebook. Walk through your full approach: how do you spend your time, what do you look at first, how do you decide what to model, and how do you present your findings? Be specific about the tools and techniques you would use at each stage. Assume the interviewer cares as much about your process and communication as the final result. A: **How to Think About It:** The biggest mistake candidates make is diving straight into modeling. Interviewers are testing whether you can think clearly under time pressure with messy, unfamiliar data. The right move is to spend the first third of your time just understanding the data -- because the single most common failure mode is building a model on data you do not actually understand. A working end-to-end pipeline with a simple model beats a half-finished complex model every time. **Key Insight:** Treat this like a real work deliverable, not an exam. You are telling a story: "Here is what I found in the data, here is what I built, here is what I would do next." The interviewer wants to see structured thinking and clear communication at every step. **The Method:** 1. **Data exploration (15-20 min):** - Load the data: `df.shape`, `df.dtypes`, `df.head()` to get oriented. - `df.describe()` for summary statistics on numerics. Look for suspicious min/max values, high cardinality, or features with zero variance. - Check missingness: `df.isnull().sum()`. If more than 30% of a column is missing, flag it but do not drop it yet. - Identify the target variable (ask the interviewer if ambiguous) and feature types (numeric, categorical, datetime). - Compute a correlation matrix and eyeball it for strong relationships or multicollinearity. 2. **Data cleaning (10-15 min):** - Handle missing values: median imputation for numeric features, mode for categorical, or create a "missing" indicator if the missingness itself might be informative. - Remove exact duplicates. - Cap outliers at the 1st/99th percentile or use a log transform for heavy-tailed features. - Convert date columns to datetime, extract useful features (day of week, month, time since some anchor). - One-hot encode categoricals with few levels; label-encode or target-encode high-cardinality ones. 3. **Exploratory analysis (15-20 min):** - Univariate: histograms or KDE plots for key features. Box plots to compare distributions across groups. - Bivariate: scatter plots of the top correlated features vs. the target. For classification, look at class balance. - If time series: plot the target over time, look for trends, seasonality, regime changes. - Write down 2-3 hypotheses based on what you see (e.g., "feature X seems to drive the target"). 4. **Modeling (15-20 min):** - Train/test split (80/20, or time-based if temporal). Never touch the test set until the end. - Start with a baseline: predict the mean (regression) or majority class (classification). Record the metric. - Fit a simple model first: linear/logistic regression. Check coefficients for sanity. - Then try one tree-based model: Random Forest or XGBoost with default hyperparameters. - Evaluate on the held-out set using an appropriate metric (RMSE for regression, AUC for classification). Compare to baseline. - Extract feature importances from the tree model. Do they match your EDA hypotheses? 5. **Presentation (5-10 min):** - Summarize in 3 bullets: what the data looks like, what you found, how well the model performs. - Show 2-3 clean visualizations (target distribution, feature importance, actual vs. predicted). - State limitations honestly: small sample size, data leakage risks, features you wish you had. - Suggest concrete next steps: additional feature engineering, hyperparameter tuning, more sophisticated models, collecting more data. **Practical Considerations:** - Communicate constantly. Narrate what you are doing and why. Silence is your enemy in an onsite. - If the data has a time dimension, be careful about leakage -- do not use future information to predict the past. - Prefer interpretable models unless the interviewer specifically asks for accuracy. A linear model you can explain beats a black box you cannot. - If something breaks or looks wrong, say so. Debugging gracefully under pressure is a signal of experience. - Keep your notebook organized with section headers. The interviewer may review it after. **Answer:** Spend roughly equal time on exploration, cleaning, EDA, and modeling, with a short wrap-up. Prioritize a working end-to-end pipeline over any single polished step. Communicate your reasoning throughout, and always compare your model against a simple baseline. --- Replicating Arbitrary Payoffs with Calls and Puts [hard, Options Pricing] --- https://quantvault.org/problem-2210-replicating-arbitrary-payoffs-with-calls-and-puts.html Q: You are given the following three quantities for a random variable $X$: - $E[X]$ - $E[\max(0, X)]$ - $E[\min(0, X)]$ Let $f$ be a twice continuously differentiable function. 1. Write a formula expressing $E[f(X)]$ in terms of $f$, its derivatives, and expectations of call/put-type payoffs on $X$. 2. Interpret each term financially. What roles do stocks, bonds, calls, and puts play in the representation? 3. How do the given quantities $E[X]$, $E[\max(0,X)]$, and $E[\min(0,X)]$ map onto standard financial instruments? A: **How to Think About It:** This is about one of the most fundamental ideas in derivatives pricing: any smooth payoff can be decomposed into a static portfolio of vanilla instruments. Before writing any math, think about what tools you have. A bond gives you a constant payoff. A forward gives you a linear payoff. Calls give you convex kinks above a strike. Puts give you convex kinks below a strike. If $f$ has curvature (non-zero second derivative), you need options to replicate it -- and the amount of curvature at each strike tells you how many options at that strike you need. This is the Breeden-Litzenberger result, and it is the theoretical backbone of volatility surface extraction from option prices. **Quick Sanity Checks:** If $f$ is linear ($f''= 0$ everywhere), the formula should collapse to just bonds and forwards -- no options needed. If $f(x) = (x - K)^+$ for some $K > 0$, the decomposition should recover a single call at strike $K$ (since $(x-K)^+$ has $f''(x) = \delta(x - K)$). If $f(x) = x^2$, we need options across all strikes because the curvature is constant. **Derivation:** Start with a second-order Taylor expansion with integral remainder. For any twice continuously differentiable $f$, we can write: $$f(x) = f(0) + f'(0)\,x + \int_0^{\infty} f''(k)\,(x - k)^+ \, dk + \int_{-\infty}^{0} f''(k)\,(k - x)^+ \, dk$$ This identity can be verified by splitting into cases ($x > 0$ and $x 0$: $$\int_0^{\infty} f''(k)(x-k)^+ \, dk = \int_0^{x} f''(k)(x-k) \, dk$$ Integrating by parts with $u = (x - k)$, $dv = f''(k)\,dk$: $$= \big[(x-k)f'(k)\big]_0^x + \int_0^x f'(k)\,dk = -xf'(0) + f(x) - f(0)$$ The put integral vanishes when $x > 0$, confirming $f(x) = f(0) + f'(0)x + \int_0^x f''(k)(x-k)\,dk$. The case $x 0$, weighted by $f''(k)$. Where $f$ is convex ($f'' > 0$), you buy calls; where concave ($f'' < 0$), you sell them. - $\int_{-\infty}^{0} f''(k)\,E[(k-X)^+]\,dk$: a continuum of **put options** with strikes $k < 0$, weighted by $f''(k)$. The given quantities map as follows: - $E[X]$ is the **forward price** of $X$. - $E[\max(0, X)] = E[(X - 0)^+]$ is the price of an **at-the-money call** (strike $K = 0$). - $E[\min(0, X)] = -E[(0 - X)^+]$ is the negative of the **at-the-money put** price. Note the consistency check: $E[X] = E[\max(0,X)] + E[\min(0,X)]$, which is just the decomposition $X = \max(0,X) + \min(0,X)$. In financial terms, this is a version of **put-call parity** at strike zero. The key practical takeaway: if you can observe call and put prices at every strike from the market, you can price any smooth derivative by reading off $f''(k)$ and integrating against the option price surface. This is exactly how traders extract the implied distribution from the volatility smile and price exotic payoffs from vanilla options. **Answer:** Any twice differentiable payoff $f(X)$ can be replicated as: $$E[f(X)] = f(0) + f'(0)\,E[X] + \int_0^{\infty} f''(k)\,C(k)\,dk + \int_{-\infty}^{0} f''(k)\,P(k)\,dk$$ where $C(k) = E[(X-k)^+]$ and $P(k) = E[(k-X)^+]$ are call and put prices. The decomposition is: bonds ($f(0)$) + forwards ($f'(0)$) + a continuum of calls and puts weighted by curvature ($f''$). --- Black-Scholes Monte Carlo Pricer [hard, Options Pricing] --- https://quantvault.org/problem-2220-black-scholes-monte-carlo-pricer.html Q: You are building a simple Monte Carlo pricer for a European call option. The underlying stock follows geometric Brownian motion: $$dS_t = \mu S_t \, dt + \sigma S_t \, dW_t$$ with initial price $S_0$, constant volatility $\sigma$, and risk-free rate $r$. The call has strike $K$ and maturity $T$. 1. Derive the risk-neutral distribution of $S_T$. Write an expression for the option price as $e^{-rT} \, E^Q[(S_T - K)^+]$. 2. Describe an algorithm that approximates this expectation via Monte Carlo using $M$ simulation paths. Specify exactly how you generate $S_T$ on each path. Analyze the time complexity in terms of $M$. 3. How would you reduce the variance of your Monte Carlo estimator? Discuss at least two techniques (e.g., antithetic variates, control variates) and explain how you would empirically verify that the pricer is converging correctly. **Example** ``` bs_mc_call(100, 100, 1.0, 0.05, 0.2, 20000, 1) ≈ 10.45058 ``` The result is a Monte-Carlo estimate that converges to 10.45058 as the sample count grows (deterministic for a fixed seed). A: **How to Think About It:** This is the bread-and-butter of quant interview take-homes. The interviewer is checking three things: (1) you know what risk-neutral pricing means and can apply Ito's lemma to get the terminal distribution, (2) you can translate a mathematical formula into a clean simulation, and (3) you understand that naive Monte Carlo is noisy and know practical tricks to fix it. Think of this as three mini-problems stacked together -- derivation, implementation, engineering. **Part (a): Risk-Neutral Distribution of $S_T$** Under the risk-neutral measure $Q$, the drift $\mu$ is replaced by $r$, so: $$dS_t = r S_t \, dt + \sigma S_t \, dW_t^Q$$ Apply Ito's lemma to $\ln S_t$. Let $f(S) = \ln S$. Then $f'(S) = 1/S$ and $f''(S) = -1/S^2$, so: $$d(\ln S_t) = \left(r - \frac{\sigma^2}{2}\right) dt + \sigma \, dW_t^Q$$ Integrating from $0$ to $T$: $$\ln S_T = \ln S_0 + \left(r - \frac{\sigma^2}{2}\right) T + \sigma \sqrt{T} \, Z, \quad Z \sim N(0,1)$$ Equivalently: $$S_T = S_0 \exp\!\left[\left(r - \frac{\sigma^2}{2}\right) T + \sigma \sqrt{T} \, Z\right]$$ So $\ln S_T$ is normally distributed with mean $\ln S_0 + (r - \sigma^2/2)T$ and variance $\sigma^2 T$, meaning $S_T$ is lognormal. The option price is: $$C = e^{-rT} \, E^Q[(S_T - K)^+]$$ **Part (b): Monte Carlo Algorithm and Code** The algorithm is direct sampling from the exact terminal distribution -- no need to discretize the path since we only care about $S_T$. **Algorithm:** 1. For each path $i = 1, \ldots, M$: draw $Z_i \sim N(0,1)$ and compute $S_T^{(i)} = S_0 \exp[(r - \sigma^2/2)T + \sigma \sqrt{T} \, Z_i]$. 2. Compute the payoff $V_i = (S_T^{(i)} - K)^+$. 3. Estimate $\hat{C} = e^{-rT} \frac{1}{M} \sum_{i=1}^{M} V_i$. ```python import numpy as np from scipy.stats import norm def bs_mc_call(S0, K, T, r, sigma, M, seed=42): rng = np.random.default_rng(seed) Z = rng.standard_normal(M) ST = S0 * np.exp((r - 0.5 * sigma**2) * T + sigma * np.sqrt(T) * Z) payoffs = np.maximum(ST - K, 0.0) price = np.exp(-r * T) * np.mean(payoffs) se = np.exp(-r * T) * np.std(payoffs, ddof=1) / np.sqrt(M) return price, se ``` **Complexity:** Each path requires $O(1)$ work (one random draw, one exp, one max). Summing over $M$ paths gives $O(M)$ total time and $O(M)$ space (or $O(1)$ space if you accumulate online). The standard error of the estimator decreases as $O(1/\sqrt{M})$, so getting one more decimal digit of accuracy requires $100\times$ more paths. **Part (c): Variance Reduction** **Antithetic variates.** For each $Z_i$, also compute the payoff using $-Z_i$. The pair $(S_T(Z_i), S_T(-Z_i))$ shares the same random draw but covers both tails. Your estimator becomes: $$\hat{C}_{\text{AV}} = e^{-rT} \frac{1}{M} \sum_{i=1}^{M} \frac{V(Z_i) + V(-Z_i)}{2}$$ This works because the payoff is a monotone function of $Z$, so $V(Z)$ and $V(-Z)$ are negatively correlated, reducing variance. In practice this cuts variance by roughly 20-30% for vanilla options. **Control variates.** Use $S_T$ itself as a control. We know $E^Q[S_T] = S_0 e^{rT}$ exactly. Define: $$\hat{C}_{\text{CV}} = \hat{C} - \beta \left(\frac{1}{M}\sum_{i=1}^{M} S_T^{(i)} - S_0 e^{rT}\right)$$ The optimal $\beta$ is $\text{Cov}(V, S_T) / \text{Var}(S_T)$, estimated from the same sample. Since the call payoff and terminal stock price are highly correlated, this typically reduces variance by 50-80%. **Empirical convergence verification:** - **Benchmark against Black-Scholes closed form.** For a European call, the BS formula gives the exact answer. Run your MC pricer for increasing $M$ (e.g., $10^3, 10^4, 10^5, 10^6$) and confirm the MC estimate converges to the BS price and that the standard error shrinks proportional to $1/\sqrt{M}$. - **Confidence intervals.** At each $M$, compute a 95% CI as $\hat{C} \pm 1.96 \cdot \text{SE}$. The BS price should fall inside the CI roughly 95% of the time across repeated runs. - **Log-log plot.** Plot $\log(\text{SE})$ vs. $\log(M)$. You should see a slope of $-1/2$. If the slope is flatter, something is wrong (e.g., correlated draws, a bug in the payoff). **Answer:** The option price is $C = e^{-rT} E^Q[(S_T - K)^+]$ where $S_T = S_0 \exp[(r - \sigma^2/2)T + \sigma\sqrt{T} Z]$ with $Z \sim N(0,1)$. The Monte Carlo estimator averages $M$ i.i.d. discounted payoffs in $O(M)$ time, converging at rate $O(1/\sqrt{M})$. Antithetic variates exploit monotonicity to pair $Z$ and $-Z$ for negative correlation; control variates use the known mean of $S_T$ to correct the estimator. Both can be verified by benchmarking against the Black-Scholes closed-form price. --- Cross-Asset Market Making with Correlated Fundamentals [hard, Market Microstructure] --- https://quantvault.org/problem-2233-cross-asset-market-making-with-correlated-fundamentals.html Q: You are a market maker quoting two correlated assets simultaneously. The latent (true) values $(V_1, V_2)$ are jointly normal: $$(V_1, V_2) \sim N\!\left(\begin{pmatrix}\mu_1 \\ \mu_2\end{pmatrix},\ \begin{pmatrix}\sigma_1^2 & \rho\sigma_1\sigma_2 \\ \rho\sigma_1\sigma_2 & \sigma_2^2\end{pmatrix}\right)$$ with correlation $\rho \in (-1, 1)$. You quote one share on each side for both assets. Your bid/ask for asset 1 are shifted from the mid by a skew term $k_1 I_1 + k_2 I_2$, where $I_1, I_2$ are your current inventory positions in each asset. Order flow is adversarial: each incoming trader observes a Gaussian signal $S_j = V_j + \epsilon_j$ with $\epsilon_j \sim N(0, \sigma^2)$ (independent of $V_j$) and trades against the most favorable quote. Derive the inventory-optimal hedging coefficients $(k_1, k_2)$ -- the skew parameters that equalize the conditional expected loss on asset 1 fills given the joint inventory $(I_1, I_2)$. In particular, explain why $k_2 \neq 0$: why does inventory in asset 2 affect how you skew asset 1? A: **How to Think About It:** This is a cross-asset adverse selection problem. In single-asset market making, you skew your quotes to manage inventory risk -- the bigger your long position, the more you shade your bid down to attract sellers. With two correlated assets, it is more subtle: a large long position in asset 2, combined with positive correlation $\rho$, means you are also effectively long on asset 1 (in a value sense). You should shade asset 1 quotes too, even if your asset 1 inventory is zero. The hedging coefficients $(k_1, k_2)$ capture this cross-asset effect. The key is to work with the conditional distribution of $V_1$ given the signal and inventory context, then equalize the expected P\&L on each side of the market. **Key Insight:** Under a Gaussian model, the optimal skew is linear in inventories. The coefficient $k_2$ on asset 2 inventory is proportional to $\rho \cdot \frac{\sigma_1}{\sigma_2}$ -- the correlation scaled by the relative volatilities. This is exactly the hedge ratio you would use to hedge asset 1 risk with asset 2. **The Method:** **Step 1: Posterior on $V_1$ given signal $S_1$.** The trader observes $S_1 = V_1 + \epsilon_1$ with $\epsilon_1 \sim N(0, \sigma^2)$. By standard Gaussian updating, the posterior mean of $V_1$ given $S_1$ is: $$E[V_1 | S_1] = \mu_1 + \frac{\sigma_1^2}{\sigma_1^2 + \sigma^2}(S_1 - \mu_1)$$ Let $\alpha = \frac{\sigma_1^2}{\sigma_1^2 + \sigma^2} \in (0,1)$ be the signal weight (informativeness). **Step 2: Inventory-adjusted mid.** Your effective mid for asset 1, accounting for inventory risk, should be: $$m_1^{\text{adj}} = \mu_1 - (k_1 I_1 + k_2 I_2)$$ The skew shifts the mid: a long position ($I_1 > 0$) lowers your effective mid so you sell cheaper and buy more expensively, reducing inventory. **Step 3: Equalize expected loss.** A buy order on asset 1 arrives at ask $a_1 = m_1^{\text{adj}} + s$ (half-spread $s$). The informed trader buys only if $E[V_1|S_1] > a_1$. Your expected loss per informed buy is: $$E[V_1 - a_1 | \text{buy}] = E\left[E[V_1|S_1] - a_1 \,\Big|\, E[V_1|S_1] > a_1\right]$$ For the symmetric case (zero inventory, symmetric spread), this equals $\alpha \cdot \phi(s/\tilde{\sigma}) / (1 - \Phi(s/\tilde{\sigma}))$ times $\tilde{\sigma}$ -- the standard adverse selection cost, where $\tilde{\sigma}^2 = \sigma_1^2 \sigma^2/(\sigma_1^2 + \sigma^2)$ is the posterior standard deviation. **Step 4: Cross-asset term.** With joint inventory $(I_1, I_2)$, the risk to the market maker is not just inventory in asset 1 but also correlated exposure from asset 2. The P\&L variance from holding $(I_1, I_2)$ is: $$\text{Var}(I_1 V_1 + I_2 V_2) = I_1^2 \sigma_1^2 + 2\rho\sigma_1\sigma_2 I_1 I_2 + I_2^2 \sigma_2^2$$ To flatten this exposure, the optimal skew on asset 1 must include a term proportional to $I_2$ that reflects the marginal contribution of asset 1 trades to the portfolio variance. Differentiating the portfolio variance with respect to $I_1$: $$\frac{\partial}{\partial I_1}\text{Var} = 2(\sigma_1^2 I_1 + \rho\sigma_1\sigma_2 I_2)$$ Setting this proportional to the skew gives the optimal hedging coefficients: $$k_1 = \gamma \sigma_1^2, \qquad k_2 = \gamma \rho\sigma_1\sigma_2$$ where $\gamma > 0$ is a risk aversion parameter (chosen to balance inventory cost against spread revenue). **Practical Considerations:** The ratio $k_2 / k_1 = \rho \sigma_2/\sigma_1$ is exactly the regression coefficient of $V_2$ on $V_1$ -- i.e., the minimum-variance hedge ratio. This makes intuitive sense: you are hedging your asset 2 inventory risk by skewing asset 1 quotes in the direction that reduces your net correlated exposure. When $\rho = 0$, $k_2 = 0$ and the assets decouple. When $|\rho| \to 1$, the cross-skew dominates. **Answer:** The inventory-optimal hedging coefficients are $k_1 = \gamma\sigma_1^2$ and $k_2 = \gamma\rho\sigma_1\sigma_2$, where $\gamma$ is the market maker's risk aversion. The cross-asset skew $k_2 \neq 0$ because correlated inventory in asset 2 creates effective exposure to $V_1$ -- the correct skew accounts for the full correlated portfolio risk, not just single-asset inventory. --- Lamppost Toggle Cycle on a Square [medium, Stochastic Process] --- https://quantvault.org/problem-2249-lamppost-toggle-cycle-on-a-square.html Q: Four lampposts sit at the corners of a square, labeled $A, B, C, D$ in clockwise order. All four lamps start on. A walker moves clockwise around the square, visiting one lamppost per step. The toggle rule is: - When the walker arrives at a lamppost, if the lamp at the **previous** lamppost is currently **on**, toggle the current lamp (flip its state). - If the previous lamp is **off**, do nothing to the current lamp. The walker starts at lamp $A$ (before taking any steps). (a) Model this system as a finite deterministic dynamical system. Describe the state space (lamp states plus walker position). (b) Starting from all lamps on with the walker at $A$, compute the lamp configuration after $N$ steps as an explicit function of $N$. Give the configuration for each step in the first period. (c) Prove the process is eventually periodic and find the minimal period. A: **How to Think About It:** This is a finite deterministic system -- the state space has $2^4 \times 4 = 64$ possible states (lamp states plus walker position). A deterministic map on a finite set must eventually cycle, so eventual periodicity is guaranteed by the pigeonhole principle. The real work is tracing the orbit from the initial state to find the period. The key mechanic: when the walker steps from position $P$ to the next position $Q$, if $L_P = 1$ then $L_Q$ flips; else $L_Q$ stays. The walker always moves clockwise regardless of lamp states -- the walk is deterministic and cyclic. With only 4 lamps and a deterministic rule, you can simulate it directly. **Quick Estimate:** The full state must repeat within 64 steps (pigeonhole on 64 states), and a repeat of the initial state requires the walker back at $A$, so the period must be a multiple of 4 and at most 64. Beware the tempting guess of 16 ($4 \times 4$): the lamps alone return to all-on at step 15, but the walker is at $D$, not $A$ -- that is not a full-state repeat. Expect a period that is a multiple of 4, somewhere near the 64-state ceiling. **Approach:** Pigeonhole gives periodicity for free; explicit simulation of the 64-state map finds the orbit and the minimal period. **Formal Solution:** **(a) The dynamical system.** Let the lamps be $(L_A, L_B, L_C, L_D) \in \{0,1\}^4$ with 1 = on, and the walker position in $\{A,B,C,D\}$. The state space is $$\boxed{S = \{0,1\}^4 \times \{A,B,C,D\}, \qquad |S| = 16 \times 4 = 64}$$ The update rule $f: S \to S$ is a well-defined deterministic function: given the current full state, the walker moves clockwise to the next position $Q$ from position $P$, and $L_Q$ flips if and only if $L_P = 1$. **(b) Explicit simulation of the orbit.** Label positions $A=0, B=1, C=2, D=3$ with clockwise steps. "Walker at $X$ after step $t$" means the walker just arrived at $X$. The walker position at step $t$ is $(t \bmod 4)$ after starting at $A=0$. - **Step 0 (start):** Lamps $(1,1,1,1)$, walker at $A$. (No step yet; previous lamp undefined -- treat as first step below.) For step $t \geq 1$: walker moves from position $t-1$ to position $t$ (mod 4). Check lamp at previous position; if on, toggle current. - **Step 1:** Walk $A \to B$. Previous = $A$, $L_A = 1$: toggle $L_B$. $L_B: 1 \to 0$. State: $(1,0,1,1)$, at $B$. - **Step 2:** Walk $B \to C$. Previous = $B$, $L_B = 0$: do nothing. State: $(1,0,1,1)$, at $C$. - **Step 3:** Walk $C \to D$. Previous = $C$, $L_C = 1$: toggle $L_D$. $L_D: 1 \to 0$. State: $(1,0,1,0)$, at $D$. - **Step 4:** Walk $D \to A$. Previous = $D$, $L_D = 0$: do nothing. State: $(1,0,1,0)$, at $A$. - **Step 5:** Walk $A \to B$. Previous = $A$, $L_A = 1$: toggle $L_B$. $L_B: 0 \to 1$. State: $(1,1,1,0)$, at $B$. - **Step 6:** Walk $B \to C$. Previous = $B$, $L_B = 1$: toggle $L_C$. $L_C: 1 \to 0$. State: $(1,1,0,0)$, at $C$. - **Step 7:** Walk $C \to D$. Previous = $C$, $L_C = 0$: do nothing. State: $(1,1,0,0)$, at $D$. - **Step 8:** Walk $D \to A$. Previous = $D$, $L_D = 0$: do nothing. State: $(1,1,0,0)$, at $A$. - **Step 9:** Walk $A \to B$. Previous = $A$, $L_A = 1$: toggle $L_B$. $L_B: 1 \to 0$. State: $(1,0,0,0)$, at $B$. - **Step 10:** Walk $B \to C$. Previous = $B$, $L_B = 0$: do nothing. State: $(1,0,0,0)$, at $C$. - **Step 11:** Walk $C \to D$. Previous = $C$, $L_C = 0$: do nothing. State: $(1,0,0,0)$, at $D$. - **Step 12:** Walk $D \to A$. Previous = $D$, $L_D = 0$: do nothing. State: $(1,0,0,0)$, at $A$. - **Step 13:** Walk $A \to B$. Previous = $A$, $L_A = 1$: toggle $L_B$. $L_B: 0 \to 1$. State: $(1,1,0,0)$, at $B$. - **Step 14:** Walk $B \to C$. Previous = $B$, $L_B = 1$: toggle $L_C$. $L_C: 0 \to 1$. State: $(1,1,1,0)$, at $C$. - **Step 15:** Walk $C \to D$. Previous = $C$, $L_C = 1$: toggle $L_D$. $L_D: 0 \to 1$. State: $(1,1,1,1)$, at $D$. - **Step 16:** Walk $D \to A$. Previous = $D$, $L_D = 1$: toggle $L_A$. $L_A: 1 \to 0$. State: $(0,1,1,1)$, at $A$. - **Step 17:** Walk $A \to B$. Previous = $A$, $L_A = 0$: do nothing. State: $(0,1,1,1)$, at $B$. - **Step 18:** Walk $B \to C$. Previous = $B$, $L_B = 1$: toggle $L_C$. $L_C: 1 \to 0$. State: $(0,1,0,1)$, at $C$. - **Step 19:** Walk $C \to D$. Previous = $C$, $L_C = 0$: do nothing. State: $(0,1,0,1)$, at $D$. - **Step 20:** Walk $D \to A$. Previous = $D$, $L_D = 1$: toggle $L_A$. $L_A: 0 \to 1$. State: $(1,1,0,1)$, at $A$. Continuing this simulation (tracking full states including walker position) is the reliable method. The orbit is deterministic, and given the finite state space of 64 states, periodicity must occur within 64 steps. The configuration after $N$ steps is the orbit value at position $N \bmod 60$ once the period (established in part (c)) is known; the table above gives the configuration for each of the early steps. **(c) Periodicity proof and minimal period.** **Claim:** The process is eventually periodic. **Proof:** The state space is $S = \{0,1\}^4 \times \{A,B,C,D\}$, which has $|S| = 64$ elements. The update rule $f: S \to S$ is a well-defined deterministic function (given the current full state, the next state is uniquely determined). The sequence of states $s_0, s_1 = f(s_0), s_2 = f(s_1), \ldots$ must eventually revisit a state (by the pigeonhole principle, within the first 65 steps). Once a state repeats, the sequence is periodic from that point onward. $\square$ **Minimal period:** The full simulation (which can be completed by hand or computer within the 64-state bound) determines the minimal period by tracking when the full state $(L_A, L_B, L_C, L_D, \text{walker})$ first repeats. A complete simulation reveals that the orbit returns to the initial state $(1,1,1,1)$ with the walker at $A$ for the first time after exactly $p = 60$ steps (15 full circuits of the square), with no earlier repeat of any full state -- so there is no transient, and the minimal period is $$\boxed{p = 60}$$ Note this is $60$, not $16$: the lamp configuration alone hitting all-on at step 15 (walker at $D$) does not close the orbit, because the walker position is part of the state. **Answer:** The state space is $\{0,1\}^4 \times \{A,B,C,D\}$ with 64 total states. The configuration after $N$ steps follows the explicit orbit computed by simulation (tabulated above for early steps), repeating with period 60. The process is eventually periodic by pigeonhole on the finite state space; the orbit from all-on/walker-at-$A$ has no transient, and the minimal period is $\boxed{60}$. --- Martingale Property of Brownian Motion [medium, Stochastic Process] --- https://quantvault.org/problem-2251-martingale-property-of-brownian-motion.html Q: Let $W_t$ be a standard Brownian motion. Define the process $M_t = W_t^2 - t$. Show that $M_t$ is a martingale with respect to the natural filtration $\mathcal{F}_s = \sigma(W_u : u \leq s)$. That is, verify that $E[M_t \mid \mathcal{F}_s] = M_s$ for all $0 \leq s < t$. A: **How to Think About It:** The key intuition is that Brownian motion's square $W_t^2$ is not a martingale -- it grows on average. How fast? Since $E[W_t^2] = t$, the square grows at rate 1 per unit time. So if you subtract $t$ to "detrend" it, you should kill the drift and get a martingale. The whole proof boils down to one move: decompose $W_t$ into what you knew at time $s$ and what is new, then expand the square and use independence of increments. **Approach:** Write $W_t = W_s + (W_t - W_s)$, expand the square inside the conditional expectation, and use the fact that the increment $W_t - W_s$ is independent of $\mathcal{F}_s$ with mean $0$ and variance $t - s$. **Formal Solution:** We verify the martingale property $E[M_t \mid \mathcal{F}_s] = M_s$ for $s < t$. Decompose $W_t = W_s + (W_t - W_s)$ and expand: $$W_t^2 = W_s^2 + 2W_s(W_t - W_s) + (W_t - W_s)^2$$ Now take the conditional expectation. Since $W_t - W_s$ is independent of $\mathcal{F}_s$ and $W_s$ is $\mathcal{F}_s$-measurable: $$E[W_t^2 \mid \mathcal{F}_s] = W_s^2 + 2W_s \cdot E[W_t - W_s] + E[(W_t - W_s)^2]$$ The increment $W_t - W_s \sim N(0, t - s)$, so: - $E[W_t - W_s] = 0$ - $E[(W_t - W_s)^2] = t - s$ Substituting: $$E[W_t^2 \mid \mathcal{F}_s] = W_s^2 + 0 + (t - s) = W_s^2 + t - s$$ Therefore: $$E[M_t \mid \mathcal{F}_s] = E[W_t^2 - t \mid \mathcal{F}_s] = W_s^2 + t - s - t = W_s^2 - s = M_s$$ This confirms the martingale property. **Answer:** $M_t = W_t^2 - t$ is a martingale because the conditional expectation of $W_t^2$ given $\mathcal{F}_s$ equals $W_s^2 + (t - s)$, and subtracting $t$ exactly cancels the accumulated variance, leaving $W_s^2 - s = M_s$. --- Robust Kalman Filtering: Handling Outlier Measurements [hard, Stochastic Process] --- https://quantvault.org/problem-2262-robust-kalman-filtering-handling-outlier-measurements.html Q: The standard Kalman filter assumes Gaussian measurement noise. In practice -- especially in financial signal processing or sensor fusion -- you will occasionally see a reading that is wildly off: a data error, a flash crash tick, a sensor glitch. A single such outlier can drag your state estimate far from the truth and take many steps to recover. Describe three distinct approaches to make a Kalman filter robust to outlier measurements. For each approach, explain the core idea, how it modifies the standard filter equations, and when you would choose it. A: **How to Think About It:** The Kalman filter is, at its heart, a weighted average between your prior (model prediction) and the new measurement. The weights are determined by the Kalman gain $K_t$, which is driven by relative confidence in the model vs. the sensor. An outlier is a measurement you should trust very little -- ideally not at all. So the three approaches differ in *how* they detect or model distrust and *how* they reduce the outlier's influence. Think of them on a spectrum from blunt (just gate it out) to principled (change your noise model entirely). **Key Insight:** Every robust approach ultimately reduces the effective Kalman gain for suspicious measurements. The methods differ in how they decide what counts as suspicious and how aggressively they down-weight. **The Method:** **Approach 1: Innovation Gating (Adaptive Measurement Covariance)** The *innovation* is the gap between what you expected and what you observed: $y_t = z_t - H\hat{x}_{t|t-1}$. Under normal conditions, the innovation should be consistent with the innovation covariance $S_t = H P_{t|t-1} H^T + R$. You can test this formally with the normalized squared innovation (Mahalanobis distance): $$d_t^2 = y_t^T S_t^{-1} y_t$$ Under Gaussianity, $d_t^2 \sim \chi^2_n$ where $n$ is the measurement dimension. If $d_t^2$ exceeds a threshold (say, the 99th percentile of $\chi^2_n$), declare the measurement an outlier and inflate the measurement noise covariance for that step: $$R_t^{\text{robust}} = \gamma \cdot R, \quad \gamma \gg 1$$ A large $R$ drives the Kalman gain $K_t = P_{t|t-1} H^T S_t^{-1}$ close to zero, so the outlier barely moves the estimate. In the limit $\gamma \to \infty$, you are simply skipping the measurement entirely. **Approach 2: Heavy-Tailed Measurement Model (Student-$t$ Filter)** Instead of binary gating, replace the Gaussian measurement noise assumption with a Student-$t$ distribution: $$v_t \sim t_\nu(0, R)$$ The $t$ distribution has heavier tails than Gaussian -- large deviations are much more probable under this model, so they receive a softer penalty. The resulting filter is an iteratively reweighted Kalman update: each iteration solves for the update while assigning weight $w_t$ to each measurement, where $$w_t \propto \frac{\nu + n}{\nu + d_t^2}$$ As $d_t^2$ grows (larger outlier), $w_t$ shrinks -- the filter automatically down-weights extreme measurements. As $\nu \to \infty$, $w_t \to 1$ and you recover the standard filter. A common practical choice is $\nu = 3$ to $5$, which is robust but still efficient for inliers. **Approach 3: Huber M-Estimation (Clipped Innovation)** The standard Kalman filter minimizes a quadratic cost in the innovations. Quadratic cost is the root of the problem: it assigns weight proportional to the size of the error, so large outliers dominate. Replace the quadratic with the Huber loss: $$\rho(y) = \begin{cases} y^2/2 & |y| \leq c \\\ c|y| - c^2/2 & |y| > c \end{cases}$$ This is quadratic for small innovations and linear for large ones -- you stop penalizing (and therefore stop trusting) innovations beyond $\pm c$ standard deviations. The resulting update clips the innovation: $$y_t^{\text{clipped}} = \text{sign}(y_t) \cdot \min\!\left(|y_t|,\, c \cdot \sqrt{S_t}\right)$$ and uses $y_t^{\text{clipped}}$ in place of $y_t$ in the standard Kalman update. This bounds the influence of any single measurement regardless of how extreme it is. **Practical Considerations:** - *Gating* (Approach 1) is the simplest to implement and most common in production. The threshold is typically set at $3\sigma$ to $5\sigma$ (equivalently, $d_t^2 > 9$ to $25$ for scalar measurements). The downside: it is binary -- measurements just inside the threshold get full weight, just outside get zero. It also permanently discards potentially valid tail observations. - *Student-$t$ filter* (Approach 2) is more principled and continuously down-weights rather than binary-gates. It requires iterating to convergence at each step, adding computational cost. Best when outliers are relatively frequent and you want a smoother transition between inlier and outlier treatment. Choosing $\nu$ can be done via EM or cross-validation. - *Huber M-estimation* (Approach 3) is a middle ground: it has a hard cap on influence (like gating) but applies it continuously (like the $t$ filter). The threshold $c$ has a cleaner interpretation than $\nu$, but the filter is an approximation and loses the clean Bayesian interpretation of the standard Kalman. - In financial applications, gating is dominant for high-frequency signal processing (straightforward, low latency). The Student-$t$ and Huber approaches appear more in state-space econometric models and factor model estimation. **Answer:** Three approaches to robust Kalman filtering: (1) *Innovation gating* -- declare measurements beyond a $\chi^2$ threshold as outliers and inflate $R$ to zero out their Kalman gain; (2) *Student-$t$ measurement model* -- replace Gaussian noise with heavy-tailed noise so the filter naturally down-weights large innovations through iterative reweighting; (3) *Huber M-estimation* -- replace the quadratic innovation cost with the Huber loss, clipping extreme innovations at $\pm c$ standard deviations. Gating is the simplest and most common in practice; the other two are more statistically principled but add implementation complexity. --- Steps to Make Three Colored Balls Match [hard, Stochastic Process] --- https://quantvault.org/problem-2344-steps-to-make-three-colored-balls-match.html Q: A bowl contains three balls, each a different color. Each step, you reach in and pull out two of the three balls uniformly at random, then repaint one of the two to match the other -- which of the two gets repainted is itself decided by a fair coin. **(a)** You stop once all three balls are the same color. On average, how many steps does this take? **(b)** Now the bowl holds $n$ balls, each a different color, and the same rule applies: pull out two balls uniformly at random and let a fair coin decide which one is repainted to match the other. Show that the expected number of steps until all $n$ balls share one color is $(n-1)^2$, and evaluate it for $n = 5$. A: **How to Think About It:** Only the *number of distinct colors* matters for stopping, so collapse the state to that count: start at $3$, absorb at $1$. The heuristic is **state-lumping into a tiny Markov chain**. The trap -- and it is the whole problem -- is assuming that grabbing two differently-coloured balls always merges the colours. It does not. Once you are at $2$ colours the pile is split $2{+}1$, and drawing the majority ball together with the lone one finishes the game only if the *lone* ball is the one repainted; repaint the majority ball instead and you land back in a $2{+}1$ split with the roles swapped. Missing that second failure route is what turns a correct $4$ into a too-clever $5/2$. For $n$ balls the colour count no longer collapses to a small chain, so part (b) instead follows one colour, whose count is a martingale, and conditions on that colour winning. **Quick Estimate:** From $3$ distinct colours any pair you pull is two different colours, so step $1$ *always* drops you to $2$ colours -- exactly $1$ step, no randomness. From the $2$-colour state (split $2{+}1$, say $A,A,B$) there are $\binom{3}{2}=3$ equally likely pairs. The matched pair $\{A,A\}$ (probability $\tfrac13$) wastes the step outright. Each mixed pair (total probability $\tfrac23$) finishes only on the coin flip that repaints $B$ -- half the time. So the per-step success probability is $\tfrac23 \times \tfrac12 = \tfrac13$, a geometric taking $3$ steps. Total $\approx 1 + 3 = 4$. The estimate *is* the exact answer here: $\boxed{4}$. **Approach:** Lump the state to the number of distinct colours, then write a first-step equation for the $2{+}1$ state, being careful to count *both* ways a step can fail to make progress. **Formal Solution:** Let the state be the number of distinct colours, $E_s$ the expected steps to finish from state $s$. **State $3$** (configuration $1{+}1{+}1$): the two chosen balls are always different colours, so repainting either one yields a $2{+}1$ split -- you move to state $2$ with certainty. Hence $E_3 = 1 + E_2$. **State $2$** (configuration $2{+}1$: two balls of colour $A$, one ball of colour $B$): pick $2$ of $3$ balls, $\binom{3}{2}=3$ equally likely pairs, then flip a fair coin for which of the two is repainted. - **Both $A$ balls** (probability $\tfrac13$): they already match, so repainting either changes nothing -- stay in state $2$. - **One $A$ and the $B$** (probability $\tfrac23$), then the coin: - repaint $B$ to $A$ (probability $\tfrac12$): all three balls are $A$ -- reach state $1$, done; - repaint that $A$ to $B$ (probability $\tfrac12$): the configuration becomes $A,B,B$ -- still a $2{+}1$ split, still state $2$. So the step succeeds with probability $\tfrac23 \cdot \tfrac12 = \tfrac13$ and otherwise returns to state $2$: $$E_2 = 1 + \tfrac23 E_2 \;\Longrightarrow\; \tfrac13 E_2 = 1 \;\Longrightarrow\; \boxed{E_2 = 3}.$$ Then $E_3 = 1 + E_2 = 1 + 3 = 4$. **Part (b): $n$ balls.** With $n$ colours the configuration is a partition of $n$, so the count of distinct colours no longer gives a tiny chain. Track a single colour instead. Note that a uniform pair plus a fair coin is the same as a uniform *ordered* pair (repainted ball, model ball) from the $n(n-1)$ ordered pairs. *Step 1 -- One colour's count is a martingale.* Fix red and let $i$ be the number of red balls. The count rises by one when a non-red ball is repainted red and falls by one when a red ball is repainted another colour. Both have probability $$p_i = \frac{i(n-i)}{n(n-1)},$$ so the red count is a martingale, absorbed at $0$ or $n$. *Step 2 -- Condition on the winner.* By optional stopping, red takes over from $i$ red balls with probability $i/n$; from the start ($i = 1$) every colour wins with probability $1/n$. By symmetry $E[T \mid c \text{ wins}]$ is the same for every colour $c$, so $E[T] = E[T \mid \text{red wins}]$. *Step 3 -- The conditioned chain.* Conditioning on red winning reweights each move $i \to j$ by $h(j)/h(i)$ with $h(i) = i/n$ (a Doob $h$-transform). Given red wins, the count moves up with probability $p_i\,\frac{i+1}{i}$, down with probability $p_i\,\frac{i-1}{i}$, and stays with probability $1 - 2p_i$. It can never step from $1$ to $0$. *Step 4 -- Solve the recursion.* Let $E_i$ be the expected remaining steps given $i$ red balls and that red wins, with $E_n = 0$. First-step analysis gives $$2p_i E_i = 1 + \frac{p_i}{i}\big[(i+1)E_{i+1} + (i-1)E_{i-1}\big].$$ Multiply by $i/p_i = \frac{n(n-1)}{n-i}$ and set $G_i = iE_i$, so $G_0 = G_n = 0$: $$G_{i+1} - 2G_i + G_{i-1} = -\frac{n(n-1)}{n-i}, \qquad 1 \le i \le n-1.$$ With $d_i = G_i - G_{i-1}$ this reads $d_{i+1} = d_i - \frac{n(n-1)}{n-i}$, so $d_i = d_1 - n(n-1)\sum_{k=1}^{i-1}\frac{1}{n-k}$. Since $\sum_{i=1}^{n} d_i = G_n - G_0 = 0$, $$n\,d_1 = n(n-1)\sum_{k=1}^{n-1}\frac{n-k}{n-k} = n(n-1)^2,$$ because each $k$ appears for the $n-k$ values $i = k+1, \ldots, n$. Hence $E[T] = E_1 = G_1 = d_1 = (n-1)^2$. *Step 5 -- Check and evaluate.* $n = 2$ gives $1$ (one step always finishes) and $n = 3$ gives $4$, matching part (a). For $n = 5$ the expected number of steps is $(5-1)^2 = 16$. Solving the exact chain over all colour partitions for $n = 2, \ldots, 6$ gives $1, 4, 9, 16, 25$, and a simulation for $n = 5$ gives $16.01$. **Answer:** (a) The expected number of steps is $E_3 = 4$. (b) For $n$ balls it is $(n-1)^2$, which is $16$ for $n = 5$. *Verification.* Solving the exact Markov chain on the full colouring of the three balls (not the lumped count) gives $E = 4$ symbolically, and a $400{,}000$-run simulation of the stated rule gives $3.993$. Note that the merge-always model -- which would give $E_2 = 3/2$ and $E_3 = 5/2$ -- is not realisable by any symmetric repainting rule: from $A,A,B$ the pair $\{A,B\}$ must become either $A,A,A$ or $A,B,B$, and the second outcome keeps two colours alive. --- Maximum Value After Repeated Doubling [easy, Coding] --- https://quantvault.org/problem-2426-maximum-value-after-repeated-doubling.html Q: You are given an array of integers and an integer $n$. Repeatedly perform the following operation: if the current value of $n$ appears somewhere in the array, double $n$; then check again. Stop as soon as the current value of $n$ is no longer present in the array. Return the final value of $n$. **Constraints:** - $1 \leq \text{len(arr)} \leq 10^{5}$ - values fit in a 64-bit integer **Examples:** - Input: $\text{arr} = [1, 2, 4, 5]$, $n = 1$ -> Output: $8$ (1 present -> 2; 2 present -> 4; 4 present -> 8; 8 absent, stop) - Input: $\text{arr} = [3, 6, 7]$, $n = 5$ -> Output: $5$ (5 absent immediately) A: **How to Think About It:** The operation only ever doubles $n$, so the sequence of values you test is $n, 2n, 4n, 8n, \ldots$ -- it grows geometrically and can repeat at most $O(\log(\max))$ times before exceeding the largest array value. Put the array into a hash set for $O(1)$ membership, then keep doubling while the current value is in the set. **Algorithm:** Build a set from the array. While $n$ is in the set, set $n \leftarrow 2n$. Return $n$. **Code:** ```python def max_after_doubling(arr, n): present = set(arr) while n in present: n *= 2 return n ``` **Complexity:** $O(\text{len(arr)})$ to build the set, then at most $O(\log V)$ doublings where $V$ is the largest value. Space $O(\text{len(arr)})$. **Answer:** Hash the array, then double $n$ while it stays in the set; terminates in $O(\log V)$ steps because each step at least doubles $n$. --- Maximum Apples That Fit in a Box [easy, Coding] --- https://quantvault.org/problem-2473-maximum-apples-that-fit-in-a-box.html Q: There is a box with a capacity of 5000 grams. The box may already hold some items, which reduces the room left. You will add apples until the box is full. You are given a zero-indexed array $A$ of $N$ integers. The first element $A[0] = K$ is the total weight already in the box; the remaining elements are individual apple weights. Implement the function `max_apples(A)` that returns the maximum number of apples that can be added without exceeding the 5000 gram capacity. **Constraints** - $1 \le N \le 100$ - Every value is between 0 and 5000 - Maximize the COUNT of apples, not the total weight **Examples** - $A = [4650, 150, 150, 150] \to 2$ (room left is 350g, so two 150g apples fit, total 4950) - $A = [4850, 100, 30, 30, 100, 50, 100] \to 3$ (pick 30, 30, 50) **Example** `max_apples([4850, 100, 30, 30, 100, 50, 100]) -> 3` The box already holds 4850g, leaving 150g of room. Sorting the apples ascending (30, 30, 50, 100, 100, 100) and greedily taking the lightest fits 30 + 30 + 50 = 110g, for 3 apples; a fourth (100g) would exceed the 150g of room. A: **How to Think About It:** You want the most apples, not the heaviest load, so you should always prefer light apples. This is the classic greedy exchange argument: if an optimal solution skipped a light apple in favor of a heavier one, swapping in the lighter apple never increases total weight and keeps the count the same, so sorting ascending and taking apples until you run out of room is optimal. **Algorithm:** Compute remaining capacity $5000 - K$ where $K = A[0]$. Sort the apple weights ($A[1:]$) ascending. Walk through them, subtracting each weight from the remaining capacity while it still fits, counting apples. **Code:** ```python def max_apples(A): # A[0] = weight already in the box; A[1:] = individual apple weights. # Box capacity is 5000 grams. Greedily take the lightest apples. remaining = 5000 - A[0] count = 0 for w in sorted(A[1:]): if w <= remaining: remaining -= w count += 1 else: break return count ``` **Complexity:** $O(N \log N)$ for the sort, $O(N)$ scan. **Answer:** Sort apples ascending and greedily take the lightest ones until the box is full -- the maximum count. --- Stabilizing a Row of Student Scores [easy, Coding] --- https://quantvault.org/problem-2475-stabilizing-a-row-of-student-scores.html Q: Students sit next to each other in a row, each with an initial integer test score. Implement the function `stabilize(A)`, where you are given a zero-indexed array $A$ of $N$ scores. Each day, every interior student's score updates simultaneously by the following rules, using the scores from the START of that day (every update reads the same snapshot, so nobody sees a neighbor's new score until the next day): - If a student sits immediately between two students with strictly higher scores, that student's score increases by 1. - If a student sits immediately between two students with strictly lower scores, that student's score decreases by 1. - The first and last students never change. The process repeats each day as long as at least one score changes. Return the final stable scores in order as a list. **Constraints** - $1 \le N \le 1000$ - Scores are between 0 and 1000 **Example** `stabilize([1, 6, 3, 4, 3, 5]) -> [1, 4, 4, 4, 4, 5]` Day 1 gives $[1, 5, 4, 3, 4, 5]$ and day 2 gives $[1, 4, 4, 4, 4, 5]$, after which no score changes, so that is the stable result. A: **How to Think About It:** The key trap is the word "simultaneously": every update on a given day must be computed from the same start-of-day snapshot, not from values you are mutating in place mid-row. Get that right and the rest is a straightforward simulate-until-no-change loop. Termination is not obvious but follows from a potential argument on the total variation of the row (see Complexity below), so the loop always ends. **Algorithm:** Loop: build a fresh copy of the array; for each interior index compare against the old neighbors and apply $+1$, $-1$, or no change; if nothing changed this pass, stop. The first and last entries are copied unchanged. **Code:** ```python def stabilize(A): # Simulate the daily score updates until no score changes, then return # the final stable scores. Each day's updates use a SNAPSHOT of the # previous day (simultaneous update). Endpoints never change. A = list(A) changed = True while changed: changed = False new = A[:] for i in range(1, len(A) - 1): if A[i-1] > A[i] and A[i+1] > A[i]: new[i] = A[i] + 1 changed = True elif A[i-1] < A[i] and A[i+1] < A[i]: new[i] = A[i] - 1 changed = True A = new return A ``` **Complexity:** $O(N)$ per day. For the day count, use the potential $V = \sum_{i=0}^{N-2} |A_{i+1} - A_i|$. Two neighbors can never both be strict local minima, nor both strict local maxima, so on every edge the gap either shrinks or stays put, and any day on which something moves shrinks $V$ by at least $1$. The process therefore stops after at most $V \le (N-1)R$ days, where $R$ is the score range, for a total cost of $O(N \cdot D)$ with $D$ the number of days. Note that $D$ is *not* bounded by $R$ alone: alternating scores $[0, 1, 0, 1, \ldots]$ across $1000$ students have range $R = 1$ but take $499$ days to settle. **Answer:** The function is named `stabilize(A)`. Simulate day by day using a snapshot of the previous scores, stopping when a full pass produces no change, then return the final list. --- Probability the Sum of Uniforms Stays Below One [medium, Probability] --- https://quantvault.org/problem-2660-probability-the-sum-of-uniforms-stays-below-one.html Q: Let $X_1, X_2, \dots$ be independent and identically distributed Uniform$(0,1)$ random variables, and write $S_n = X_1 + \cdots + X_n$. **(a)** For a fixed integer $n$, what is the probability that the first $n$ of them sum to less than $1$, i.e. $P(S_n 1\}$ be the number of uniforms you must draw before the running sum first exceeds $1$. Find $E[N]$. A: **How to Think About It:** The event $\{X_1 + \cdots + X_n n)$ in (c) are exactly these simplex volumes. **Quick Estimate:** For $n = 1$, $P(X_1 x$ because $S_n \ge 0$. *Step 4 -- Part (a).* Setting $x = 1$, $$P\!\left(\sum_{i=1}^n X_i n$ exactly when $S_n \le 1$, so $P(N > n) = 1/n!$ for $n \ge 0$ (with $P(N > 0) = 1$). By the tail-sum formula, $$E[N] = \sum_{n \ge 0} P(N > n) = \sum_{n \ge 0} \frac{1}{n!} = e \approx 2.718.$$ A simulation of one million runs gives $E[N] \approx 2.718$. **Answer:** (a) $\dfrac{1}{n!}$. (b) $P(S_n < x) = \dfrac{x^n}{n!}$, so $P(X_1 + X_2 + X_3 < 1/2) = \dfrac{1}{48}$. (c) $E[N] = e \approx 2.718$. --- Evaluate a Reverse-Polish-Notation Expression [easy, Coding] --- https://quantvault.org/problem-2759-evaluate-a-reverse-polish-notation-expression.html Q: You are given an arithmetic expression in Reverse Polish Notation (RPN) as a list of string `tokens`. Each token is either an integer literal (a possibly-negative base-10 integer) or one of the four binary operators `+`, `-`, `*`, `/`. Evaluate the expression and return the resulting integer. Processing rule: scan left to right; push every integer literal onto a stack; on an operator pop the top two values `b` (popped first, the right operand) and `a` (the left operand), push `a OP b`. Division `/` is integer division that TRUNCATES TOWARD ZERO (the C/C++ convention), e.g. `(-7)/2 == -3` and `7/(-2) == -3`. The expression is guaranteed valid (no underflow, no division by zero) and reduces to exactly one value, which you return. Complete: ``` rpn_eval(tokens: List[str]) -> int ``` Example: `['2','1','+','3','*']` is `(2+1)*3 = 9`. A: **How to Think About It:** RPN evaluation is the canonical **stack-machine** exercise: postfix needs no parentheses because operator order already encodes precedence — you just push operands and let each operator consume the top two. The one genuine trap is **operand order**: the value popped *first* is the *right* operand $b$, the one popped *second* is the *left* operand $a$, so you compute $a\ \text{OP}\ b$, not $b\ \text{OP}\ a$. This is invisible for $+$ and $*$ but flips the sign of $-$ and inverts $/$. The second trap is **truncation toward zero**: Python's `//` *floors* (rounds toward $-\infty$), so $-7\,//\,2 = -4$, but the C-convention answer is $-3$. Use `int(a / b)`, which truncates toward zero. Naming: `-3` as a literal is a valid token, so the "is it an operator?" test must check membership in the operator set, not "does it start with a digit." **Quick Estimate:** Trace `['2','1','+','3','*']` on paper-in-your-head. Push $2$, push $1$; `+` pops $b=1$, $a=2$, pushes $a+b=3$; push $3$; `*` pops $b=3$, $a=3$, pushes $9$. Stack $=[9]$ $\Rightarrow 9$. Now stress the two traps: `['-7','2','/']` $\to$ $a=-7,b=2$, `int(-7/2)=int(-3.5)=-3` (floor would give $-4$ — wrong); `['7','-2','/']` $\to$ `int(7/-2)=int(-3.5)=-3`. Both match the C convention. All verified in Python. **Approach:** Left-to-right scan; operators pop two (right then left), literals push `int(token)`; truncate division with `int(a/b)`. **Formal Solution:** ```python def rpn_eval(tokens): st = [] for t in tokens: if t in ('+', '-', '*', '/'): b = st.pop(); a = st.pop() # b = right, a = left if t == '+': st.append(a + b) elif t == '-': st.append(a - b) elif t == '*': st.append(a * b) else: st.append(int(a / b)) # truncate toward zero else: st.append(int(t)) # handles negative literals return st[-1] ``` Invariant: after processing a prefix of tokens, the stack holds exactly the values of the maximal already-reduced subexpressions, top = most recent. A valid RPN expression leaves exactly one value, returned as `st[-1]`. Membership test `t in ('+','-','*','/')` correctly classifies `'-3'` as a literal (it is not equal to `'-'`). $$\text{time } O(n),\qquad \text{space } O(n)\ \text{(stack depth)}.$$ **Answer:** Stack-evaluate with right-then-left pops and toward-zero division; the example gives $\boxed{9}$. --- Odd-Length Strings Containing a Vowel [easy, Coding] --- https://quantvault.org/problem-2767-odd-length-strings-containing-a-vowel.html Q: Given an array of strings `arr`, count how many strings `s` have ODD length AND contain at least one vowel (a, e, i, o, u, case-insensitive). Complete: ``` count(arr: List[str]) -> int ``` Example: `count(['abc', 'xy', 'Hi', 'rhythm'])` returns `1` -- only 'abc' (length 3, contains 'a') satisfies both conditions; 'Hi' (len 2), 'rhythm' (len 6) and 'xy' (len 2) have even length. The empty string has length 0 (even) so never counts. A: **How to Think About It:** Two independent per-string predicates joined by AND — the heuristic is a **single streaming pass with short-circuit evaluation**, no sorting or precomputation. Odd length is $O(1)$; the vowel test is a membership scan that `any(...)` aborts at the first vowel, so worst case (no vowels) it reads the whole string but typically stops early. The one subtle point is **case-insensitivity done right**: rather than lowercasing every string (which allocates a copy per string), put both cases into the vowel set once. Order matters for cost too — testing `len(s) % 2 == 1` *first* skips the character scan entirely for even-length strings. **Quick Estimate:** Trace the spec `['abc','xy','Hi','rhythm']`. Filter by odd length first: `'abc'` (3, odd ✓), `'xy'` (2 ✗), `'Hi'` (2 ✗), `'rhythm'` (6 ✗) — only `'abc'` survives. Vowel check on `'abc'`: `'a'` is a vowel ✓. Count $= \boxed{1}$, matching the spec. Sanity on the empty string mentioned in the prompt: `len('') = 0` is even, filtered out before any vowel scan — never counted. **Approach:** One `sum` over a generator with both predicates. **Formal Solution:** Precompute the vowel set `V = set('aeiouAEIOU')` so membership is $O(1)$ and case is handled without transforming the input. Then sum `1` over each string satisfying both conditions: ```python def count(arr): V = set('aeiouAEIOU') return sum(1 for s in arr if len(s) % 2 == 1 and any(c in V for c in s)) ``` The `and` short-circuits: `len(s) % 2 == 1` gates the vowel scan, so even-length strings cost $O(1)$; `any(c in V for c in s)` stops at the first vowel. The empty string has even length $0$, so it is excluded, as required. Complexity $\boxed{O(\sum_s |s|)\text{ time (worst case)},\ O(1)\text{ extra space}}$ — the vowel set is a fixed 10 elements. **Answer:** One pass counting odd-length strings that contain a vowel; the example returns `1`. --- Uniform Sampling Game - Fair Price [easy, Coding] --- https://quantvault.org/problem-2791-uniform-sampling-game-fair-price.html Q: Bob plays an optimal-stopping game. A fresh value $v$ is drawn uniformly from $[0, n]$. After seeing the current draw Bob either KEEPS it (the game ends and he is paid $v$ dollars) or pays a resample fee of $d$ dollars to throw the value away and draw again. There is no limit on the number of resamples. The first draw is free; every resample after that costs $d$. Bob plays the value-maximizing strategy, which is a single threshold: keep the current draw iff it is at least the optimal threshold $T = n - \sqrt{2 n d}$, otherwise pay $d$ and redraw. Under this policy each individual draw stops the game with probability $p = (n - T)/n$, so the number of draws is geometric and the expected number of resamples is $E[\text{resamples}] = T/(n - T)$. The expected payout conditioned on stopping is the mean of a uniform variable above the threshold, $E[v \mid v \ge T] = (n + T)/2$. A player pays an entry price $x$ up front to play, then receives Bob's net result (payout minus all resample fees). The FAIR price is the $x$ at which the player's expected profit is exactly zero: $x = \dfrac{n + T}{2} - d \cdot \dfrac{T}{n - T}$. Implement `calc_optimal_price(n, d)` returning this fair price $x$ as a float (full precision, NOT rounded to the nearest cent). I/O contract: inputs are two numbers `n` (the upper bound of the uniform draw, $n > 0$) and `d` (the per-resample fee, $0 < d < n/2$ so that $T$ lies strictly in $(0, n)$). The function returns one float, the fair entry price. Worked example: `calc_optimal_price(100, 8)`. Here $T = 100 - \sqrt{2 \cdot 100 \cdot 8} = 100 - \sqrt{1600} = 100 - 40 = 60$. The expected payout is $(100 + 60)/2 = 80$, the expected number of resamples is $60/(100 - 60) = 1.5$, so $x = 80 - 8 \cdot 1.5 = 80 - 12 = 68.0$. A: **How to Think About It:** This builds on the optimal-stopping threshold $T = n - \sqrt{2nd}$: Bob keeps a draw iff $v \ge T$. The fair entry price is Bob's expected *net* winnings under that policy — expected payout minus expected resample fees. Each draw independently stops the game with probability $p = (n - T)/n$, so the number of draws is Geometric($p$) with mean $n/(n - T)$; since the first draw is free, the expected number of *paid* resamples is $n/(n-T) - 1 = T/(n - T)$. The payout, conditioned on stopping, is uniform on $[T, n]$ with mean $(n + T)/2$. **Algorithm:** 1. **Threshold:** $T = n - \sqrt{2 n d}$. 2. **Expected payout:** $E[v \mid v \ge T] = (n + T)/2$. 3. **Expected fees:** $d \cdot T/(n - T)$ (geometric draw count, first draw free). 4. **Fair price:** return $x = \dfrac{n + T}{2} - d \cdot \dfrac{T}{n - T}$ at full float precision — do not round. **Code:** ```python import math def calc_optimal_price(n, d): T = n - math.sqrt(2 * n * d) return (n + T) / 2 - d * (T / (n - T)) ``` **Complexity:** $O(1)$ time and space — a square root and a few arithmetic operations. **Answer:** $x = \frac{n + T}{2} - \frac{dT}{n - T}$ with $T = n - \sqrt{2nd}$. Check with $n = 100$, $d = 8$: $T = 60$, payout $= 80$, resamples $= 1.5$, so $x = 80 - 12 = 68$. (The constraint $0 < d < n/2$ keeps $T \in (0, n)$, so the division is safe.) --- Expected Wait for a Poisson Bus [medium, Probability] --- https://quantvault.org/problem-2956-expected-wait-for-a-poisson-bus.html Q: Buses arrive at a stop according to a Poisson process, averaging one bus every 10 minutes. **(a)** You walk up to the stop at the exact instant the previous bus pulls away. How long should you expect to wait for the next bus? Now suppose instead that you show up at an arbitrary moment, unrelated to the bus schedule. **(b)** What is your expected wait until the next bus? **(c)** On average, how long ago did the previous bus leave? **(d)** What is the expected length of the gap between buses that contains your arrival time? Reconcile your answers with the fact that the average gap between buses is 10 minutes. A: **How to Think About It:** The magic word is "Poisson." For a Poisson process the gaps between arrivals are i.i.d. exponential, and the exponential is memoryless -- the process has no memory of how long it has been since the last event. In (a) you are at the stop at a departure instant, so you are simply waiting for one fresh interarrival gap. Mean gap = mean wait. When you arrive at a random moment instead, memorylessness still settles the forward wait, time reversal settles the time since the last bus, and length-biased sampling (the inspection paradox) explains why those two add up to more than one average gap. **Quick Estimate:** Rate is $\lambda = 1/10$ bus per minute, so the mean gap between buses is $1/\lambda = 10$ minutes. You wait one full gap: about 10 minutes. For a random arrival the forward wait is still 10 (memorylessness), the time back to the last bus is also about 10, so the gap you land in is about 20, twice a typical gap. **Formal Solution:** **Part (a): Arriving at a departure.** Let $T$ be the time from the previous departure to the next arrival. In a Poisson$(\lambda)$ process, interarrival times are $T \sim \text{Exp}(\lambda)$ with $\lambda = 1/10$ per minute, so $$E[T] = \frac{1}{\lambda} = 10 \text{ minutes.}$$ A quick simulation of exponential gaps with mean 10 confirms an average wait of $\approx 10.0$ minutes. **Part (b): Forward wait from a random arrival.** Suppose $s$ minutes have passed since the last bus when you arrive. Memorylessness says $P(T > s + t \mid T > s) = P(T > t)$, so the remaining time is again $\text{Exp}(\lambda)$, whatever $s$ is. Hence $E[\text{wait}] = 1/\lambda = 10$ minutes. The tempting answer of 5 ("I land in the middle of a 10-minute gap") would be right for buses on a fixed 10-minute schedule, not for exponential gaps. **Part (c): Time since the last bus.** Run the process backward in time from your arrival instant. A Poisson process reversed in time is again a Poisson process with the same rate, so the time back to the most recent bus (the age $A$) is the first arrival of the reversed process: $A \sim \text{Exp}(\lambda)$ and $E[A] = 10$ minutes. (Strictly, $A = \min(\text{Exp}(\lambda), t_0)$ if the process started $t_0$ minutes ago; for a process that has been running a long time this is 10.) **Part (d): The gap you land in (inspection paradox).** The gap containing your arrival is $A + W$, with age $A$ and forward wait $W$ independent $\text{Exp}(\lambda)$. So it is $\text{Gamma}(2, \lambda)$ with mean $2/\lambda = 20$ minutes. This does not contradict the 10-minute average gap. A random instant is more likely to fall in a long gap than a short one: the chance of landing in a gap of length $x$ is proportional to $x$, so the gap you sample is length-biased, with density $$\frac{x f(x)}{E[X]} = \lambda^2 x e^{-\lambda x},$$ exactly $\text{Gamma}(2, \lambda)$. You land, on average, in the middle of that longer gap, so the age and the wait are $20/2 = 10$ each. A simulation of a long run of exponential gaps gives forward wait $\approx 10.0$, age $\approx 10.0$ and containing gap $\approx 19.9$ minutes. **Answer:** (a) $10$ minutes. (b) $10$ minutes: memorylessness makes the arrival time irrelevant. (c) $10$ minutes: the time-reversed process is Poisson with the same rate. (d) $20$ minutes: the gap containing a random instant is length-biased, $\text{Gamma}(2, \lambda)$, which is twice a typical 10-minute gap and is why $10 + 10 = 20$. --- Range of a Union Probability from Marginals [medium, Probability] --- https://quantvault.org/problem-3397-range-of-a-union-probability-from-marginals.html Q: Two names in a portfolio have default indicators $I_1$ and $I_2$ with $P(I_1) = 0.7$ and $P(I_2) = 0.8$. You are told nothing about their dependence. **(a)** What is the full range of possible values for $P(I_1 \text{ or } I_2)$, the probability that at least one defaults? **(b)** What is the full range of possible values for the correlation between the two default indicators? A: **How to Think About It:** With the marginals fixed but the joint free, the union probability is pinned only by the overlap $P(I_1 \text{ and } I_2)$. Push the events to overlap as much as possible to make the union small; pull them apart to make it large. Those two extremes are exactly the Frechet bounds, and you can read them off without algebra. The correlation of the indicators is also an increasing function of the overlap, so the same two extremes bound it too. **Quick Estimate:** Maximum overlap: the smaller event ($I_1$, prob $0.7$) sits entirely inside the larger ($I_2$, prob $0.8$), so the union is just $0.8$. Minimum overlap: $0.7 + 0.8 = 1.5 > 1$, so the events cannot be disjoint -- they must overlap by at least $0.5$, which forces the union up to its ceiling of $1$. Independence would give an overlap of $0.56$ and a union of $0.94$, with correlation $0$; because the marginals differ, the correlation cannot reach $\pm 1$ at either extreme. **Formal Solution:** **Part (a): Union.** Use inclusion-exclusion: $$P(I_1 \cup I_2) = P(I_1) + P(I_2) - P(I_1 \cap I_2) = 1.5 - P(I_1 \cap I_2).$$ The intersection is bounded by the Frechet inequalities: $$\max(0,\, 0.7 + 0.8 - 1) \le P(I_1 \cap I_2) \le \min(0.7, 0.8),$$ i.e. $0.5 \le P(I_1 \cap I_2) \le 0.7$. Substituting the endpoints: - Largest intersection $0.7$ gives the smallest union $1.5 - 0.7 = 0.8$. - Smallest intersection $0.5$ gives the largest union $1.5 - 0.5 = 1.0$. **Part (b): Default correlation.** The same overlap $q = P(I_1 \cap I_2)$ drives the correlation. For indicators, $\text{Var}(I_1) = 0.7 \cdot 0.3 = 0.21$, $\text{Var}(I_2) = 0.8 \cdot 0.2 = 0.16$ and $\text{Cov}(I_1, I_2) = q - 0.7 \cdot 0.8 = q - 0.56$, so $$\rho = \frac{q - 0.56}{\sqrt{0.21 \cdot 0.16}} = \frac{q - 0.56}{\sqrt{0.0336}},$$ increasing in $q$. Map the Frechet endpoints $q \in [0.5, 0.7]$ through it: - $q = 0.7$ (nesting): $\rho = 0.14/\sqrt{0.0336} = \sqrt{7/12} \approx 0.764$. - $q = 0.5$ (least overlap): $\rho = -0.06/\sqrt{0.0336} = -\sqrt{3/28} \approx -0.327$. So $\rho \in [-\sqrt{3/28},\ \sqrt{7/12}] \approx [-0.327,\ 0.764]$. The range is not $[-1, 1]$: correlation $+1$ would need $I_1 = I_2$, impossible with different marginals, and $-1$ would need $I_2 = 1 - I_1$, impossible since $0.8 \ne 0.3$. It is also lopsided, because both names are likely to default, so they are forced to overlap and cannot be strongly negatively correlated. Independence ($q = 0.56$, union $0.94$) sits inside both ranges. **Answer:** (a) $P(I_1 \text{ or } I_2) \in [0.8,\ 1.0]$. The lower end is a perfect nesting ($I_1 \subseteq I_2$); the upper end is the least-overlapping configuration the marginals allow, with overlap $0.5$. (b) $\rho \in [-\sqrt{3/28},\ \sqrt{7/12}] \approx [-0.327,\ 0.764]$, with the same two configurations at the endpoints. --- Finding the Three Fastest of 25 Horses in Seven Races [medium, Brain Teaser] --- https://quantvault.org/problem-3611-finding-the-three-fastest-of-25-horses-in-seven-races.html Q: You have 25 horses. Each horse runs at its own constant speed, all 25 speeds are distinct, and a horse's speed never changes from race to race. Your track has exactly 5 lanes, so a race can involve at most 5 horses at a time, and you have no stopwatch: the only information a race gives you is the finishing order of the horses in that race. **(a)** What is the minimum number of races needed to identify the single fastest horse? **(b)** What is the minimum number of races needed to identify the three fastest horses, in order? Describe a strategy that achieves this number and explain why no smaller number can work. A: **How to Think About It:** With no stopwatch, races only give ordinal information, so this is a comparison-based selection problem where each "comparison" ranks 5 items at once. Think about what each race can rule out. A horse is out of the top 3 as soon as three horses are known (directly or by transitivity) to be faster than it. **Approach:** Race in groups, race the winners, then use transitivity to shrink the candidate pool for places 2 and 3 to five horses, and race them. **Formal Solution:** *Step 1 -- Group races (races 1 to 5).* Split the 25 horses into 5 groups of 5 and race each group. Within each group we now know the full order. Label the groups so that their winners are $A_1, B_1, C_1, D_1, E_1$ and the horses of group $A$ in finishing order are $A_1, A_2, \ldots, A_5$, and similarly for the other groups. *Step 2 -- Race of winners (race 6).* Race $A_1, B_1, C_1, D_1, E_1$. Relabel the groups so this race finishes in the order $A_1, B_1, C_1, D_1, E_1$. Then $A_1$ is the fastest horse of all: it beat every other group winner, and each group winner beat every horse in its group. This settles part (a): six races suffice for the fastest horse. Fewer cannot work: call a horse *unbeaten* if it has never finished behind another horse. You cannot name the fastest horse while two horses are unbeaten, since either could be faster than everyone. All 25 start unbeaten, and one race can make at most 4 of them beaten (its winner loses to nobody in it), so getting down to one unbeaten horse takes at least $24/4 = 6$ races. *Step 3 -- Eliminate by transitivity.* Which horses can still be second or third overall? - Groups $D$ and $E$: their winners $D_1$ and $E_1$ already lost to $A_1, B_1, C_1$, so every horse in $D$ and $E$ has at least 3 faster horses. All 10 are out. - Group $C$: $C_1$ lost to $A_1$ and $B_1$, so $C_1$ could still be third. But $C_2$ is behind $C_1, B_1, A_1$, so $C_2, \ldots, C_5$ are out. - Group $B$: $B_1$ could be second, $B_2$ could be third (behind $B_1$ and $A_1$ only). $B_3$ is behind $B_2, B_1, A_1$, so $B_3, B_4, B_5$ are out. - Group $A$: $A_2$ could be second, $A_3$ could be third. $A_4$ is behind $A_1, A_2, A_3$, so $A_4, A_5$ are out. Exactly five candidates remain for places 2 and 3: $A_2, A_3, B_1, B_2, C_1$. *Step 4 -- Final race (race 7).* Race those five horses. The first two finishers are the second- and third-fastest horses overall. *Step 5 -- Why 6 races are not enough for the top 3.* Answer every race like an adversary who stays consistent with all earlier results: the winner is an unbeaten horse (one who has raced before, if there is one), and the rest finish in any order consistent with what is already known. Making an unbeaten horse win is always consistent, because nobody is known to be faster than it. - Against these answers a race removes at most (number of unbeaten horses in it) $- 1 \le 4$ horses from the unbeaten pool. Six races must remove $24$, so every race has to be five unbeaten horses. In particular, a horse that has lost never races again. - Race 6 must include a horse that raced before: if all five were newcomers, the other $20$ horses would still contain an unbeaten horse after race 5, leaving two unbeaten horses at the end. So the adversary lets a veteran win race 6; call it $X$, the fastest horse. - Let $R$ be the runner-up in $X$'s first race and $W$ the runner-up in race 6. Each has lost only to $X$. $R$ never races again, and $W$ was unbeaten before race 6, so $W$ was not in $X$'s first race and the two never met. Nothing orders $R$ against $W$. Either could be second, so six races cannot even pin down the top two, let alone the top three in order. Seven is the minimum. **Answer:** (a) 6 races. (b) 7 races: 5 group races, 1 race of the group winners (its winner is fastest overall), then 1 race among $A_2, A_3, B_1, B_2, C_1$ whose top two are the second- and third-fastest horses. --- Packing 53 Bricks of Size 1x1x4 Into a 6x6x6 Box [hard, Brain Teaser] --- https://quantvault.org/problem-3612-packing-53-bricks-of-size-1x1x4-into-a-6x6x6-box.html Q: You have a cubic box with internal dimensions $6 \times 6 \times 6$ (volume $216$) and 53 rigid bricks, each of dimensions $1 \times 1 \times 4$ (total volume $53 \times 4 = 212$). Bricks must be placed with their edges parallel to the box's edges, so each brick occupies four unit cubes in a straight line along one of the three axes. Can all 53 bricks be packed inside the box? Prove your answer. A: **How to Think About It:** A packing question with a tiny volume slack is a signal to look for a colouring invariant, as in the mutilated chessboard. We need a colouring such that (i) every brick covers the same number of cells of each colour, and (ii) the box has an unequal number of cells of each colour. The mismatch then bounds how many bricks can fit. **Approach:** Tile the box with 27 blocks of size $2 \times 2 \times 2$, checkerboard-colour those blocks, count cells per colour, and count how each brick splits across colours. **Formal Solution:** *Step 1 -- The colouring.* Index unit cubes by $(x, y, z)$ with $x, y, z \in \{0, 1, \ldots, 5\}$. Partition the box into $27$ blocks of size $2 \times 2 \times 2$; the block containing $(x, y, z)$ is $(\lfloor x/2 \rfloor, \lfloor y/2 \rfloor, \lfloor z/2 \rfloor) \in \{0,1,2\}^3$. Colour a block black if $\lfloor x/2 \rfloor + \lfloor y/2 \rfloor + \lfloor z/2 \rfloor$ is odd and white if it is even, and give every unit cube the colour of its block. *Step 2 -- Count cells per colour.* Among the 27 blocks, the parity of the coordinate sum is even for 14 blocks and odd for 13 blocks (the corner blocks such as $(0,0,0)$ and $(2,2,2)$ are even, and a $3 \times 3 \times 3$ checkerboard has one more of the corner colour). So there are $14 \times 8 = 112$ white cells and $13 \times 8 = 104$ black cells. *Step 3 -- What one brick covers.* A brick occupies 4 consecutive cells along one axis, say $(x, y, z), (x+1, y, z), (x+2, y, z), (x+3, y, z)$ with $x \in \{0, 1, 2\}$. As $x$ runs over four consecutive values, $\lfloor x/2 \rfloor$ takes two consecutive values, each exactly twice: for $x_0 = 0$ we get $0, 0, 1, 1$; for $x_0 = 1$ we get $0, 1, 1, 2$; for $x_0 = 2$ we get $1, 1, 2, 2$. Adjacent blocks along an axis have opposite colours, so every brick covers exactly 2 black cells and 2 white cells. (A direct check over all $324$ possible brick placements confirms this.) *Step 4 -- The bound.* If $k$ bricks are packed, they cover $2k$ black cells, and there are only $104$ black cells. Hence $2k \le 104$, so $k \le 52$. *Step 5 -- Conclusion.* 53 bricks would need 106 black cells, but the box has only 104. No packing exists. (The volume slack of 4 cells was a red herring: what actually binds is the colour imbalance.) *Step 6 -- 52 is achievable.* Fill the slab $z \in \{0,1,2,3\}$ with $36$ vertical bricks, one per $(x, y)$ column. In each of the two remaining layers $z = 4, 5$, lay $6$ bricks along $x$ covering $x \in \{0,\dots,3\}$, one per row, and $2$ bricks along $y$ covering $y \in \{0,\dots,3\}$ in the columns $x = 4, 5$: $8$ per layer. That is $36 + 16 = 52$ bricks, so the bound is tight. **Answer:** No. Checkerboard-colouring the 27 sub-blocks of size $2 \times 2 \times 2$ gives 104 cells of one colour and 112 of the other; every $1 \times 1 \times 4$ brick covers exactly 2 cells of each colour, so at most $104 / 2 = 52$ bricks fit, and 53 cannot. 52 do fit (36 vertical bricks in a $4 \times 6 \times 6$ slab plus 8 in each of the two remaining layers). --- Calendar Cubes: Two Dice That Show Every Date [easy, Brain Teaser] --- https://quantvault.org/problem-3613-calendar-cubes-two-dice-that-show-every-date.html Q: A desk calendar shows the day of the month using two cubes placed side by side. Each of the 12 faces carries a single digit from $0$ to $9$, and the two cubes can be placed in either order, so the pair must be able to display every two-digit day from $01$ to $31$ (single-digit days are shown with a leading zero, so $01, 02, \ldots, 09$ must all be displayable). **(a)** Which six digits go on each cube? Find a labelling that works, and explain the key trick that makes it possible. **(b)** Show that without that trick no labelling can display all 31 dates. A: **How to Think About It:** Start from the dates that impose the tightest constraints (repeated digits and leading zeros), count faces, and see where the count fails. Then look for slack in the physical setup rather than in the arithmetic. **Approach:** Derive the forced digits, count, and resolve the deficit with the 6/9 rotation. **Formal Solution:** *Step 1 -- Forced repeated digits.* The dates $11$ and $22$ require a $1$ on both cubes and a $2$ on both cubes. *Step 2 -- Forced zeros.* The dates $01, 02, \ldots, 09$ require every digit $1$ through $9$ to be paired with a $0$ on the other cube. Suppose only one cube carried a $0$; then the other cube would have to hold all of $1$ through $9$, which is nine digits on six faces. Impossible, so both cubes carry a $0$. *Step 3 -- Counting the remaining faces.* Each cube now holds $0, 1, 2$, using $6$ of the $12$ faces. The remaining six faces must accommodate the digits $3, 4, 5, 6, 7, 8, 9$, which is seven distinct digits. Since $7 > 6$, no assignment of ten distinct glyphs works. This proves part (b): with each face showing one fixed digit, the calendar is impossible. *Step 4 -- The trick.* A $6$ rotated by $180^\circ$ reads as a $9$. So a single face can serve as both $6$ and $9$, and only six symbols $\{3, 4, 5, 6, 7, 8\}$ need placing on the six free faces. The count now matches exactly. *Step 5 -- A working labelling.* Cube A: $\{0, 1, 2, 3, 4, 5\}$. Cube B: $\{0, 1, 2, 6, 7, 8\}$ with the $6$ doubling as $9$. Checks: $01$-$09$ use a $0$ from one cube and the digit from the other (the $9$ is the flipped $6$ on cube B with the $0$ on cube A); $10$-$19$ use the $1$ on one cube and the units digit on the other; $20$-$29$ likewise with the $2$; $30$ and $31$ use the $3$ on cube A with $0$ or $1$ on cube B. A brute-force enumeration of all labellings confirms that solutions exist only when the 6/9 rotation is allowed (there are 20 such unordered pairs of labellings, all of which put $0, 1, 2$ on both cubes), and this one is the standard choice. **Answer:** (a) Cube A: $0, 1, 2, 3, 4, 5$; cube B: $0, 1, 2, 6, 7, 8$, where the $6$ is turned upside down to display $9$. (b) Both cubes must carry $0, 1, 2$, leaving six faces for the seven digits $3$ through $9$, so without the 6/9 trick it is impossible. --- Computing an Average Salary Without Revealing Anyone's Salary [easy, Brain Teaser] --- https://quantvault.org/problem-3615-computing-an-average-salary-without-revealing-anyone-s-salar.html Q: Eight quants on a desk are curious about the average salary of the group, but none of them is willing to disclose his or her own salary to anyone else. They have no trusted third party and no computer they all trust; all they can do is talk to each other and pass slips of paper. Design a procedure that lets the eight quants compute the exact average salary of the group while guaranteeing that no quant learns any other individual's salary (assuming no two quants collude). A: **How to Think About It:** The obstacle is not the arithmetic but the information flow. Whoever receives a number must not be able to reconstruct anyone's salary from it. Masking the very first contribution with a secret random offset makes every intermediate value uninformative, while the offset is trivially removed at the end. **Approach:** Additive masking around a ring. **Formal Solution:** *Step 1 -- Setup.* Label the quants $1, 2, \ldots, 8$ with salaries $s_1, \ldots, s_8$. Arrange them in a ring: quant $i$ passes to quant $i+1$, and quant $8$ passes back to quant $1$. *Step 2 -- Masking.* Quant $1$ privately picks a random number $R$ (large enough that $s_1 + R$ looks like noise, and known only to quant $1$). He writes $T_1 = s_1 + R$ on a slip and passes it to quant $2$. *Step 3 -- Accumulation.* Each subsequent quant $i$ receives $T_{i-1}$, adds his own salary, and passes $T_i = T_{i-1} + s_i$ to the next quant. After quant $8$, the slip returns to quant $1$ carrying $$T_8 = R + s_1 + s_2 + \cdots + s_8 .$$ *Step 4 -- Unmasking.* Quant $1$ subtracts $R$ to get $S = \sum_{i=1}^8 s_i$ and announces the average $S / 8$. *Step 5 -- Why nothing leaks.* Quant $2$ sees only $s_1 + R$, which is uninformative because $R$ is unknown to him. Quant $i \ge 3$ sees $R + s_1 + \cdots + s_{i-1}$, a sum masked by $R$ and containing several salaries, so no individual value can be extracted. Quant $1$ sees $R + S$ and learns only $S$, which everyone learns anyway from the announced average. The published average itself reveals only the total. (If quants $i-1$ and $i+1$ collude they can recover $s_i$ by differencing, which is why the guarantee assumes no collusion; a stronger version has each quant split his salary into random shares handed to different people.) **Answer:** Pass a slip around the ring: quant 1 starts with (salary + secret random number $R$), each of the other seven adds his salary, and quant 1 finally subtracts $R$ and divides the total by 8 to announce the average. Every intermediate total is masked by $R$, so no individual salary is revealed. --- Breaking a Clock Face Into Three Pieces With Equal Sums [easy, Brain Teaser] --- https://quantvault.org/problem-3617-breaking-a-clock-face-into-three-pieces-with-equal-sums.html Q: A round clock face carries the numbers $1$ through $12$ arranged in the usual order around its rim. It falls and breaks into exactly three pieces. When you pick up the pieces, you notice that the numbers on each piece add up to the same total. The break was clean and the pieces are not oddly shaped: the clock was cut along two straight lines that do not cross inside the face, so each piece is the region between the rim and a chord, or between two chords. Which numbers are on each of the three pieces? A: **How to Think About It:** First use the arithmetic to pin down the per-piece total. Then translate "not oddly shaped" into a structural constraint: pieces cut by two straight non-crossing lines. That means the outer pieces are runs of consecutive numbers on the circular rim (with $12$ wrapping to $1$), and the middle piece takes everything else. **Quick Estimate:** $1 + 2 + \cdots + 12 = 78$, so each piece sums to $26$. Numbers opposite each other across a horizontal axis pair up as $12 + 1$, $11 + 2$, $10 + 3$, $9 + 4$, $8 + 5$, $7 + 6$, all equal to $13$. Two such pairs make $26$, so horizontal cuts are natural. **Formal Solution:** *Step 1 -- The target.* $\sum_{k=1}^{12} k = \frac{12 \times 13}{2} = 78$, and $78 / 3 = 26$, so every piece must sum to $26$. *Step 2 -- Structure of the pieces.* Two straight cuts that do not cross inside the face give two "cap" pieces, each containing a set of consecutive numbers along the rim, and one "band" piece between the chords containing the numbers on both remaining sides of the face. So we need two disjoint runs of consecutive clock numbers (cyclically, so $12$ is followed by $1$) each summing to $26$, with the leftover numbers also summing to $26$. *Step 3 -- Find a run summing to 26 that contains 12.* Runs of consecutive numbers around the top: $12 + 1 = 13$, $11 + 12 + 1 = 24$, $11 + 12 + 1 + 2 = 26$. So the top cap $\{11, 12, 1, 2\}$ works. *Step 4 -- Find a second run summing to 26 among the rest.* On the bottom, $5 + 6 + 7 + 8 = 26$. That is the bottom cap. *Step 5 -- The band.* The remaining numbers are $\{3, 4\}$ on the right side and $\{9, 10\}$ on the left side, and $3 + 4 + 9 + 10 = 26$. Geometrically, this is the horizontal band between the two chords, so the three pieces are consistent with two parallel straight cuts. *Step 6 -- Uniqueness.* Checking every way to choose two disjoint cyclic runs as the caps, with the rest as the band, gives this as the only split with all three sums equal to $26$. The trap is forgetting that $12$ and $1$ are neighbours: without wrapping around, no run summing to $26$ contains $12$, and the puzzle looks impossible. **Answer:** Each piece sums to $26$: the top piece $\{11, 12, 1, 2\}$, the bottom piece $\{5, 6, 7, 8\}$, and the middle band $\{9, 10, 3, 4\}$. --- Finding the Counterfeit Bag With One Weighing [easy, Brain Teaser] --- https://quantvault.org/problem-3618-finding-the-counterfeit-bag-with-one-weighing.html Q: There are 10 bags, each containing 100 coins. In 9 of the bags every coin is genuine and weighs exactly 10 grams. In the remaining bag every coin is counterfeit; all counterfeit coins in that bag have the same weight, which is either 9 grams or 11 grams (you do not know which). The bags look identical from the outside. You have a digital scale that displays the exact weight of whatever is placed on it, but you may use it only once. How can you use that one weighing to find the counterfeit bag and also tell whether its coins are light (9 grams) or heavy (11 grams)? A: **How to Think About It:** A single exact reading is one real number; you need an injective map from the 20 possible states (10 bags times light/heavy) into readings. Give each bag a distinct "signature" by taking a distinct number of coins from it. **Quick Estimate:** If bag $k$ is counterfeit and you took $k$ coins from it, the total is off by $k$ grams, in the direction of the fake weight. Distinct $k$ means distinct deviations, so 1, 2, ..., 10 coins from bags 1 through 10 does it. **Formal Solution:** *Step 1 -- Construct the sample.* Number the bags $1$ to $10$ and take $i$ coins from bag $i$. The sample has $1 + 2 + \cdots + 10 = 55$ coins. *Step 2 -- Nominal weight.* If all coins were genuine the scale would read $55 \times 10 = 550$ grams. *Step 3 -- Read the deviation.* Let the reading be $W$ and let bag $k$ be the counterfeit bag with coins of weight $w \in \{9, 11\}$. The $k$ coins from bag $k$ each differ from $10$ g by $w - 10 = \pm 1$, so $$W - 550 = k \, (w - 10) = \pm k .$$ *Step 4 -- Decode.* $|W - 550| = k$ identifies the bag, and the sign tells you the type: $W 550$ means 11 g. Every one of the 20 possible states maps to a distinct reading in $\{-10, \ldots, -1, 1, \ldots, 10\} + 550$, so the decoding is unambiguous. *Step 5 -- Remark.* Taking $0, 1, \ldots, 9$ coins also works (45 coins, 450 g nominal), as long as the deviation $0$ is interpreted as "the bag with zero coins is counterfeit". **Answer:** Take $i$ coins from bag $i$ for $i = 1, \ldots, 10$ (55 coins) and weigh them once. The reading minus $550$ g equals $+k$ or $-k$, where $k$ is the counterfeit bag and the sign tells you whether its coins weigh 11 g (positive) or 9 g (negative). --- Two Guests Who Shook the Same Number of Hands [easy, Brain Teaser] --- https://quantvault.org/problem-3620-two-guests-who-shook-the-same-number-of-hands.html Q: You attend a party with your 25 teammates, so there are 26 people in the room. Every one of your 25 teammates shakes your hand. Beyond that, the teammates shake hands among themselves in some arbitrary pattern: any pair of teammates may or may not shake hands, and you do not get to see who shook hands with whom. No pair shakes hands more than once, and nobody shakes their own hand. Without knowing anything about the pattern of handshakes among your teammates, can you be certain that there are two people in the room who shook exactly the same number of hands? Prove your answer. A: **How to Think About It:** "Can you be certain" with no information about the pattern is a cue for a pigeonhole argument: identify the range of possible values (the holes) and compare it with the number of people (the pigeons). The one piece of structure you do have is that you shook hands with everyone, and that is what closes the gap. **Approach:** Bound the possible handshake counts and count. **Formal Solution:** *Step 1 -- Model.* Represent the room as a graph with 26 vertices (you and 25 teammates), with an edge between two people if they shook hands. Each person's handshake count is the degree of their vertex. *Step 2 -- Upper bound on degrees.* No one can shake more than 25 hands, since there are only 25 other people. So every degree is at most $25$. *Step 3 -- Lower bound on degrees.* You shook hands with all 25 teammates, so your degree is exactly $25$, and every teammate shook at least your hand, so every teammate's degree is at least $1$. Hence every degree lies in $\{1, 2, \ldots, 25\}$. *Step 4 -- Pigeonhole.* There are 26 people (pigeons) and only 25 possible degree values (holes). By the pigeonhole principle at least two people have the same degree, i.e. shook the same number of hands. *Step 5 -- Why the assumption about you matters.* In a general graph on 26 vertices, degrees range over $\{0, 1, \ldots, 25\}$, which is 26 values, so pigeonhole does not apply directly. The classical fix is that $0$ and $25$ cannot both occur (a person who shook everyone's hand rules out a person who shook nobody's), which again leaves at most 25 values. Here the problem hands you that step: because you shook everyone's hand, the value $0$ is excluded outright. **Answer:** Yes. Every one of the 26 people shook between 1 and 25 hands (nobody shook zero, since everyone shook yours), so 26 people share only 25 possible counts and two of them must have shaken the same number of hands. --- Six People at a Party: Three Mutual Friends or Three Mutual Strangers [medium, Brain Teaser] --- https://quantvault.org/problem-3621-six-people-at-a-party-three-mutual-friends-or-three-mutual-s.html Q: Six people arrive at a party. For any two of them, either they had met before the party or they had not (the relation is symmetric: if A has met B then B has met A). Prove that there must be either three people who had all met each other before the party, or three people no two of whom had met before. Also explain why the same claim can fail for five people. A: **How to Think About It:** The two-valued relation on six people suggests a 2-coloured complete graph $K_6$. We want a monochromatic triangle. Pigeonhole on one vertex's five edges gives three edges of the same colour, and a short case split finishes the argument. **Approach:** Pick any person, apply pigeonhole to their five relationships, then split into cases. **Formal Solution:** *Step 1 -- Setup.* Represent the six people as the vertices of the complete graph $K_6$. Colour the edge between two people red if they had met before and blue if they had not. Every one of the $\binom{6}{2} = 15$ edges gets exactly one colour. We must show there is a red triangle or a blue triangle. *Step 2 -- Pigeonhole at one vertex.* Choose any person $P$. Five edges leave $P$, each red or blue. By the pigeonhole principle, at least $\lceil 5/2 \rceil = 3$ of them have the same colour. Without loss of generality (swap the colour names otherwise), $P$ is joined by red edges to three people $A$, $B$, $C$. *Step 3 -- Case split on the triangle $ABC$.* - If any of the edges $AB$, $BC$, $CA$ is red, say $AB$, then $P$, $A$, $B$ are pairwise joined by red edges: three people who had all met before. - Otherwise all three edges $AB$, $BC$, $CA$ are blue, and $A$, $B$, $C$ are three mutual strangers. Either way a monochromatic triangle exists, which proves the claim. (A brute-force check of all $2^{15} = 32768$ colourings of $K_6$ confirms that every one contains a monochromatic triangle.) *Step 4 -- Five people are not enough.* Arrange five people in a circle and let each person have met exactly their two neighbours (a red 5-cycle); the remaining five pairs (the pentagram) are blue. Any three people include two non-adjacent ones in the cycle, so no three are mutual acquaintances, and any three include two adjacent ones, so no three are mutual strangers. Thus the claim fails for five people, and six is the smallest number that forces it: $R(3,3) = 6$. **Answer:** Among any six people there are always three mutual acquaintances or three mutual strangers: pick one person, three of their five relationships share a type by pigeonhole, and those three people either contain a pair of the same type (completing a trio with the chosen person) or form a trio of the opposite type. Five people do not suffice, as a 5-cycle of acquaintances shows. --- Covering Three of 51 Ants With a Small Glass [easy, Brain Teaser] --- https://quantvault.org/problem-3622-covering-three-of-51-ants-with-a-small-glass.html Q: Fifty-one ants are sitting on a square table whose side length is $1$ (all ants are inside or on the boundary of the unit square). You have a circular glass whose base is a disc of radius $1/7$. Show that, no matter how the 51 ants are arranged, you can always place the glass so that it covers at least three ants (an ant on the boundary of the disc counts as covered). A: **How to Think About It:** You cannot control where the ants are, so the argument must be an existence proof: partition the square into pieces small enough that the glass can cover any one of them, and use counting to show some piece has at least three ants. The only tension is choosing the grid so that both conditions hold with the given radius. **Quick Estimate:** We want cells that fit inside a disc of diameter $2/7 \approx 0.2857$. A square of side $s$ fits if its diagonal $s\sqrt{2} \le 2/7$, i.e. $s \le 0.202$. Side $1/5 = 0.2$ works, giving 25 cells, and $51 > 2 \times 25$ forces a triple. **Formal Solution:** *Step 1 -- Partition.* Divide the unit square into a $5 \times 5$ grid of $25$ closed squares of side $1/5$. Every ant lies in at least one of these cells (an ant on a grid line belongs to more than one; assign it to any one of them). *Step 2 -- Pigeonhole.* If every cell contained at most $2$ ants, there would be at most $2 \times 25 = 50$ ants. There are $51$, so some cell contains at least $3$ ants. (Formally, some cell holds at least $\lceil 51 / 25 \rceil = 3$ ants.) *Step 3 -- The glass covers a cell.* A square of side $1/5$ has diagonal $\frac{\sqrt{2}}{5} \approx 0.2828$. Its circumscribed circle has radius half the diagonal, $\frac{\sqrt{2}}{10} \approx 0.1414$. The glass has radius $\frac{1}{7} \approx 0.1429 > 0.1414$, so a disc of radius $1/7$ centred at the centre of the cell contains the entire cell, corners included. Equivalently, $\left(\frac{\sqrt{2}}{10}\right)^2 = \frac{2}{100} = 0.02 < \frac{1}{49} \approx 0.0204 = \left(\frac{1}{7}\right)^2$. *Step 4 -- Conclusion.* Place the glass centred on the cell found in Step 2. It covers the whole cell and therefore at least three ants. Since the arrangement was arbitrary, this is always possible. **Answer:** Yes, always. Cut the unit square into a $5 \times 5$ grid of cells of side $1/5$; with 51 ants some cell holds at least 3. A cell of side $1/5$ has circumradius $\sqrt{2}/10 \approx 0.141 < 1/7$, so the glass centred on that cell covers all of it, and hence at least three ants. --- Classifying Five Bags of Coins With a Single Weighing [medium, Brain Teaser] --- https://quantvault.org/problem-3623-classifying-five-bags-of-coins-with-a-single-weighing.html Q: There are 5 bags, each containing 100 coins. Within any one bag all coins are of the same type: every coin in the bag weighs 9 grams, or every coin weighs 10 grams, or every coin weighs 11 grams. Different bags may be of different types, several bags may share a type, and any combination is possible. You do not know the type of any bag. You have a digital scale that reports the exact total weight of whatever you place on it. **(a)** What is the minimum number of weighings needed to determine the type of every bag? **(b)** Describe explicitly how many coins to take from each bag and how to decode the reading. A: **How to Think About It:** There are $3^5 = 243$ possible assignments of types to bags, and one weighing yields one real number. If we can make every assignment produce a different number, one weighing suffices; since zero weighings obviously cannot, the answer is one. The design problem is to pick coin counts $c_1, \ldots, c_5$ such that the map from deviation vectors $(d_1, \ldots, d_5) \in \{-1, 0, 1\}^5$ to $\sum_i c_i d_i$ is injective. **Quick Estimate:** With counts $1, 3, 9, 27, 81$ the deviation $\sum c_i d_i$ ranges from $-121$ to $+121$, which is exactly $243$ integers, one per configuration. That is the tightest possible fit, so the encoding is perfectly efficient. **Formal Solution:** *Step 1 -- Lower bound.* Zero weighings give no information and there are 243 possibilities, so at least one weighing is required. *Step 2 -- The sample.* Take $1$ coin from bag $1$, $3$ coins from bag $2$, $9$ from bag $3$, $27$ from bag $4$ and $81$ from bag $5$, for $121$ coins in total. Let $d_i \in \{-1, 0, +1\}$ be the per-coin deviation of bag $i$ from $10$ g (so $d_i = -1$ means 9 g coins, $d_i = +1$ means 11 g coins). *Step 3 -- The reading.* The scale shows $$W = 10 \times 121 + \sum_{i=1}^{5} 3^{\,i-1} d_i = 1210 + D, \qquad D = d_1 + 3 d_2 + 9 d_3 + 27 d_4 + 81 d_5 .$$ *Step 4 -- Uniqueness of decoding.* $D$ is a balanced ternary numeral with digits $d_i \in \{-1, 0, 1\}$. Balanced ternary representations are unique: if two different digit vectors gave the same $D$, subtracting would give $\sum 3^{i-1} e_i = 0$ with $e_i \in \{-2, -1, 0, 1, 2\}$ not all zero; taking the smallest $j$ with $e_j \ne 0$, the sum equals $3^{j-1}(e_j + 3 \cdot \text{integer})$, and $e_j + 3k = 0$ is impossible for $e_j \in \{\pm 1, \pm 2\}$. So the $243$ configurations produce $243$ distinct values of $D$, namely every integer from $-121$ to $121$. *Step 5 -- Decoding procedure.* Compute $D = W - 1210$. Then read off the digits from the least significant: $d_1 \equiv D \pmod 3$ interpreted in $\{-1, 0, 1\}$ (residue $2$ means $-1$); replace $D$ by $(D - d_1)/3$ and repeat for $d_2$, and so on up to $d_5$. Bag $i$ is light, genuine, or heavy according to $d_i = -1, 0, +1$. *Step 6 -- Example.* A reading of $W = 1196$ gives $D = -14$. Then $-14 \equiv 1 \pmod 3$, so $d_1 = 1$ and $D \to (-14 - 1)/3 = -5$; $-5 \equiv 1$, so $d_2 = 1$ and $D \to -2$; $-2 \equiv 1$, so $d_3 = 1$ and $D \to -1$; $d_4 = -1$ and $D \to 0$; $d_5 = 0$. Check: $1 + 3 + 9 - 27 + 0 = -14$. So bags 1, 2, 3 hold 11 g coins, bag 4 holds 9 g coins, bag 5 is genuine. **Answer:** (a) One weighing suffices (and is obviously necessary). (b) Take $1, 3, 9, 27, 81$ coins from bags $1$ to $5$ (121 coins), weigh them, and set $D = W - 1210$. Writing $D$ in balanced ternary, $D = d_1 + 3 d_2 + 9 d_3 + 27 d_4 + 81 d_5$ with $d_i \in \{-1, 0, 1\}$, gives the type of each bag: $d_i = -1$ means 9 g, $0$ means 10 g, $+1$ means 11 g. --- The Digit-Sum Test for Divisibility by 9 [easy, Combinatorics] --- https://quantvault.org/problem-3624-the-digit-sum-test-for-divisibility-by-9.html Q: Let $a$ be a positive integer with decimal digits $a_n a_{n-1} \cdots a_1 a_0$, so that $a = \sum_{k=0}^{n} a_k 10^k$ with each $a_k \in \{0, 1, \ldots, 9\}$. **(a)** State a simple test, based only on the digits of $a$, for deciding whether $a$ is divisible by $9$. **(b)** Prove that the test is correct. **(c)** Explain why the same test also works for divisibility by $3$, and why it does not work for divisibility by $7$. A: **How to Think About It:** A divisibility test based on digits must come from the behaviour of powers of $10$ modulo the divisor. If $10^k$ leaves remainder $1$ for every $k$, the number reduces to its digit sum modulo that divisor. **Quick Estimate:** $10 = 9 + 1$, $100 = 99 + 1$, $1000 = 999 + 1$: every power of ten is one more than a multiple of nine. So stripping the "$-1$" from each power of ten leaves only the digits, which is why the digit sum controls divisibility by $9$. **Formal Solution:** **Part (a): The test.** An integer is divisible by $9$ if and only if the sum of its decimal digits is divisible by $9$. (Apply the test repeatedly if the digit sum is still large: $a$ is divisible by $9$ iff its digital root is $9$.) **Part (b): Proof.** *Step 1 -- Powers of ten modulo 9.* Since $10 = 9 + 1$, we have $10 \equiv 1 \pmod 9$. Multiplying this congruence by itself $k$ times gives $10^k \equiv 1^k = 1 \pmod 9$ for every $k \ge 0$. Equivalently, $10^k - 1 = 9 \times \underbrace{11\cdots1}_{k}$ is a multiple of $9$. *Step 2 -- Compare the number with its digit sum.* Let $S = \sum_{k=0}^{n} a_k$ be the digit sum. Then $$a - S = \sum_{k=0}^{n} a_k 10^k - \sum_{k=0}^{n} a_k = \sum_{k=0}^{n} a_k \left(10^k - 1\right).$$ Each term $a_k (10^k - 1)$ is a multiple of $9$ by Step 1, so $a - S$ is a multiple of $9$; that is, $a \equiv S \pmod 9$. *Step 3 -- Conclude.* Since $a$ and $S$ differ by a multiple of $9$, $9 \mid a$ if and only if $9 \mid S$. This proves both directions of the test. (Numerically, the equivalence checks out for every integer up to $200{,}000$.) **Part (c): Other divisors.** For $3$: the same argument works because $10 \equiv 1 \pmod 3$ as well, so $a \equiv S \pmod 3$ and $3 \mid a$ iff $3 \mid S$. In fact $a \equiv S$ modulo any divisor of $9$. For $7$: $10 \equiv 3 \pmod 7$, and the powers of $10$ cycle through $1, 3, 2, 6, 4, 5$ modulo $7$, never staying at $1$. So $a \equiv \sum a_k 3^k \pmod 7$, which is not the digit sum. Concrete counterexample: $7$ itself is divisible by $7$ but its digit sum is $7$, while $16$ has digit sum $7$ and is not divisible by $7$. A working test for $7$ must weight the digits by the residues $1, 3, 2, 6, 4, 5, \ldots$ (or use the "double the last digit and subtract" rule, which is the same idea in disguise). **Answer:** (a) $a$ is divisible by $9$ iff its digit sum is divisible by $9$. (b) Because $10^k \equiv 1 \pmod 9$ for all $k$, $a - \sum a_k = \sum a_k (10^k - 1)$ is a multiple of $9$, so $a \equiv \sum a_k \pmod 9$. (c) The proof only needs $10 \equiv 1$ modulo the divisor, which holds for $3$ and $9$ but not for $7$ (where $10 \equiv 3$). --- Can the Chameleons Ever Be All One Colour? [medium, Brain Teaser] --- https://quantvault.org/problem-3625-can-the-chameleons-ever-be-all-one-colour.html Q: On a remote island live 45 chameleons: 13 are red, 15 are green and 17 are blue. Whenever two chameleons of different colours meet, both of them change to the third colour (for example, a red and a green chameleon that meet both turn blue). When two chameleons of the same colour meet, nothing happens. Meetings happen one pair at a time, in any order. Is it possible for all 45 chameleons to eventually become the same colour? Prove your answer. A: **How to Think About It:** Reachability questions for a state machine with simple transitions call for an invariant: a function of the state that every transition preserves. If the invariant differs between the start and the target, the target is unreachable. Here the transitions are so symmetric that differences of counts modulo $3$ are the natural candidate. **Quick Estimate:** Every meeting changes the counts by $(+2, -1, -1)$ in some order. The difference between any two counts changes by $2 - (-1) = 3$, by $-1 - 2 = -3$, or by $(-1) - (-1) = 0$. So differences modulo $3$ never move, and $13, 15, 17$ have pairwise differences $2, 2, 4$, none of which is a multiple of $3$. **Formal Solution:** *Step 1 -- The transitions.* Let the state be $(r, g, b)$ with $r + g + b = 45$. A meeting of a red and a green chameleon maps $(r, g, b) \to (r - 1, g - 1, b + 2)$; similarly the other two meeting types give $(r - 1, g + 2, b - 1)$ and $(r + 2, g - 1, b - 1)$. *Step 2 -- The invariant.* Consider the differences $r - g$, $g - b$ and $b - r$ modulo $3$. Under the first transition, $r - g$ is unchanged, $g - b$ decreases by $3$, and $b - r$ increases by $3$. The other transitions likewise change each difference by $0$ or $\pm 3$. Hence $$r - g \pmod 3, \quad g - b \pmod 3, \quad b - r \pmod 3$$ are all invariant under every possible meeting. Equivalently, the residues $(r \bmod 3, g \bmod 3, b \bmod 3)$ are shifted by a common amount ($-1$ or $+2 \equiv -1$) at each step, so whether any two of them coincide never changes. *Step 3 -- The initial state.* $(13, 15, 17) \equiv (1, 0, 2) \pmod 3$. The three residues are pairwise distinct, so no pairwise difference is $0$ modulo $3$: $r - g \equiv 1$, $g - b \equiv 1$, $b - r \equiv 1 \pmod 3$. *Step 4 -- The target state.* An all-one-colour state is $(45, 0, 0)$, $(0, 45, 0)$ or $(0, 0, 45)$. In each, two of the counts are equal (both $0$), so at least one pairwise difference is $0 \equiv 0 \pmod 3$. *Step 5 -- Conclusion.* The invariant takes the value "all pairwise differences nonzero modulo $3$" at the start and would have to take the value "some pairwise difference is $0$ modulo $3$" at the target. Since it never changes, no sequence of meetings reaches a monochromatic state. (A breadth-first search over the 360 reachable states from $(13, 15, 17)$ confirms that none of them is monochromatic.) *Step 6 -- When it would be possible.* If two colour counts were congruent modulo $3$ (for example $13, 16, 16$), the island could become monochromatic: repeatedly have the two congruent colours meet until they are both zero, adjusting with intermediate meetings as needed. So the modulo-$3$ condition is exactly the obstruction. **Answer:** No. Every meeting changes each pairwise difference of colour counts by $0$ or $\pm 3$, so the differences modulo $3$ are invariant. Initially the counts $13, 15, 17$ are $1, 0, 2$ modulo $3$, pairwise distinct, whereas a single-colour island would have two counts equal to $0$. The required state is unreachable. --- Splitting 1000 Coins: The Sum of Products Is Always the Same [easy, Brain Teaser] --- https://quantvault.org/problem-3626-splitting-1000-coins-the-sum-of-products-is-always-the-same.html Q: You start with a single pile of $1000$ coins. In each step you choose any pile with at least two coins and split it into two smaller piles of sizes $x$ and $y$ (with $x + y$ equal to the size of the pile you split, $x, y \ge 1$), and you write down the product $xy$. You keep splitting piles until every pile contains exactly one coin, then add up all the products you wrote down. **(a)** Show that the final sum does not depend on the order or the sizes of the splits you chose. **(b)** What is the final sum for $1000$ coins? A: **How to Think About It:** When a process seems to depend on many choices but the problem claims the result is fixed, look for a quantity that each step changes by exactly the amount recorded. Here the natural candidate is the number of pairs of coins that have already been separated: a split of a pile of size $x + y$ into $x$ and $y$ separates precisely $xy$ pairs. **Quick Estimate:** For $n = 4$, splitting $2 + 2$ then $1 + 1$ twice gives $4 + 1 + 1 = 6$; splitting $1 + 3$, then $1 + 2$, then $1 + 1$ gives $3 + 2 + 1 = 6$. Both equal $\binom{4}{2} = 6$. For $n = 1000$ this suggests $\binom{1000}{2} = 499{,}500$. **Formal Solution:** **Part (a): Path independence.** *Method 1 -- Counting pairs.* Consider any two specific coins. At the start they sit in the same pile; at the end they are in different piles. Exactly one split moves them apart: the split of the last pile that contained both, into two parts with one coin in each. When a pile of $x + y$ coins is split into parts of sizes $x$ and $y$, the pairs that are separated are exactly those with one coin in each part, and there are $xy$ of them. Thus each recorded product $xy$ counts the pairs separated at that step, and over the whole process every pair is counted exactly once. The final sum is therefore the total number of pairs, $\binom{n}{2}$, independent of the splitting sequence. *Method 2 -- Strong induction.* Let $f(n)$ denote the total for a pile of $n$ coins under any splitting strategy. Claim: $f(n) = \frac{n(n-1)}{2}$ for all $n \ge 1$. Base case: $f(1) = 0$ (no splits), and $\frac{1 \cdot 0}{2} = 0$. Inductive step: assume $f(k) = \frac{k(k-1)}{2}$ for all $k < n$. The first split of the $n$-coin pile produces piles of sizes $x$ and $y = n - x$ with $1 \le x \le n - 1$, and afterwards the two piles evolve independently, so $$f(n) = xy + f(x) + f(y) = x(n-x) + \frac{x(x-1)}{2} + \frac{(n-x)(n-x-1)}{2}.$$ Expanding, $x(n-x) + \frac{x^2 - x + (n-x)^2 - (n-x)}{2} = \frac{2xn - 2x^2 + x^2 - x + n^2 - 2nx + x^2 - n + x}{2} = \frac{n^2 - n}{2}$. The choice of $x$ cancels out, so $f(n) = \frac{n(n-1)}{2}$ no matter how the first (or any later) split is made. **Part (b): The value.** $$f(1000) = \frac{1000 \times 999}{2} = 499{,}500 .$$ (A simulation of 2000 random splitting sequences of 1000 coins returns 499,500 every time.) **Answer:** (a) Each split of a pile into sizes $x$ and $y$ separates exactly $xy$ pairs of coins, and every pair is separated exactly once, so the total is always $\binom{n}{2} = \frac{n(n-1)}{2}$ (equivalently, $f(n) = xy + f(x) + f(y)$ has the unique solution $n(n-1)/2$ by strong induction). (b) $\frac{1000 \times 999}{2} = 499{,}500$. --- Gas Cans Around a Circular Track: A Lap Is Always Possible [medium, Brain Teaser] --- https://quantvault.org/problem-3627-gas-cans-around-a-circular-track-a-lap-is-always-possible.html Q: A one-way circular race track has $N$ gas cans placed at arbitrary points along it. The cans contain arbitrary (possibly different, possibly zero) amounts of fuel, but the total fuel in all $N$ cans is exactly enough to drive one full lap. Your car starts with an empty tank, has unlimited tank capacity, and consumes fuel at a constant rate per unit distance. You may choose which can to start next to; when you reach a can you pour all of its fuel into the tank. Prove that no matter how the cans are placed and how the fuel is distributed among them, you can always choose a starting can from which you complete the entire lap without running out of fuel. Describe how to find such a can. A: **How to Think About It:** The problem gives a global equality (total fuel equals lap length) and asks for a local guarantee (a good starting point). Induction works by finding a can that can reach its successor, merging the two, and recursing. Alternatively, think of the cumulative fuel balance around the loop, which returns to its starting value after a lap; start right after its minimum. **Approach:** Give the induction proof for existence and the cumulative-minimum construction for finding the can. **Formal Solution:** *Step 1 -- Notation.* Number the cans $1, 2, \ldots, N$ in driving order. Let $g_i \ge 0$ be the fuel in can $i$ (measured in units of distance it can propel the car) and $d_i > 0$ the distance from can $i$ to can $i + 1$ (with can $N + 1$ meaning can $1$). The hypothesis is $\sum_{i=1}^{N} g_i = \sum_{i=1}^{N} d_i = L$, the lap length. *Step 2 -- Base case.* For $N = 1$, the single can holds $g_1 = L$, so starting there you drive the full lap. *Step 3 -- Inductive step.* Assume the claim holds for every configuration of $N$ cans, and consider $N + 1$ cans. There must be some can $i$ with $g_i \ge d_i$: otherwise $g_i m$ (before picking up can $j$), and after wrapping around it is $B_N - B_m + B_{j-1} = B_{j-1} - B_m$ for $j \le m$. In both cases this is $\ge 0$ by minimality of $B_m$, so the tank never runs dry and the lap is completed. (Random configurations with the total-fuel constraint confirm that this rule always succeeds.) **Answer:** Yes, always. Induction: with $N + 1$ cans some can holds enough fuel to reach the next one (else the total fuel would be less than the lap), so merging it with its successor gives an equivalent $N$-can problem, and a valid start for $N$ cans is valid for $N + 1$. Constructively, compute the running balance of fuel gained minus distance driven around the loop and start at the can immediately after the point where that balance is lowest. --- Proving That the Square Root of 2 Is Irrational [easy, Combinatorics] --- https://quantvault.org/problem-3628-proving-that-the-square-root-of-2-is-irrational.html Q: Prove that $\sqrt{2}$ is irrational, that is, that there are no positive integers $m$ and $n$ with $\left(\frac{m}{n}\right)^2 = 2$. A: **How to Think About It:** A statement of the form "no integers satisfy this" is naturally attacked by contradiction: assume a solution exists, choose it in a canonical minimal form (lowest terms), and derive that it was not minimal after all. The equation $m^2 = 2n^2$ makes parity the natural lens. **Approach:** Assume $\sqrt{2} = m/n$ in lowest terms and show both $m$ and $n$ must be even. **Formal Solution:** *Step 1 -- Assume the opposite.* Suppose $\sqrt{2}$ is rational. Then $\sqrt{2} = \frac{m}{n}$ for some positive integers $m, n$. Dividing out any common factors, we may assume $\gcd(m, n) = 1$, so $m$ and $n$ are not both even. *Step 2 -- Square.* Squaring gives $m^2 = 2 n^2$, so $m^2$ is even. *Step 3 -- $m$ is even.* If $m$ were odd, say $m = 2j + 1$, then $m^2 = 4j^2 + 4j + 1$ would be odd. Since $m^2$ is even, $m$ must be even: $m = 2k$ for some integer $k$. *Step 4 -- $n$ is even.* Substituting, $(2k)^2 = 2n^2$, i.e. $4k^2 = 2n^2$, so $n^2 = 2k^2$. By the same argument as in Step 3, $n$ is even. *Step 5 -- Contradiction.* Both $m$ and $n$ are even, so $2$ divides $\gcd(m, n)$, contradicting $\gcd(m, n) = 1$. Hence no such $m, n$ exist, and $\sqrt{2}$ is irrational. *Step 6 -- Remarks.* The same argument with "divisible by $p$" in place of "even" proves that $\sqrt{p}$ is irrational for any prime $p$; a cleaner version compares the exponent of $2$ in the prime factorisations of $m^2$ (even) and $2n^2$ (odd), which can never be equal. **Answer:** $\sqrt{2}$ is irrational. If $\sqrt{2} = m/n$ in lowest terms, then $m^2 = 2n^2$ forces $m$ even, $m = 2k$, whence $n^2 = 2k^2$ forces $n$ even as well, contradicting that $m/n$ was in lowest terms. --- Seven Prisoners, Seven Rainbow Colours: Guarantee One Correct Guess [medium, Brain Teaser] --- https://quantvault.org/problem-3629-seven-prisoners-seven-rainbow-colours-guarantee-one-correct-.html Q: Seven prisoners are given a chance at freedom. Each will be given a hat in one of the seven colours of the rainbow (red, orange, yellow, green, blue, indigo, violet). The colours are chosen by the warden, who may use any assignment he likes: colours can repeat, and he will choose adversarially after learning the prisoners' strategy. Each prisoner can see the hats of the other six but not his own. There is no communication of any kind once the hats are on. At a signal, every prisoner simultaneously writes down a guess for the colour of his own hat. If at least one prisoner guesses correctly, all seven are freed; otherwise none are. The prisoners may agree on a strategy beforehand. **(a)** Is there a strategy that guarantees freedom no matter how the warden assigns the colours? If so, describe it and prove it works. **(b)** Show that no strategy can guarantee two or more correct guesses. A: **How to Think About It:** Against an adversary, no prisoner can improve his own odds above $1/7$: whatever he sees, his hat can be any colour. So the strategy must make the prisoners' errors perfectly anti-correlated, ensuring that in every possible assignment someone is right. The colour sum modulo $7$ partitions all assignments into $7$ classes, and there are exactly $7$ prisoners to cover them. **Approach:** Assign each prisoner one residue class of the colour sum modulo $7$; a counting argument gives the upper bound. **Formal Solution:** **Part (a): A winning strategy.** *Step 1 -- Encode.* Label the colours $0, 1, \ldots, 6$ and the prisoners $0, 1, \ldots, 6$. Let $h_j$ be the colour of prisoner $j$'s hat and let $S = \sum_{j=0}^{6} h_j \bmod 7$. *Step 2 -- The rule.* Prisoner $i$ assumes that $S \equiv i \pmod 7$. He can see every hat but his own, so he computes the sum of the six visible hats and guesses $$g_i \equiv i - \sum_{j \ne i} h_j \pmod 7 .$$ *Step 3 -- Exactly one is correct.* The true total $S$ is some value $s \in \{0, \ldots, 6\}$. For prisoner $s$, $g_s \equiv s - \sum_{j \ne s} h_j \equiv S - \sum_{j \ne s} h_j = h_s \pmod 7$, so prisoner $s$ guesses correctly. For any other prisoner $i \ne s$, $g_i - h_i \equiv i - S \not\equiv 0 \pmod 7$, so prisoner $i$ is wrong. Thus in every one of the $7^7$ possible assignments exactly one prisoner is right, and the group is always freed. (A brute-force check over all $7^7 = 823{,}543$ assignments confirms exactly one correct guess in each.) Example: with hats $2, 6, 1, 4, 4, 0, 5$ the total is $22 \equiv 1 \pmod 7$, so prisoner $1$ is the one who is right: he sees $16$ and writes $1 - 16 \equiv 6$, which is his colour. **Part (b): No strategy guarantees two correct guesses.** *Step 4 -- Counting.* Fix any deterministic strategy: prisoner $i$'s guess is a function $g_i$ of the six hats he sees. For each prisoner $i$ and each fixed choice of the other six hats, exactly one of the $7$ possible values of $h_i$ makes $g_i$ correct. So prisoner $i$ is correct in exactly $7^6$ of the $7^7$ assignments, i.e. a fraction $1/7$. Summing over the seven prisoners, the total number of correct guesses across all assignments is $7 \times 7^6 = 7^7$: on average exactly one correct guess per assignment. *Step 5 -- Conclusion.* If some strategy guaranteed at least $2$ correct guesses in every assignment, the total would be at least $2 \times 7^7$, contradicting Step 4. Hence the modular strategy, which achieves exactly one correct guess in every assignment, is the best possible. (Randomised strategies do no better in the worst case, since the warden chooses adversarially after learning the strategy.) **Answer:** (a) Yes. Number colours and prisoners $0$ to $6$; prisoner $i$ guesses the colour that would make the sum of all seven hats congruent to $i$ modulo $7$, i.e. $g_i \equiv i - \sum_{j \ne i} h_j \pmod 7$. The true sum is congruent to exactly one index, so exactly one prisoner is always correct. (b) Any prisoner is correct in exactly $1/7$ of all assignments, so the average number of correct guesses is $1$ for every strategy; no strategy can guarantee two. --- Differentiating (ln x) to the Power ln x [easy, Optimization] --- https://quantvault.org/problem-3630-differentiating-ln-x-to-the-power-ln-x.html Q: Let $$y = (\ln x)^{\ln x}, \qquad x > 1,$$ so that both the base and the exponent are $\ln x$ (the restriction $x > 1$ keeps the base positive). Find $\dfrac{dy}{dx}$ in closed form. A: **How to Think About It:** A function of the form $u(x)^{v(x)}$ is neither a pure power nor a pure exponential, so take logarithms to separate base and exponent, differentiate the resulting product, and then undo the log by multiplying by $y$. **Quick Estimate:** At $x = e$ we have $\ln x = 1$, so $y = 1^1 = 1$. The formula below gives $y'(e) = \frac{1}{e}\,(\ln 1 + 1) = 1/e \approx 0.368$. A finite difference check, $\frac{y(e + 0.001) - y(e - 0.001)}{0.002} \approx 0.368$, agrees. **Formal Solution:** *Step 1 -- Take logs.* For $x > 1$, $\ln x > 0$ and $y > 0$, so $$\ln y = \ln x \cdot \ln(\ln x).$$ *Step 2 -- Differentiate both sides.* The left side gives $\dfrac{y'}{y}$. On the right, use the product rule with $u = \ln x$ and $v = \ln(\ln x)$, where $u' = 1/x$ and, by the chain rule, $v' = \dfrac{1}{\ln x}\cdot\dfrac{1}{x}$: $$\frac{y'}{y} = \frac{1}{x}\ln(\ln x) + \ln x \cdot \frac{1}{x \ln x} = \frac{\ln(\ln x) + 1}{x}.$$ *Step 3 -- Multiply back by $y$.* $$\frac{dy}{dx} = (\ln x)^{\ln x}\,\frac{\ln(\ln x) + 1}{x}.$$ *Step 4 -- Sanity check.* The derivative vanishes when $\ln(\ln x) = -1$, that is at $x = e^{1/e} \approx 1.44$, which is where $(\ln x)^{\ln x}$ attains its minimum value $(1/e)^{1/e} \approx 0.69$. That matches the shape of $t^t$ on $t > 0$, which bottoms out at $t = 1/e$. **Answer:** $\dfrac{dy}{dx} = \dfrac{(\ln x)^{\ln x}}{x}\left(\ln(\ln x) + 1\right)$. --- Two Limits by L'Hospital: e^x / x^2 and x^2 ln x [easy, Optimization] --- https://quantvault.org/problem-3632-two-limits-by-l-hospital-e-x-x-2-and-x-2-ln-x.html Q: Evaluate the following limits, justifying each step. **(a)** $\displaystyle \lim_{x \to \infty} \frac{e^{x}}{x^{2}}$ **(b)** $\displaystyle \lim_{x \to 0^{+}} x^{2} \ln x$ A: **How to Think About It:** Both limits are indeterminate forms. L'Hospital's rule handles $0/0$ and $\infty/\infty$ quotients, so the only work is to put each expression in that shape and differentiate until the indeterminacy disappears. **Quick Estimate:** At $x = 20$, $e^{20} \approx 4.9 \times 10^{8}$ while $x^2 = 400$, so the ratio is already about $10^{6}$ and growing. At $x = 0.01$, $x^2 \ln x = 10^{-4} \times (-4.6) \approx -0.00046$, clearly heading to $0$. **Formal Solution:** **Part (a): $\lim_{x \to \infty} e^{x}/x^{2}$** *Step 1 -- Check the form.* As $x \to \infty$ both $e^x$ and $x^2$ tend to $\infty$, so the quotient is of type $\infty/\infty$ and L'Hospital applies. *Step 2 -- Apply L'Hospital twice.* $$\lim_{x \to \infty} \frac{e^{x}}{x^{2}} = \lim_{x \to \infty} \frac{e^{x}}{2x} = \lim_{x \to \infty} \frac{e^{x}}{2} = \infty.$$ After the first application the form is still $\infty/\infty$, so a second application is legitimate; the second one leaves a quotient with a constant denominator. **Part (b): $\lim_{x \to 0^{+}} x^{2} \ln x$** *Step 1 -- Rewrite as a quotient.* As $x \to 0^{+}$, $x^2 \to 0$ and $\ln x \to -\infty$: a $0 \cdot \infty$ form. Write $$x^{2} \ln x = \frac{\ln x}{x^{-2}},$$ which is of type $-\infty/\infty$. *Step 2 -- Apply L'Hospital once.* $$\lim_{x \to 0^{+}} \frac{\ln x}{x^{-2}} = \lim_{x \to 0^{+}} \frac{1/x}{-2x^{-3}} = \lim_{x \to 0^{+}} \left(-\frac{x^{2}}{2}\right) = 0.$$ *Step 3 -- Note the general pattern.* The same computation gives $\lim_{x \to 0^{+}} x^{a} \ln x = 0$ for every $a > 0$, and $\lim_{x \to \infty} e^{x}/x^{k} = \infty$ for every fixed $k$. **Answer:** (a) $+\infty$ (the exponential dominates any polynomial). (b) $0$ (the power $x^2$ dominates the logarithm near $0$). --- The Antiderivative of ln x [easy, Optimization] --- https://quantvault.org/problem-3633-the-antiderivative-of-ln-x.html Q: Find the indefinite integral $$\int \ln x \, dx, \qquad x > 0,$$ and verify your result by differentiation. Then use it to evaluate $\int_{1}^{e} \ln x\,dx$ and the improper integral $\int_{0}^{1} \ln x\,dx$. A: **How to Think About It:** $\ln x$ has an ugly integral but a very simple derivative, $1/x$. Integration by parts lets you differentiate $\ln x$ and integrate the trivial factor $1$ instead. **Quick Estimate:** $\int_{1}^{e} \ln x\,dx$ is the area under a curve that rises from $0$ to $1$ over an interval of length $e - 1 \approx 1.72$, concave, so somewhere between $0.86$ (triangle) and $1.72$ (rectangle). The formula below gives exactly $1$, which is plausible. **Formal Solution:** *Step 1 -- Set up integration by parts.* Take $$u = \ln x, \quad dv = dx \qquad \Longrightarrow \qquad du = \frac{dx}{x}, \quad v = x.$$ *Step 2 -- Apply $\int u\,dv = uv - \int v\,du$.* $$\int \ln x\,dx = x \ln x - \int x \cdot \frac{1}{x}\,dx = x \ln x - \int 1\,dx = x \ln x - x + C.$$ *Step 3 -- Verify by differentiating.* $$\frac{d}{dx}\left(x \ln x - x\right) = \ln x + x \cdot \frac{1}{x} - 1 = \ln x. \checkmark$$ *Step 4 -- A useful definite version.* $\int_{1}^{e} \ln x\,dx = [x\ln x - x]_{1}^{e} = (e - e) - (0 - 1) = 1$, and $\int_{0}^{1} \ln x\,dx = -1$ (improper, but convergent since $x \ln x \to 0$). **Answer:** $\displaystyle \int \ln x\,dx = x \ln x - x + C$, so $\int_{1}^{e} \ln x\,dx = 1$ and $\int_{0}^{1} \ln x\,dx = -1$. --- Integrating sec x from 0 to pi/6 [easy, Optimization] --- https://quantvault.org/problem-3634-integrating-sec-x-from-0-to-pi-6.html Q: Evaluate $$\int_{0}^{\pi/6} \sec x \, dx.$$ Give the exact value in closed form and its decimal value to three places. Derive the antiderivative of $\sec x$ yourself rather than quoting it from a table. A: **How to Think About It:** Recognize $\sec x$ as a logarithmic derivative in disguise: $\sec x = \dfrac{(\sec x + \tan x)'}{\sec x + \tan x}$. Then the definite integral is just a difference of logs. **Quick Estimate:** On $[0, \pi/6]$ the integrand runs from $\sec 0 = 1$ to $\sec(\pi/6) = 2/\sqrt{3} \approx 1.155$. The interval has length $\pi/6 \approx 0.524$, so the integral is between $0.524$ and $0.605$, roughly $0.55$. The exact answer $\tfrac{1}{2}\ln 3 \approx 0.549$ fits. **Formal Solution:** *Step 1 -- Find the antiderivative.* Differentiate $\sec x + \tan x$: $$\frac{d}{dx}(\sec x + \tan x) = \sec x \tan x + \sec^{2} x = \sec x\,(\tan x + \sec x).$$ Therefore $$\sec x = \frac{\frac{d}{dx}(\sec x + \tan x)}{\sec x + \tan x} \quad \Longrightarrow \quad \int \sec x\,dx = \ln|\sec x + \tan x| + C.$$ *Step 2 -- Evaluate the endpoints.* At $x = \pi/6$: $\sec(\pi/6) = \dfrac{2}{\sqrt{3}}$ and $\tan(\pi/6) = \dfrac{1}{\sqrt{3}}$, so $\sec x + \tan x = \dfrac{3}{\sqrt{3}} = \sqrt{3}$. At $x = 0$: $\sec 0 + \tan 0 = 1$. *Step 3 -- Subtract.* $$\int_{0}^{\pi/6} \sec x\,dx = \ln\sqrt{3} - \ln 1 = \ln \sqrt{3} = \tfrac{1}{2}\ln 3 \approx 0.5493.$$ **Answer:** $\displaystyle \int_{0}^{\pi/6} \sec x\,dx = \ln\sqrt{3} = \tfrac{1}{2}\ln 3 \approx 0.549$. --- Volume Common to Two Perpendicular Cylinders [medium, Brain Teaser] --- https://quantvault.org/problem-3635-volume-common-to-two-perpendicular-cylinders.html Q: Two solid circular cylinders, each of radius $1$, intersect at right angles: their axes cross (they meet at a single point) and are perpendicular to each other. What is the volume of the region that lies inside both cylinders? For full credit, also give the volume when both cylinders have radius $r$. A: **How to Think About It:** Volume problems become one-dimensional integrals once you find a slicing direction in which every cross-section has a simple shape. Slicing the intersection (a Steinmetz solid) perpendicular to the plane of the two axes turns each slice into a square. **Quick Estimate:** The solid contains the unit sphere (volume $4\pi/3 \approx 4.19$) and is contained in the cube of side $2$ (volume $8$). Each horizontal slice is a square rather than the sphere's inscribed circle, and a square is $4/\pi \approx 1.27$ times the area of its inscribed circle, so expect about $1.27 \times 4.19 \approx 5.33$. **Formal Solution:** *Step 1 -- Coordinates.* Let the first cylinder be $\{x^2 + z^2 \le r^2\}$ (axis along the $y$-axis) and the second be $\{y^2 + z^2 \le r^2\}$ (axis along the $x$-axis). The axes are perpendicular and cross at the origin. *Step 2 -- Cross-sections at height $z$.* Fix $z$ with $|z| \le r$. A point $(x, y, z)$ lies in both cylinders exactly when $$|x| \le \sqrt{r^2 - z^2} \quad \text{and} \quad |y| \le \sqrt{r^2 - z^2}.$$ These are independent constraints on $x$ and $y$, so the slice is a square of side $2\sqrt{r^2 - z^2}$ and area $4(r^2 - z^2)$. *Step 3 -- Integrate.* $$V = \int_{-r}^{r} 4\,(r^2 - z^2)\,dz = 4\left[r^2 z - \frac{z^3}{3}\right]_{-r}^{r} = 4\left(2r^3 - \frac{2r^3}{3}\right) = \frac{16 r^3}{3}.$$ *Step 4 -- Cavalieri check.* The sphere $x^2 + y^2 + z^2 \le r^2$ has slice area $\pi(r^2 - z^2)$ at the same height, so $V_{\text{solid}} / V_{\text{sphere}} = 4/\pi$ slice by slice, and $V = \frac{4}{\pi}\cdot\frac{4}{3}\pi r^3 = \frac{16r^3}{3}$, confirming Step 3 without any integration. **Answer:** With $r = 1$ the common volume is $\dfrac{16}{3} \approx 5.33$; in general it is $\dfrac{16 r^{3}}{3}$. --- The Snowplow Problem: When Did It Start Snowing? [medium, Brain Teaser] --- https://quantvault.org/problem-3636-the-snowplow-problem-when-did-it-start-snowing.html Q: One morning it starts snowing at a heavy and constant rate. At noon a snowplow sets out to clear a road. The plow removes snow at a constant rate: the volume of snow it clears per unit time is the same at all times, so the deeper the snow, the slower the plow moves. By 1:00 pm the plow has traveled $2$ miles; by 2:00 pm it has traveled a total of $3$ miles. At what time did it start snowing? A: **How to Think About It:** Translate the two physical statements into an ODE. Constant snowfall makes depth linear in elapsed time; constant clearing rate makes speed inversely proportional to depth. Integrating the speed gives distance as a logarithm, and the two observed distances leave one unknown, the start time. **Quick Estimate:** If the snow started $T$ hours before noon, the distance after $t$ hours is proportional to $\ln\frac{t+T}{T}$, so the ratio $D(2)/D(1)$ depends on $T$ alone. It runs from $1$ (snow just started: almost all the distance comes in the first hour) to $2$ (snow started long ago: both hours look alike), and we need $3/2$. Try round values: $T = 1$ gives $\ln 3/\ln 2 \approx 1.58$, too high, and $T = 0.5$ gives $\ln 5/\ln 3 \approx 1.47$, slightly low. Interpolating gives $T \approx 0.65$ hours, so expect a start roughly $35$ to $40$ minutes before noon. **Formal Solution:** *Step 1 -- Model.* Let snow begin $T$ hours before noon and let $t \ge 0$ be hours after noon. Snow depth is $h(t) = c\,(t + T)$ for a constant $c$. The plow clears a constant volume per hour, so $v(t)\,h(t) = \text{const}$, giving $$v(t) = \frac{k}{t + T}$$ for some constant $k > 0$. *Step 2 -- Distance traveled.* $$D(t) = \int_0^{t} \frac{k\,ds}{s + T} = k \ln\frac{t + T}{T}.$$ *Step 3 -- Impose the observations.* $$k \ln\frac{1 + T}{T} = 2, \qquad k \ln\frac{2 + T}{T} = 3.$$ Dividing the second by the first eliminates $k$: $$\frac{\ln\frac{2+T}{T}}{\ln\frac{1+T}{T}} = \frac{3}{2} \quad \Longrightarrow \quad \left(\frac{2 + T}{T}\right)^{2} = \left(\frac{1 + T}{T}\right)^{3}.$$ *Step 4 -- Solve.* Multiply through by $T^3$: $$T(2 + T)^2 = (1 + T)^3 \;\Longrightarrow\; T^3 + 4T^2 + 4T = T^3 + 3T^2 + 3T + 1 \;\Longrightarrow\; T^2 + T - 1 = 0.$$ The positive root is $$T = \frac{\sqrt{5} - 1}{2} \approx 0.618 \text{ hours} \approx 37.1 \text{ minutes}.$$ *Step 5 -- Interpret.* The snow started about $37$ minutes before noon, i.e. at roughly $11{:}23$ am. (For completeness, $k = 2/\ln\frac{1+T}{T} = 2/\ln(1 + \phi) \approx 2.08$ miles, where $\phi = (1+\sqrt5)/2$ and $\frac{1+T}{T} = \phi^{2}$.) **Answer:** Snow started $T = \dfrac{\sqrt{5} - 1}{2}$ hours $\approx 37$ minutes before noon, at about $11{:}23$ am. --- Conditional Mean of a Standard Normal Given It Is Positive [easy, Expectation] --- https://quantvault.org/problem-3637-conditional-mean-of-a-standard-normal-given-it-is-positive.html Q: Let $X \sim N(0, 1)$ be a standard normal random variable. Compute $$E[X \mid X > 0]$$ by direct integration. Give the exact value and a decimal approximation. A: **How to Think About It:** A conditional expectation given an event is the partial expectation over that event divided by the event's probability. The partial expectation is a Gaussian integral that the substitution $u = x^2/2$ kills instantly; the only trap is forgetting to divide by $P(X > 0) = 1/2$. **Quick Estimate:** Given $X > 0$, the density is $2\phi(x)$ on $(0, \infty)$: decreasing and right-skewed, so its mean sits above its median $0.674$ (the standard normal's 75th percentile) but well below $1$. Something around $0.8$ is a good guess. **Formal Solution:** *Step 1 -- Write the definition.* $$E[X \mid X > 0] = \frac{E[X \mathbf{1}_{\{X>0\}}]}{P(X > 0)} = \frac{\int_0^{\infty} x\,\phi(x)\,dx}{1/2}, \qquad \phi(x) = \frac{1}{\sqrt{2\pi}} e^{-x^2/2}.$$ *Step 2 -- Evaluate the numerator.* Substitute $u = x^2/2$, $du = x\,dx$: $$\int_0^{\infty} \frac{x}{\sqrt{2\pi}} e^{-x^2/2}\,dx = \frac{1}{\sqrt{2\pi}} \int_0^{\infty} e^{-u}\,du = \frac{1}{\sqrt{2\pi}}.$$ Equivalently, note $\phi'(x) = -x\phi(x)$, so $\int_0^\infty x\phi(x)\,dx = \phi(0) - \phi(\infty) = \phi(0) = 1/\sqrt{2\pi}$. *Step 3 -- Divide by the probability.* $$E[X \mid X > 0] = \frac{1/\sqrt{2\pi}}{1/2} = \frac{2}{\sqrt{2\pi}} = \sqrt{\frac{2}{\pi}} \approx 0.7979.$$ *Step 4 -- Cross-checks.* By symmetry $E[X \mid X 0] = \sqrt{2/\pi} \approx 0.798$. --- What Is i to the Power i? [medium, Brain Teaser] --- https://quantvault.org/problem-3639-what-is-i-to-the-power-i.html Q: Let $i = \sqrt{-1}$. Evaluate $i^{\,i}$. Show that the (principal) value is a real number, give it in closed form, and comment on whether the value is unique. A: **How to Think About It:** Complex powers are defined through the exponential and logarithm: $a^{b} = e^{b \ln a}$. So the whole problem is to compute $\ln i$, which Euler's formula gives as $i\pi/2$ (plus multiples of $2\pi i$). **Quick Estimate:** $e^{-\pi/2} = e^{-1.571}$. Since $e^{-1.5} \approx 0.223$ and $e^{-0.07} \approx 0.93$, the value is about $0.21$. **Formal Solution:** *Step 1 -- Euler's formula from Taylor series.* Using $e^{x} = \sum x^n/n!$ with $x = i\theta$ and separating even and odd powers of $i$, $$e^{i\theta} = \sum_{n \text{ even}} \frac{(-1)^{n/2}\theta^{n}}{n!} + i\sum_{n \text{ odd}} \frac{(-1)^{(n-1)/2}\theta^{n}}{n!} = \cos\theta + i\sin\theta.$$ *Step 2 -- Polar form of $i$.* Taking $\theta = \pi/2$ gives $e^{i\pi/2} = \cos\frac{\pi}{2} + i\sin\frac{\pi}{2} = i$. Hence the principal logarithm is $\ln i = i\pi/2$. *Step 3 -- Exponentiate.* $$i^{\,i} = e^{\,i \ln i} = e^{\,i \cdot i\pi/2} = e^{-\pi/2} \approx 0.2079.$$ *Step 4 -- Non-uniqueness.* Since $e^{i\theta}$ has period $2\pi$, also $i = e^{i(\pi/2 + 2\pi k)}$ for any integer $k$, so $i^{i}$ can equal $e^{-\pi/2 - 2\pi k}$ for any $k \in \mathbb{Z}$: all real, all positive. The principal branch ($k = 0$, argument in $(-\pi, \pi]$) gives $e^{-\pi/2}$. **Answer:** $i^{\,i} = e^{-\pi/2} \approx 0.208$ (principal value); in general $e^{-\pi/2 - 2\pi k}$, $k \in \mathbb{Z}$, all of which are real. --- Bernoulli's Inequality via Taylor's Theorem [medium, Optimization] --- https://quantvault.org/problem-3640-bernoulli-s-inequality-via-taylor-s-theorem.html Q: Prove that for every real $x > -1$ and every integer $n \ge 2$, $$(1 + x)^{n} \;\ge\; 1 + nx.$$ Identify exactly when equality holds. Give a proof based on Taylor's theorem with remainder; a second proof by induction is welcome but not required. A: **How to Think About It:** The right-hand side $1 + nx$ is the tangent line to $f(x) = (1 + x)^n$ at $x = 0$. Taylor's theorem writes $f$ as that tangent line plus a second-order remainder, so the inequality holds as soon as the remainder is nonnegative, which is a sign check on $f''$. **Approach:** Expand $f(x) = (1+x)^n$ to first order about $0$ with the Lagrange form of the remainder, then show the remainder term is $\ge 0$ whenever $x > -1$. Confirm with induction. **Formal Solution:** *Step 1 -- Taylor expansion with Lagrange remainder.* Let $f(x) = (1 + x)^n$, which is infinitely differentiable on $(-1, \infty)$. For any $x > -1$ there exists $\xi$ strictly between $0$ and $x$ (so $\xi > -1$) such that $$f(x) = f(0) + f'(0)\,x + \frac{f''(\xi)}{2}\,x^{2}.$$ *Step 2 -- Compute the terms.* $$f(0) = 1, \qquad f'(x) = n(1+x)^{n-1} \Rightarrow f'(0) = n, \qquad f''(\xi) = n(n-1)(1+\xi)^{n-2}.$$ *Step 3 -- Sign of the remainder.* Since $n \ge 2$, $n(n-1) > 0$; since $\xi > -1$, $(1 + \xi)^{n-2} > 0$ (for $n = 2$ it equals $1$). Therefore $$\frac{f''(\xi)}{2}x^{2} = \frac{n(n-1)}{2}(1+\xi)^{n-2}x^{2} \ge 0,$$ and consequently $(1 + x)^n = 1 + nx + (\text{nonnegative}) \ge 1 + nx$. *Step 4 -- Equality.* The remainder is zero exactly when $x^2 = 0$, so for $n \ge 2$ equality holds only at $x = 0$. For $x > -1$, $x \ne 0$, the inequality is strict. *Step 5 -- Induction check (alternative proof).* Base case $n = 2$: $(1+x)^2 = 1 + 2x + x^2 \ge 1 + 2x$. Inductive step: if $(1+x)^k \ge 1 + kx$, multiply by $1 + x > 0$: $$(1+x)^{k+1} \ge (1 + kx)(1 + x) = 1 + (k+1)x + kx^{2} \ge 1 + (k+1)x.$$ The hypothesis $x > -1$ is used precisely to keep $1 + x$ positive so the inequality direction is preserved. **Answer:** For all $x > -1$ and integers $n \ge 2$, $(1+x)^n \ge 1 + nx$, because the Taylor remainder $\frac{n(n-1)}{2}(1+\xi)^{n-2}x^2$ is nonnegative; equality holds only at $x = 0$. --- Bisection vs Secant: Counting Steps to a Root [medium, Optimization] --- https://quantvault.org/problem-3642-root-finding-beyond-newton-bisection-and-the-secant-method.html Q: Take $f(x) = x^3 - 2x - 5$, which has a single real root $r \approx 2.0945514815$, and the bracket $[2, 3]$, where $f(2) = -1$ and $f(3) = 16$. You want the root without using $f'$. **(a)** Bisection halves the bracket each step. How many bisection steps are needed before the bracket width is at most $10^{-6}$? **(b)** Run the secant method from $x_0 = 2$, $x_1 = 3$. What is the first new iterate $x_2$? **(c)** Keep iterating the secant method. Counting each new iterate $x_2, x_3, \dots$ as one step, after how many steps is $|x_k - r| < 10^{-6}$ for the first time? **(d)** False position (regula falsi) also takes the zero of the chord through the two bracket endpoints, but then replaces whichever endpoint keeps the sign change. What happens when you run it on $[2, 3]$, and how fast does it converge? A: **How to Think About It:** All three methods only evaluate $f$, so compare them by how fast the error shrinks per evaluation. Bisection is pure bookkeeping: the width halves each step. The secant method replaces $f'$ in Newton's step by the slope of the chord through the last two iterates, which gives superlinear convergence once you are close. False position uses the same chord but keeps a bracket, and the bracket rule can freeze one endpoint. **Quick Estimate:** $2^{10} \approx 10^3$, so $2^{20} \approx 10^6$: about $20$ bisection steps. The secant error starts near $10^{-1}$ and its exponent grows by a factor of about $1.6$ per step ($10^{-1.5}, 10^{-2}, 10^{-3.5}, 10^{-5.5}, 10^{-9.5}$ is the right shape), so about $5$ steps. **Formal Solution:** *Step 1 -- Bisection count (a).* After $n$ steps the bracket width is $(3 - 2)/2^n = 2^{-n}$. We need $2^{-n} \le 10^{-6}$, i.e. $n \ge \log_2 10^6 \approx 19.93$. Since $2^{19} = 524{,}288 0$ and $f''(x) = 6x > 0$: $f$ is increasing and convex, so every chord between a point left of $r$ and the point $(3, 16)$ lies above the graph and crosses the axis to the left of $r$. Each new point therefore has $f < 0$ and replaces the left endpoint; the right endpoint $3$ is never replaced. The first two points coincide with the secant iterates ($2.0588$, $2.0813$), but after that the chord always pivots on $(3, 16)$ instead of on the latest point: the next errors are $4.9 \times 10^{-3}$ and $1.8 \times 10^{-3}$, against $2.7 \times 10^{-4}$ and $2.1 \times 10^{-6}$ for the secant method. With $b = 3$ fixed, the error ratio tends to $$\frac{e_{k+1}}{e_k} \to 1 - \frac{f'(r)\,(3 - r)}{f(3)} = 1 - \frac{11.161 \times 0.9054}{16} \approx 0.368.$$ So convergence is linear (order $1$), and the bracket width tends to $3 - r \approx 0.905$ instead of $0$. Here the rate happens to beat bisection's $0.5$, but it is nowhere near the secant method, and with a less favourable endpoint it can be far slower than bisection. The Illinois variant (halve the stored $f$ value of an endpoint that stays put twice) or Brent's method fixes this. **Answer:** (a) $20$ steps. (b) $x_2 = 35/17 \approx 2.0588$. (c) $5$ steps ($|x_6 - r| \approx 3 \times 10^{-10}$, while $|x_5 - r| \approx 2 \times 10^{-6}$). (d) Because $f$ is convex and increasing on the bracket, the endpoint $3$ is never replaced; the bracket width stalls near $0.905$ and the error shrinks linearly by a factor of about $0.37$ per step. --- Distance From the Origin to the Plane 2x + 3y + 4z = 12 [easy, Optimization] --- https://quantvault.org/problem-3643-distance-from-the-origin-to-the-plane-2x-3y-4z-12.html Q: Find the shortest distance from the origin to the plane $$2x + 3y + 4z = 12,$$ and the point on the plane where that distance is attained. Solve it as a constrained optimization problem using Lagrange multipliers, then generalize: what is the distance from the origin to the plane $ax + by + cz = d$? A: **How to Think About It:** The distance from a point to a plane is a constrained minimization: minimize $\|(x,y,z)\|$ subject to the plane equation. Lagrange multipliers turn it into a linear system, and the geometry (closest point along the normal) falls out of the stationarity conditions. **Quick Estimate:** The plane crosses the axes at $(6, 0, 0)$, $(0, 4, 0)$ and $(0, 0, 3)$, so the distance to the origin is less than $3$. The normal $(2, 3, 4)$ has length $\sqrt{29} \approx 5.39$, and $12/5.39 \approx 2.23$. **Formal Solution:** *Step 1 -- Set up the Lagrangian.* Minimize $f(x, y, z) = x^2 + y^2 + z^2$ subject to $g(x, y, z) = 2x + 3y + 4z - 12 = 0$: $$L(x, y, z, \lambda) = x^2 + y^2 + z^2 - \lambda\,(2x + 3y + 4z - 12).$$ *Step 2 -- Stationarity conditions.* $$\frac{\partial L}{\partial x} = 2x - 2\lambda = 0, \quad \frac{\partial L}{\partial y} = 2y - 3\lambda = 0, \quad \frac{\partial L}{\partial z} = 2z - 4\lambda = 0,$$ so $x = \lambda$, $y = \tfrac{3}{2}\lambda$, $z = 2\lambda$: the point is $\tfrac{\lambda}{2}(2, 3, 4)$, a multiple of the plane's normal vector. *Step 3 -- Impose the constraint.* $$2\lambda + \tfrac{9}{2}\lambda + 8\lambda = \tfrac{29}{2}\lambda = 12 \;\Longrightarrow\; \lambda = \frac{24}{29}.$$ The minimizer is $$(x^*, y^*, z^*) = \left(\frac{24}{29}, \frac{36}{29}, \frac{48}{29}\right) = \frac{12}{29}(2, 3, 4).$$ *Step 4 -- Distance.* $$\sqrt{x^{*2} + y^{*2} + z^{*2}} = \frac{12}{29}\sqrt{4 + 9 + 16} = \frac{12\sqrt{29}}{29} = \frac{12}{\sqrt{29}} \approx 2.228.$$ Because $f$ is convex and the constraint is affine, this stationary point is the global minimum. *Step 5 -- General plane $ax + by + cz = d$.* The same computation gives the minimizer $\frac{d}{a^2 + b^2 + c^2}(a, b, c)$ and $$\text{dist} = \frac{|d|}{\sqrt{a^2 + b^2 + c^2}},$$ which is the projection of any point of the plane onto the unit normal $(a, b, c)/\sqrt{a^2 + b^2 + c^2}$. **Answer:** The distance is $\dfrac{12}{\sqrt{29}} \approx 2.228$, attained at $\left(\frac{24}{29}, \frac{36}{29}, \frac{48}{29}\right)$. In general, the distance from the origin to $ax + by + cz = d$ is $\dfrac{|d|}{\sqrt{a^2 + b^2 + c^2}}$. --- Separable ODE: y' + 6xy = 0 with y(0) = 1 [easy, Optimization] --- https://quantvault.org/problem-3644-separable-ode-y-6xy-0-with-y-0-1.html Q: Solve the initial value problem $$\frac{dy}{dx} + 6xy = 0, \qquad y(0) = 1.$$ Give $y(x)$ explicitly for all real $x$, and check your answer by substituting it back into the equation. A: **How to Think About It:** The equation is linear and homogeneous with a non-constant coefficient, but more simply it is separable: $y'/y$ is a pure function of $x$. Integrate, exponentiate, and use the initial condition. **Quick Estimate:** At $x = 0$ the slope is $y'(0) = -6 \cdot 0 \cdot 1 = 0$, so the solution starts flat at height $1$ and then decays increasingly fast as $x$ grows in either direction, since $y' = -6xy$ has the opposite sign of $x$. A bell shape centered at $0$ is expected. **Formal Solution:** *Step 1 -- Separate variables.* Rewrite as $\dfrac{dy}{dx} = -6xy$. For $y \ne 0$, $$\frac{dy}{y} = -6x\,dx.$$ *Step 2 -- Integrate.* $$\ln|y| = -3x^{2} + C \quad \Longrightarrow \quad y = A\,e^{-3x^{2}}, \qquad A = \pm e^{C}.$$ (The constant solution $y \equiv 0$ corresponds to $A = 0$, so $y = Ae^{-3x^2}$ is the complete family.) *Step 3 -- Apply the initial condition.* $y(0) = A e^{0} = A = 1$. *Step 4 -- Verify.* With $y = e^{-3x^2}$, $y' = -6x e^{-3x^2} = -6xy$, so $y' + 6xy = 0$ and $y(0) = 1$. $\checkmark$ **Answer:** $y(x) = e^{-3x^{2}}$. --- Solving y' = (x - y)/(x + y) by Substitution [medium, Optimization] --- https://quantvault.org/problem-3645-solving-y-x-y-x-y-by-substitution.html Q: Find the general solution of $$\frac{dy}{dx} = \frac{x - y}{x + y}, \qquad x + y \neq 0.$$ Express the answer as an implicit relation between $x$ and $y$. A: **How to Think About It:** The right-hand side depends on $x$ and $y$ only through $x + y$ and $x - y$, which suggests using $x + y$ as a new variable. After the substitution the equation separates. A second route notices that the equation is exact. **Quick Estimate:** Check a candidate answer along one curve. On the line $y = 0$ the slope should be $x/x = 1$. Differentiating the implicit solution $y^2 + 2xy - x^2 = c$ gives $2yy' + 2y + 2xy' - 2x = 0$, i.e. $y' = (x - y)/(x + y)$, which at $y = 0$ equals $1$. $\checkmark$ **Formal Solution:** *Step 1 -- Substitute $z = x + y$.* Then $y = z - x$ and $\dfrac{dz}{dx} = 1 + \dfrac{dy}{dx}$. The equation becomes $$\frac{dz}{dx} = 1 + \frac{x - (z - x)}{z} = \frac{z + 2x - z}{z} = \frac{2x}{z}.$$ *Step 2 -- Separate and integrate.* $$z\,dz = 2x\,dx \quad \Longrightarrow \quad \frac{z^{2}}{2} = x^{2} + C_1 \quad \Longrightarrow \quad z^{2} = 2x^{2} + C_2.$$ *Step 3 -- Substitute back.* With $z = x + y$: $$(x + y)^{2} = 2x^{2} + C_2 \;\Longrightarrow\; x^{2} + 2xy + y^{2} - 2x^{2} = C_2 \;\Longrightarrow\; y^{2} + 2xy - x^{2} = c.$$ *Step 4 -- Alternative: exactness.* Write the equation as $(x - y)\,dx - (x + y)\,dy = 0$. With $M = x - y$ and $N = -(x + y)$, $\partial M/\partial y = -1 = \partial N/\partial x$, so the form is exact. A potential $F$ with $F_x = M$, $F_y = N$ is $F = \tfrac12 x^{2} - xy - \tfrac12 y^{2}$, and the solutions are the level sets $F = \text{const}$, i.e. $x^2 - 2xy - y^2 = \text{const}$, the same family. *Step 5 -- Verify.* Implicit differentiation of $y^2 + 2xy - x^2 = c$: $2yy' + 2y + 2xy' - 2x = 0 \Rightarrow y'(x + y) = x - y$. $\checkmark$ (Explicitly, $y = -x \pm \sqrt{2x^2 + c}$; the family is a set of hyperbolas.) **Answer:** $y^{2} + 2xy - x^{2} = c$ (equivalently $(x+y)^2 = 2x^2 + C$), a family of hyperbolas. --- First-Order Linear ODE: y' + y/x = 1/x^2 with y(1) = 1 [easy, Optimization] --- https://quantvault.org/problem-3646-first-order-linear-ode-y-y-x-1-x-2-with-y-1-1.html Q: Solve the initial value problem $$\frac{dy}{dx} + \frac{y}{x} = \frac{1}{x^{2}}, \qquad x > 0, \qquad y(1) = 1.$$ Give $y(x)$ explicitly and check your answer by substituting it back into the equation. A: **How to Think About It:** A first-order linear ODE $y' + P(x)y = Q(x)$ is solved by multiplying through by the integrating factor $I(x) = \exp\left(\int P\,dx\right)$, which turns the left side into $(I y)'$. Here $P = 1/x$ gives the especially clean factor $I = x$. **Quick Estimate:** At $x = 1$: $y' = 1/x^2 - y/x = 1 - 1 = 0$, so the solution starts flat at $1$. For large $x$ both forcing $1/x^2$ and the solution should decay, roughly like $(\ln x)/x$, since $(xy)' = 1/x$ makes $xy$ grow logarithmically. **Formal Solution:** *Step 1 -- Integrating factor.* With $P(x) = 1/x$, $$I(x) = \exp\left(\int \frac{dx}{x}\right) = e^{\ln x} = x \qquad (x > 0).$$ *Step 2 -- Multiply and recognize a derivative.* $$x y' + y = \frac{1}{x} \quad \Longleftrightarrow \quad \frac{d}{dx}\left(x y\right) = \frac{1}{x}.$$ *Step 3 -- Integrate.* $$xy = \ln x + C \quad \Longrightarrow \quad y = \frac{\ln x + C}{x}.$$ *Step 4 -- Initial condition.* $y(1) = (\ln 1 + C)/1 = C = 1$. *Step 5 -- Verify.* $y = \dfrac{\ln x + 1}{x}$ gives $y' = \dfrac{1 - (\ln x + 1)}{x^{2}} = -\dfrac{\ln x}{x^{2}}$, and $y' + \dfrac{y}{x} = -\dfrac{\ln x}{x^2} + \dfrac{\ln x + 1}{x^2} = \dfrac{1}{x^2}$. $\checkmark$ **Answer:** $y(x) = \dfrac{\ln x + 1}{x}$. --- General Solution of y'' + y' + y = 0 [easy, Optimization] --- https://quantvault.org/problem-3647-general-solution-of-y-y-y-0.html Q: Find the general (real-valued) solution of the homogeneous linear differential equation $$y'' + y' + y = 0.$$ Write it using real functions only (no complex exponentials), and say whether solutions grow, decay or oscillate as $x \to \infty$. A: **How to Think About It:** Because the coefficients are constant, exponentials $e^{rx}$ are the natural trial solutions; the ODE reduces to a quadratic in $r$. Complex roots signal oscillation, and Euler's formula converts complex exponentials into real sines and cosines. **Quick Estimate:** Think of $y'' + y' + y = 0$ as a damped oscillator $m\ddot{x} + c\dot{x} + kx = 0$ with $m = c = k = 1$. Damping ratio $\zeta = c/(2\sqrt{mk}) = 1/2 < 1$: underdamped, so expect decaying oscillations with decay rate $\zeta\omega_0 = 1/2$ and damped frequency $\omega_0\sqrt{1 - \zeta^2} = \sqrt{3}/2$. **Formal Solution:** *Step 1 -- Characteristic equation.* Substitute $y = e^{rx}$: $r^2 e^{rx} + r e^{rx} + e^{rx} = 0$, so $$r^{2} + r + 1 = 0 \quad \Longrightarrow \quad r = \frac{-1 \pm \sqrt{1 - 4}}{2} = -\frac{1}{2} \pm \frac{\sqrt{3}}{2}\,i.$$ *Step 2 -- Complex solutions.* The functions $e^{(-1/2 + i\sqrt3/2)x}$ and $e^{(-1/2 - i\sqrt3/2)x}$ are two linearly independent solutions. By Euler's formula, $e^{(-1/2 \pm i\sqrt{3}/2)x} = e^{-x/2}\left(\cos\tfrac{\sqrt3}{2}x \pm i\sin\tfrac{\sqrt3}{2}x\right)$. *Step 3 -- Real basis.* Taking the sum and difference (divided by $2$ and $2i$) of the two complex solutions gives the real solutions $e^{-x/2}\cos\frac{\sqrt3}{2}x$ and $e^{-x/2}\sin\frac{\sqrt3}{2}x$, which are linearly independent (their Wronskian is $\frac{\sqrt3}{2}e^{-x} \neq 0$). Hence the general solution is $$y(x) = e^{-x/2}\left(c_1 \cos\frac{\sqrt{3}}{2}x + c_2 \sin\frac{\sqrt{3}}{2}x\right), \qquad c_1, c_2 \in \mathbb{R}.$$ *Step 4 -- Verify one basis function.* For $u = e^{-x/2}\cos\beta x$ with $\beta = \sqrt3/2$: $u' = e^{-x/2}(-\tfrac12\cos\beta x - \beta\sin\beta x)$ and $u'' = e^{-x/2}\left((\tfrac14 - \beta^2)\cos\beta x + \beta\sin\beta x\right)$. Then $u'' + u' + u = e^{-x/2}\cos\beta x\,(\tfrac14 - \beta^2 - \tfrac12 + 1) = e^{-x/2}\cos\beta x\,(\tfrac34 - \tfrac34) = 0$. $\checkmark$ **Answer:** $y = e^{-x/2}\left(c_1\cos\dfrac{\sqrt{3}}{2}x + c_2\sin\dfrac{\sqrt{3}}{2}x\right)$ with arbitrary constants $c_1, c_2$. Every nonzero solution oscillates with angular frequency $\sqrt3/2$ inside a decaying envelope $e^{-x/2}$, so it tends to $0$. --- Nonhomogeneous ODEs: y'' + y' + y = 1 and y'' + y' + y = x [easy, Optimization] --- https://quantvault.org/problem-3648-nonhomogeneous-odes-y-y-y-1-and-y-y-y-x.html Q: Find the general solution of each nonhomogeneous equation. **(a)** $y'' + y' + y = 1$ **(b)** $y'' + y' + y = x$ **(c)** For equation (b), find the solution with $y(0) = 0$ and $y'(0) = 0$, and give $y(1)$ to three decimal places. You may use that the homogeneous equation $y'' + y' + y = 0$ has general solution $y_h = e^{-x/2}\left(c_1\cos\frac{\sqrt3}{2}x + c_2\sin\frac{\sqrt3}{2}x\right)$. A: **How to Think About It:** Because the operator $L[y] = y'' + y' + y$ is linear, the general solution is $y = y_h + y_p$: the full homogeneous family plus a single particular solution. With polynomial forcing and $0$ not a root of $r^2 + r + 1$, a polynomial of matching degree always works as $y_p$. **Quick Estimate:** For large $x$ the homogeneous part $e^{-x/2}(\cdots)$ dies out, so the particular solution is the long-run behavior. For (a) the long-run level must satisfy $y = 1$ (all derivatives zero). For (b), if $y \approx x + b$ then $y' = 1$, $y'' = 0$, and $1 + x + b = x$ forces $b = -1$. **Formal Solution:** **Part (a): $y'' + y' + y = 1$** *Step 1 -- Particular solution.* Try $y_p = A$ (constant). Then $y_p'' = y_p' = 0$ and the equation gives $A = 1$. So $y_p = 1$. *Step 2 -- General solution.* $$y = e^{-x/2}\left(c_1\cos\tfrac{\sqrt3}{2}x + c_2\sin\tfrac{\sqrt3}{2}x\right) + 1.$$ **Part (b): $y'' + y' + y = x$** *Step 1 -- Particular solution.* Try $y_p = Ax + B$. Then $y_p' = A$, $y_p'' = 0$, and $$0 + A + (Ax + B) = x \quad \Longrightarrow \quad Ax + (A + B) = x.$$ Matching coefficients: $A = 1$ and $A + B = 0$, so $B = -1$. Thus $y_p = x - 1$. *Step 2 -- Check.* $y_p'' + y_p' + y_p = 0 + 1 + (x - 1) = x$. $\checkmark$ *Step 3 -- General solution.* $$y = e^{-x/2}\left(c_1\cos\tfrac{\sqrt3}{2}x + c_2\sin\tfrac{\sqrt3}{2}x\right) + x - 1.$$ *Remark.* Why does a polynomial ansatz work? $L$ maps polynomials of degree $n$ to polynomials of degree $n$ (the $y$ term preserves the degree because $0$ is not a characteristic root), so matching coefficients is a solvable triangular system. **Part (c): Initial conditions $y(0) = 0$, $y'(0) = 0$ for (b)** *Step 1 -- Fix $c_1$.* At $x = 0$ the homogeneous part equals $c_1$ and $y_p = -1$, so $y(0) = c_1 - 1 = 0$ and $c_1 = 1$. *Step 2 -- Fix $c_2$.* Differentiating, $y'(x) = e^{-x/2}\left[\left(-\tfrac12 c_1 + \tfrac{\sqrt3}{2} c_2\right)\cos\tfrac{\sqrt3}{2}x - \left(\tfrac{\sqrt3}{2} c_1 + \tfrac12 c_2\right)\sin\tfrac{\sqrt3}{2}x\right] + 1$, so $y'(0) = -\tfrac12 c_1 + \tfrac{\sqrt3}{2} c_2 + 1 = 0$. With $c_1 = 1$ this gives $c_2 = -\tfrac{1}{\sqrt3} \approx -0.577$. *Step 3 -- Evaluate.* $$y(x) = e^{-x/2}\left(\cos\tfrac{\sqrt3}{2}x - \tfrac{1}{\sqrt3}\sin\tfrac{\sqrt3}{2}x\right) + x - 1,$$ and $y(1) = e^{-1/2}\,(0.6479 - 0.4398) \approx 0.6065 \times 0.2081 \approx 0.126$. Check: $y(0) = 1 - 1 = 0$ and $y'(0) = -\tfrac12 - \tfrac12 + 1 = 0$. **Answer:** (a) $y = e^{-x/2}\left(c_1\cos\frac{\sqrt3}{2}x + c_2\sin\frac{\sqrt3}{2}x\right) + 1$. (b) $y = e^{-x/2}\left(c_1\cos\frac{\sqrt3}{2}x + c_2\sin\frac{\sqrt3}{2}x\right) + x - 1$. (c) $y = e^{-x/2}\left(\cos\frac{\sqrt3}{2}x - \frac{1}{\sqrt3}\sin\frac{\sqrt3}{2}x\right) + x - 1$, so $y(1) \approx 0.126$. --- Implement Least Squares From Scratch With a QR Decomposition [medium, Regression] --- https://quantvault.org/problem-3650-implement-least-squares-from-scratch-with-a-qr-decomposition.html Q: You are asked to fit an ordinary least squares regression $$Y = X\beta + \varepsilon, \qquad X \in \mathbb{R}^{n \times p},\; Y \in \mathbb{R}^{n},\; n > p,$$ in a language with no regression routine. Only basic array arithmetic is available. **(a)** Derive the estimator $\hat\beta$ that minimizes $\|Y - X\beta\|^{2}$ and state the linear system it satisfies. **(b)** Explain how to compute $\hat\beta$ numerically without forming $(X^{T}X)^{-1}$ explicitly, using a QR decomposition of $X$, and why that is preferable to solving the normal equations directly. **(c)** Write the code, and run it on the design with an intercept and $x = (0, 1, 2)$, i.e. $X$ with columns $(1, 1, 1)$ and $(0, 1, 2)$, and $Y = (1, 2, 4)$. Report $R$ and $\hat\beta$. A: **How to Think About It:** Least squares is a calculus problem (minimize a quadratic) whose answer is a linear system, and a numerical-linear-algebra problem (solve that system stably). The normal equations are correct but numerically fragile; a QR factorization solves the same problem while working with $X$ itself rather than $X^{T}X$. **Approach:** Derive the normal equations by differentiating the residual sum of squares, substitute $X = QR$ to obtain a triangular system, then implement Gram-Schmidt QR plus back substitution. **Formal Solution:** **Part (a): The estimator** *Step 1 -- Objective.* $$S(\beta) = (Y - X\beta)^{T}(Y - X\beta) = Y^{T}Y - 2\beta^{T}X^{T}Y + \beta^{T}X^{T}X\beta.$$ *Step 2 -- First-order condition.* $$\nabla_\beta S = -2X^{T}Y + 2X^{T}X\beta = 0 \quad \Longrightarrow \quad X^{T}X\,\hat\beta = X^{T}Y \quad (\text{the normal equations}).$$ The Hessian $2X^{T}X$ is positive definite when $X$ has full column rank, so this is the unique minimizer, $\hat\beta = (X^{T}X)^{-1}X^{T}Y$. Geometrically the residual $Y - X\hat\beta$ is orthogonal to every column of $X$. **Part (b): Solving via QR** *Step 3 -- Substitute the factorization.* Write $X = QR$ with $Q \in \mathbb{R}^{n \times p}$ having orthonormal columns ($Q^{T}Q = I_p$) and $R \in \mathbb{R}^{p \times p}$ upper triangular, invertible when $X$ has full column rank. Then $$X^{T}X = R^{T}Q^{T}QR = R^{T}R, \qquad X^{T}Y = R^{T}Q^{T}Y,$$ so the normal equations become $R^{T}R\hat\beta = R^{T}Q^{T}Y$. Cancelling the invertible $R^{T}$: $$R\,\hat\beta = Q^{T}Y,$$ a $p \times p$ upper-triangular system solved by back substitution in $O(p^2)$ operations (after the $O(np^2)$ factorization). *Step 4 -- Why not invert $X^{T}X$?* Forming $X^{T}X$ squares the condition number: $\kappa(X^{T}X) = \kappa(X)^2$. With nearly collinear regressors ($\kappa(X) \sim 10^{6}$, common for factor exposures) the normal equations lose roughly twice as many digits as the QR route, and can even report a singular matrix in floating point. QR also never needs an explicit inverse. For rank-deficient $X$, use a pivoted QR or the SVD instead. *Step 5 -- Building $Q$ and $R$.* Modified Gram-Schmidt: for each column $j$, subtract its projections onto the already-orthonormalized columns $q_1, \ldots, q_{j-1}$ (recording the coefficients in $R_{ij}$), then normalize (recording $R_{jj}$). Householder reflections are more stable still and are what LAPACK uses; Gram-Schmidt is simpler to write and adequate for well-conditioned problems. **Part (c): Code** ```python import numpy as np def qr_mgs(X): # Modified Gram-Schmidt: X (n x p) = Q (n x p, orthonormal cols) @ R (p x p, upper) X = np.array(X, dtype=float) n, p = X.shape Q = np.zeros((n, p)); R = np.zeros((p, p)) for j in range(p): v = X[:, j].copy() for i in range(j): R[i, j] = Q[:, i] @ v v = v - R[i, j] * Q[:, i] R[j, j] = np.sqrt(v @ v) if R[j, j] == 0: raise ValueError("X is rank deficient") Q[:, j] = v / R[j, j] return Q, R def back_substitute(R, b): # Solve R beta = b for upper-triangular R p = len(b); beta = np.zeros(p) for i in range(p - 1, -1, -1): beta[i] = (b[i] - R[i, i + 1:] @ beta[i + 1:]) / R[i, i] return beta def ols(X, Y, intercept=True): X = np.asarray(X, dtype=float) if intercept: X = np.column_stack([np.ones(len(X)), X]) Q, R = qr_mgs(X) beta = back_substitute(R, Q.T @ np.asarray(Y, dtype=float)) resid = Y - X @ beta return beta, resid ``` *Worked check.* For $X$ with columns $(1, 1, 1)$ and $(0, 1, 2)$ and $Y = (1, 2, 4)$: $R_{11} = \sqrt3$ and $q_1 = (1, 1, 1)/\sqrt3$; $R_{12} = q_1^{T}(0, 1, 2) = \sqrt3$; the remainder $(0, 1, 2) - \sqrt3\,q_1 = (-1, 0, 1)$ gives $R_{22} = \sqrt2$ and $q_2 = (-1, 0, 1)/\sqrt2$. Then $Q^{T}Y = (7/\sqrt3, 3/\sqrt2)$, and back substitution gives $\hat\beta_1 = (3/\sqrt2)/\sqrt2 = 3/2$ and $\hat\beta_0 = (7/\sqrt3 - \sqrt3 \cdot \tfrac32)/\sqrt3 = \tfrac73 - \tfrac32 = \tfrac56$, the same line $\hat y = \tfrac56 + \tfrac32 x$ that the normal equations give. In code: `ols([0, 1, 2], [1, 2, 4])` returns these coefficients. Complexity: $O(np^2)$ for the factorization, $O(np)$ for $Q^{T}Y$, $O(p^2)$ for back substitution. (Optionally, standard errors follow from $(X^{T}X)^{-1} = R^{-1}R^{-T}$, again without inverting $X^{T}X$ directly.) **Answer:** (a) $\hat\beta$ solves the normal equations $X^{T}X\hat\beta = X^{T}Y$, i.e. $\hat\beta = (X^{T}X)^{-1}X^{T}Y$. (b) Factor $X = QR$; then $\hat\beta$ solves the triangular system $R\hat\beta = Q^{T}Y$ by back substitution, which avoids forming $X^{T}X$ and squaring the condition number. (c) Modified Gram-Schmidt (or Householder) QR plus back substitution, as in the code above; on the worked example $R = \begin{pmatrix} \sqrt3 & \sqrt3 \\ 0 & \sqrt2 \end{pmatrix}$ and $\hat\beta = (5/6, 3/2)$. --- Eigenvalues and Eigenvectors of the Matrix [[2, 1], [1, 2]] [easy, Linear Algebra] --- https://quantvault.org/problem-3651-eigenvalues-and-eigenvectors-of-the-matrix-2-1-1-2.html Q: Let $$A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}.$$ Find all eigenvalues of $A$ and a corresponding (normalized) eigenvector for each. Check your answer using the trace and the determinant. Then use the eigendecomposition to find the off-diagonal entry of $A^{10}$ without multiplying matrices. A: **How to Think About It:** For a $2 \times 2$ matrix the characteristic polynomial is determined by the trace and determinant, so the eigenvalues can be read off almost without computation. Because $A$ is symmetric, its eigenvectors are orthogonal, which both guides the search and checks the result. **Quick Estimate:** $A = I + \mathbf{1}\mathbf{1}^{T}$ with $\mathbf{1} = (1, 1)^{T}$. The rank-one part $\mathbf{1}\mathbf{1}^{T}$ has eigenvalues $\mathbf{1}^{T}\mathbf{1} = 2$ (along $\mathbf{1}$) and $0$ (orthogonal to $\mathbf{1}$); adding $I$ shifts both by $1$, giving $3$ and $1$. **Formal Solution:** *Step 1 -- Characteristic equation.* $$\det(A - \lambda I) = \det\begin{pmatrix} 2 - \lambda & 1 \\ 1 & 2 - \lambda \end{pmatrix} = (2 - \lambda)^{2} - 1 = \lambda^{2} - 4\lambda + 3 = (\lambda - 1)(\lambda - 3).$$ So $\lambda_1 = 3$ and $\lambda_2 = 1$. *Step 2 -- Trace and determinant check.* $\lambda_1 + \lambda_2 = 4 = \operatorname{tr}A$ and $\lambda_1\lambda_2 = 3 = \det A = 4 - 1$. $\checkmark$ *Step 3 -- Eigenvector for $\lambda_1 = 3$.* Solve $(A - 3I)v = 0$: $$\begin{pmatrix} -1 & 1 \\ 1 & -1 \end{pmatrix}\begin{pmatrix} v_1 \\ v_2 \end{pmatrix} = 0 \;\Longrightarrow\; v_1 = v_2, \qquad v^{(1)} = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 \\ 1 \end{pmatrix}.$$ *Step 4 -- Eigenvector for $\lambda_2 = 1$.* Solve $(A - I)v = 0$: $$\begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix}\begin{pmatrix} v_1 \\ v_2 \end{pmatrix} = 0 \;\Longrightarrow\; v_1 = -v_2, \qquad v^{(2)} = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 \\ -1 \end{pmatrix}.$$ *Step 5 -- Verify.* $A v^{(1)} = \frac{1}{\sqrt2}(3, 3)^{T} = 3v^{(1)}$ and $A v^{(2)} = \frac{1}{\sqrt2}(1, -1)^{T} = 1 \cdot v^{(2)}$. The eigenvectors are orthogonal, as they must be for a symmetric matrix, and $A = 3\,v^{(1)}v^{(1)T} + 1\,v^{(2)}v^{(2)T}$ is the spectral decomposition. *Step 6 -- Powers.* The spectral decomposition gives $A^{n} = 3^{n}\,v^{(1)}v^{(1)T} + 1^{n}\,v^{(2)}v^{(2)T}$, and $v^{(1)}v^{(1)T} = \tfrac12\begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix}$, $v^{(2)}v^{(2)T} = \tfrac12\begin{pmatrix} 1 & -1 \\ -1 & 1 \end{pmatrix}$. So $$A^{n} = \frac12\begin{pmatrix} 3^{n} + 1 & 3^{n} - 1 \\ 3^{n} - 1 & 3^{n} + 1 \end{pmatrix}, \qquad (A^{10})_{12} = \frac{3^{10} - 1}{2} = \frac{59049 - 1}{2} = 29524.$$ Check at $n = 1$: $\tfrac12(3 + 1) = 2$ and $\tfrac12(3 - 1) = 1$. $\checkmark$ **Answer:** Eigenvalues $\lambda = 3$ and $\lambda = 1$, with normalized eigenvectors $\frac{1}{\sqrt2}(1, 1)^{T}$ and $\frac{1}{\sqrt2}(1, -1)^{T}$ respectively (any nonzero scalar multiples also work). The off-diagonal entry of $A^{10}$ is $(3^{10} - 1)/2 = 29524$. --- Generating Correlated Normals From Independent Ones [medium, Linear Algebra] --- https://quantvault.org/problem-3653-generating-correlated-normals-from-independent-ones.html Q: You have a random number generator that produces independent standard normal draws $z_1, z_2, \ldots$ **(a)** How do you produce two standard normal random variables $x_1, x_2$ (each $N(0,1)$) with correlation $\rho$? Prove that your construction has the required means, variances and correlation. **(b)** Generalize: given a mean vector $\mu \in \mathbb{R}^{n}$ and a covariance matrix $\Sigma \in \mathbb{R}^{n \times n}$ (symmetric positive definite), how do you generate $X \sim N(\mu, \Sigma)$ from a vector $Z$ of $n$ independent standard normals? Name the matrix decomposition involved. A: **How to Think About It:** Linear maps of Gaussian vectors are Gaussian, with covariance transformed as $\operatorname{Cov}(LZ) = L\,\operatorname{Cov}(Z)\,L^{T} = LL^{T}$. So generating $N(\mu, \Sigma)$ reduces to finding a square root $L$ of $\Sigma$; in two dimensions you can find it by hand, and in general Cholesky does it. **Approach:** Build $x_2$ as a mix of $z_1$ and an independent $z_2$ with weights chosen to hit variance $1$ and covariance $\rho$; then observe that these weights are the second row of the Cholesky factor of the $2 \times 2$ correlation matrix and state the $n$-dimensional recipe. **Formal Solution:** **Part (a): Two correlated standard normals** *Step 1 -- Construction.* Let $$x_1 = z_1, \qquad x_2 = \rho\,z_1 + \sqrt{1 - \rho^{2}}\,z_2 .$$ *Step 2 -- Distribution.* Each $x_i$ is a linear combination of independent normals, hence normal, and $(x_1, x_2)$ is jointly normal (any linear combination of them is a linear combination of $z_1, z_2$). Means: $E[x_1] = E[x_2] = 0$. *Step 3 -- Variances.* $\operatorname{Var}(x_1) = 1$ and, by independence of $z_1, z_2$, $$\operatorname{Var}(x_2) = \rho^{2}\operatorname{Var}(z_1) + (1 - \rho^{2})\operatorname{Var}(z_2) = \rho^{2} + 1 - \rho^{2} = 1.$$ *Step 4 -- Correlation.* $$\operatorname{cov}(x_1, x_2) = \operatorname{cov}\!\left(z_1,\ \rho z_1 + \sqrt{1-\rho^2}\,z_2\right) = \rho\operatorname{Var}(z_1) + \sqrt{1-\rho^2}\operatorname{cov}(z_1, z_2) = \rho,$$ and since both variances are $1$, $\operatorname{corr}(x_1, x_2) = \rho$. The construction works for every $\rho \in [-1, 1]$. **Part (b): $n$ dimensions** *Step 5 -- The general recipe.* Let $Z = (z_1, \ldots, z_n)^{T}$ with $\operatorname{Cov}(Z) = I$. For any matrix $L$ with $LL^{T} = \Sigma$, set $$X = \mu + LZ.$$ Then $X$ is Gaussian with $E[X] = \mu$ and $\operatorname{Cov}(X) = L\,\operatorname{Cov}(Z)\,L^{T} = LL^{T} = \Sigma$. *Step 6 -- Cholesky decomposition.* Since $\Sigma$ is symmetric positive definite it has a unique Cholesky factorization $\Sigma = LL^{T}$ with $L$ lower triangular and positive diagonal (equivalently $\Sigma = R^{T}R$ with $R = L^{T}$ upper triangular). Triangularity means $x_k$ depends only on $z_1, \ldots, z_k$: each new coordinate is its regression on the previous ones plus fresh noise. For the $2 \times 2$ correlation matrix $\begin{pmatrix} 1 & \rho \\ \rho & 1\end{pmatrix}$, $$L = \begin{pmatrix} 1 & 0 \\ \rho & \sqrt{1 - \rho^{2}} \end{pmatrix},$$ which is exactly the construction of part (a). (Any other square root, e.g. the symmetric $\Sigma^{1/2}$ from the eigendecomposition, also works; Cholesky is the cheapest at $O(n^3/3)$ flops.) If $\Sigma$ is only positive semidefinite, use a pivoted Cholesky or the eigendecomposition $\Sigma = V\Lambda V^{T}$ with $L = V\Lambda^{1/2}$. ```python import numpy as np def correlated_pair(rho, size, rng=np.random.default_rng()): z1, z2 = rng.standard_normal(size), rng.standard_normal(size) return z1, rho * z1 + np.sqrt(1 - rho**2) * z2 def mvn(mu, Sigma, size, rng=np.random.default_rng()): L = np.linalg.cholesky(Sigma) # Sigma = L @ L.T, L lower triangular Z = rng.standard_normal((size, len(mu))) return mu + Z @ L.T # each row is mu + L z ``` **Answer:** (a) $x_1 = z_1$, $x_2 = \rho z_1 + \sqrt{1 - \rho^{2}}\,z_2$; both are $N(0,1)$ and $\operatorname{corr}(x_1, x_2) = \rho$. (b) Compute the Cholesky factor $L$ with $LL^{T} = \Sigma$ and set $X = \mu + LZ$; the two-dimensional case is $L = \begin{pmatrix} 1 & 0 \\ \rho & \sqrt{1-\rho^2}\end{pmatrix}$. --- Higher Card Wins: One Draw Against the Dealer [easy, Probability] --- https://quantvault.org/problem-3654-higher-card-wins-one-draw-against-the-dealer.html Q: You and a dealer play a one-round card game with a standard, well-shuffled 52-card deck. You draw one card, then the dealer draws one card from the remaining 51 (so the draws are without replacement). Only ranks matter: $2 < 3 < \cdots < 10 < J < Q < K < A$, and suits are ignored. You win if and only if your card's rank is **strictly higher** than the dealer's. A tie counts as a loss for you. What is the probability that you win? A: **How to Think About It:** The two cards are exchangeable random draws, so before we look at them there is no asymmetry between "my card" and "the dealer's card." The only outcome that is not a win for one of us is a tie. So find the tie probability, and split what is left evenly. **Quick Estimate:** After you draw, 3 of the remaining 51 cards match your rank, so a tie happens about 6% of the time. Roughly 94% of games have a winner, and half of those go to you: about 47%. **Formal Solution:** *Step 1 -- Probability of a tie.* Whatever your card is, exactly 3 of the 51 remaining cards share its rank: $$P(\text{tie}) = \frac{3}{51} = \frac{1}{17}.$$ *Step 2 -- Symmetry.* The ordered pair (your card, dealer's card) is a uniformly random ordered pair of distinct cards. Swapping the two cards is a bijection on this sample space that maps every "you win" outcome to a "you lose" outcome and vice versa, so $$P(\text{win}) = P(\text{lose}).$$ *Step 3 -- Combine.* Since win, lose and tie partition the sample space, $$P(\text{win}) = \frac{1 - P(\text{tie})}{2} = \frac{1 - \tfrac{1}{17}}{2} = \frac{16/17}{2} = \frac{8}{17} \approx 0.4706.$$ *Check by direct counting.* There are $52 \cdot 51 = 2652$ ordered outcomes. Ties: $52 \cdot 3 = 156$. Wins: for each of the 13 ranks with $k$ ranks below it, $4 \cdot 4k$ ordered pairs, giving $16 \cdot (0 + 1 + \cdots + 12) = 16 \cdot 78 = 1248$. So $P(\text{win}) = 1248/2652 = 8/17$. **Answer:** $P(\text{win}) = \dfrac{8}{17} \approx 0.471$. --- Hopping Rabbit: Counting Ways Up an n-Step Staircase [easy, Combinatorics] --- https://quantvault.org/problem-3655-hopping-rabbit-counting-ways-up-an-n-step-staircase.html Q: A rabbit sits at the bottom of a staircase with $n$ steps. On every hop it climbs either exactly 1 step or exactly 2 steps, and it must land exactly on the top step (it cannot overshoot). Two routes are different if their sequences of hop sizes differ, so for $n = 3$ the routes $1\text{-}2$, $2\text{-}1$ and $1\text{-}1\text{-}1$ are three different routes. Let $f(n)$ be the number of distinct routes to the top. **(a)** Find a recurrence for $f(n)$, with its initial conditions, and identify the resulting sequence. **(b)** Evaluate $f(10)$. A: **How to Think About It:** Do not try to count routes directly by how many 2-hops they contain (that works, but it is a sum of binomial coefficients). Instead think recursively: the last hop of any route lands on step $n$ from either step $n-1$ or step $n-2$. That gives a recurrence immediately. **Quick Estimate:** Fibonacci numbers grow like $\varphi^n/\sqrt{5}$ with $\varphi \approx 1.618$, so $f(n) = F_{n+1} \approx 1.618^{n+1}/2.236$. For $n = 10$: $1.618^{11} \approx 199$, divided by $2.236$ gives about $89$. **Formal Solution:** **Part (a): Recurrence** *Step 1 -- Partition by the last hop.* Let $A_n$ be the set of routes ending on step $n$ with a 1-hop, and $B_n$ the set ending with a 2-hop. Removing the last hop gives a bijection $A_n \leftrightarrow \{\text{routes to } n-1\}$ and $B_n \leftrightarrow \{\text{routes to } n-2\}$. The two sets are disjoint, so $$f(n) = f(n-1) + f(n-2), \qquad n \ge 3.$$ *Step 2 -- Initial conditions.* $f(1) = 1$ (a single 1-hop) and $f(2) = 2$ (either $1\text{-}1$ or $2$). Equivalently set $f(0) = 1$ (the empty route) so the recurrence holds from $n = 2$. *Step 3 -- Identify the sequence.* With $F_1 = F_2 = 1$ the Fibonacci numbers satisfy the same recurrence, and $f(1) = F_2$, $f(2) = F_3$, so by induction $$f(n) = F_{n+1} = \frac{\varphi^{n+1} - \psi^{n+1}}{\sqrt{5}}, \qquad \varphi = \frac{1+\sqrt{5}}{2},\ \psi = \frac{1-\sqrt{5}}{2}.$$ *Alternative count.* A route with $k$ two-hops and $n - 2k$ one-hops is an arrangement of $n - k$ hops, so $f(n) = \sum_{k=0}^{\lfloor n/2 \rfloor} \binom{n-k}{k}$, the classic diagonal-sum identity for Fibonacci numbers. **Part (b): $f(10)$** Iterating: $1, 2, 3, 5, 8, 13, 21, 34, 55, 89$. So $f(10) = 89$. **Answer:** (a) $f(n) = f(n-1) + f(n-2)$ with $f(1) = 1$, $f(2) = 2$, so $f(n) = F_{n+1}$ (Fibonacci). (b) $f(10) = 89$. --- The 100th Decimal Digit of (1 + sqrt 2)^3000 [medium, Brain Teaser] --- https://quantvault.org/problem-3657-the-100th-decimal-digit-of-1-sqrt-2-3000.html Q: What is the 100th digit after the decimal point in the decimal expansion of $$\left(1 + \sqrt{2}\right)^{3000}?$$ Give the digit and justify it without a computer. A: **How to Think About It:** A 3000th power of an irrational number looks hopeless until you notice its algebraic conjugate. Adding $(1 - \sqrt{2})^{3000}$ kills every $\sqrt{2}$ term and leaves an integer; and because $|1 - \sqrt{2}| < 1$, the conjugate term is astronomically small. That pins down the fractional part almost exactly. **Quick Estimate:** $\log_{10} |1 - \sqrt{2}| = \log_{10}(0.4142) \approx -0.383$, so $(1 - \sqrt{2})^{3000} \approx 10^{-1148}$. The number is an integer minus $10^{-1148}$, so its first 1148 decimal digits are all 9. In particular the 100th one is 9. **Formal Solution:** *Step 1 -- The conjugate sum is an integer.* By the binomial theorem, $$(1 + \sqrt{2})^n + (1 - \sqrt{2})^n = \sum_{k=0}^{n} \binom{n}{k} \left(\sqrt{2}\right)^k \left(1 + (-1)^k\right) = 2 \sum_{k \text{ even}} \binom{n}{k} 2^{k/2},$$ which is an integer. Call it $N$ for $n = 3000$. *Step 2 -- The conjugate term is tiny and positive.* Since $1 - \sqrt{2} \approx -0.4142$ and $3000$ is even, $$0 < (1 - \sqrt{2})^{3000} = (\sqrt{2} - 1)^{3000} < (0.5)^{3000} = 2^{-3000} < 10^{-900}.$$ (In fact it is about $10^{-1148}$.) *Step 3 -- Read off the digits.* Therefore $$(1 + \sqrt{2})^{3000} = N - \varepsilon, \qquad 0 < \varepsilon < 10^{-900}.$$ A number of the form $N - \varepsilon$ with $N$ an integer and $0 < \varepsilon < 10^{-900}$ has decimal expansion $(N - 1).999\ldots9\ldots$ with at least 900 consecutive 9s after the decimal point. The 100th digit after the decimal point is therefore 9. *Remark.* If the exponent were odd, $(1 - \sqrt{2})^n$ would be negative, the number would be $N + \varepsilon$, and the answer would be 0 instead. The parity of the exponent matters. **Answer:** The 100th digit after the decimal point is $9$. --- Cube Ending in 11: A Random Integer up to 10^12 [medium, Brain Teaser] --- https://quantvault.org/problem-3658-cube-ending-in-11-a-random-integer-up-to-10-12.html Q: An integer $x$ is chosen uniformly at random from $\{1, 2, \ldots, 10^{12}\}$. What is the probability that the decimal representation of $x^3$ ends in the two digits $11$? A: **How to Think About It:** Since $10^{12}$ is a multiple of $100$, the last two digits of $x$ are uniform over $00$ through $99$, and the last two digits of $x^3$ depend only on them. So the problem is: how many residues $r \in \{0, \ldots, 99\}$ satisfy $r^3 \equiv 11 \pmod{100}$? Attack the units digit first, then the tens digit. **Quick Estimate:** Cubing permutes the ten possible units digits, so about $1/10$ of integers have cubes ending in 1; among those, a further $1/10$ should have the right tens digit, for roughly $1/100$. The exact analysis confirms exactly one residue works. **Formal Solution:** *Step 1 -- Reduce modulo 100.* Write $x = a + 10b + 100c$ with digits $a, b$. Then modulo 100, $$x^3 \equiv (a + 10b)^3 = a^3 + 30a^2 b + 300ab^2 + 1000b^3 \equiv a^3 + 30 a^2 b \pmod{100}.$$ *Step 2 -- Units digit.* We need $a^3 \equiv 1 \pmod{10}$. The cubes of $0, \ldots, 9$ end in $0, 1, 8, 7, 4, 5, 6, 3, 2, 9$ respectively, so $a = 1$ is the unique solution. *Step 3 -- Tens digit.* With $a = 1$: $x^3 \equiv 1 + 30b \pmod{100}$. We need $1 + 30b \equiv 11$, i.e. $30b \equiv 10 \pmod{100}$, i.e. $3b \equiv 1 \pmod{10}$, whose unique digit solution is $b = 7$ (since $3 \cdot 7 = 21$). *Step 4 -- Count.* So $x^3$ ends in $11$ exactly when $x$ ends in $71$. Check: $71^3 = 357{,}911$. Among $1, \ldots, 10^{12}$ exactly $10^{10}$ integers end in $71$, so $$P = \frac{10^{10}}{10^{12}} = \frac{1}{100}.$$ **Answer:** $P = \dfrac{1}{100}$; the cube ends in $11$ exactly when $x$ ends in $71$. --- Dart Game: Will the Next Throw Also Be Worse Than the First? [medium, Probability] --- https://quantvault.org/problem-3659-dart-game-will-the-next-throw-also-be-worse-than-the-first.html Q: Jason throws darts at a board. Each throw's distance from the bullseye is an independent draw from the same continuous distribution (his skill neither improves nor deteriorates, and ties have probability zero). **(a)** Jason's second throw lands farther from the bullseye than his first. He now throws a third dart. What is the probability that the third throw also lands farther from the bullseye than the first? **(b)** Generalize: Jason's throws $2, 3, \ldots, n$ all land farther from the bullseye than his first throw. What is the probability that throw $n + 1$ also lands farther from the bullseye than the first? A: **How to Think About It:** Every statement of the form "throw $k$ is farther than throw 1" is a statement about the ranking of the throws, and all $n!$ rankings of $n$ i.i.d. continuous draws are equally likely. Translate the conditioning event into a statement about ranks and the answer falls out. **Quick Estimate:** After seeing that throw 2 was worse, throw 1 is "the better of two," which is like a draw from the upper half of the skill distribution. A fresh throw beats a typical upper-half throw well under half the time; the exact answer is $1/3$, so the third throw is worse with probability $2/3$. **Formal Solution:** **Part (a): Three throws** *Step 1 -- Translate to ranks.* Let $D_1, D_2, D_3$ be the distances. "Throw 2 is worse than throw 1" is the event $D_1 < D_2$, and "throw 3 is worse than throw 1" is $D_1 < D_3$. *Step 2 -- Use exchangeability.* All $3! = 6$ orderings of $(D_1, D_2, D_3)$ are equally likely. $D_1 < D_2$ holds in 3 of them, and $D_1 < D_2$ together with $D_1 < D_3$ (i.e. $D_1$ is the minimum) holds in 2 of them. So $$P(D_1 < D_3 \mid D_1 < D_2) = \frac{2/6}{3/6} = \frac{2}{3}.$$ **Part (b): $n$ throws, then one more** *Step 1 -- Restate the events.* $A_n = \{D_1 < D_k \text{ for all } 2 \le k \le n\}$ is the event that throw 1 is the best of the first $n$ throws. By exchangeability each throw is equally likely to be the best, so $P(A_n) = 1/n$. *Step 2 -- Condition.* $A_n \cap \{D_1 < D_{n+1}\} = A_{n+1}$, the event that throw 1 is the best of $n+1$ throws, with probability $1/(n+1)$. Hence $$P(D_1 < D_{n+1} \mid A_n) = \frac{P(A_{n+1})}{P(A_n)} = \frac{1/(n+1)}{1/n} = \frac{n}{n+1}.$$ For $n = 2$ this recovers $2/3$. As $n$ grows the probability tends to 1: a throw that has beaten many others is almost surely an excellent throw. *Alternative (integral) check for (a).* Let $F$ be the CDF of the distance. Conditional on $D_1 = d$, the other throws are worse independently with probability $1 - F(d)$, so $$P(D_1 < D_2, D_1 < D_3) = \int (1 - F(d))^2 \, dF(d) = \int_0^1 (1-u)^2\,du = \frac{1}{3},$$ and dividing by $P(D_1 < D_2) = 1/2$ gives $2/3$. **Answer:** (a) $\dfrac{2}{3}$. (b) $\dfrac{n}{n+1}$. --- Three Dice in Strictly Increasing Order [easy, Probability] --- https://quantvault.org/problem-3660-three-dice-in-strictly-increasing-order.html Q: A fair six-sided die is rolled three times in sequence, giving results $X_1, X_2, X_3$. What is the probability that the results are strictly increasing, that is, $X_1 < X_2 < X_3$? A: **How to Think About It:** There are two independent conditions hiding here: the three rolls must be distinct, and, given distinct values, they must come out in ascending order. Symmetry handles the second condition: once you know the three distinct values, every one of their $3! = 6$ arrangements is equally likely. **Quick Estimate:** Rolls are distinct about $56\%$ of the time ($\tfrac{5}{6} \cdot \tfrac{4}{6}$), and one in six of those is increasing: roughly $9\%$. **Formal Solution:** *Step 1 -- All three distinct.* The second roll differs from the first with probability $5/6$, and the third differs from both with probability $4/6$, so $$P(\text{all distinct}) = \frac{5}{6} \cdot \frac{4}{6} = \frac{20}{36} = \frac{5}{9}.$$ *Step 2 -- Order given distinct.* Conditional on three distinct values, the $3!$ orderings are equally likely (the rolls are exchangeable), and exactly one ordering is increasing: $$P(X_1 < X_2 < X_3 \mid \text{all distinct}) = \frac{1}{6}.$$ *Step 3 -- Multiply.* $$P(X_1 < X_2 < X_3) = \frac{5}{9} \cdot \frac{1}{6} = \frac{5}{54} \approx 0.0926.$$ *Direct count.* An increasing triple is the same as choosing 3 different faces out of 6: $\binom{6}{3} = 20$ outcomes out of $6^3 = 216$, and $20/216 = 5/54$. **Answer:** $P(X_1 < X_2 < X_3) = \dfrac{5}{54} \approx 0.0926$. --- Amoeba Extinction: Die, Stay, Split in Two, or Split in Three [medium, Probability] --- https://quantvault.org/problem-3661-amoeba-extinction-die-stay-split-in-two-or-split-in-three.html Q: A single amoeba sits in a dish. Every minute, each amoeba alive does exactly one of four things, each with probability $1/4$ and independently of everything else: - it dies, - it stays as it is (one amoeba), - it splits into two amoebas, - it splits into three amoebas. Every descendant behaves the same way in subsequent minutes. **(a)** Starting from this one amoeba, what is the probability that the population eventually dies out completely? **(b)** The fixed-point equation for the extinction probability is a cubic with three real roots. Find all three (by factoring, or numerically), and explain which one is the extinction probability and why the other two are rejected. **(c)** Is the process subcritical, critical or supercritical? A: **How to Think About It:** Extinction of the whole family is a self-similar event: after the first minute you have $k$ independent amoebas, each of which starts an identical copy of the original problem. That gives a fixed-point equation for the extinction probability. The subtlety is that $p = 1$ always solves the equation, so you must argue which root is the true answer. **Quick Estimate:** With probability $1/4$ the line dies at once, so $p > 0.25$, and the mean number of offspring is $1.5 > 1$, so $p 0.40$ and $g(0.42) \approx 0.418 0$, the sequence $p_n$ is increasing and bounded above by $1$, so it converges to some fixed point $p^* = g(p^*)$. Induction gives $p_n \le q$ for every fixed point $q \in [0,1]$: it holds at $n = 0$, and if $p_n \le q$ then $p_{n+1} = g(p_n) \le g(q) = q$. Hence the limit is the smallest fixed point in $[0,1]$, namely $\sqrt{2} - 1$. The iteration in the code block above shows exactly this: starting at $0$ it climbs to $0.41421$ and stops, never reaching $1$. - *When would $1$ be the right answer?* $g$ is convex on $[0,1]$ with $g(1) = 1$, so the graph of $g$ crosses the diagonal at most twice there, and $1$ is the smallest fixed point exactly when $g'(1) \le 1$. Since $g'(1) = \mu$, the mean number of offspring, the root $1$ is the extinction probability precisely when $\mu \le 1$. Here $\mu = 1.5 > 1$, so the curve dips below the diagonal before $1$, a second fixed point exists strictly inside $(0,1)$, and $1$ is rejected. **Part (c): Criticality** The offspring distribution puts mass $1/4$ on each of $0, 1, 2, 3$ children, so the mean offspring per amoeba per minute is $$\mu = g'(1) = \frac{0 + 1 + 2 + 3}{4} = \frac{3}{2} = 1.5.$$ Since $\mu > 1$ the process is **supercritical**: extinction is possible but not certain, $P(\text{extinction}) = \sqrt{2} - 1 1$, so the process is supercritical and survives with probability $2 - \sqrt{2} \approx 0.5858$. --- One Ace Each: Dealing 52 Cards to Four Players [easy, Probability] --- https://quantvault.org/problem-3662-one-ace-each-dealing-52-cards-to-four-players.html Q: A standard 52-card deck is shuffled and dealt out completely to four players, 13 cards each. What is the probability that every player receives exactly one ace? A: **How to Think About It:** A completed deal is a uniformly random partition of the deck into four labeled hands of 13. The only thing that matters is which hand each ace lands in, so you can ignore the 48 non-aces and just track the four aces as they are placed into the 52 slots one at a time. **Quick Estimate:** Each ace after the first needs to avoid the hands already holding an ace. The three factors are roughly $0.76$, $0.52$ and $0.27$; their product is about $0.105$. **Formal Solution:** *Step 1 -- Sequential placement of aces.* Think of the 52 card positions as 4 hands of 13 slots. Drop the aces in one by one into uniformly random unused slots. - Ace 1 lands in some hand (probability 1). - Ace 2 must avoid the 12 remaining slots of that hand: $39/51$. - Ace 3 must avoid the two hands already holding an ace ($2 \times 12 = 24$ slots): $26/50$. - Ace 4 must land in the last empty-of-aces hand: $13/49$. $$P = \frac{39}{51} \cdot \frac{26}{50} \cdot \frac{13}{49} = \frac{13{,}182}{124{,}950} = \frac{2197}{20825} \approx 0.1055.$$ *Step 2 -- Combinatorial check.* Count deals directly: assign the aces to the four hands ($4!$ ways), then fill each hand with non-aces: $\binom{48}{12}\binom{36}{12}\binom{24}{12}$ ways, out of $\binom{52}{13}\binom{39}{13}\binom{26}{13}$ total deals. This ratio simplifies to the same $2197/20825$. *Step 3 -- Interpretation.* $2197 = 13^3$ and $20825 = 51 \cdot 50 \cdot 49 / 6$; the answer is $13^3 / \binom{51}{3}$: choose the three slots of the other three aces relative to the first, and they must fall one per hand. **Answer:** $P = \dfrac{39 \cdot 26 \cdot 13}{51 \cdot 50 \cdot 49} = \dfrac{2197}{20825} \approx 0.1055$. --- Cars on the Highway: From a 20-Minute Window to a 5-Minute Window [easy, Probability] --- https://quantvault.org/problem-3664-cars-on-the-highway-from-a-20-minute-window-to-a-5-minute-wi.html Q: Cars pass a fixed point on a highway at a constant average rate, and arrivals in disjoint time intervals are independent (a Poisson process). The probability of seeing at least one car in any 20-minute window is $\dfrac{609}{625}$. What is the probability of seeing at least one car in a 5-minute window? A: **How to Think About It:** The trap is to divide by four: "at least one car in 5 minutes" is not one quarter of the 20-minute probability. What does scale cleanly is the complement. With a constant rate and independent increments, "no car in 20 minutes" is the intersection of four independent "no car in 5 minutes" events. **Quick Estimate:** $609/625 \approx 0.974$, so no car in 20 minutes has probability about $0.026$. The fourth root of $0.026$ is about $0.4$, so a 5-minute window is car-free about $40\%$ of the time and contains a car about $60\%$ of the time. **Formal Solution:** *Step 1 -- Complement over 20 minutes.* $$P(\text{no car in 20 min}) = 1 - \frac{609}{625} = \frac{16}{625}.$$ *Step 2 -- Split into four 5-minute windows.* Let $p$ be the probability of at least one car in a 5-minute window. By stationarity each 5-minute window is car-free with probability $1 - p$, and by independent increments the four windows are independent: $$(1 - p)^4 = \frac{16}{625} = \left(\frac{2}{5}\right)^4 \quad\Longrightarrow\quad 1 - p = \frac{2}{5}, \qquad p = \frac{3}{5}.$$ *Step 3 -- Poisson-rate check.* For a Poisson process with rate $\lambda$ per minute, $e^{-20\lambda} = 16/625$ gives $e^{-5\lambda} = 2/5$, so $P(\text{at least one in 5 min}) = 1 - e^{-5\lambda} = 3/5$. Same answer. **Answer:** $P(\text{at least one car in 5 minutes}) = \dfrac{3}{5}$. --- Turning Cards Until the First Ace [medium, Expectation] --- https://quantvault.org/problem-3667-turning-cards-until-the-first-ace.html Q: A standard 52-card deck is shuffled and you turn over cards one at a time from the top. Let $N$ be the number of cards you turn over up to and including the first ace. **(a)** Find $E[N]$. **(b)** Generalize: a deck contains $m$ ordinary cards and $n$ special cards. What is the expected number of cards turned over until the first special card appears? A: **How to Think About It:** The distribution of the position of the first ace is a hypergeometric-flavored mess, but its expectation is easy if you count the non-aces sitting in front of it with indicators. Each non-ace is either before all the aces or not, and symmetry gives that probability immediately. **Quick Estimate:** Four aces split the 48 other cards into 5 segments of equal expected size, $48/5 = 9.6$ cards. The first ace comes after the first segment, so at position $9.6 + 1 = 10.6$. **Formal Solution:** **Part (a): The 52-card deck** *Step 1 -- Indicators.* Label the 48 non-aces $1, \ldots, 48$ and let $I_j = 1$ if non-ace $j$ appears before all four aces. Then $N = 1 + \sum_{j=1}^{48} I_j$. *Step 2 -- Probability of each indicator.* Consider only the five cards consisting of non-ace $j$ and the four aces. In a uniformly random shuffle all $5!$ relative orders of these five cards are equally likely, so non-ace $j$ is first among them with probability $1/5$: $E[I_j] = 1/5$. *Step 3 -- Linearity.* $$E[N] = 1 + 48 \cdot \frac{1}{5} = 1 + 9.6 = 10.6 = \frac{53}{5}.$$ **Part (b): $m$ ordinary and $n$ special cards** By the identical argument each ordinary card precedes all $n$ special cards with probability $1/(n+1)$, so $$E[N] = 1 + \frac{m}{n+1}.$$ With $m = 48$, $n = 4$: $1 + 48/5 = 10.6$. Sanity checks: $n = 0$ special cards is degenerate; $m = 0$ gives $E[N] = 1$; $n = 1$ gives $1 + m/2$, the expected position of a single marked card in a deck of $m + 1$, i.e. $(m+2)/2$, which is correct. *Direct check.* $P(N = k) = \binom{52-k}{3}/\binom{52}{4}$ for $k = 1, \ldots, 49$, and $\sum_k k\, P(N = k) = 53/5$. **Answer:** (a) $E[N] = 1 + \dfrac{48}{5} = 10.6$. (b) $E[N] = 1 + \dfrac{m}{n+1}$. --- Sum of 12 Before Two Consecutive 7s [medium, Stochastic Process] --- https://quantvault.org/problem-3670-sum-of-12-before-two-consecutive-7s.html Q: Two fair dice are rolled repeatedly and the sum of the two faces is recorded after each roll. Player A wins if a sum of $12$ appears before two consecutive rolls both show a sum of $7$. Player B wins if two consecutive $7$s appear before any $12$. What is the probability that A wins? A: **How to Think About It:** A's target is a single roll; B's target is a two-roll pattern that can be interrupted. The natural state variable is "is B halfway there?" Condition on the next roll from each state and solve two linear equations. **Quick Estimate:** From scratch, the next "decisive" event is a 12 (probability $1/36$ per roll) or a 7 followed immediately by another 7 (roughly $6/36 \cdot 6/36 = 1/36$ per pair of rolls, but the first 7 also gives A an extra shot at a 12). So the race is close to even with a slight edge to A. **Formal Solution:** *Step 1 -- States and roll probabilities.* Per roll: $P(12) = \tfrac{1}{36}$, $P(7) = \tfrac{6}{36}$, $P(\text{other}) = \tfrac{29}{36}$. Let $x$ be the probability A wins from state $S$ (last roll was not a 7, or the game just started) and $y$ the probability A wins from state $T$ (last roll was a 7). *Step 2 -- First-step equations.* From $S$: a 12 wins for A, a 7 moves to $T$, anything else stays in $S$: $$x = \frac{1}{36}\cdot 1 + \frac{6}{36}\,y + \frac{29}{36}\,x.$$ From $T$: a 12 wins for A, a 7 wins for B (second consecutive 7), anything else returns to $S$: $$y = \frac{1}{36}\cdot 1 + \frac{6}{36}\cdot 0 + \frac{29}{36}\,x.$$ *Step 3 -- Solve.* From the first equation, $\tfrac{7}{36}x = \tfrac{1}{36} + \tfrac{6}{36}y$, i.e. $7x = 1 + 6y$. Substituting $y = \tfrac{1 + 29x}{36}$: $$7x = 1 + \frac{6(1 + 29x)}{36} = 1 + \frac{1 + 29x}{6} \quad\Longrightarrow\quad 42x = 6 + 1 + 29x \quad\Longrightarrow\quad 13x = 7,$$ so $x = \tfrac{7}{13}$ and $y = \tfrac{6}{13}$. *Step 4 -- Interpretation.* Being one 7 into B's pattern costs A only $1/13$ in win probability, because the next roll is far more likely to be a reset than a second 7. **Answer:** $P(\text{A wins}) = \dfrac{7}{13} \approx 0.538$. --- Roll Until You Stop or Hit a 6: Optimal Strategy and Fair Price [medium, Optimization] --- https://quantvault.org/problem-3673-roll-until-you-stop-or-hit-a-6-optimal-strategy-and-fair-pri.html Q: A casino offers the following game with a fair six-sided die. You may roll as many times as you like. Each roll of $1, 2, 3, 4$ or $5$ adds that many dollars to your running total. A roll of $6$ wipes out your entire running total and ends the game immediately. After any roll you may choose to stop and walk away with your running total. You are risk-neutral. **(a)** What is the optimal strategy: when should you stop rolling? **(b)** What is the most you should be willing to pay to play this game? A: **How to Think About It:** This is a dynamic programming / optimal stopping problem. The state is the running total $n$. At each state you compare the value of stopping ($n$) with the expected value of continuing optimally. The trick is that the one-step comparison "stop vs. roll once more and stop" already identifies the threshold, and then the values below the threshold follow by backward recursion. **Quick Estimate:** Rolling once more from total $n$ adds $3$ on average on the five safe faces but forfeits $n$ with probability $1/6$: the expected one-step change is $\tfrac{5}{6}\cdot 3 - \tfrac{1}{6}\,n = 2.5 - n/6$, which is positive only while $n < 15$. So the threshold is $15$, and starting from zero you will typically survive four or five rolls, so the game should be worth roughly $\$6$. **Formal Solution:** **Part (a): Optimal strategy** *Step 1 -- One-step lookahead.* From total $n$, rolling once and then stopping is worth $$\frac{1}{6}\sum_{i=1}^{5}(n + i) + \frac{1}{6}\cdot 0 = \frac{5n}{6} + \frac{5}{2}.$$ This exceeds $n$ iff $n < 15$. So at $n \ge 15$ even a single further roll is unfavorable. *Step 2 -- Threshold is optimal.* Let $f(n)$ be the optimal value from total $n$. Suppose $n \ge 15$ and consider any continuation strategy. Its terminal total is $n + G$ on the no-6 event, where $G \ge 0$ is the gain, and $0$ if a 6 occurs first. Every additional roll from a total $m \ge 15$ has negative expected one-step gain, and the same holds inductively for any sequence of rolls, so no continuation from $n \ge 15$ beats stopping: $f(n) = n$ for $n \ge 15$. Formally, the process $M_k = \text{(current total)}$ under any rolling policy is a supermartingale once the total is at least 15, so by optional stopping its expected terminal value is at most $n$. For $n \le 14$ one more roll strictly improves on stopping, so the optimal policy is: $$\text{keep rolling while the total is } \le 14; \text{ stop as soon as it is } \ge 15.$$ **Part (b): Value of the game** *Step 3 -- Backward recursion.* With $f(n) = n$ for $n \ge 15$ and $$f(n) = \frac{1}{6}\sum_{i=1}^{5} f(n+i), \qquad n \le 14,$$ compute downward: | $n$ | $14$ | $13$ | $12$ | $11$ | $10$ | $9$ | $8$ | $7$ | |---|---|---|---|---|---|---|---|---| | $f(n)$ | $14.167$ | $13.361$ | $12.588$ | $11.853$ | $11.161$ | $10.522$ | $9.914$ | $9.340$ | | $n$ | $6$ | $5$ | $4$ | $3$ | $2$ | $1$ | $0$ | |---|---|---|---|---|---|---|---| | $f(n)$ | $8.798$ | $8.289$ | $7.811$ | $7.359$ | $6.933$ | $6.532$ | $6.154$ | For example $f(14) = \tfrac{1}{6}(15 + 16 + 17 + 18 + 19) = \tfrac{85}{6} \approx 14.167$ and $f(13) = \tfrac{1}{6}(f(14) + 15 + 16 + 17 + 18) = \tfrac{481}{36} \approx 13.361$. *Step 4 -- Fair price.* The value of the game at the start is $f(0) \approx 6.15$ (exactly $2893395172951/470184984576$). A risk-neutral player should pay at most about $\$6.15$. **Answer:** (a) Keep rolling while your total is at most $\$14$; stop as soon as it reaches $\$15$ or more. (b) The game is worth $f(0) \approx \$6.15$, so pay at most that. --- Hitting 3 Before -5: Brownian Motion With and Without Drift [medium, Stochastic Process] --- https://quantvault.org/problem-3676-hitting-3-before-5-brownian-motion-with-and-without-drift.html Q: **(a)** A standard Brownian motion $W(t)$ starts at $0$. What is the probability that it reaches $+3$ before it reaches $-5$? **(b)** Now let the process have drift: $dX(t) = m\,dt + dW(t)$ with $X(0) = 0$ and constant $m$. What is the probability that $X$ reaches $+3$ before $-5$? A: **How to Think About It:** Two-barrier hitting probabilities are optional-stopping exercises: find a martingale of the process, evaluate it at the exit time where the position is one of two known values, and solve one linear equation for the probability. For driftless Brownian motion the martingale is $W$ itself. With drift, the trick is the exponential martingale $e^{-2mX(t)}$. **Quick Estimate:** With no drift, $-5$ is farther away than $3$, so hitting $3$ first is more likely than not: $5/8 = 62.5\%$. A positive drift $m$ pushes the probability up; with $m = 0.1$ it is already about $79\%$, and with $m = 1$ it is essentially $1$. **Formal Solution:** **Part (a): No drift** *Step 1 -- Optional stopping.* Let $\tau = \inf\{t : W(t) \in \{3, -5\}\}$ and $p = P(W(\tau) = 3)$. Since $W$ is a martingale, $E[\tau] < \infty$ and $W(t \wedge \tau)$ is bounded, optional stopping gives $E[W(\tau)] = W(0) = 0$: $$3p - 5(1 - p) = 0 \quad\Longrightarrow\quad p = \frac{5}{8}.$$ In general the probability of hitting $\alpha$ before $-\beta$ is $\beta/(\alpha + \beta)$. **Part (b): With drift $m$** *Step 2 -- Find a martingale.* For $Y(t) = e^{\lambda X(t)}$, Ito's lemma gives $$dY = \lambda Y\,dX + \tfrac{1}{2}\lambda^2 Y\,(dX)^2 = Y\left[\left(\lambda m + \tfrac{1}{2}\lambda^2\right)dt + \lambda\,dW\right].$$ The drift vanishes for $\lambda = -2m$, so $M(t) = e^{-2mX(t)}$ is a martingale. *Step 3 -- Optional stopping.* With $p = P(X \text{ hits } 3 \text{ before } -5)$ and $M(0) = 1$, $$E[M(\tau)] = p\,e^{-6m} + (1 - p)\,e^{10m} = 1.$$ *Step 4 -- Solve.* $$p\left(e^{-6m} - e^{10m}\right) = 1 - e^{10m} \quad\Longrightarrow\quad p = \frac{e^{10m} - 1}{e^{10m} - e^{-6m}}.$$ *Step 5 -- Checks.* As $m \to 0$, expand $e^{u} \approx 1 + u$: $p \to \dfrac{10m}{10m + 6m} = \dfrac{5}{8}$, matching part (a). As $m \to +\infty$, $p \to 1$; as $m \to -\infty$, $p \to 0$. For $m = 0.1$: $p = (e - 1)/(e - e^{-0.6}) \approx 0.792$. *Alternative (ODE).* $p(x)$, the probability from start $x$, solves the generator equation $m\,p'(x) + \tfrac{1}{2}p''(x) = 0$ with $p(-5) = 0$, $p(3) = 1$, whose solution is $p(x) = \dfrac{e^{-2mx} - e^{10m}}{e^{-6m} - e^{10m}}$; at $x = 0$ this is the same formula. **Answer:** (a) $P = \dfrac{5}{8}$. (b) $P = \dfrac{e^{10m} - 1}{e^{10m} - e^{-6m}}$ (reducing to $5/8$ as $m \to 0$). --- Brownian Motion With Unit Drift: Probability of Ever Reaching -1 [medium, Stochastic Process] --- https://quantvault.org/problem-3677-brownian-motion-with-unit-drift-probability-of-ever-reaching.html Q: A process follows $$dX(t) = dt + dW(t), \qquad X(0) = 0,$$ where $W$ is a standard Brownian motion (so $X$ has drift $+1$ and unit volatility). What is the probability that $X$ ever reaches the level $-1$? A: **How to Think About It:** With positive drift the process drifts off to $+\infty$, so "ever reaching $-1$" is a genuine event with probability strictly between 0 and 1. Set up the standard two-barrier problem with barriers at $-1$ and $+N$, solve it with the exponential martingale, and let the upper barrier go to infinity. **Quick Estimate:** Replace the process by a random walk with steps of $\pm\tfrac12$, each lasting $\tfrac14$ unit of time, that goes up with probability $\tfrac34$; this matches drift $1$ and variance $1$ per unit time. By gambler's ruin such a walk ever drops one step with probability $q/p = \tfrac13$, so it reaches $-1$ (two steps down) with probability $(\tfrac13)^2 \approx 0.11$. Halving the step to $\tfrac14$ (up with probability $\tfrac58$) gives $(\tfrac35)^4 \approx 0.13$, so expect a little above $0.13$. **Formal Solution:** *Step 1 -- Exponential martingale.* For $M(t) = e^{\lambda X(t)}$, Ito's lemma gives $$dM = M\left[\left(\lambda \cdot 1 + \tfrac{1}{2}\lambda^2\right)dt + \lambda\,dW\right],$$ so $M$ is a driftless (and, when stopped between bounded barriers, bounded) martingale for $\lambda = -2$: $M(t) = e^{-2X(t)}$, $M(0) = 1$. *Step 2 -- Two-barrier problem.* Fix $N > 0$ and let $\tau_N$ be the first time $X$ hits $-1$ or $+N$; let $p_N = P(X \text{ hits } -1 \text{ before } +N)$. Optional stopping ($E[\tau_N] 0$, the same argument with $\lambda = -2\mu/\sigma^2$ gives $P(\text{ever reach } -b) = e^{-2\mu b/\sigma^2}$; the running minimum of the process is exponentially distributed with rate $2\mu/\sigma^2$. **Answer:** $P(X \text{ ever reaches } -1) = e^{-2} \approx 0.135$. --- Which Way Do Option Prices Move? Six Inputs, Four Options [easy, Options Pricing] --- https://quantvault.org/problem-3678-which-way-do-option-prices-move-six-inputs-four-options.html Q: Consider plain vanilla European and American calls and puts on a stock. For each of the following inputs, explain how the price of each of the four options changes when that input increases while everything else is held fixed: **(a)** the current stock price $S$; **(b)** the strike price $K$; **(c)** the time to maturity $T$; **(d)** the volatility $\sigma$; **(e)** the risk-free interest rate $r$; **(f)** the dividends paid by the stock before expiry. Give a one-line economic reason for each direction, and point out any case where the direction is *not* determined without further assumptions. A: **How to Think About It:** Change one input at a time and ask two questions: does the change make the payoff at expiry larger, and does it change what the payoff is worth today (discounting, or the risk-neutral drift of the stock)? For American options add a third question: does the change give the holder more choices? A longer-dated American option always contains the shorter-dated one, so extra time can never hurt. A European holder has no such protection, and that is where the only ambiguous sign lives. **Quick Estimate:** Under Black-Scholes-Merton with continuous dividend yield $y$, $$c = S e^{-yT} N(d_1) - K e^{-rT} N(d_2), \qquad p = K e^{-rT} N(-d_2) - S e^{-yT} N(-d_1),$$ and the signs of the partial derivatives are: $\partial c/\partial S = e^{-yT}N(d_1) > 0$, $\partial p/\partial S = -e^{-yT}N(-d_1) 0$; $\partial c/\partial r = TKe^{-rT}N(d_2) > 0$, $\partial p/\partial r = -TKe^{-rT}N(-d_2) T_1$) can be exercised at any time up to $T_1$ as well, so it is worth at least as much. Both American calls and puts rise (weakly). European: extra time raises the dispersion of $S_T$ (good for both) but also changes the present value of the strike and the dividends lost. For a European call on a non-dividend stock, early exercise is never optimal, so the European and American calls coincide and the value rises with $T$. For a European put, longer maturity lowers $Ke^{-rT}$, which is what the holder ultimately receives; a deep in-the-money put is worth about $Ke^{-rT} - S$ and *falls* with $T$. Numerically, with $S = 50$, $K = 100$, $r = 10\%$, $\sigma = 20\%$, the European put is worth $47.5$ at $T = 0.25$, $40.5$ at $T = 1$, and $16.0$ at $T = 5$. For a European call on a stock paying large dividends before expiry, a longer maturity means more dividends are paid to shareholders rather than the call holder: with $S = 150$, $K = 100$, $r = 1\%$, $y = 8\%$, $\sigma = 20\%$ the call is worth $47.3$ at $T = 0.25$ but $39.9$ at $T = 1$. So the sign is ambiguous for European puts and for European calls on dividend-paying stocks. *Step 4 -- Volatility $\sigma$ increases.* Both payoffs are convex in $S_T$ with a floor at zero: the holder keeps the upside of larger moves and is protected on the downside. More dispersion therefore raises both calls and puts, American or European. *Step 5 -- Risk-free rate $r$ increases.* Two effects point the same way. The present value of the strike $Ke^{-rT}$ falls, which helps a call (pays $K$ later, cheaper) and hurts a put (receives $K$ later, cheaper). Also the risk-neutral drift of the stock rises, pushing $S_T$ up. Calls rise, puts fall. *Step 6 -- Dividends increase.* On the ex-dividend date the stock drops by roughly the dividend, and the option holder does not receive it. Lower expected $S_T$ means calls fall and puts rise. (For an American call, large dividends can also make early exercise just before the ex-date optimal, but the price still falls as dividends increase.) **Answer:** (a) $S$ up: calls up, puts down. (b) $K$ up: calls down, puts up. (c) $T$ up: American calls and puts up; European call on a non-dividend stock up; European puts and European calls on dividend-paying stocks are ambiguous (deep in-the-money European puts fall with $T$). (d) $\sigma$ up: all four up. (e) $r$ up: calls up, puts down. (f) Dividends up: calls down, puts up. --- Delta of an At-the-Money Call: Above or Below One Half? [easy, Options Pricing] --- https://quantvault.org/problem-3680-delta-of-an-at-the-money-call-above-or-below-one-half.html Q: Consider a European call on a non-dividend-paying stock that is exactly at the money: $S = K$. Under Black-Scholes-Merton with $r > 0$, **(a)** Estimate the delta of the call. Is it above or below $0.5$, and why? **(b)** How does the delta of an at-the-money call change with the time to maturity $\tau$? What does it converge to as $\tau \to 0$? A: **How to Think About It:** For a non-dividend stock, $\Delta_{\text{call}} = N(d_1)$. At the money the messy $\ln(S/K)$ term disappears and $d_1$ collapses to a single expression in $r$, $\sigma$ and $\tau$. Everything about the answer, including the maturity dependence, is read off from the sign and size of that expression. **Quick Estimate:** Take $r = 5\%$, $\sigma = 20\%$, $\tau = 1$ year: $d_1 = (0.05/0.2 + 0.1)\cdot 1 = 0.35$ and $N(0.35) \approx 0.64$. For a 3-month option, $d_1 = 0.35 \times 0.5 = 0.175$ and $\Delta \approx 0.57$. For a 1-day option, $d_1 \approx 0.35 \times 0.052 = 0.018$ and $\Delta \approx 0.507$. So the delta is a bit above one half and moves toward one half as expiry approaches. **Formal Solution:** **Part (a): Size of the at-the-money delta** *Step 1 -- Specialize $d_1$ to $S = K$.* $$d_1 = \frac{\ln(S/K) + (r + \sigma^2/2)\tau}{\sigma\sqrt{\tau}} = \frac{(r + \sigma^2/2)\tau}{\sigma\sqrt{\tau}} = \left(\frac{r}{\sigma} + \frac{\sigma}{2}\right)\sqrt{\tau}.$$ *Step 2 -- Sign.* Both $r/\sigma$ and $\sigma/2$ are positive, so $d_1 > 0$ and $\Delta = N(d_1) > N(0) = 0.5$. The delta of an at-the-money call is slightly above one half. (For typical parameters $d_1$ is small, so $\Delta \approx 0.5 + d_1/\sqrt{2\pi} \approx 0.5 + 0.4\,d_1$.) **Part (b): Dependence on maturity** *Step 3 -- Monotonicity.* $d_1 = (r/\sigma + \sigma/2)\sqrt{\tau}$ is increasing in $\tau$, and $N$ is increasing, so the at-the-money delta rises with time to maturity: a longer-dated at-the-money call has a higher delta. *Step 4 -- Limit.* As $\tau \to 0$, $d_1 \to 0$ and $\Delta \to N(0) = 1/2$. Intuitively, right before expiry an at-the-money call is a coin flip between finishing worth $S_T - K > 0$ (delta 1) and expiring worthless (delta 0), and the average is $1/2$. *Step 5 -- Caveat.* With dividends, $\Delta = e^{-y\tau}N(d_1)$ and $d_1 = ((r - y)/\sigma + \sigma/2)\sqrt{\tau}$, so a large enough yield can push the at-the-money delta below one half. **Answer:** (a) $\Delta = N\!\left((r/\sigma + \sigma/2)\sqrt{\tau}\right)$, slightly above $0.5$ because $d_1 > 0$ (for example $0.64$ with $r = 5\%$, $\sigma = 20\%$, $\tau = 1$). (b) The delta increases with $\tau$ (as $\sqrt{\tau}$ inside $N$) and tends to $N(0) = 0.5$ as $\tau \to 0$. --- Delta-Hedging a Long Call After a Sudden Jump [easy, Options Pricing] --- https://quantvault.org/problem-3681-delta-hedging-a-long-call-after-a-sudden-jump.html Q: You are long a European call option on GM stock and you want to eliminate the exposure to GM's stock price dynamically. Assume Black-Scholes-Merton holds; GM may pay a continuous dividend yield $y$. **(a)** How do you hedge the position? State precisely what you trade in the stock and in the risk-free asset, per call held. **(b)** GM's stock price suddenly jumps up. How do you rebalance the hedge, and why? A: **How to Think About It:** A long call has positive delta, so its value rises and falls with the stock. Hedging means adding a position whose delta is the negative of the call's, which for one call is $-\Delta$ shares, and parking the cash difference in the risk-free asset so the whole package has zero initial cost. Since delta itself moves with $S$, the hedge must be rebalanced, and the direction of rebalancing follows from the sign of gamma. **Quick Estimate:** Take $K = 100$, $r = 5\%$, $\sigma = 20\%$, $y = 0$, six months to expiry. At $S = 100$: $d_1 = 0.247$, $\Delta = 0.598$, $c = 6.89$. Hedge: short $0.598$ shares (receive $59.77$), and lend $59.77 - 6.89 = 52.88 = Ke^{-r\tau}N(d_2)$. If GM jumps to $110$: $d_1 = 0.921$, $\Delta = 0.822$. You now need to be short $0.822$ shares, so you sell a further $0.224$ shares at $110$ and lend the extra proceeds. **Formal Solution:** **Part (a): Constructing the hedge** *Step 1 -- Delta of the call.* With dividend yield $y$, the call is worth $c = Se^{-y\tau}N(d_1) - Ke^{-r\tau}N(d_2)$ with $d_1 = [\ln(S/K) + (r - y + \sigma^2/2)\tau]/(\sigma\sqrt{\tau})$, and $$\Delta = \frac{\partial c}{\partial S} = e^{-y\tau}N(d_1).$$ *Step 2 -- Replicating portfolio and its mirror.* The formula says a call equals $e^{-y\tau}N(d_1)$ shares minus $Ke^{-r\tau}N(d_2)$ of risk-free borrowing. To hedge a long call, take the opposite position per call: short $\Delta = e^{-y\tau}N(d_1)$ shares of GM and lend $Ke^{-r\tau}N(d_2)$ at the risk-free rate. The cash flows balance: shorting brings in $\Delta S = c + Ke^{-r\tau}N(d_2)$, of which $c$ paid for the call and the remainder is lent. The net position (call, short stock, loan) has zero delta and zero value, and it is instantaneously immune to small moves in $S$. *Step 3 -- Dynamic.* $\Delta$ changes with $S$ and with time, so the share position must be adjusted continuously (in practice, at discrete intervals or when delta drifts past a tolerance). **Part (b): Rebalancing after an up-jump** *Step 4 -- Sign of gamma.* $\Gamma = \partial\Delta/\partial S = e^{-y\tau}N'(d_1)/(S\sigma\sqrt{\tau}) > 0$, so the call's delta is increasing in $S$. After GM jumps up, $\Delta$ is larger; the existing short of $\Delta_{\text{old}}$ shares is no longer enough. *Step 5 -- Action.* Short an additional $\Delta_{\text{new}} - \Delta_{\text{old}} > 0$ shares and lend the proceeds (the required loan $Ke^{-r\tau}N(d_2)$ also rises because $d_2$ rose). Symmetrically, after a drop you would buy back shares. Note that you sell after the price rises and buy after it falls, which is where a long-gamma hedger's profits from realized volatility come from, paid for by the option's time decay. **Answer:** (a) Short $\Delta = e^{-y\tau}N(d_1)$ shares of GM per call and lend $Ke^{-r\tau}N(d_2)$ at the risk-free rate; rebalance as delta changes. (b) Delta increases when $S$ jumps up (positive gamma), so short additional shares to bring the short position up to the new $e^{-y\tau}N(d_1)$ and lend the extra proceeds. --- Quick Value of an At-the-Money Call: 0.4 Sigma S Root Tau [easy, Options Pricing] --- https://quantvault.org/problem-3682-quick-value-of-an-at-the-money-call-0-4-sigma-s-root-tau.html Q: A European call on a non-dividend-paying stock is at the money, $S = K$. Interest rates are low and the time to maturity $\tau$ is short, so that both $r\tau$ and $\sigma^2\tau$ are small. Give a quick approximation for the call price $c$ in terms of $S$, $\sigma$ and $\tau$, and explain where it comes from. Illustrate with $S = K = 100$, $\sigma = 20\%$ per year and $\tau = 3$ months. A: **How to Think About It:** At the money and with negligible rates, Black-Scholes reduces to $S$ times a difference of two normal CDFs evaluated at $\pm\sigma\sqrt{\tau}/2$. Because those arguments are tiny for short maturities, the difference of CDFs is just the density at zero times the gap between the arguments. That single Taylor step gives a formula you can do in your head. **Quick Estimate:** With $S = 100$, $\sigma = 0.2$, $\tau = 0.25$: $\sigma\sqrt{\tau} = 0.1$, so $c \approx 0.4 \times 0.1 \times 100 = 4$. The exact Black-Scholes value with $r = 0$ is $3.99$; with $r = 2\%$ it is $4.23$, so the approximation is within a few percent when rates are low. **Formal Solution:** *Step 1 -- Specialize Black-Scholes.* With $S = K$ and $r \approx 0$, $$c = S\,N(d_1) - Ke^{-r\tau}N(d_2) \approx S\,[N(d_1) - N(d_2)], \qquad d_1 = \frac{(r + \sigma^2/2)\tau}{\sigma\sqrt{\tau}} \approx \frac{\sigma\sqrt{\tau}}{2}, \quad d_2 = d_1 - \sigma\sqrt{\tau} \approx -\frac{\sigma\sqrt{\tau}}{2}.$$ *Step 2 -- Linearize the normal CDF.* For small $x$, $N(x) \approx N(0) + N'(0)\,x = \tfrac12 + x/\sqrt{2\pi}$. Hence $$N(d_1) - N(d_2) \approx \frac{d_1 - d_2}{\sqrt{2\pi}} = \frac{\sigma\sqrt{\tau}}{\sqrt{2\pi}}.$$ (The second-order terms cancel because $d_1$ and $d_2$ are symmetric about zero; the error is third order in $\sigma\sqrt{\tau}$.) *Step 3 -- Assemble.* Since $1/\sqrt{2\pi} = 0.3989 \approx 0.4$, $$c \approx \frac{\sigma S\sqrt{\tau}}{\sqrt{2\pi}} \approx 0.4\,\sigma S\sqrt{\tau}.$$ Equivalently $c/S \approx 0.4\,\sigma\sqrt{\tau}$: an at-the-money call costs about $40\%$ of one standard deviation of the stock's move over the option's life. By put-call parity with $r \approx 0$, the at-the-money put has the same value, so the straddle costs about $0.8\,\sigma S\sqrt{\tau}$. *Step 4 -- Example.* $S = 100$, $\sigma = 0.20$, $\tau = 0.25$: $c \approx 0.4 \times 0.20 \times 100 \times 0.5 = 4.0$ versus the exact $3.99$ (with $r = 0$). **Answer:** $c \approx 0.4\,\sigma S\sqrt{\tau}$ (more precisely $\sigma S\sqrt{\tau}/\sqrt{2\pi}$), because $c = S[N(d_1) - N(d_2)]$ with $d_1 \approx -d_2 \approx \sigma\sqrt{\tau}/2$ and $N(d_1) - N(d_2) \approx (d_1 - d_2)/\sqrt{2\pi}$. For $S = 100$, $\sigma = 20\%$, $\tau = 0.25$ this gives about $4$. --- When Can a European Option Have Positive Theta? [medium, Options Pricing] --- https://quantvault.org/problem-3683-when-can-a-european-option-have-positive-theta.html Q: Theta, $\Theta = \partial V/\partial t$, measures how an option's value changes as calendar time passes with everything else fixed. For most options theta is negative: the option loses value as expiry approaches. Under Black-Scholes-Merton (stock with continuous dividend yield $y$, risk-free rate $r$, volatility $\sigma$, time to maturity $\tau$), when can a *European* option have **positive** theta? Identify the cases, and justify each one from the theta formulas $$\Theta_{\text{call}} = -\frac{S e^{-y\tau} N'(d_1)\,\sigma}{2\sqrt{\tau}} - rKe^{-r\tau}N(d_2) + yS e^{-y\tau}N(d_1), \qquad \Theta_{\text{put}} = -\frac{S e^{-y\tau} N'(d_1)\,\sigma}{2\sqrt{\tau}} + rKe^{-r\tau}N(-d_2) - yS e^{-y\tau}N(-d_1).$$ A: **How to Think About It:** Split theta into a decay term that is always negative and "carry" terms driven by the drift of $Ke^{-r\tau}$ and $Se^{-y\tau}$. Theta is positive only where the option has almost no time value, so the decay term vanishes and the carry term has the right sign: a deep in-the-money put (carry $+rK$) or a deep in-the-money call on a heavy dividend payer (carry $+yS$ beating $rK$). **Quick Estimate:** Deep in-the-money put with $S = 50$, $K = 100$, $r = 5\%$, $\sigma = 20\%$, $\tau = 1$, $y = 0$: $d_1 = -3.12$, so $N'(d_1) \approx 0.003$ and $N(-d_2) \approx 0.9996$. Then $\Theta_{\text{put}} \approx 0.05 \times 100 \times e^{-0.05} = +4.76$ per year, and the exact value is $+4.74$. The put is worth $Ke^{-r\tau} - S$ plus almost nothing, and that quantity grows by about $rKe^{-r\tau}$ per year. **Formal Solution:** *Step 1 -- Structure of theta.* In both formulas the first term $-Se^{-y\tau}N'(d_1)\sigma/(2\sqrt{\tau})$ is nonpositive; it is the erosion of optionality (time value). The remaining terms are the interest and dividend carry of the replicating portfolio. Positive theta requires the carry to be positive and larger than the decay. *Step 2 -- Deep in-the-money European put.* As $S/K \to 0$ (or more generally $d_1, d_2 \to -\infty$): $N'(d_1) \to 0$, $N(-d_2) \to 1$, $N(-d_1) \to 1$, so $$\Theta_{\text{put}} \to rKe^{-r\tau} - ySe^{-y\tau}.$$ With no or small dividends this is $\approx rKe^{-r\tau} > 0$. Interpretation: the put is essentially worth $Ke^{-r\tau} - Se^{-y\tau}$ and the discounted strike accretes toward $K$ as time passes. *Step 3 -- Deep in-the-money European call on a high-dividend stock.* As $S/K \to \infty$: $N'(d_1) \to 0$, $N(d_1), N(d_2) \to 1$, so $$\Theta_{\text{call}} \to ySe^{-y\tau} - rKe^{-r\tau},$$ which is positive when $ySe^{-y\tau} > rKe^{-r\tau}$, i.e. when the dividend yield on the stock leg exceeds the interest on the strike leg. Example: $S = 150$, $K = 100$, $r = 1\%$, $y = 8\%$, $\sigma = 20\%$, $\tau = 1$ gives $\Theta_{\text{call}} = +8.59$ per year. Without dividends ($y = 0$) a European call's theta is always negative. *Step 4 -- Everything else.* Near the money, $N'(d_1)$ is at its largest and the decay term $\propto S\sigma/(2\sqrt{\tau})$ dominates (it blows up as $\tau \to 0$), so theta is negative: for example the at-the-money put above has $\Theta = -1.66$ and the at-the-money call $\Theta = -6.41$ per year. Deep out-of-the-money options have all terms near zero with the decay term slightly negative. *Step 5 -- Connection to early exercise.* The two positive-theta regions are exactly where the corresponding American option may be exercised early: a deep in-the-money American put (collect $K$ now rather than $Ke^{-r\tau}$ later) and a deep in-the-money American call on a dividend payer (capture the dividends). The European holder cannot exercise, so the value simply drifts up toward the intrinsic value as expiry nears. **Answer:** A European put that is deep in the money has $\Theta \approx rKe^{-r\tau} - ySe^{-y\tau} > 0$ (positive whenever dividends are small), and a European call that is deep in the money on a stock with a high dividend yield has $\Theta \approx ySe^{-y\tau} - rKe^{-r\tau} > 0$ when $yS e^{-y\tau} > rKe^{-r\tau}$. In both cases the decay term $Se^{-y\tau}N'(d_1)\sigma/(2\sqrt{\tau})$ is negligible because $N'(d_1) \approx 0$. --- Long Call, Delta Neutral: Gains Both Ways, Yet No Arbitrage [medium, Options Pricing] --- https://quantvault.org/problem-3684-long-call-delta-neutral-gains-both-ways-yet-no-arbitrage.html Q: You buy a European call on GM (assume GM pays no dividends) and make the position delta neutral by shorting $\Delta = N(d_1)$ shares of GM stock against it. **(a)** GM's stock price immediately jumps up. What happens to the value of your portfolio? What if it immediately jumps down instead? **(b)** If the portfolio gains value whichever way the stock moves, isn't this an arbitrage? Explain why or why not, using the relationship between theta, gamma and delta implied by the Black-Scholes-Merton PDE. A: **How to Think About It:** Delta neutrality kills the first-order exposure to $S$, so the next term in the Taylor expansion, gamma, decides the sign of an instantaneous move's P&L. For a long call gamma is positive, so both directions look profitable. The catch is the other variable, time: the Black-Scholes-Merton PDE forces a long-gamma position to have negative theta of exactly the size that makes the position fair when the stock moves at its implied volatility. **Quick Estimate:** Take $S = K = 100$, $r = 5\%$, $\sigma = 20\%$, $\tau = 0.5$: $c = 6.89$, $\Delta = 0.598$, $\Gamma = 0.0274$, $\Theta = -8.12$ per year. An instantaneous move of $\pm 5$ changes the hedged portfolio by about $\tfrac12\Gamma(5)^2 = 0.34$ (exact: $+0.32$ up, $+0.35$ down). But a day of theta costs $8.12/365 = 0.022$, and a typical daily move is $100 \times 0.2/\sqrt{252} = 1.26$, whose gamma gain is $\tfrac12 \times 0.0274 \times 1.26^2 = 0.022$. The two match: on an average day the hedged position breaks even. **Formal Solution:** **Part (a): Instantaneous moves** *Step 1 -- Taylor expansion of the portfolio.* Let $\Pi = c - \Delta S$ with $\Delta = N(d_1)$ fixed at the moment of the move. For an instantaneous change $\Delta S$ (no time passes), $$\Delta\Pi = \left[\Delta\cdot\Delta S + \tfrac12\Gamma(\Delta S)^2 + O((\Delta S)^3)\right] - \Delta\cdot\Delta S = \tfrac12\Gamma(\Delta S)^2 + O((\Delta S)^3).$$ *Step 2 -- Sign.* For a long call $\Gamma = N'(d_1)/(S\sigma\sqrt{\tau}) > 0$, so $\Delta\Pi > 0$ whether the stock jumps up or down. (In the example, $+10$ gives $+1.21$ and $-10$ gives $+1.44$; the asymmetry is the third-order term.) **Part (b): Why this is not an arbitrage** *Step 3 -- Bring in time.* The gain in Step 1 assumes no time passes. Over an interval $dt$ the call also loses time value. The Black-Scholes-Merton PDE for any derivative $V$ on a non-dividend stock is $$\Theta + rS\frac{\partial V}{\partial S} + \tfrac12\sigma^2S^2\Gamma = rV.$$ Apply it to the delta-neutral portfolio $\Pi = c - \Delta S$ (whose delta is zero and whose gamma equals the call's): $$\Theta_\Pi + \tfrac12\sigma^2S^2\Gamma = r\Pi.$$ Numerically: $-8.12 + \tfrac12(0.04)(10^4)(0.0274) = -8.12 + 5.47 = -2.64 = 0.05 \times (6.89 - 59.77)$, as the identity requires. *Step 4 -- Interpretation.* Over $dt$ the P&L of the hedged position is approximately $$d\Pi \approx \Theta_\Pi\,dt + \tfrac12\Gamma(dS)^2 = \tfrac12\Gamma\left[(dS)^2 - \sigma^2S^2\,dt\right] + r\Pi\,dt.$$ The term $r\Pi\,dt$ is just interest on the (negative) cash value of the hedged package; the rest is $\tfrac12\Gamma S^2$ times realized minus implied variance over the interval. Under the risk-neutral measure $E[(dS)^2] = \sigma^2S^2dt$, so the expected excess P&L is zero: the positive gamma is exactly paid for by negative theta. You profit only if the stock actually moves more than the implied volatility predicts, and you lose if it moves less. That is a volatility bet, not an arbitrage. *Step 5 -- Practical caveat.* Even with continuous rehedging, the position is exposed to the difference between realized and implied volatility and to jumps; with discrete rehedging there is additional hedging error. Long gamma, short theta positions are common but they are risk positions. **Answer:** (a) The delta-neutral portfolio gains approximately $\tfrac12\Gamma(\Delta S)^2 > 0$ for an immediate jump in either direction, because a long call has positive gamma. (b) No. Time decay offsets the gamma gain: the PDE gives $\Theta_\Pi + \tfrac12\sigma^2S^2\Gamma = r\Pi$ for the delta-neutral portfolio, so the expected gamma profit $\tfrac12\Gamma\sigma^2S^2\,dt$ is exactly cancelled by theta when the stock moves at its implied volatility. The position is a bet that realized variance exceeds implied variance, not a riskless profit. --- Reading the Skew: Implied Vols From Four Quotes [medium, Options Pricing] --- https://quantvault.org/problem-3685-reading-the-skew-implied-vols-from-four-quotes.html Q: A stock trades at $S = 100$. Interest rates and dividends are zero, and all options are European with $T = 0.25$ years to expiry. You see these mid quotes: - 100-strike call: $3.99$ - 90-strike put: $1.45$ - 90-strike call: $11.45$ - 110-strike call: $0.48$ Use the Black-Scholes model, $C = S\,N(d_1) - K\,N(d_2)$ with $d_{1,2} = \dfrac{\ln(S/K) \pm \sigma^2 T/2}{\sigma\sqrt{T}}$ when $r = 0$. **(a)** What is the implied volatility of the 100-strike call? (With $r = 0$ and $K = S$ the inversion can be done by hand.) **(b)** What would the 90-strike put be worth under Black-Scholes at a flat volatility of $20\%$? **(c)** What is the implied volatility of the 90-strike put? **(d)** What implied volatility does the 90-strike call carry, and why? Together with the 110-strike call (implied volatility about $16\%$), what shape does the volatility curve have? A: **How to Think About It:** Each quote maps to exactly one volatility, because the Black-Scholes price is strictly increasing in $\sigma$ (vega is positive). With $r = 0$ and $K = S$ the map can be inverted with a normal table. Away from the money you bracket and interpolate (or take Newton steps). Comparing the implied vols across strikes is what "reading the skew" means. **Quick Estimate:** At the money, $C \approx 0.4\,\sigma S\sqrt{T}$, so $\sigma \approx 3.99/(0.4 \times 100 \times 0.5) \approx 0.20$. The 90 put is worth well under $1$ at $20\%$ but trades at $1.45$, so its implied vol is clearly higher, roughly in the mid-twenties. **Formal Solution:** *Step 1 -- At-the-money implied vol (a).* With $r = 0$ and $K = S$, $d_1 = \sigma\sqrt{T}/2$ and $d_2 = -\sigma\sqrt{T}/2$, so $$C = S\big(N(d_1) - N(-d_1)\big) = S\big(2N(\sigma\sqrt{T}/2) - 1\big).$$ Then $N(\sigma\sqrt{T}/2) = (1 + 3.99/100)/2 = 0.51995$, so $\sigma\sqrt{T}/2 = N^{-1}(0.51995) = 0.05003$ and $$\sigma = \frac{2 \times 0.05003}{\sqrt{0.25}} = 0.2001.$$ The 100-strike implied vol is about $20.0\%$. *Step 2 -- The 90 put at a flat 20% (b).* With $\sigma = 0.2$, $\sigma\sqrt{T} = 0.1$: $$d_1 = \frac{\ln(100/90) + 0.005}{0.1} = \frac{0.10536 + 0.005}{0.1} = 1.1036, \qquad d_2 = 1.0036.$$ $N(-d_1) = 0.13488$ and $N(-d_2) = 0.15778$, so $$P = 90 \times 0.15778 - 100 \times 0.13488 = 14.2006 - 13.4882 = 0.712.$$ The market's $1.45$ is about twice that: the downside put is priced at a much higher volatility than the at-the-money option. *Step 3 -- The 90 put's implied vol (c).* The put price increases with $\sigma$. At $25\%$ it is $1.3193$; at $26\%$ it is $1.4535$. The quote $1.45$ lies between them, and linear interpolation gives $$\sigma \approx 0.25 + 0.01 \times \frac{1.45 - 1.3193}{1.4535 - 1.3193} = 0.2597.$$ Bisection to full precision gives $0.25974$. A single Newton step from $20\%$ (vega $10.85$) overshoots to $26.8\%$; a second step corrects it. The 90-strike implied vol is about $26.0\%$. *Step 4 -- The 90 call (d).* With $r = q = 0$, put-call parity reads $C - P = S - K$ for every value of $\sigma$. The quotes satisfy $11.45 - 1.45 = 10 = 100 - 90$, so the 90 call priced at the put's implied vol comes out at exactly $11.45$: it carries the same $26.0\%$. If the two vols differed, parity would be violated and a riskless trade would exist. Skew is a function of strike and expiry, not of whether the option is a call or a put; in-the-money calls carry the same vol as the out-of-the-money puts at the same strike. *Step 5 -- The shape.* The implied vols at strikes $90$, $100$, $110$ are about $26\%$, $20\%$ and $16\%$ (the 110 call at $0.48$ inverts to $16.0\%$): a downward-sloping skew. Under Black-Scholes all three would be equal. The skew says the market's risk-neutral distribution has a fatter left tail than a lognormal with one volatility. **Answer:** (a) $\sigma \approx 0.200$ ($20.0\%$). (b) $0.712$. (c) $\sigma \approx 0.260$ ($25.97\%$). (d) The same $26.0\%$ as the 90 put, because put-call parity ($C - P = S - K$ at any $\sigma$) ties the two prices together; with the 110 call at about $16\%$ the curve is a downward-sloping skew ($26\%$, $20\%$, $16\%$ for strikes $90$, $100$, $110$). --- Constant 30% Volatility or Random Volatility With Mean 30%? [medium, Options Pricing] --- https://quantvault.org/problem-3686-constant-30-volatility-or-random-volatility-with-mean-30.html Q: You price a European call on a non-dividend stock two ways. In the first, the volatility is a constant $30\%$. In the second, the volatility is random: at time $0$ a value $\sigma$ is drawn from a distribution with mean $30\%$ (independent of the stock's Brownian motion), and the stock then follows geometric Brownian motion with that volatility until expiry, so the option price is the expectation of the Black-Scholes price over the draw of $\sigma$. Which of the two option prices is higher? Does the answer depend on the strike? Justify your answer using the shape of the Black-Scholes price as a function of $\sigma$. A: **How to Think About It:** Under the random-volatility model the price is the average of Black-Scholes prices over the draw of $\sigma$. Comparing an average of a function with the function of the average is Jensen's inequality, so the question reduces to the curvature of $c_{\text{BS}}$ in $\sigma$. Compute volga and read off its sign. **Quick Estimate:** Let $S = 100$, $r = 0$, $\tau = 1$, and let the random $\sigma$ be $20\%$ or $40\%$ with equal probability (mean $30\%$). At $K = 130$: constant-vol price $3.57$, random-vol price $\tfrac12(1.01 + 6.94) = 3.97$; random wins. At $K = 100$: constant $11.92$, random $\tfrac12(7.97 + 15.85) = 11.91$; constant wins by a hair. So the answer flips near the money. **Formal Solution:** *Step 1 -- Set-up.* Conditional on the drawn $\sigma$, the stock is a geometric Brownian motion with that volatility, so the conditional price is $c_{\text{BS}}(\sigma)$ and the unconditional price is $E[c_{\text{BS}}(\sigma)]$. The constant-volatility price is $c_{\text{BS}}(E[\sigma]) = c_{\text{BS}}(0.3)$. *Step 2 -- Jensen.* If $c_{\text{BS}}$ is convex in $\sigma$ over the support of the draw, then $E[c_{\text{BS}}(\sigma)] \ge c_{\text{BS}}(E[\sigma])$ and the random-volatility price is higher; if concave, the constant-volatility price is higher. *Step 3 -- Curvature: volga.* Vega is $\mathcal{V} = S\sqrt{\tau}N'(d_1)$. Differentiating again with respect to $\sigma$, using $\partial d_1/\partial\sigma = -d_2/\sigma$ and $N''(x) = -xN'(x)$, $$\frac{\partial^2 c}{\partial\sigma^2} = S\sqrt{\tau}N'(d_1)\cdot(-d_1)\cdot\left(-\frac{d_2}{\sigma}\right) = \mathcal{V}\,\frac{d_1 d_2}{\sigma}.$$ Since $\mathcal{V} > 0$, the price is convex in $\sigma$ where $d_1 d_2 > 0$ and concave where $d_1 d_2 d_2$, the product is negative only when $d_1 > 0 > d_2$, i.e. $$-\tfrac12\sigma^2\tau 0$) whenever $d_1 d_2 > 0$, and Jensen's inequality gives $E[c(\sigma)] > c(E[\sigma])$. The exception is a narrow band around the money where $d_1 > 0 > d_2$, the price is concave in $\sigma$, and the constant-volatility price is (slightly) higher. So the answer depends on the strike. --- Exchange Option: Pricing the Right to Swap One Stock for Another [hard, Options Pricing] --- https://quantvault.org/problem-3688-exchange-option-pricing-the-right-to-swap-one-stock-for-anot.html Q: An exchange option gives its holder the right, at maturity $T$, to exchange one share of stock 1 for one share of stock 2; its payoff is $\max(S_{2,T} - S_{1,T},\,0)$. Both stocks pay no dividends and follow geometric Brownian motions, $$\frac{dS_1}{S_1} = \mu_1\,dt + \sigma_1\,dW_1, \qquad \frac{dS_2}{S_2} = \mu_2\,dt + \sigma_2\,dW_2, \qquad dW_1\,dW_2 = \rho\,dt,$$ and the risk-free rate is $r$. Find the price of the exchange option today (time $0$), when the stock prices are $S_1$ and $S_2$. A: **How to Think About It:** The payoff is homogeneous of degree one in $(S_1, S_2)$, so the problem has one fewer dimension than it looks. Choose stock 1 as the numeraire: prices measured in shares of stock 1 are martingales under the corresponding measure, and the payoff in those units is a vanilla call on the ratio $S_2/S_1$ with strike $1$. Black-Scholes with zero rate does the rest. **Quick Estimate:** With $S_1 = S_2 = 100$, $\sigma_1 = 20\%$, $\sigma_2 = 30\%$, $\rho = 0.5$, $T = 1$: $\sigma_s = \sqrt{0.04 - 0.06 + 0.09} = \sqrt{0.07} = 0.265$. The option is at the money in ratio terms, so the at-the-money approximation gives $0.4\,\sigma_s S\sqrt{T} = 0.4 \times 0.265 \times 100 = 10.6$. The exact value is $10.52$. **Formal Solution:** *Step 1 -- Dynamics of the ratio.* Let $X = S_2/S_1$. By Ito's lemma, $d\ln X = d\ln S_2 - d\ln S_1$, and since $\ln S_i$ has diffusion $\sigma_i\,dW_i$, $$\text{Var}(d\ln X) = (\sigma_1^2 - 2\rho\sigma_1\sigma_2 + \sigma_2^2)\,dt \equiv \sigma_s^2\,dt.$$ So $X$ is a geometric Brownian motion with volatility $\sigma_s = \sqrt{\sigma_1^2 - 2\rho\sigma_1\sigma_2 + \sigma_2^2}$. *Step 2 -- Change of numeraire.* Take $S_1$ as the numeraire. Under the associated measure $Q^{1}$, every traded asset divided by $S_1$ is a martingale; in particular $X = S_2/S_1$ has zero drift, and the option's value satisfies $$\frac{V_0}{S_1} = E^{Q^1}\!\left[\frac{\max(S_{2,T} - S_{1,T}, 0)}{S_{1,T}}\right] = E^{Q^1}\!\left[\max(X_T - 1, 0)\right].$$ (Equivalently: the payoff is homogeneous of degree one, so $V(S_1, S_2, t) = S_1\,g(S_2/S_1, t)$ and $g$ satisfies the Black-Scholes PDE with rate $0$ and volatility $\sigma_s$.) *Step 3 -- Black-Scholes on the ratio.* $E^{Q^1}[\max(X_T - 1, 0)]$ is the undiscounted Black-Scholes price of a call with spot $X_0 = S_2/S_1$, strike $1$, rate $0$ and volatility $\sigma_s$: $$E^{Q^1}[\max(X_T - 1,0)] = X_0\,N(d_1) - N(d_2), \qquad d_1 = \frac{\ln(S_2/S_1) + \tfrac12\sigma_s^2 T}{\sigma_s\sqrt{T}}, \quad d_2 = d_1 - \sigma_s\sqrt{T}.$$ *Step 4 -- Convert back to dollars.* Multiply by $S_1$: $$V_0 = S_2\,N(d_1) - S_1\,N(d_2).$$ Neither $r$ nor the real-world drifts $\mu_i$ appear: both assets are traded, so their risk-neutral drifts are $r$ and cancel in the ratio. *Step 5 -- Example and sanity checks.* $S_1 = S_2 = 100$, $\sigma_1 = 0.2$, $\sigma_2 = 0.3$, $\rho = 0.5$, $T = 1$: $\sigma_s = 0.2646$, $d_1 = 0.1323$, $d_2 = -0.1323$, $V_0 = 100\,[N(0.1323) - N(-0.1323)] = 10.52$. If $\rho \to 1$ and $\sigma_1 = \sigma_2$, then $\sigma_s \to 0$ and the option is worth $\max(S_2 - S_1, 0)$ today (the stocks move in lockstep). If $S_1$ is replaced by a constant $K$ (so $\sigma_1 = 0$ and $S_1$ is a bond worth $Ke^{-rT}$), the formula collapses to Black-Scholes. **Answer:** $V_0 = S_2\,N(d_1) - S_1\,N(d_2)$ with $\sigma_s = \sqrt{\sigma_1^2 - 2\rho\sigma_1\sigma_2 + \sigma_2^2}$, $d_1 = \dfrac{\ln(S_2/S_1) + \sigma_s^2 T/2}{\sigma_s\sqrt{T}}$, $d_2 = d_1 - \sigma_s\sqrt{T}$ (Margrabe's formula). For $S_1 = S_2 = 100$, $\sigma_1 = 20\%$, $\sigma_2 = 30\%$, $\rho = 0.5$, $T = 1$ the price is $10.52$. --- Swap Two Integers Without a Temporary Variable [easy, Coding] --- https://quantvault.org/problem-3689-swap-two-integers-without-a-temporary-variable.html Q: Two integer variables $i$ and $j$ hold values. Write code that swaps their values **without using any extra storage** (no temporary variable, no third register, no stack). Give at least two different methods, and discuss the pitfalls of each (overflow, aliasing, and anything else an interviewer would probe). A: **How to Think About It:** A swap needs to remember one value while the other is overwritten. Without a temporary, the memory has to come from the two variables themselves: fold both values into one variable with an invertible operation, then peel them apart one at a time. **Approach:** Use addition/subtraction, or XOR. Both are three-statement sequences; verify each by tracking the invariant that the pair $(i, j)$ always determines the original values $(a, b)$. **Formal Solution:** *Step 1 -- Arithmetic swap.* Start with $i = a$, $j = b$. $$i \leftarrow i + j = a + b, \qquad j \leftarrow i - j = (a + b) - b = a, \qquad i \leftarrow i - j = (a + b) - a = b.$$ Pitfall: in a fixed-width integer type, $a + b$ can overflow. In C/C++ signed overflow is undefined behaviour; with unsigned (or two's-complement wraparound) arithmetic the method still works because addition and subtraction modulo $2^{w}$ are exact inverses. Multiplication/division variants ($i \leftarrow ij$, $j \leftarrow i/j$, $i \leftarrow i/j$) are worse: they fail when either value is $0$ and overflow far sooner. *Step 2 -- XOR swap.* Using $x \oplus x = 0$, $x \oplus 0 = x$, associativity and commutativity: $$i \leftarrow i \oplus j = a \oplus b, \qquad j \leftarrow i \oplus j = (a \oplus b) \oplus b = a, \qquad i \leftarrow i \oplus j = (a \oplus b) \oplus a = b.$$ No overflow is possible since XOR is bitwise. Pitfall: if $i$ and $j$ alias the same location (for example `swap(&arr[k], &arr[k])`), the first step sets that location to $a \oplus a = 0$ and the value is destroyed; the arithmetic version fails the same way: $i \leftarrow i + j$ stores $2a$, then $j \leftarrow i - j$ stores $2a - 2a = 0$, and $i \leftarrow i - j$ leaves $0$. Any in-place swap that combines the two operands is destroyed by aliasing, so guard with `if (i != j)` (compare the addresses) or just use a temporary. *Step 3 -- Complexity.* Both methods run in $O(1)$ time and $O(1)$ extra space (in fact zero extra words). *Step 4 -- Practical note.* On modern hardware a plain temporary-variable swap is at least as fast: the compiler keeps the temporary in a register, and the XOR sequence creates a dependency chain of three instructions. The trick matters for the reasoning, and occasionally in register-starved embedded code. ```python def swap_arith(i, j): i = i + j j = i - j # old i i = i - j # old j return i, j def swap_xor(i, j): i ^= j j ^= i # old i i ^= j # old j return i, j assert swap_arith(3, 5) == (5, 3) and swap_xor(-7, 12) == (12, -7) ``` **Answer:** Either `i = i + j; j = i - j; i = i - j` (beware overflow in fixed-width integers) or `i ^= j; j ^= i; i ^= j` (no overflow, but zeroes the value if both operands are the same memory location). Both are $O(1)$ with no extra storage. --- Three Ways to Sort n Numbers and What They Cost [medium, Coding] --- https://quantvault.org/problem-3690-three-ways-to-sort-n-numbers-and-what-they-cost.html Q: You are given $n$ distinct numbers in an array. Describe **three** different algorithms that sort them into increasing order, and analyze the time complexity of each: give the worst-case and average-case running times, and explain where each bound comes from (recurrences, comparison counts, or probabilistic arguments). A: **How to Think About It:** Every comparison-based sort is a sequence of comparisons, so count them. The three classic strategies are: grow a sorted prefix one element at a time (insertion sort), split the array in half and merge (merge sort), and split around a pivot value (quicksort). The first two have fixed structure so their counts come from direct summation or a recurrence; quicksort's structure depends on the pivots, so its average case needs a probabilistic argument. **Approach:** State each algorithm, give code, then derive the bounds: sums of inversions for insertion sort, the Master theorem for merge sort, and pairwise comparison probabilities for quicksort. **Formal Solution:** **Algorithm 1 -- Insertion sort.** For $i = 2, \dots, n$, take $A[i]$ and shift it left past every larger element in the already sorted prefix $A[1..i-1]$. ```python def insertion_sort(a): for i in range(1, len(a)): key, j = a[i], i - 1 while j >= 0 and a[j] > key: a[j + 1] = a[j] j -= 1 a[j + 1] = key return a ``` *Analysis.* Inserting element $i$ costs up to $i - 1$ comparisons, so the worst case (reverse-sorted input) is $\sum_{i=2}^n (i - 1) = n(n-1)/2 = \Theta(n^2)$. Each shift removes exactly one inversion (a pair $p A[q]$), and a uniformly random permutation has $\binom{n}{2}/2 = n(n-1)/4$ inversions on average, so the average case is also $\Theta(n^2)$; the best case (already sorted) is $\Theta(n)$. Space is $O(1)$. **Algorithm 2 -- Merge sort.** Split the array into halves, sort each recursively, and merge the two sorted halves in linear time. ```python def merge_sort(a): if len(a) p])) ``` *Worst case.* If the pivot is always the smallest or largest element (for example, the first element on already-sorted input), one side is empty and $T(n) = T(n-1) + (n-1)$, so $T(n) = n(n-1)/2 = \Theta(n^2)$. *Average case.* Label the elements by rank $z_1 < z_2 < \dots < z_n$. Two elements $z_p$ and $z_q$ ($p < q$) are compared at most once, and only if one of them is chosen as pivot before any other element of $\{z_p, \dots, z_q\}$ is; once a pivot strictly between them is chosen they land on different sides and never meet. With random pivots (or random input), each element of that range is equally likely to be the first pivot chosen from it, so $$P(z_p \text{ and } z_q \text{ compared}) = \frac{2}{q - p + 1}.$$ By linearity of expectation the expected number of comparisons is $$E[C_n] = \sum_{p=1}^{n-1}\sum_{q=p+1}^{n}\frac{2}{q - p + 1} = \sum_{p=1}^{n-1}\sum_{k=1}^{n-p}\frac{2}{k+1} \le 2n\,H_n = O(n\log n),$$ and more precisely $E[C_n] = 2(n+1)H_n - 4n \approx 2n\ln n \approx 1.39\,n\log_2 n$. Equivalently, the recurrence $T(n) = (n-1) + \frac{2}{n}\sum_{k=0}^{n-1}T(k)$ solves to the same value. In-place partitioning gives $O(\log n)$ expected extra space; quicksort is not stable. *Empirical check* (random inputs of size $n = 5000$): insertion sort made about $6.3\times 10^6$ comparisons ($\approx n^2/4$), merge sort about $55{,}000$ (a little under $n\log_2 n = 61{,}000$, because a merge stops comparing once one side runs out), and quicksort about $70{,}000$, in line with the exact average $2(n+1)H_n - 4n = 71{,}000$; the leading term $2n\ln n = 85{,}000$ overstates it at this size. **Answer:** Insertion sort: worst and average $\Theta(n^2)$, best $\Theta(n)$, in place. Merge sort: $\Theta(n\log n)$ in every case from $T(n) = 2T(n/2) + \Theta(n)$, but $\Theta(n)$ extra space. Quicksort: worst case $\Theta(n^2)$ with a bad pivot sequence, average $\Theta(n\log n)$ (about $2n\ln n$ comparisons) because elements of rank $p$ and $q$ are compared with probability $2/(q-p+1)$. --- Shuffling 52 Cards So Every Order Is Equally Likely [medium, Coding] --- https://quantvault.org/problem-3691-shuffling-52-cards-so-every-order-is-equally-likely.html Q: You have a deck of $52$ cards and a random number generator that produces uniform random numbers. Design an algorithm that shuffles the deck so that **every one of the $52!$ orderings is equally likely**. Analyze the running time, and prove that your algorithm is uniform. Also explain why the following "obvious" method is wrong: for $i = 1, \dots, n$, swap card $i$ with a card chosen uniformly at random from all $n$ positions. A: **How to Think About It:** A uniform shuffle must give each of the $n!$ orders probability exactly $1/n!$. Think of building the order sequentially: if each slot is filled by a uniform choice among the remaining cards, the probabilities multiply to $1/n!$. The alternative is to reduce shuffling to sorting random keys, which is uniform because i.i.d. continuous keys are equally likely to come out in any order. **Approach:** Present both correct methods, prove uniformity by induction or by the product rule, then show the naive method fails by a counting (divisibility) argument. **Formal Solution:** **Method 1 -- Random keys and sort.** Draw $n$ i.i.d. $U(0,1)$ keys $u_1, \dots, u_n$, attach $u_k$ to card $k$, and sort the cards by key. With probability $1$ the keys are distinct, and by exchangeability every one of the $n!$ relative orderings of $(u_1, \dots, u_n)$ has the same probability, so each deck order has probability $1/n!$. Cost: $n$ random numbers plus a sort, $\Theta(n\log n)$ time and $\Theta(n)$ space. **Method 2 -- Fisher-Yates / Knuth shuffle.** ```python import random def knuth_shuffle(a): n = len(a) for i in range(n - 1): # positions 0 .. n-2 j = random.randrange(i, n) # uniform over i .. n-1 (inclusive) a[i], a[j] = a[j], a[i] return a ``` *Uniformity, product argument.* After step $i$, position $i$ holds a card chosen uniformly among the $n - i$ cards that were still in positions $i, \dots, n-1$ (0-indexed), independently of the earlier choices. For any target order $(c_0, c_1, \dots, c_{n-1})$, the probability that step $0$ places $c_0$ is $1/n$, that step $1$ then places $c_1$ is $1/(n-1)$, and so on, giving $\prod_{k=0}^{n-1}\frac{1}{n-k} = \frac{1}{n!}$. *Uniformity, induction.* Claim: after processing position $i$, the multiset in positions $i+1, \dots, n-1$ is the set of unplaced cards, and the prefix $(a_0, \dots, a_i)$ is a uniformly random ordered selection of $i+1$ cards (each of the $n(n-1)\cdots(n-i)$ possibilities has equal probability). Base case $i = 0$: $a_0$ is uniform over $n$ cards. Step: given any prefix of length $i$, step $i$ appends each remaining card with probability $1/(n-i)$, so every prefix of length $i+1$ has probability $\frac{1}{n(n-1)\cdots(n-i)}$. At $i = n-1$ this is $1/n!$. *Cost.* $n - 1$ random numbers and swaps: $\Theta(n)$ time, $O(1)$ extra space, in place. **Why the naive method is biased.** The procedure "for $i = 1..n$ swap card $i$ with a uniformly random position in $1..n$" makes $n$ independent choices with $n$ options each, so it has $n^n$ equally likely execution paths. These paths map onto $n!$ orderings, and each ordering's probability is (number of paths producing it)$/n^n$. For uniformity every ordering would need exactly $n^n/n!$ paths, but $n!$ does not divide $n^n$ for $n \ge 3$ (for example $27/6$ is not an integer), so uniformity is impossible. Concretely, for $n = 3$ the orders $(1,2,3)$, $(3,1,2)$, $(3,2,1)$ each get probability $4/27$, and $(1,3,2)$, $(2,1,3)$, $(2,3,1)$ get $5/27$ (enumerate the $27$ paths to check). A simulation of $600{,}000$ shuffles of four cards gave chi-square statistics of $20$ (Knuth, consistent with uniform on $23$ degrees of freedom) versus $18{,}000$ (naive). **Answer:** Fisher-Yates / Knuth shuffle: for $i = 1, \dots, n-1$ swap $A[i]$ with $A[j]$ where $j$ is uniform on $\{i, \dots, n\}$; each order has probability $\frac1n\cdot\frac1{n-1}\cdots\frac11 = \frac{1}{n!}$, in $\Theta(n)$ time and $O(1)$ space. Alternatively assign i.i.d. uniform keys and sort, $\Theta(n\log n)$. Swapping each position with a uniformly random position among all $n$ is biased because $n!$ does not divide $n^n$. --- Pick a Uniformly Random Character From a Stream of Unknown Length [medium, Coding] --- https://quantvault.org/problem-3692-pick-a-uniformly-random-character-from-a-stream-of-unknown-l.html Q: A file contains a sequence of characters. You may read the file only once, sequentially, one character at a time, and you do not know its length in advance (and cannot afford to store the whole file). When you reach the end, you must output a single character chosen so that **each character in the file is equally likely** to be the one selected. Design an algorithm that uses $O(1)$ memory, and prove that it is uniform. A: **How to Think About It:** You cannot wait until the end because you do not know when the end is and cannot store everything; you cannot decide early because later characters deserve an equal chance. The resolution is to always hold one candidate and let each new character overthrow it with a carefully chosen probability, so that uniformity holds after every prefix. Then the unknown stopping point does not matter. **Approach:** Reservoir sampling with a reservoir of size one; prove uniformity by induction on the number of characters read. **Formal Solution:** *Step 1 -- Algorithm.* Maintain a counter $k$ and a single stored character `pick`. - Read the first character; set `pick` to it ($k = 1$). - When the $k$-th character $x_k$ arrives ($k \ge 2$): with probability $1/k$ set `pick` $= x_k$; with probability $1 - 1/k$ leave `pick` unchanged. - At end of file, output `pick`. ```python import random def reservoir_pick(stream): pick = None for k, ch in enumerate(stream, start=1): if random.randrange(k) == 0: # probability 1/k pick = ch return pick ``` Memory is $O(1)$ (one character and one counter); time is $O(m)$ for a file of $m$ characters, one random number per character. *Step 2 -- Uniformity by induction.* Claim: after the first $k$ characters have been processed, $P(\text{pick} = x_i) = 1/k$ for every $i \le k$. - Base case $k = 1$: the pick is $x_1$ with probability $1$. - Inductive step: assume the claim for $k$. When $x_{k+1}$ arrives, it becomes the pick with probability $1/(k+1)$. For $i \le k$, $x_i$ remains the pick if it was the pick before (probability $1/k$ by hypothesis) and $x_{k+1}$ is rejected (probability $k/(k+1)$, independent of the past): $$P(\text{pick} = x_i) = \frac1k\cdot\frac{k}{k+1} = \frac{1}{k+1}.$$ So all $k + 1$ characters have probability $1/(k+1)$. *Step 3 -- Conclusion.* If the file has $m$ characters, the output is $x_i$ with probability $1/m$ for each $i$, regardless of $m$, and the algorithm never needed to know $m$. Direct computation confirms this: $P(\text{pick} = x_i) = \frac1i\prod_{k=i+1}^{m}\left(1 - \frac1k\right) = \frac1i\cdot\frac{i}{m} = \frac1m$ (telescoping product). *Step 4 -- Generalization.* To pick $s$ characters uniformly without replacement, store the first $s$; when the $k$-th ($k > s$) arrives, with probability $s/k$ replace a uniformly chosen stored item. The same induction shows every character ends up in the reservoir with probability $s/m$ and every $s$-subset is equally likely. A simulation of the single-pick algorithm on a 10-character stream ($500{,}000$ runs) gave counts between $49{,}500$ and $50{,}400$ per character, consistent with $50{,}000$. **Answer:** Reservoir sampling: keep one candidate; when the $k$-th character arrives, replace the candidate with it with probability $1/k$. By induction each of the first $k$ characters is the candidate with probability $1/k$, so at end of file (length $m$) every character is chosen with probability $1/m$, using $O(1)$ memory and $O(m)$ time. --- Is This Integer a Power of Two? One Bitwise Operation [easy, Coding] --- https://quantvault.org/problem-3693-is-this-integer-a-power-of-two-one-bitwise-operation.html Q: Given an integer $x$, determine whether it is a power of $2$ (that is, whether $x = 2^n$ for some integer $n \ge 0$). Find a method that uses $O(1)$ time and no loops, explain why it works, and handle the edge cases $x = 0$ and $x < 0$. A: **How to Think About It:** Powers of two are the integers whose binary representation contains a single $1$. So the question is really "does $x$ have exactly one set bit?", and there is a classic bit trick that removes the lowest set bit in one operation. **Approach:** Show that $x \,\&\, (x - 1)$ clears the lowest set bit, so it is zero if and only if $x$ has at most one set bit; add the guard $x > 0$. **Formal Solution:** *Step 1 -- Binary structure of $x - 1$.* Let $x > 0$ have lowest set bit at position $n$, so $x = (\ldots b\,1\,0\ldots0)_2$ with $n$ trailing zeros. Subtracting $1$ borrows through the trailing zeros: $x - 1 = (\ldots b\,0\,1\ldots1)_2$. All bits above position $n$ are unchanged, bit $n$ becomes $0$, and the $n$ bits below become $1$. *Step 2 -- Effect of the AND.* Bits above $n$: identical in $x$ and $x - 1$, so they survive. Bit $n$: $1$ in $x$, $0$ in $x - 1$, cleared. Bits below $n$: $0$ in $x$, cleared. Hence $x \,\&\, (x - 1)$ equals $x$ with its lowest set bit removed. *Step 3 -- Characterization.* The result is $0$ if and only if $x$ had no set bits above its lowest one, i.e. exactly one set bit, i.e. $x = 2^n$. So for $x > 0$: $x$ is a power of two $\iff x \,\&\, (x - 1) = 0$. *Step 4 -- Edge cases.* $x = 0$: $0 \,\&\, (-1) = 0$, but $0$ is not a power of two, so the test must be `x > 0 and (x & (x - 1)) == 0`. Negative $x$: in two's complement $-2^n$ is not a power of two by the usual definition, and the guard excludes it (note $-2^{31}$ in 32-bit arithmetic would otherwise pass the bitwise test, since `INT_MIN & (INT_MIN - 1)` wraps to `INT_MIN & INT_MAX = 0`). Also `x - 1` underflows for `INT_MIN`, another reason to check the sign first. *Step 5 -- Alternatives.* `x & -x` isolates the lowest set bit, so `x > 0 and (x & -x) == x` is equivalent. A population count (`bin(x).count('1') == 1` in Python, `__builtin_popcount(x) == 1` in GCC) also works. Any loop over bits is $O(\log x)$ instead of $O(1)$. ```python def is_power_of_two(x: int) -> bool: return x > 0 and (x & (x - 1)) == 0 assert [is_power_of_two(v) for v in (0, 1, 2, 3, 4, 6, 8, 1024, 1025, -4)] == \ [False, True, True, False, True, False, True, True, False, False] ``` The function was checked against a popcount reference for every integer from $-5$ to $10^5$ and for $2^{62}$ and $2^{62} + 1$. **Answer:** $x$ is a power of two if and only if $x > 0$ and $x \,\&\, (x - 1) = 0$, because $2^n$ has a single set bit while $2^n - 1$ has exactly the $n$ bits below it set, so the two share no bits; any other positive integer keeps its higher bits after clearing the lowest one. $O(1)$ time; guard against $x = 0$ and negatives. --- Multiply an Integer by 7 Without the Multiplication Operator [easy, Coding] --- https://quantvault.org/problem-3694-multiply-an-integer-by-7-without-the-multiplication-operator.html Q: Give a fast way to compute $7x$ for an integer $x$ **without using the multiplication operator** (no `*`, and no loop that adds $x$ seven times). Explain why it works and what can go wrong. A: **How to Think About It:** Multiplication by a power of two is free (a shift), and $7$ is one less than a power of two. So build $8x$ with a shift and subtract $x$ once. **Approach:** Use $7x = 8x - x = (x \ll 3) - x$, or equivalently $(x \ll 2) + (x \ll 1) + x$; verify on a range of integers and analyze overflow. **Formal Solution:** *Step 1 -- Shifts as multiplication.* For a nonnegative integer $x = \sum_i b_i 2^i$, the left shift $x \ll k$ produces $\sum_i b_i 2^{i+k} = 2^k x$. In two's complement the same identity holds for negative values as long as the result fits in the type (arithmetically; in C the left shift of a negative signed value is formally undefined, so use unsigned or a wider type). *Step 2 -- Decompose $7$.* $7 = 2^3 - 1$, so $$7x = 8x - x = (x \ll 3) - x.$$ This costs one shift and one subtraction. The alternative $7 = 4 + 2 + 1$ gives $(x \ll 2) + (x \ll 1) + x$, two shifts and two additions, slightly more work but with a smaller largest intermediate ($4x$ rather than $8x$). *Step 3 -- Correctness check.* $(x \ll 3) - x = 7x$ was verified for every integer $x \in [-10^4, 10^4]$ in Python (arbitrary-precision integers, so no overflow). *Step 4 -- Overflow.* In a $w$-bit type the intermediate $8x$ needs one more bit than $7x$ (roughly): for signed 32-bit integers, $x = 2^{29}$ gives $7x = 3{,}758{,}096{,}384 > 2^{31} - 1$, so the true product overflows anyway; but there is also a range where $7x$ fits and $8x$ does not, for example $x = 2^{28} + 1$: $7x = 1{,}879{,}048{,}199$ fits, while $8x = 2^{31} + 8$ does not. In unsigned (wraparound) arithmetic the final answer is still correct modulo $2^w$ because subtraction undoes the wrap; in signed arithmetic in C it is undefined behaviour. Use a wider intermediate type or `x * 7` and let the compiler do the strength reduction. ```python def times7(x: int) -> int: return (x << 3) - x assert all(times7(x) == 7 * x for x in range(-10_000, 10_001)) ``` **Answer:** $7x = (x \ll 3) - x$: shifting left by $3$ multiplies by $8$, then subtract $x$ once. Equivalently $(x \ll 2) + (x \ll 1) + x$. Watch for overflow of the intermediate $8x$ in fixed-width types (and undefined behaviour when shifting negative signed values in C). --- Explicit Finite Differences for a Call: Stability, Cost, Convergence [medium, Options Pricing] --- https://quantvault.org/problem-3695-finite-difference-methods-for-pricing-derivatives.html Q: You price a European call with $S_0 = K = 100$, $r = 0.05$, $\sigma = 0.2$, $T = 1$ (no dividends) by explicit finite differences. With $k = 2r/\sigma^2$, the change of variables $S = Ke^x$, $\tau = \sigma^2(T - t)/2$, $V = K e^{\alpha x + \beta\tau}u(x, \tau)$, $\alpha = -(k - 1)/2$, $\beta = -(k + 1)^2/4$ turns the Black-Scholes PDE into the heat equation $u_\tau = u_{xx}$ for $\tau \in [0, \sigma^2 T/2]$, with the payoff as initial condition. You truncate to $x \in [-3, 3]$ with $J$ equal space steps, use $N$ equal time steps, and march $$u_j^{n+1} = a\,u_{j-1}^n + (1 - 2a)\,u_j^n + a\,u_{j+1}^n, \qquad a = \frac{\delta\tau}{\delta x^2}.$$ **(a)** What Black-Scholes price should the grid reproduce? **(b)** With $J = 600$, what is the smallest number of time steps $N$ for which the scheme is stable? **(c)** Keeping $a$ fixed, you refine to $J = 1200$. By about what factor does the total number of grid-point updates grow? **(d)** At $a \approx 0.45$ the grid gives $P_{600} = 10.44992$ and $P_{1200} = 10.45042$. What do these numbers say about the error, and what should a run with $J = 2400$ give? A: **How to Think About It:** A finite difference price is only useful if you know how far it is from the truth and what it costs to get closer. Three facts settle that for the explicit scheme: the stability bound $a \le 1/2$ fixes the minimum number of time steps, refining at fixed $a$ costs $2 \times 4 = 8$ times the work, and the error at fixed $a$ is $O(\delta x^2)$. The Black-Scholes formula gives the exact answer to measure against. **Quick Estimate:** At the money the call is roughly $0.4\,\sigma S\sqrt{T}$ plus about half the rate carry, $0.4 \times 20 + 0.5 \times 100(1 - e^{-0.05}) \approx 8 + 2.4 \approx 10.4$. With $\delta x = 0.01$, $a \le 1/2$ means $\delta\tau \le 5 \times 10^{-5}$ over a total time of $0.02$: about $400$ steps. **Formal Solution:** *Step 1 -- The benchmark (a).* $d_1 = (0.05 + 0.02)/0.2 = 0.35$, $d_2 = 0.15$, $N(d_1) = 0.63683$, $N(d_2) = 0.55962$: $$C = 100 \times 0.63683 - 100e^{-0.05} \times 0.55962 = 63.683 - 53.232 = 10.4506.$$ *Step 2 -- Stability (b).* The explicit update multiplies a Fourier mode by $1 - 4a\sin^2(\theta/2)$, which stays in $[-1, 1]$ for all $\theta$ only if $a \le 1/2$ (equivalently, all three weights are nonnegative). Here $\delta x = 6/600 = 0.01$ and $\tau_{\max} = \sigma^2 T/2 = 0.02$, so $$a = \frac{0.02/N}{0.01^2} = \frac{200}{N} \le \frac12 \iff N \ge 400.$$ At $N = 400$ the run is stable ($10.4454$, less accurate than at $a = 0.45$); at $N = 300$ ($a = 0.67$) it explodes to about $-2 \times 10^{64}$. *Step 3 -- Cost of refinement (c).* Doubling $J$ halves $\delta x$. Keeping $a = \delta\tau/\delta x^2$ fixed then needs $\delta\tau$ cut by $4$, so $N$ grows by $4$ (from $445$ to $1778$ at $a = 0.45$). The number of node updates is about $J \times N$, so it grows by $2 \times 4 = 8$ (the exact ratio of $(J - 1)N$ is $7.998$). *Step 4 -- Error and extrapolation (d).* Against $10.45058$, the errors are $P_{600} - C = -0.00066$ and $P_{1200} - C = -0.00016$, a ratio of about $4$. That is what $O(\delta x^2)$ predicts: at fixed $a$, $\delta\tau = a\,\delta x^2$, so the first-order time error is also $O(\delta x^2)$. The next halving should cut the error by another $4$: $$P_{2400} \approx 10.45058 - \frac{0.00016}{4} \approx 10.45054,$$ and the run gives exactly $10.45054$. Writing $P_J \approx C + c\,\delta x^2$ and eliminating $c$ gives Richardson extrapolation, $$\frac{4P_{1200} - P_{600}}{3} = \frac{41.80168 - 10.44992}{3} = 10.45059,$$ within $10^{-5}$ of Black-Scholes at no extra cost. Truncating $x$ to $[-3, 3]$ is not the issue: widening to $[-4, 4]$ at the same $\delta x$ changes the price by less than $10^{-12}$. The scheme in code: ```python import numpy as np def bs_call_explicit_fd(S0, K, r, sigma, T, J=600, xmin=-3.0, xmax=3.0, a=0.45): k = 2 * r / sigma**2 alpha, beta = -(k - 1) / 2, -(k + 1)**2 / 4 dx = (xmax - xmin) / J tau_max = sigma**2 * T / 2 N = int(np.ceil(tau_max / (a * dx**2))); dt = tau_max / N; a = dt / dx**2 x = np.linspace(xmin, xmax, J + 1) u = np.maximum(np.exp((k + 1) / 2 * x) - np.exp((k - 1) / 2 * x), 0.0) # payoff at tau = 0 tau = 0.0 for _ in range(N): tau += dt u[1:-1] = a * u[:-2] + (1 - 2 * a) * u[1:-1] + a * u[2:] u[0] = 0.0 u[-1] = np.exp((k + 1) / 2 * xmax + (k + 1)**2 / 4 * tau) - np.exp((k - 1) / 2 * xmax + (k - 1)**2 / 4 * tau) V = K * np.exp(alpha * x + beta * tau) * u return np.interp(np.log(S0 / K), x, V) # bs_call_explicit_fd(100, 100, 0.05, 0.2, 1.0, J=600) -> 10.44992 (N = 445) # bs_call_explicit_fd(100, 100, 0.05, 0.2, 1.0, J=1200) -> 10.45042 (N = 1778) # bs_call_explicit_fd(100, 100, 0.05, 0.2, 1.0, J=2400) -> 10.45054 (N = 7112); Black-Scholes: 10.45058 ``` **Answer:** (a) $10.4506$. (b) $N = 400$ (need $a = 200/N \le 1/2$). (c) About $8$ times the work ($2\times$ the nodes, $4\times$ the steps). (d) The error falls by about $4$ per halving of $\delta x$ ($-0.00066$ to $-0.00016$), as $O(\delta x^2)$ predicts at fixed $a$, so $J = 2400$ should give about $10.45054$; Richardson extrapolation $(4P_{1200} - P_{600})/3 = 10.45059$ already matches Black-Scholes. --- Explicit Finite Differences: Too Many Time Steps or Too Many Space Steps? [medium, Options Pricing] --- https://quantvault.org/problem-3696-explicit-finite-differences-too-many-time-steps-or-too-many-.html Q: You are solving a parabolic PDE, say the heat equation $u_\tau = u_{xx}$ obtained from the Black-Scholes equation, with the **explicit** finite difference scheme. Which is the worse mistake: using too many time steps, or using too many space steps? Explain, and state the precise condition involved. Does the same concern apply to the implicit scheme? A: **How to Think About It:** Both errors add work, but only one can destroy the answer. Stability of the explicit scheme depends on the *ratio* $a = \delta\tau/\delta x^2$. More time steps shrink $\delta\tau$ and hence $a$: safe. More space steps shrink $\delta x$ and grow $a$ like $1/\delta x^2$: eventually unstable. **Quick Estimate:** Suppose you use $N = 400$ time steps on $[0, 0.1]$ ($\delta\tau = 2.5 \times 10^{-4}$) and start with $J = 20$ space intervals on $[0, 1]$: $a = 2.5\times10^{-4}/(0.05)^2 = 0.1$, fine. Refine to $J = 50$: $a = 2.5\times10^{-4}/(0.02)^2 = 0.625 > 0.5$, and the computed solution reaches $10^{53}$ instead of $O(1)$. Conversely, with $J = 20$ fixed, going from $N = 100$ ($a = 0.4$, error $10^{-3}$) to $N = 10{,}000$ ($a = 0.004$, error $7\times10^{-4}$) just costs a hundred times more work. **Formal Solution:** *Step 1 -- The scheme and its weights.* The explicit scheme is $$u_j^{n+1} = a\,u_{j-1}^n + (1 - 2a)\,u_j^n + a\,u_{j+1}^n, \qquad a = \frac{\delta\tau}{\delta x^2}.$$ If $a \le 1/2$ all three weights are nonnegative and sum to $1$, so each new value is a convex combination of old values and $\max_j |u_j^{n}|$ can never grow (a discrete maximum principle). If $a > 1/2$ the middle weight is negative and this argument fails. *Step 2 -- Von Neumann stability analysis.* Insert a Fourier mode $u_j^n = g^n e^{i\theta j}$: $$g = 1 + a\left(e^{i\theta} - 2 + e^{-i\theta}\right) = 1 - 2a(1 - \cos\theta) = 1 - 4a\sin^2(\theta/2).$$ The mode is amplified unless $|g| \le 1$ for every $\theta$. The worst case is $\theta = \pi$ (the sawtooth mode), where $g = 1 - 4a$; requiring $g \ge -1$ gives $$a = \frac{\delta\tau}{\delta x^2} \le \frac12.$$ For $a > 1/2$ the sawtooth mode grows by $|1 - 4a| > 1$ per step: at $a = 0.625$ that is a factor $1.5$ per step, about $10^{70}$ over $400$ steps. The smooth initial data contains the sawtooth only at rounding level (about $10^{-16}$), so the computed solution reaches about $10^{53}$ rather than $10^{70}$; either way it is useless. *Step 3 -- Time steps versus space steps.* Holding $\delta x$ fixed and adding time steps reduces $\delta\tau$, so $a$ falls and the condition is satisfied more comfortably; the only cost is run time (and slightly more accumulated rounding). Holding $\delta\tau$ fixed and adding space steps raises $a \propto 1/\delta x^2$; halving $\delta x$ quadruples $a$, so a modest refinement crosses the threshold and the scheme diverges. To refine the space grid safely you must refine time quadratically, $\delta\tau \propto \delta x^2$, which makes fine explicit grids expensive. Hence too many space steps is the worse mistake. *Step 4 -- Implicit and Crank-Nicolson.* For the implicit scheme the amplification factor is $g = 1/(1 + 4a\sin^2(\theta/2))$, which satisfies $0 0$; for Crank-Nicolson $g = (1 - 2a\sin^2(\theta/2))/(1 + 2a\sin^2(\theta/2))$, with $|g| \le 1$ for every $a$. Both are unconditionally stable, so the time and space steps can be chosen independently for accuracy alone. Numerically, on the grid that broke the explicit scheme ($J = 50$, $400$ time steps, $a = 0.625$), the implicit and Crank-Nicolson errors were $6\times10^{-4}$ and $1\times10^{-4}$. **Answer:** Too many space steps is worse. The explicit scheme is stable only if $a = \delta\tau/\delta x^2 \le 1/2$ (all weights $a$, $1 - 2a$, $a$ nonnegative; von Neumann factor $1 - 4a\sin^2(\theta/2)$). Extra time steps shrink $a$ and only waste computation, whereas shrinking $\delta x$ with $\delta\tau$ fixed raises $a$ quadratically and makes the solution blow up. Implicit and Crank-Nicolson schemes are unconditionally stable, so the concern does not apply to them.