KatInterview

Sponsors Active in Both Opening Weeks

SQL & DataAuto-graded · 21 test cases
EasyAuto-gradedSQL & Datasetsdeduplicationintersection

Last 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_id is a string identifying the sponsor.
  • creative_id is an integer identifying the individual ad. It has no effect on the answer.
  • week_number is 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_id or 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.