Medium · System Design

Phone directory

Design a phone directory with three calls: addContact(number, name) stores a contact under a unique digit-string number; 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

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More System Design problems