← Problems57. Audience segment overlapMediumSQL
00:00 / 30:00

Audience segment overlap

MediumNot attempted yetAsked at Adobe

Marketing wants to know which audience segments overlap before buying against them, so the same visitor is not paid for twice. Membership is appended by several jobs and the same visitor can be written into a segment more than once.

Input schema

segment_members segment_id int visitor_id string

Constraints

  • Return segment_a, segment_b, shared_visitors for every pair sharing at least one visitor
  • segment_a < segment_b; report each pair once, never both directions and never a self-pair
  • A visitor written into the same segment more than once is one member
  • Order by shared_visitors descending, then segment_a, then segment_b
  • Up to 50,000,000 membership rows

Example 1two segments sharing one visitor

Input · segment_members
segment_idvisitor_id
1v1
1v2
2v2
2v3
Output
segment_asegment_bshared_visitors
121

Example 2a visitor appended twice is still one member

Input · segment_members
segment_idvisitor_id
1v1
1v1
2v1
2v1
2v1
Output
segment_asegment_bshared_visitors
121

Topics

joinsdeduplicationaggregation

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.sql
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.