🚀 AI-Powered Mock Interviews Launching Soon - Join the Waitlist for Early Access

technicalmedium

Write a function that, given an array of event objects with userId and timestamp, returns the number of unique users who performed any event on each day within a specified date range.

technical screen · 3-5 minutes

How to structure your answer

  1. Clarify input: array of events, startDate, endDate. 2. Validate dates and handle empty array. 3. Iterate once, using a hash map keyed by date to a set of userIds. 4. Convert timestamps to UTC date strings. 5. After loop, map each date to set size. 6. Return object or array of {date, dau}. 7. Discuss time complexity O(n) and space O(n). 8. Mention edge cases: duplicate events, out‑of‑range dates, timezone handling.

Sample answer

Sure. First, I’ll define the function signature: function calculateDAU(events, startDate, endDate). I’ll validate that startDate <= endDate and that events is an array. I’ll create a Map where each key is a UTC date string and the value is a Set of userIds. I’ll iterate over events once: for each event, convert timestamp to a UTC date string, skip if outside the range, then add userId to the Set for that date. After processing, I’ll build an array of objects {date, dau: set.size} for each date in the range, ensuring dates with no events return 0. The algorithm runs in O(n) time and O(n) space, and handles duplicates naturally via the Set. I’ll also write tests for empty input, out‑of‑range dates, and duplicate events.

Key points to mention

  • • Input validation and edge cases
  • • Single-pass algorithm
  • • Use of Set to deduplicate users
  • • Time complexity O(n)
  • • Space complexity O(n)

Common mistakes to avoid

  • ✗ Using nested loops leading to O(n²)
  • ✗ Ignoring timezone normalization
  • ✗ Not handling duplicate events