Objects seen on both days by at least two clients
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Nov 2025. It tests hash sets, sorting and two pointers.
You get two log files, one per day. Each line has timestamp, object_id, client_id, and neither file is sorted. An object is interesting when it appears at least once on each day and, across both days together, was accessed by at least two different clients. Return the interesting object ids. Then solve it when the logs don't fit in memory.
Part 1: in memory
-
shared_objects(day1: Iterable[tuple], day2: Iterable[tuple]) -> list[str]— the interesting ids, sorted.
day1 = [("t1", "o1", "c1"), ("t2", "o2", "c1")]
day2 = [("t3", "o1", "c2"), ("t4", "o2", "c1")]
shared_objects(day1, day2) -> ["o1"] # o2 only ever saw c1
day1 = [(1, "a", "x"), (2, "b", "x"), (3, "c", "y"), (4, "c", "y")]
day2 = [(5, "a", "x"), (6, "b", "y"), (7, "c", "y"), (8, "d", "z")]
shared_objects(day1, day2) -> ["b"] # a and c have one client; d is only on day 2
State the time and space cost in terms of the total number of lines.
Part 2: the logs don't fit in memory
You can scan each file sequentially and run an external sort. Describe the approach and its cost. For example: externally sort each file by object_id, then walk both sorted files together with two pointers, handling one object's lines from both days at a time.
Constraints
- Up to billions of lines per file; ids are strings.
- Memory holds a small fraction of one file.
Clarifying questions
- Do the two clients have to be on different days, or can both be on one day?
- Are ids strings or integers, and is there a bounded number of distinct objects?
- Does the output need to be sorted?
- Is this a one-off batch job or a daily pipeline?
What a strong answer covers
- Part 1: one pass over day 1 builds
object -> set of clients(or the first client plus a "has two" flag, to save memory); one pass over day 2 checks membership and adds clients. O(N) time, O(distinct objects) space. - Stopping early per object: once two clients are known you only need the day flags.
- Part 2: external sort (or hash partition both files by
hash(object_id) % Pso each partition fits) then process partitions or merge the sorted runs. O(N log N) I/O for sorting, O(N) for partitioning. - When a distributed job (map by object id, reduce per object) is the right answer.
- Tests: the same client on both days, an object only on one day, duplicate lines.
Follow-ups
- Generalise to
kdays with at leastmclients. - Return the count of distinct clients per interesting object.
- Use a Bloom filter of day-1 objects to cut what day 2 has to keep. What error does it allow?
Where it's asked
- Google · First round: coding · Last asked Nov 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
hash sets · sorting · two pointers
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.
