Count Distinct Engaged Ads Per Day from an Event Log
Arrays & HashingAuto-graded · 22 test casesLast updated
Tests filtering an event log by type, grouping by date, counting distinct ids with sets, and returning date-sorted results while omitting dates that have no qualifying events.
Daily Engaged Ads
You have a log of advertisement events. Each entry is a list [ad_id, event_type, event_date]:
ad_idis an integer identifying the ad.event_typeis a string, such as"click","impression", or"view".event_dateis a string inYYYY-MM-DDformat.
An ad counts as engaged on a given date if the log contains at least one event for that ad on that date whose event_type is exactly "click" or "impression". Events of any other type are ignored completely.
Task
Write a function that returns, for every date that has at least one engaged ad, the number of distinct engaged ads on that date.
Output format
- Return a list of
[event_date, engaged_ad_count]pairs. - Sort the pairs by
event_dateascending (chronological order). - Dates with no engaged ads must not appear in the result, even if the log contains other events (such as
"view") on those dates. - If no date qualifies, return an empty list.
Rules
- An ad with several qualifying events on the same date, in any mix of clicks and impressions, counts once for that date.
- Identical duplicate rows count once.
- The same ad can be engaged on several different dates; it counts once on each of them.
- Event type matching is case-sensitive, so
"Click"is not"click". - The input may be in any order.
Constraints
0 <= len(rows) <= 10^5ad_idis an integer.event_dateis always a validYYYY-MM-DDstring.event_typeis an arbitrary string; only"click"and"impression"qualify.
Examples
Example 1
Input: count_daily_engaged_ads([[101, 'click', '2023-10-01'], [101, 'view', '2023-10-01'], [102, 'impression', '2023-10-01'], [101, 'view', '2023-10-02']])
Output: [['2023-10-01', 2]]
Example 2
Input: count_daily_engaged_ads([[1, 'click', '2023-10-01'], [1, 'impression', '2023-10-01'], [2, 'click', '2023-10-01'], [3, 'view', '2023-10-02']])
Output: [['2023-10-01', 2]]
Hints
Hint 1
Think of the task as three steps: filter the rows, group them by date, then count unique ad ids per group.
Hint 2
Keep a mapping from date to a set of ad ids. Adding to a set automatically ignores repeats, so you get distinct counts for free.
Hint 3
Accept only the two qualifying event types, so dates holding only other events never get an entry. Because dates are YYYY-MM-DD strings, sorting them as plain strings gives chronological order.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a filter, group, count-distinct, sort pipeline.
- Walk through the events once. Skip any event whose type is not exactly
"click"or"impression". - For each remaining event, add its
ad_idto a set stored under itsevent_datein a dictionary. - After the pass, sort the dictionary's date keys and emit
[date, size_of_set]for each.
Why it is correct
- A date gets an entry only when at least one qualifying event occurs on it, so dates with only other event types are omitted, as required.
- Each date has its own set, so an ad is counted once per date no matter how many qualifying events it has, and an ad active on several dates is counted on each.
- Using a whitelist of the two accepted types ensures every other string, including unexpected ones or different casing, is ignored.
YYYY-MM-DDstrings sort lexicographically in the same order as chronologically, so a plainsortedgives the required ordering.- The input order does not matter because sorting happens at the end.
Complexity
Let n be the number of events and d the number of distinct qualifying dates.
- Time:
O(n + d log d), a single pass with averageO(1)set and dict operations, plus sorting the dates. - Space:
O(u), whereuis the number of distinct (date, ad) pairs among qualifying events, at mostn.
Reference solution
from collections import defaultdict
from typing import List
def count_daily_engaged_ads(events: List[List]) -> List[List]:
engaged = defaultdict(set)
for ad_id, event_type, event_date in events:
if event_type in ("click", "impression"):
engaged[event_date].add(ad_id)
return [[date, len(engaged[date])] for date in sorted(engaged)]