Java · Coding problem
Clean Up a Contact Import
A free coding problem from the Java 17: New APIs, Modules and the Platform skill. Read it here, then solve it in the app in Java.
- Easy
- Coding problem
- Free
- Java
The problem
A CRM in Toronto imports contacts that people paste from spreadsheets and old systems: one email address per line, with stray spaces (some of them Unicode spaces copied from documents), empty lines, comment lines starting with #, and the same person in different capitals.
Write Contacts.importEmails(text) so that it returns the clean list:
- Split the text into lines and remove surrounding white space from each line, including Unicode spaces such as the em space (U+2003).
- Skip lines that are blank (empty or only white space) and lines that start with
#. - Lowercase each address with
Locale.ROOT. - Remove duplicates, sort alphabetically, and return an unmodifiable list (adding to it must throw
UnsupportedOperationException).
The driver prints how many contacts there are, each one in brackets (so stray spaces would show), and whether the list could be changed.
Example
Input: Ana@Example.com
(an empty line)
ben@example.com
# exported from the old CRM
ana@example.com
Output: 2 contacts
[ana@example.com]
[ben@example.com]
the list is read-only
Java 11's String methods and Java 16's Stream.toList() make this short.
Constraints
- up to 1,000 lines
- lines may contain Unicode white space
How it's graded
- Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
Solve it Clean Up a Contact Import free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime