Design an in-memory contact directory with exact and prefix search

Quick Overview

Design an in-memory contact directory that supports exact and prefix search on first name, last name and phone, with case-insensitive names and results sorted by ID in ASCII order. It tests trie-based indexing, consistent normalization, and removing stale index entries when a contact is updated.

Design an in-memory contact directory with exact and prefix search

Company: CRED

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an in-memory contact directory that stores contacts keyed by a unique, case-sensitive `id`. Each contact has a `firstName`, a `lastName`, and a `phone`. Inserting a contact with an existing `id` replaces every field of that contact, and different IDs may share the same name or phone. The directory supports searching any one field by exact match or prefix match. Name matching is case-insensitive (ASCII `A-Z` folds to `a-z`), while phone matching compares digit strings exactly, so leading zeros matter. An empty query matches every contact in prefix mode and no contact in exact mode. Search results are returned in ascending ASCII order of ID. After an update, the contact must no longer be found under any of its old values, either exactly or by prefix. Lookups must go through a prefix-capable index; scanning every contact on each query is not allowed. This is a serial, single-threaded data-structure exercise. Implement the following methods: - `upsertContact(id: str, firstName: str, lastName: str, phone: str)`: create a contact, or replace all fields of the existing contact with this ID, removing its stale index entries. - `searchIds(field: str, query: str, prefix: bool) -> List[str]`: return the IDs whose given field exactly equals (`prefix=false`) or starts with (`prefix=true`) the query, sorted in ascending ASCII order. Names compare case-insensitively. - `countMatches(field: str, query: str, prefix: bool) -> int`: return the number of IDs the equivalent `searchIds` call would return. For example: ```text upsertContact("a1", "Ann", "Lee", "0123") upsertContact("B2", "anna", "Smith", "0123") upsertContact("c3", "Bob", "Lee", "123") searchIds("firstName", "an", true) -> ["B2", "a1"] searchIds("firstName", "ANN", false) -> ["a1"] countMatches("phone", "123", false) -> 1 countMatches("lastName", "", true) -> 3 searchIds("lastName", "", false) -> [] upsertContact("a1", "Zed", "Moss", "555") searchIds("firstName", "an", true) -> ["B2"] countMatches("lastName", "Lee", false) -> 1 searchIds("phone", "0", true) -> ["B2"] countMatches("lastName", "", true) -> 3 ``` "B2" sorts before "a1" because uppercase letters come before lowercase letters in ASCII. The exact phone query "123" matches only c3, since "0123" is a different digit string. After the second upsert of a1, its old first name, last name and phone no longer match anything, and the total stays at 3 because the upsert replaced a1 in place. ```hint Index shape Look for a structure where walking the query one character at a time ends on a node that already knows which contacts sit at or below it. ``` ```hint Updates Removing stale matches needs the contact's previous values. Think about what has to be cleaned along the whole path of each old value, and when a node can be dropped. ``` ### Constraints & Assumptions - At most 20,000 total operations. - At most 10,000 live contacts. - The total number of IDs returned across all `searchIds` calls is at most 200,000. - `id` is non-empty and contains only case-sensitive ASCII letters and digits. - `firstName` and `lastName` contain 1 to 32 ASCII letters. - `phone` contains 1 to 20 digits, and leading zeros are significant. - `field` is one of `"firstName"`, `"lastName"`, `"phone"`. - Query length is 0 to 32 for name fields and 0 to 20 for `phone`, using the same character rules as the field. ### What a Strong Answer Covers - A prefix-capable index per field that answers both exact and prefix queries without scanning all contacts. - One normalization rule applied the same way at insert, removal and query time: ASCII case folding for names, phone digits kept verbatim. - Update handling that removes every stale exact and prefix entry of the old values before indexing the new ones, without double-counting a replaced ID. - The empty-query rules and ASCII ordering of the returned IDs. - Time and space complexity of each method, including why `countMatches` does not need to build a list. ### Follow-up Questions - How would you add `deleteContact(id)`, and what in the index has to change? - A prefix query can match thousands of IDs. How would you return results in pages of `k` IDs in ASCII order without sorting the whole match set on every call? - How would the design change if many threads called these methods at once? - If the directory grew to millions of contacts, where does the memory go, and how would you cut it?

Overview: Design an in-memory contact directory that supports exact and prefix search on first name, last name and phone, with case-insensitive names and results sorted by ID in ASCII order. It tests trie-based indexing, consistent normalization, and removing stale index entries when a contact is updated.

CRED logo
CRED
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an in-memory contact directory that stores contacts keyed by a unique, case-sensitive id. Each contact has a firstName, a lastName, and a phone. Inserting a contact with an existing id replaces every field of that contact, and different IDs may share the same name or phone.

The directory supports searching any one field by exact match or prefix match. Name matching is case-insensitive (ASCII A-Z folds to a-z), while phone matching compares digit strings exactly, so leading zeros matter. An empty query matches every contact in prefix mode and no contact in exact mode. Search results are returned in ascending ASCII order of ID.

After an update, the contact must no longer be found under any of its old values, either exactly or by prefix. Lookups must go through a prefix-capable index; scanning every contact on each query is not allowed. This is a serial, single-threaded data-structure exercise.

Implement the following methods:

  • upsertContact(id: str, firstName: str, lastName: str, phone: str) : create a contact, or replace all fields of the existing contact with this ID, removing its stale index entries.
  • searchIds(field: str, query: str, prefix: bool) -> List[str] : return the IDs whose given field exactly equals ( prefix=false ) or starts with ( prefix=true ) the query, sorted in ascending ASCII order. Names compare case-insensitively.
  • countMatches(field: str, query: str, prefix: bool) -> int : return the number of IDs the equivalent searchIds call would return.

For example:

upsertContact("a1", "Ann", "Lee", "0123")
upsertContact("B2", "anna", "Smith", "0123")
upsertContact("c3", "Bob", "Lee", "123")
searchIds("firstName", "an", true)      -> ["B2", "a1"]
searchIds("firstName", "ANN", false)    -> ["a1"]
countMatches("phone", "123", false)     -> 1
countMatches("lastName", "", true)      -> 3
searchIds("lastName", "", false)        -> []
upsertContact("a1", "Zed", "Moss", "555")
searchIds("firstName", "an", true)      -> ["B2"]
countMatches("lastName", "Lee", false)  -> 1
searchIds("phone", "0", true)           -> ["B2"]
countMatches("lastName", "", true)      -> 3

"B2" sorts before "a1" because uppercase letters come before lowercase letters in ASCII. The exact phone query "123" matches only c3, since "0123" is a different digit string. After the second upsert of a1, its old first name, last name and phone no longer match anything, and the total stays at 3 because the upsert replaced a1 in place.

Constraints & Assumptions

  • At most 20,000 total operations.
  • At most 10,000 live contacts.
  • The total number of IDs returned across all searchIds calls is at most 200,000.
  • id is non-empty and contains only case-sensitive ASCII letters and digits.
  • firstName and lastName contain 1 to 32 ASCII letters.
  • phone contains 1 to 20 digits, and leading zeros are significant.
  • field is one of "firstName" , "lastName" , "phone" .
  • Query length is 0 to 32 for name fields and 0 to 20 for phone , using the same character rules as the field.

What a Strong Answer Covers Guidance

  • A prefix-capable index per field that answers both exact and prefix queries without scanning all contacts.
  • One normalization rule applied the same way at insert, removal and query time: ASCII case folding for names, phone digits kept verbatim.
  • Update handling that removes every stale exact and prefix entry of the old values before indexing the new ones, without double-counting a replaced ID.
  • The empty-query rules and ASCII ordering of the returned IDs.
  • Time and space complexity of each method, including why countMatches does not need to build a list.

Follow-up Questions Guidance

  • How would you add deleteContact(id) , and what in the index has to change?
  • A prefix query can match thousands of IDs. How would you return results in pages of k IDs in ASCII order without sorting the whole match set on every call?
  • How would the design change if many threads called these methods at once?
  • If the directory grew to millions of contacts, where does the memory go, and how would you cut it?