Contact Book
A contact book you can add to, search, update, delete and list in sorted order.
๐งฉ Problem statement
Process commands: “add:NAME:PHONE”, “update:NAME:PHONE”, “delete:NAME”, “search:TEXT” (show “NAME PHONE” for names starting with TEXT, any case), “list” (all contacts in alphabetical order as “NAME PHONE”). Updating or deleting a missing name shows “NAME not found”.
๐ Requirements
- Add
- Search
- Update
- Delete
- Sort
๐ฅ Inputs
- COMMANDS: list of text commands
๐ค Outputs
- Results of search/list and error messages
๐ Rules
- Names are unique
- Search matches the start of the name, ignoring case
๐ง Constraints
- Up to a few hundred contacts
๐งญ Suggested approach
- Store contacts in a map: name โ phone
- Split commands on ":"
- For list, SORT(KEYS(BOOK))
๐ง Required concepts
- Maps
- Strings
- Sorting
Design your solution here. Use Run to dry-run it step by step and Run tests to check it. Hints unlock one at a time โ try on your own first!
๐งช Edge cases to test
- Update or delete a missing contact
- Search that matches nothing
- Names with different capitals
๐ Complexity analysis
Add/update/delete: O(1). List and search: O(n log n) because of sorting.
๐ Solution walkthrough (open after youโve tried!)
A map gives instant lookup by name. Update and delete share one validation check. Search uses SUBSTRING to compare the first letters in lower case. Listing sorts the keys so the output is alphabetical.
๐ Challenge extensions
- Store several numbers per contact (a list)
- Search by phone number too
- Import contacts from a single text line