Skip to content
  • Overview
  • Curriculum
    • FLUMental maths and numerical fluency
    • MKTMarkets and products
    • CSData structures and algorithms
      • 1Complexity

        • Complexity: reading it off, and deriving it
      • 2Linear structures

        • Linear structures: arrays, hash maps and monotonic stacks
      • 3Trees and heaps

        • Trees and heaps: BSTs, priority queues and range queries
      • 4Graphs

        • Graphs: traversal, shortest paths and union–find
      • 5Core techniques

        • Core techniques: binary search on the answer, two pointers, sliding windows
      • 6Dynamic programming

        • Dynamic programming, and why it is the same as an EV recursion
      • 7Bit manipulation and number theory

        • Bit manipulation and modular arithmetic
    • PYPython and data for quants
    • NUMNumerical methods
    • SYSSystems and low latency

Practise

  • Question bank
  • Mental arithmetic
  • Market simulator
  • Arbitrage trees
  • Horse racing
  • Bid book
  • Screening tests
  • Mock papers

Reference

  • Formula reference
  • Search

Your record

  • Review queue
  • Progress
  • Leaderboard
  • Profile
  • Invite friends
AccountSend feedback
  1. Curriculum
  2. /Quantitative development
  3. /Data structures and algorithms
  4. /Dynamic programming

Dynamic programming, and why it is the same as an EV recursion

CS · Chapter 6·14 min read·Asked at Hudson River Trading, Jump, Jane Street, Citadel Securities

Assumes Complexity: reading it off, and deriving it.

After this lesson you should be able to

  • Identify the state and transition of a dynamic programme.
  • Convert a recursion into either memoisation or a table, and say which fits.
  • Recognise that a probability brainteaser and a DP problem are the same object.

Dynamic programming is recursion with the repeated work removed. The hard part is never the code — it is choosing the state. And once you see that an expected-value recursion is a dynamic programme over the same state space, half the probability questions in a trading interview become coding questions you already know how to do.

Proposition 6.1

The four questions

Every dynamic programme answers the same four questions, and answering them aloud in order is most of the interview. What is the state? What is the transition? What are the base cases? In what order can the states be evaluated so that each one’s dependencies are already computed?

Holds when

  • If the state is right, the transition is usually one line.
  • If you cannot find an ordering, the problem may need memoisation on a graph rather than a table — or it may have a cycle, in which case it is a linear system, not a DP.
AspectMemoisation (top-down)Tabulation (bottom-up)
Written asThe natural recursion plus a cacheLoops filling an array
VisitsOnly reachable statesEvery state in the table
RiskStack depth on deep recursionsComputing states you never needed
SpaceHard to reduce below the full state spaceOften reducible to one or two rows
Best whenThe state space is sparse or awkward to orderThe state space is dense and the order is obvious
Table 6.2 · Memoisation against tabulation.
from functools import cache

def climb_memo(n: int) -> int:
    """Ways to climb n stairs taking 1 or 2 at a time."""
    @cache
    def f(k: int) -> int:
        if k < 0:
            return 0
        if k == 0:
            return 1
        return f(k - 1) + f(k - 2)
    return f(n)

def climb_table(n: int) -> int:
    prev, cur = 1, 1          # f(0), f(1)
    for _ in range(2, n + 1):
        prev, cur = cur, cur + prev
    return cur                 # O(n) time, O(1) space
Listing 6.3 · Both forms, one problem. The tabulated version drops to constant space because the transition looks back only two states. Spotting that reduction is a standard follow-up. Time O(n) · Space O(n) memoised, O(1) tabulated.
f(0)=1f(1)=1f(2)=2f(3)=3f(4)=5
Figure 6.4 · A recursion is a graph of reusable results. Each f(n) needs the two earlier states. The arrows point from a prerequisite to the result that uses it. A naive recursive call tree visits f(2) more than once; a table or cache computes this graph one node at a time.
Plain recursion331,160,281With a cache40
Figure 6.5 · What memoisation is worth, on the fortieth Fibonacci number. The naive tree recomputes the same subproblems until the numbers stop being writable; the cache turns it into one pass. It is the same move as writing an expected-value recursion in terms of states you have already solved — dynamic programming and backward induction are one idea.

The rest of this lesson is in Premium

You have read the opening. 11 more sections follow, including 4 worked examples and 3 quick checks.

Start the free 7-day trialSign in

Nothing is charged for 7 days, and you can cancel before then. Or read Complexity: reading it off, and deriving it in full, free.

← Core techniques: binary search on the answer, two pointers, sliding windowsBit manipulation and modular arithmetic →
On this page
  • The four questions
  • Memoisation against tabulation
  • Both forms, one problem
  • A recursion is a graph of reusable results
  • What memoisation is worth, on the fortieth Fibonacci number

QuantMax · 141 lessons · 1342 questions · c5c0caa

  • Premium
  • Arbitrage trees
  • Horse racing
  • Invite friends
  • Account
  • About QuantMax
  • Terms
  • Privacy

Firm names identify publicly reported question patterns and nothing more. QuantMax is not affiliated with, endorsed by, or recruiting for any firm named in the curriculum. Everything you do in lessons and the question bank is kept to your account.