Phone Directory
Updated
Problem statement
Given operations, run them in order on a phone directory and return all appended outputs in order.
Each entry in the directory pairs one name with one phone number. A name can have at most one phone number, and a phone number can belong to at most one name. The directory supports two lookups: phone by name and name by phone.
Each operation is a string array:
["ADD", name, phone]: store the new pair. If the name or the phone is already in the directory, first remove every old entry that uses that name or that phone. This operation appends nothing.["GET_NAME", phone]: append the name for this phone, or""if the phone number is not in the directory.["GET_PHONE", name]: append the phone number for this name, or""if the name is not in the directory.["REMOVE_NAME", name]: remove the entry for this name. Append"true"if an entry was removed, otherwise append"false".["REMOVE_PHONE", phone]: remove the entry for this phone number. Append"true"if an entry was removed, otherwise append"false".
A removed entry is gone from both lookups. This holds for REMOVE_PHONE and REMOVE_NAME, and for an old entry that ADD replaces.
Function
processPhoneDirectory(operations: String[][]) → String[]Examples
Example 1
operations = [["ADD","Alice","111"],["ADD","Bob","222"],["GET_NAME","111"],["GET_PHONE","Bob"],["REMOVE_PHONE","111"],["GET_PHONE","Alice"]]return = ["Alice","222","true",""]Phone 111 belongs to Alice, and Bob's phone is 222. Removing phone 111 appends "true" and removes Alice's entry from both lookups, so looking up Alice's phone at the end returns an empty string.
Example 2
operations = [["ADD","Alice","111"],["ADD","Alice","333"],["GET_NAME","111"],["GET_PHONE","Alice"],["ADD","Bob","333"],["GET_PHONE","Alice"],["GET_NAME","333"]]return = ["","333","","Bob"]Moving Alice from 111 to 333 frees her old phone, so looking it up returns an empty string. Adding Bob with 333 then removes Alice's entry, because a phone number can belong to at most one name.
Constraints
1 <= operations.length <= 10^5- Each
operations[i]is one of the five operations above. AnADDoperation has three strings, and every other operation has two. 1 <= operations[i][j].length <= 20- Names contain only English letters, and phone numbers contain only digits.
More Apple Phone Screen questions
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
- Maintain a Consistent-Hashing RingPHONE SCREEN · Seen Oct 2026
- Count Nodes in a ChainPHONE SCREEN · Seen Oct 2026
- Run-Length DecodingPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Mutable Points in a Hash Set and a ListMillennium · PHONE SCREENMillennium interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions
- Corporation Reporting LinesMicrosoft · PHONE SCREENMicrosoft interview questions
- Key-Value Store on a Block DeviceLambda · PHONE SCREEN
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)