Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Data Structure Patterns
DSA

Data Structure Patterns

Learn how combining fundamental data structures can solve complex coding interview problems.

Design problems ask you to combine two plain structures so their strengths cancel each other’s weaknesses.

Think design when you see:

  • “Design a data structure supporting X, Y, Z in O(1)”
  • Cache with an eviction policy
  • Insert/delete/search/getRandom together
  • Values keyed by (key, timestamp)

Quick Recognition Cheat Sheet

If you see…Think…
Evict least recently usedHashmap + doubly-linked list
Evict least frequently usedHashmap + frequency buckets
insert + delete + getRandom all O(1)Hashmap + dynamic array
Value changes over time (timestamps)Key → sorted version list
Ordered operations (floor/ceiling)Balanced BST / sorted map

Pattern Table

PatternCore ComboTrick
LRU CacheMap + linked listMove-to-front on every touch
LFU CacheMap + freq bucketsTrack minFreq incrementally
RandomizedSetMap + arraySwap-with-last delete
TimeMapMap + append-only listsBinary search timestamps

Mental Trigger

O(1) requirement + one weak operation = bolt a second structure onto the first.

Decision Guide

Need O(1) lookup?

   Hashmap (always the anchor)

Also need ordering / recency / frequency?
        ├─ recency   → linked list nodes in the map
        ├─ frequency → bucket per frequency
        └─ ordered   → TreeMap / sorted container

Need uniform random?

   Dense array + swap-delete

My Private Notes

Notes are auto-saved locally to this device.