← Questions
Process login logs split across shards
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's onsite coding round, last asked Apr 2026. It tests logs, merging and hash maps.
Login records are split across several shards. Each shard holds (timestamp, user_id) records sorted by timestamp, but the shards overlap in time. Combine them correctly and efficiently to answer questions about users' logins, and explain how the approach changes when the data no longer fits in one machine's memory.
Part 1: one ordered stream
Unless you agree on a different interface with the interviewer, assume:
-
merge_shards(shards: list[list[tuple[int, str]]]) -> Iterator[tuple[int, str]]— yields every record once across all shards, in timestamp order (ties by shard index).
shards = [[(1,"u1"), (5,"u2"), (9,"u1")],
[(2,"u2"), (7,"u3")],
[(3,"u1")]]
list(merge_shards(shards))
-> [(1,"u1"), (2,"u2"), (3,"u1"), (5,"u2"), (7,"u3"), (9,"u1")]
Part 2: per-user answers
-
first_and_last(shards) -> dict[str, tuple[int, int]]— each user's first and last login time.
first_and_last(shards) -> {"u1": (1, 9), "u2": (2, 5), "u3": (7, 7)}
Constraints
-
kshards withNrecords in total;kup to 10^3,Nup to 10^9 in the large version. - Shards are each sorted; records may be duplicated across shards in the large version.
Clarifying questions
- Is each shard sorted? Can the same record appear in two shards?
- Are shards split by time, by user, or arbitrarily?
- Which questions matter: global ordering, per-user stats, sessions, active users per day?
- How big is the data, and how many machines?
What a strong answer covers
- Part 1: a k-way merge with a min-heap holding the head of each shard,
O(N log k)time andO(k)memory; a generator so the output is never fully materialised. - Part 2: a single pass with a hash map per user; when the questions are per user, no global ordering is needed at all.
- Choosing the cheapest structure per question: ordering needs the merge, aggregates only need a pass.
- Too big for memory: stream from disk with buffered readers for the merge; for per-user questions, partition records by
hash(user_id)so each partition fits, then aggregate per partition (map-reduce); handle skewed users. - Duplicates across shards: dedupe on
(timestamp, user_id)while merging. - Tests: empty shards, one shard, equal timestamps across shards.
Follow-ups
- Count daily active users across shards.
- Detect accounts that log in from two shards (regions) within one minute.
- Shards are live streams rather than files.
Where it's asked
- Google · Onsite coding · Last asked Apr 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)
What it tests
logs · merging · hash maps
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
