KatInterview

Daily Tally of Users Who Posted on Their Signup Day

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datahash-mapdatesdedup

Last updated

Given user and post records with timestamps, count per signup date how many users published at least one post that same calendar day, counting each user once.

Signup-Day Posters

You work with two collections of records, given as lists of dictionaries.

Users (users): each dictionary has

  • user_id: unique identifier of the user
  • registered_at: timestamp of signup, as a string such as "2024-01-01 09:00:00"

Posts (posts): each dictionary has

  • post_id: unique identifier of the post
  • user_id: identifier of the author
  • created_at: timestamp of publication, in the same string format

Implement a function that, for every calendar date, reports how many distinct users signed up on that date and also published at least one post on that same date.

Rules

  • The calendar date of a timestamp is its first 10 characters (YYYY-MM-DD). Ignore the time of day and do not apply any time-zone conversion.
  • A user qualifies when at least one of their posts has the same calendar date as their registered_at. The time of the post relative to the signup time does not matter.
  • A user who made several qualifying posts is counted only once.
  • Posts whose user_id does not appear in users are ignored.
  • Only dates with at least one qualifying user appear in the output; do not include dates with a count of zero.

Output

Return a list of dictionaries, each with the keys:

  • registration_date: the date as a YYYY-MM-DD string
  • user_count: the number of qualifying users for that date (integer)

Sort the list by registration_date in ascending order. If no user qualifies, return an empty list.

Constraints

  • 0 <= len(users), len(posts) <= 10^5
  • user_id values in users are unique.
  • Timestamps are strings that begin with a YYYY-MM-DD date.
  • The solution should run in roughly linear time (plus sorting the distinct dates).

Examples

Example 1

Input: count_signup_day_posters([{'user_id': 1, 'registered_at': '2024-01-01 09:00:00'}, {'user_id': 2, 'registered_at': '2024-01-01 12:30:00'}, {'user_id': 3, 'registered_at': '2024-01-02 08:00:00'}], [{'post_id': 10, 'user_id': 1, 'created_at': '2024-01-01 10:00:00'}, {'post_id': 11, 'user_id': 1, 'created_at': '2024-01-01 11:00:00'}, {'post_id': 12, 'user_id': 2, 'created_at': '2024-01-02 00:05:00'}, {'post_id': 13, 'user_id': 3, 'created_at': '2024-01-02 08:30:00'}])

Output: [{'registration_date': '2024-01-01', 'user_count': 1}, {'registration_date': '2024-01-02', 'user_count': 1}]

Hints

Hint 1

Reduce each timestamp to its date string first. The time of day plays no role in the comparison.

Hint 2

Build a lookup from user_id to registration date, then scan the posts once and test whether each post's date equals its author's registration date.

Hint 3

Record qualifying users in a set so several same-day posts from one user count once, then tally the set per date and sort the dates at the end.