Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 09 · DSA · Four patterns the ladders lean on · about 16 min to read

Play it as a journeyTutor comes with a plan

Two structures holding hands: designing data structures

Hashing
Hashing · Open board

A hash map finding a key in one step, the tool that every structure in this lesson leans on.

An interviewer says: “Build a set where you can insert, delete and also pick a random member, each in constant time.” You know a list can pick a random member. You know a hash map can delete in one step. Neither can do all three. A list is slow to delete from the middle. A hash map has no positions, so it cannot pick at random. The question is not asking for a new idea. It is asking you to combine two ideas you already have so that each covers the weakness of the other.

By the end you will be able to solve three such problems (a random set, a snapshot array and an LFU cache) and to run the same five-step method on any “design a data structure” question.

The history (optional)

A cache that throws out the least recently used entry (LRU) can be built with O(1) work per operation. A cache that throws out the least frequently used entry (LFU) was long thought to need a heap, which costs O(log n) per operation. A paper dated 16 August 2010, “An O(1) algorithm for implementing the LFU cache eviction scheme” by Ketan Shah, Anirban Mitra and Dhruv Matani, showed that a hash table and linked lists are enough. The idea is in the third section below, and nothing later in the lesson depends on this note.

Before you start. Design an LRU cache (a map and a list that keep each other honest) showed a hash map and a linked list that always describe the same entries. That promise is called an invariant, and every method must leave it true. Hashing gave you the hash map: a key goes in and its value comes out in one step. Clinic #11: Binary Search gave you binary search: find a value in a sorted list by halving the range. Design a parking lot (the first whole round) gave you the order of a design round. You also need to read a little Python.

Lesson 89 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • Why one structure is never enough
  • Swap and pop: deleting from a list without the shift
  • Snapshot array: remember only what changed
  • Frequency bucket: LFU in constant time
  • The design round for this family
  • Insert delete getrandom O(1)
  • Snapshot array
  • LFU cache
  • Where LFU beats LRU
  • Choosing

Terms it explains: invariant, list, hash map, swap and pop, history, fewest, frequency bucket, List the questions, Write the invariant, Write each method, Common mistakes, average O(1) vs worst O(n), Bloom filter, bucket, chaining, collision, hash function, hash join, hash set / map, hash table, HashDoS, load factor α, open addressing, resize/rehash, SipHash, tombstone.

Review DSA