Build a phone directory that parses first [middle] last, number lines and looks up numbers by last name
Problem
A support tool receives contact lines like "Ana Maria Lopez, 415-555-0134" and needs a small API to answer "which numbers do we have for the Lopez family?". Build it.
Line format. A line is a name, one comma, and a phone number.
- The name is a first name, an optional middle name and a last name: 2 or 3 words separated by whitespace (spaces or tabs, any amount). A word is any run of characters that are not whitespace or a comma.
- The number is exactly
ddd-ddd-dddd, ten ASCII digits in groups of 3, 3 and 4 joined by hyphens. - Whitespace at the start or end of the line and around the comma is ignored.
- Anything else is malformed: no comma or more than one, 1 or 4+ name words, or a number in another format.
class PhoneDirectory
new PhoneDirectory(lines = [])adds each line in order. Malformed lines, and lines that are not strings, are skipped, not thrown. Alinesvalue that is not an array throws aTypeError.skipped(a getter): a new array of the skipped lines, exactly as they were given, in order.add(line)adds one line. It returnstrueif that person did not have this number yet, andfalseif they did (nothing changes then). It throws aTypeErroriflineis not a string and aSyntaxErrorif it is malformed. A thrown line is not added toskipped; only the constructor fills that list.getNumbersByLastName(lastName)returns every number listed under that last name, for all people with it.getNumbersByName(fullName)returns the numbers of one person. The full name must match all of the person's words, so"Ana Lopez"and"Ana Maria Lopez"are different people.
Both lookups:
- ignore case and surrounding whitespace (
" lOPEZ "findsLopez), andgetNumbersByNamealso ignores how much whitespace separates the words; - return numbers in the order they were first added, each number once, even if two people share it;
- return a new empty array when nothing matches;
- throw a
TypeErrorfor an argument that is not a string.
const directory = new PhoneDirectory([
"Ana Maria Lopez, 415-555-0134",
"Luis Lopez, 415-555-0199",
"Grace Hopper, 212-555-0101",
"Lopez, 415-555-0000", // malformed: one name word
"Ana Maria Lopez, 415-555-0134", // already listed, no change
]);
directory.getNumbersByLastName("lopez"); // ["415-555-0134", "415-555-0199"]
directory.getNumbersByName("ana maria LOPEZ"); // ["415-555-0134"]
directory.getNumbersByName("Ana Lopez"); // []: a different person
directory.skipped; // ["Lopez, 415-555-0000"]
directory.add(" Grace Brewster Hopper ,212-555-0177 "); // true
directory.getNumbersByLastName("HOPPER"); // ["212-555-0101", "212-555-0177"]
directory.add("Grace Hopper 212-555-0101"); // throws SyntaxError: no commaRef. UR-1542 · © UIRound · uiround.com/questions/build-a-phone-directory-that-parses-first-middle-last-number-lines-and-looks-up-numbers-by-last-name
Unlock this question
Its tests and the tested solution are part of Pro.
- 2
- code listings
- 8
- edge cases
- 5
- follow-up questions
What's inside
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Private, and saved in this browser as you type. All your notes are on My lists.