← 返回 uber 的题目列表First Unique IP Hitting the Server (Streaming)
类型:online_judge
Problem: First Unique IP Hitting the Server (Streaming)
Your server receives a continuous stream of IP hit events. Design a data structure that supports:
add(ip): record one hit from ip.
first_unique(): return the earliest IP that has appeared exactly once so far; return empty (e.g., null / empty string) if none exists.
Requirements
Make both add and first_unique as efficient as possible.
IPs are given as strings (e.g., "10.0.0.1"); you do not need to validate the format.
Example
Calls:
add("1.1.1.1")
add("2.2.2.2")
add("1.1.1.1")
add("3.3.3.3")
Now first_unique() should return "2.2.2.2" (it appeared earliest among those that appear exactly once).
Constraints
Number of events N up to 10^7.
Number of distinct IPs U up to 10^6.
Follow-up: Scalability (Millions of Records)
When the data volume is huge (e.g., tens of millions of events, millions of distinct IPs, and potentially multiple writers across machines/data centers):
How would you scale the design without significantly increasing query latency?
Explain your sharding/storage/consistency/memory-control approach (no code required).
Example
Input
add 1.1.1.1
add 2.2.2.2
add 1.1.1.1
first_unique
Output
2.2.2.2