← [ BACK_TO_NOTES ]

Data Structure Design Interview Notes

Common O(1)/amortized O(1) structure designs:setAll map, LRU, random set, median stream

#algorithm #concept #growing #hash

[!info] related notes 算法面试题型 MOC

Data Structure Design Interview Notes

setAll HashMap

  • Store for each key: (value, setTime).
  • Maintain global (setAllValue, setAllTime).
  • get(k) returns key’s value if keyTime > setAllTime, else returns setAllValue.

LRU Cache

  • HashMap for key -> node.
  • Doubly linked list for recency order.
  • Operations:
    • get: move node to tail.
    • put: update + move, or insert; if over capacity, evict head.

Insert/Delete/GetRandom in O(1)

  • Array stores values.
  • HashMap stores value -> index in array.
  • Delete by swapping with last element and updating index.

GetRandom with Duplicates Allowed

  • Similar, but value -> set of indices.

Median from Data Stream

  • Two heaps:
    • max-heap for smaller half
    • min-heap for larger half
  • Balance sizes so that size diff <= 1.
创建于 2026/3/16 更新于 2026/7/15
Digital Biome
Access

键盘导航

j / k / ↑ / ↓
选择对象
Enter / o
打开选择
/ 或 Ctrl+K
全站搜索
i / f / 1–4
筛选当前列表
g → h / l / p / t / a
首页 / 笔记 / 项目 / 工具 / About
r → 1–9
打开所选对象的关联
u
返回上一页
Esc
关闭浮层 / 取消命令;输入框内清空文本