runtime
364 / 500
question 364 Profiling & Optimization · medium

Customers sharing an email, without comparing every pair

A CRM clean-up finds email addresses used by more than one customer account. Emails are compared after strip() and lower(). Write shared_emails(customers) that works in one pass, so it stays fast on 100,000 accounts: the hidden test counts how often the customer list is read.

Functionshared_emails(customers)
customersa list of {"customer_id": ..., "email": ...} dictionaries
Returnsa dictionary from normalised email to the sorted list of customer IDs using it, only for emails used by two or more different IDs
Constraintread the list about once: at most 2 × len(customers) reads
Examples
callshared_emails([{'customer_id': 'C00038', 'email': 'nora_garcia@fastmail.com'}, {'customer_id': 'C00077', 'email': ' Nora_Garcia@Fastmail.com'}, {'customer_id': 'C01338', 'email': 'grace_walker29@yahoo.com'}])returns{'nora_garcia@fastmail.com': ['C00038', 'C00077']}
callshared_emails([{'customer_id': 'C1', 'email': 'a@x.com'}, {'customer_id': 'C2', 'email': 'b@x.com'}])returns{}
callshared_emails([])returns{}

Loading your progress…

Customers sharing an email, without comparing every pair — Python practice · Runtime