← 返回 uber 的题目列表Design an In-Memory Queue (Tradeoffs: Clearing, Background Cleanup, Stale Data)
类型:online_judge
Problem: Design an In-Memory Queue (with tradeoff discussion)
Design and implement an in-memory queue supporting:
enqueue(x)
dequeue() (return empty/error if the queue is empty)
size()
After implementing the basics, discuss tradeoffs:
When should the queue be cleared or memory reclaimed?
Should a background thread be used for cleanup/compaction/expiration? How to avoid races with foreground operations?
How do you handle stale data (e.g., TTL, slow consumers, visibility/consistency)?
One possible coding I/O format
Read m commands:
ENQ v
DEQ
SIZE
CLEAR
Print outputs for commands that produce output.
Constraints
1 <= m <= 2e5
Target amortized O(1) for operations.
Example
Input:
8
ENQ 1
ENQ 2
SIZE
DEQ
DEQ
DEQ
ENQ 3
SIZE
Output:
2
1
2
EMPTY
1
Example
Input
8
ENQ 1
ENQ 2
SIZE
DEQ
DEQ
DEQ
ENQ 3
SIZE
Output
2
1
2
EMPTY
1