Medium · System Design
Phone Directory
Design a phone directory with three calls: addContact(number, name) stores a contact under a digit-string number not previously added; lookup(number) returns its name, or nothing; suggest(prefix) returns the names of all contacts whose number starts with prefix, in the order they were added. Return the results of the lookup and suggest calls.
Examples
Example 1
{
"ops": [
{"kind": "add", "number": "123", "name": "Alice"},
{"kind": "add", "number": "124", "name": "Bob"},
{"kind": "lookup", "number": "123"},
{"kind": "suggest", "prefix": "12"}
]
}Output: Alice; [Alice, Bob]
Example 2
{
"ops": [
{"kind": "add", "number": "5550100", "name": "Dana"},
{"kind": "add", "number": "5551234", "name": "Fay"},
{"kind": "add", "number": "5550199", "name": "Eli"},
{"kind": "suggest", "prefix": "5550"},
{"kind": "suggest", "prefix": "5551"},
{"kind": "suggest", "prefix": "556"},
{"kind": "suggest", "prefix": ""},
{"kind": "lookup", "number": "5550199"},
{"kind": "lookup", "number": "5550000"}
]
}Output: [Dana, Eli]; [Fay]; []; [Dana, Fay, Eli]; Eli; none
Rebuild it in the studio
Core interview problems are free with a full runner and lessons; Pro opens the complete library.