Data structures for LLD
medium 45 minInside a class the choice of container decides which method is fast, which is correct, and which lock is needed.
Watch lesson4:56
Where it shows up
The same pattern appears across everyday software.
Redis sorted sets: two structures in one
The documentation says sorted sets are implemented with a skip list and a hash table together. One gives order, the other gives the score of a member in one step.
Java LinkedHashMap
Keeps insertion order, or access order if asked in the constructor. The class page says that the second kind is well suited to building LRU caches.
Linux CFS scheduler: red-black tree
The kernel documentation of CFS describes a time-ordered red-black tree of runnable tasks. The scheduler picks the leftmost one, the task that is furthest behind.
Python asyncio: a heap of timers
The event loop in CPython keeps scheduled callbacks in a heap, so the next timer is always first. Cancelled timers stay inside and are cleaned up when there are many: lazy deletion.
Java ThreadPoolExecutor: a blocking queue
Any BlockingQueue may be used to hold submitted tasks, and the kind of queue changes how the pool grows.
LMAX Disruptor: a ring buffer
All memory for the ring is allocated at the start. Producers and consumers move along it with sequence numbers.
Lucene FixedBitSet
A bit set of fixed length on top of an array of longs, used to mark matching documents by number.
PostGIS: R-tree over GiST
Shapes and points are found through a spatial index. A plain ordered index has one dimension.
Java CopyOnWriteArrayList
Every change makes a fresh copy of the array. The class page says that this is usually too costly, but can be better when reading vastly outnumbers changing.