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. /Trees and heaps

Trees and heaps: BSTs, priority queues and range queries

CS · Chapter 3·12 min read·Asked at Hudson River Trading, Jump, Citadel Securities, Optiver

Assumes Linear structures: arrays, hash maps and monotonic stacks.

After this lesson you should be able to

  • Say what a BST gives you that a hash map does not.
  • Use a heap for the top-kkk and streaming-median problems.
  • Choose a structure for range queries with updates.

Hash maps answer "is this key present" and nothing else. The moment a question involves order — the smallest, the next one up, everything in a range — you need a tree or a heap, and which one depends on whether you need the whole order or only the extreme.

Proposition 3.1

What a BST buys over a hash map

Ordered operations: find the smallest key above a value, iterate in sorted order, count everything in a range. All are O(log⁡n)O(\log n)O(logn) in a balanced tree and impossible in a hash map, which is exactly the trade for its constant-time lookup.

Holds when

  • An unbalanced BST degenerates to a list on sorted input — hence AVL, red-black and treaps.
  • In-order traversal yields sorted output, which is often the whole reason to use one.
  • An order book’s price levels are the canonical financial example, though a fixed tick grid lets an array beat it.

Proposition 3.2

Heaps

A binary heap keeps the minimum at the root with O(log⁡n)O(\log n)O(logn) insert and extract, stored as a flat array with no pointers at all. It gives you the extreme element cheaply and tells you nothing about the rest of the order, which is precisely the trade that makes it faster than a tree.

Holds when

  • Building a heap from nnn items is O(n)O(n)O(n), not O(nlog⁡n)O(n\log n)O(nlogn) — the analysis is a standard interview follow-up.
  • Finding an arbitrary element is O(n)O(n)O(n); use a tree if you need that.
  • Two heaps facing each other — a max-heap of the low half and a min-heap of the high — maintain a streaming median.
2589121014
Figure 3.3 · The heap is ordered only from parent to child. Every parent is no larger than its children, so the minimum is at the root. Siblings and cousins have no sorted order: 12 sits left of 10. That weaker invariant is what makes insertion and removal fast.
Sort everything19,931,569Heap of size k6,643,856Quickselect1,000,000
Figure 3.4 · Three ways to take the top hundred of a million. Sorting solves a harder problem than the one asked. A heap of size kkk costs nlog⁡kn\log knlogk and quickselect is linear on average — and the reason to know all three is that only the heap works when the million arrives as a stream you cannot store.

Why a bounded heap beats sorting. To keep the largest kkk of a stream, hold a *min*-heap of size kkk: the root is the weakest survivor, so each new element is compared against it and either discarded or swapped in. The heap never grows past kkk, so the space is O(k)O(k)O(k) and each step costs log⁡k\log klogk rather than log⁡n\log nlogn. Sorting would need every element in memory and O(nlog⁡n)O(n\log n)O(nlogn) time — and on a genuine stream it is not available at all. The counter-intuitive part is using a min-heap to track maxima, and it is the whole trick.

The rest of this lesson is in Premium

You have read the opening. 12 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.

← Linear structures: arrays, hash maps and monotonic stacksGraphs: traversal, shortest paths and union–find →
On this page
  • What a BST buys over a hash map
  • Heaps
  • The heap is ordered only from parent to child
  • Three ways to take the top hundred of a million

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.