r/chalkinterview • u/Uahakan • 15d ago
Library question Google, Meta, Microsoft coding question: Unique visitors per time block
Count distinct visitor ids in every window of a fixed size as it slides along a log.
You help maintain a website's visitor log. The analytics team checks how many different visitors appear in each fixed-size stretch of consecutive entries.
You get:
ids: visitor IDs in time order; IDs may be negative or zero, e.g.[12, -3, 12]block_size: how many consecutive entries each block contains, e.g.3
Return a list with the number of distinct visitor IDs in every block:
- Start with the first
block_sizeentries, then move one entry right for each new block, because visitors arrive one at a time. - Include only full blocks. If
idsis empty orblock_sizeis larger than its length, return[]because there is no complete block. - Return counts from the earliest block to the latest. No tie-breaking is needed because each block gets its own count.
For example, with ids = [4, 7, 4, 9, 7] and block_size = 3:
- The blocks are
[4, 7, 4],[7, 4, 9], and[4, 9, 7]. Their distinct visitor counts are
2,3, and3, so return[2, 3, 3].def count_distinct_visitors(ids: list[int], block_size: int) -> list[int]
Source: reported by a candidate in a public write-up. The wording above is ours.
Practise it out loud with follow-ups: https://chalkinterview.com/questions/google-unique-visitors-per-time-block/?utm_source=reddit&utm_campaign=sub
How would you answer it? Drop your approach below.