Sponsors Active in Both Opening Weeks
SQL & DataAuto-graded · 21 test casesLast updated
Given a log of ad submission rows, find the sponsors with at least one submission in week 1 and at least one in week 2, returned sorted, which tests set intersection and deduplication.
Sponsors Active in Both Opening Weeks
A platform keeps a log of ad submissions. Each log entry is a list of three values:
[sponsor_id, creative_id, week_number]
sponsor_idis a string identifying the sponsor.creative_idis an integer identifying the individual ad. It has no effect on the answer.week_numberis an integer for the week the ad was submitted.
Write a function that returns every sponsor that submitted at least one ad in week 1 and at least one ad in week 2.
Rules
- A sponsor may appear many times in the same week. Each sponsor must appear at most once in the result.
- Entries whose week number is anything other than 1 or 2 are ignored.
- A sponsor that appears in only one of the two weeks is not included.
- Return the sponsor IDs as a list sorted in ascending (lexicographic) order.
- If no sponsor qualifies, or the log is empty, return an empty list.
Constraints
0 <= len(log) <= 10^5- Each entry has exactly three elements: a string, an integer, and an integer.
- Week numbers are integers and may be outside the range 1 to 2.
- The same
creative_idor the same entry may repeat.
Examples
Example 1
Input: sponsors_in_both_weeks([['A123', 9001, 1], ['A123', 9005, 2], ['B456', 9010, 1], ['C789', 9020, 2], ['B456', 9012, 2]])
Output: ['A123', 'B456']
Hints
Hint 1
Only two groups matter: the sponsors seen in week 1 and the sponsors seen in week 2. Entries from other weeks can be skipped.
Hint 2
Repeated sponsors in the same week should not count twice, so think about which data structure removes duplicates automatically.
Hint 3
A sponsor qualifies exactly when it is in both collections. Intersect them and sort the result.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a set intersection problem.
- Walk through the log once.
- Add each sponsor from a week-1 entry to a
first_weekset, and each sponsor from a week-2 entry to asecond_weekset. Entries from any other week are skipped. - Intersect the two sets to get the sponsors present in both weeks.
- Sort the result and return it.
Why it is correct
A sponsor belongs in the answer exactly when at least one entry has week 1 and at least one entry has week 2 for that sponsor. A set records "appeared at least once" and ignores repeats, so each set holds exactly the sponsors active in its week. The intersection keeps only sponsors satisfying both conditions. Because other weeks are never added to either set, they cannot produce a false match, for example a sponsor active in weeks 1 and 3 only. Sorting gives the required deterministic order.
Complexity
- Time: O(n + k log k), where n is the number of entries and k is the number of qualifying sponsors.
- Space: O(u), where u is the number of distinct sponsors in weeks 1 and 2.
Reference solution
from typing import List
def sponsors_in_both_weeks(log: List[List]) -> List[str]:
first_week = set()
second_week = set()
for sponsor_id, creative_id, week_number in log:
if week_number == 1:
first_week.add(sponsor_id)
elif week_number == 2:
second_week.add(sponsor_id)
return sorted(first_week & second_week)