Home › Interview Questions › How can you extract unique domains from a comma‑se…

How can you extract unique domains from a comma‑separated string of email addresses?

🟡 Medium Coding Junior level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

To extract the unique domains from a comma‑separated string of email addresses you can split the string, isolate the domain part of each address, collect the domains in a set to remove duplicates, and then join them back into a string if needed. In Python you would do something like: email_str = 'alice@karat.com,bob@example.com,charlie@karat.com'; domains = set(); for email in email_str.split(','): domain = email.split('@')[-1]; domains.add(domain); unique_domains = ','.join(domains). The split operation runs in O(n) time where n is the number of characters in the string. Building the set also takes O(m) where m is the number of email addresses, because each insertion into a hash set is amortized O(1). The overall time complexity is O(n) and the space complexity is O(m) for the set. This approach works regardless of the number of addresses and handles cases where the same domain appears multiple times. If you need the result in a specific order you can sort the set before joining. You should also strip whitespace and normalize case (e.g., lowercasing) to avoid treating 'Example.com' and 'example.com' as distinct. This pattern is common in data cleaning tasks and can be adapted to other languages with similar string and collection utilities.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500