Daily Tally of Users Who Posted on Their Signup Day
SQL & DataAuto-graded · 14 test casesLast 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 userregistered_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 postuser_id: identifier of the authorcreated_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_iddoes not appear inusersare 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 aYYYY-MM-DDstringuser_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^5user_idvalues inusersare unique.- Timestamps are strings that begin with a
YYYY-MM-DDdate. - 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A user counts toward date D only if they registered on D and published at least one post on D.
- Convert every timestamp to its calendar date (the first 10 characters).
- Build a dictionary from
user_idto registration date. - Scan the posts once. For each post whose author exists in the dictionary, compare the post date to the author's registration date. When they match, add the pair
(date, user_id)to a set. - Because it is a set, a user with many same-day posts is stored only once.
- Count the set entries per date, then output the dates in ascending order.
Why it is correct
- The match test is exactly the definition: same user, and the post's date equals the user's registration date.
- Each user has a single registration date, so a user can belong to at most one date group.
- The set removes duplicates, so no user is counted twice.
- Posts by unknown users are skipped by the dictionary lookup, and dates with no qualifying user never get a counter, so they are absent from the output.
YYYY-MM-DDstrings sort chronologically, so a plain string sort gives ascending date order.
Complexity
With U users, P posts and D distinct qualifying dates:
- Time:
O(U + P + D log D) - Space:
O(U + P)in the worst case
Reference solution
from datetime import date, datetime
from typing import Dict, List
def count_signup_day_posters(users: List[Dict], posts: List[Dict]) -> List[Dict]:
def calendar_date(value):
if isinstance(value, datetime):
return value.date().isoformat()
if isinstance(value, date):
return value.isoformat()
return str(value)[:10]
registration_dates = {
user["user_id"]: calendar_date(user["registered_at"])
for user in users
}
qualifying = set()
for post in posts:
user_id = post["user_id"]
if user_id in registration_dates:
post_date = calendar_date(post["created_at"])
if registration_dates[user_id] == post_date:
qualifying.add((post_date, user_id))
counts = {}
for day, _ in qualifying:
counts[day] = counts.get(day, 0) + 1
return [
{"registration_date": day, "user_count": counts[day]}
for day in sorted(counts)
]