Catch the Hacker
Course launching November 17, 2020
Follow me on YouTube for free coding interview videos.
Users who sign up for the email list will receive an exclusive 75% discount at launch.
You don’t like studying for coding interviews alone, so before you go to bed you post a signup form online where people can add their email if they want to practice interview questions together. When you wake up, you realize that other people dread studying for interviews as much as you, and A LOT of devs have signed up.
However, some prankster thought it would be funny to mess up your list, and they duplicated every email address multiple times. But this person made a mistake - the algorithm they used to duplicate emails actually created more copies of their email address in the list than anyone else. To get this hacker back, you want to find their email address in the list... using a computationally efficient algorithm! 💪 🙌
Write a function
findHackerEmail that will return the email address of the hacker.
// Input const emails = ['firstname.lastname@example.org', 'email@example.com', 'firstname.lastname@example.org', 'email@example.com']; // Output: findHackerEmail(emails); 'firstname.lastname@example.org'
Validate My Answer
The brute force solution is O(n2), but you can do better.
You may be tempted to sort the list so the duplicate emails are grouped together. Sorts cost O(n log n) which is better than a brute force solution, but think of techniques that allow us to get a better time complexity than this.
Using an additional data structure, we can get the time complexity down to O(n).