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. /Linear structures

Linear structures: arrays, hash maps and monotonic stacks

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

Assumes Complexity: reading it off, and deriving it.

After this lesson you should be able to

  • Choose between an array, a hash map and a linked list from the access pattern.
  • Explain how a hash map handles collisions and when it degrades.
  • Recognise the problems a monotonic stack solves in linear time.

Most interview problems are solved by choosing the right linear structure, and the choice follows from one question: what do you need to look up, and by what? Everything else — the hash map, the deque, the monotonic stack — is an answer to a particular version of that.

You needUseWhy
Index-based accessArrayO(1)O(1)O(1) and contiguous
Lookup by keyHash mapO(1)O(1)O(1) average
Ordered iteration by keyBalanced tree / sorted mapO(log⁡n)O(\log n)O(logn), keeps order
Insert and remove at both endsDequeO(1)O(1)O(1) at either end
Last-in, first-outStackMatching, nesting, backtracking
Frequent insert and delete mid-sequenceLinked listO(1)O(1)O(1) given the node
Table 2.1 · Choosing the structure. Linked lists win far less often than their prominence in courses suggests. Their O(1)O(1)O(1) insertion requires already holding the node, and their cache behaviour is so poor that an array beats them at realistic sizes even for the operations they are supposed to win.

Proposition 2.2

How a hash map works

Hash the key to a bucket index, then handle collisions by chaining a list in each bucket or by probing for the next free slot. Average lookup is O(1)O(1)O(1) because the load factor is kept bounded — the table resizes and rehashes when it fills past a threshold, which is what makes the amortised cost constant.

Holds when

  • Worst case is O(n)O(n)O(n) when every key collides, which an adversary can arrange if the hash is predictable.
  • Open addressing is faster in cache terms; chaining degrades more gracefully at high load.
  • Keys must be immutable, or a mutation changes the hash and the entry becomes unreachable.
Array by index1Hash map by key1Sorted array, binary search20Linked list, scan1,000,000
Figure 2.3 · Finding one item among a million. The first three are indistinguishable at this scale and the fourth is the entire reason hashing exists. Note the middle entry: twenty steps is close enough to constant that "sort it once and binary search" beats a hash map whenever you also need order.

The hash map is the trade you make first. A very large share of interview problems reduce to "I am doing a linear scan inside a loop, so this is quadratic" — and the fix is nearly always to precompute a hash map and replace the inner scan with a lookup. Two-sum, finding duplicates, grouping anagrams, counting pairs: all the same move, trading O(n)O(n)O(n) memory for a factor of nnn in time. Recognising that shape quickly is worth more than any individual algorithm.

Proposition 2.4

Monotonic stacks and deques

Keep a stack whose contents are always increasing or decreasing, popping anything that violates the order as you push. Each element is pushed and popped once, so the whole scan is linear despite the inner loop — and it answers "next greater element" style questions directly.

Holds when

  • Next greater or smaller element: a monotonic stack, in one pass.
  • Sliding-window maximum: a monotonic deque, popping from both ends.
  • Largest rectangle in a histogram: a monotonic stack, and the canonical hard example.

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.

← Complexity: reading it off, and deriving itTrees and heaps: BSTs, priority queues and range queries →
On this page
  • Choosing the structure
  • How a hash map works
  • Finding one item among a million
  • Monotonic stacks and deques

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.