Top-k endpoints from a log stream
Return the k most requested paths from access logs with deterministic tie-breaking, in O(n log k) with bounded memory.
Statement
Given access log lines (ts method path status latency_ms), return the k paths with the most requests as a list of (path, count), highest count first. Break ties by path ascending so the output is deterministic.
Then discuss what changes if the log is an unbounded stream and you can only afford O(k) memory.
Examples
k = 2
1700000001 GET /api 200 10
1700000002 GET /health 200 1
1700000003 GET /api 200 12
1700000004 GET /login 200 30
1700000005 GET /health 200 1
→ [("/api", 2), ("/health", 2)] # tie broken by pathConstraints
Up to 10⁷ lines, 10⁵ distinct paths, k ≤ 1000. Must not sort all distinct paths when k is small.
Open to everyone until Pro goes on sale · sign in to save your attemptSolve it in the editor with visible tests
Open in the app