Autocomplete Contacts by Prefix Ranked by Call Frequency

Last updated: November 29, 2026

Quick Overview

Build a structure for the Phone app that supports add_call(name), which records a call to a contact, and suggest(prefix, k), which returns up to k contact names starting with the prefix, ordered by call count descending and then alphabetically.

Apple
Coding & Algorithms
Software Engineer
Apple
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Build a structure for the Phone app that supports add_call(name), which records a call to a contact, and suggest(prefix, k), which returns up to k contact names starting with the prefix, ordered by call count descending and then alphabetically.

Keypad and search suggestions in Phone and Messages are latency sensitive, so Apple interviewers like trie questions with a ranking twist. They check that the trie narrows the search and that ranking and ties are handled cleanly.

What the Interviewer Expects
  • Store names in a trie with case-insensitive keys
  • Keep call counts at terminal nodes or in a side map
  • Collect candidates under the prefix node and rank with a heap
  • Discuss caching top-k at each node for faster reads
  • Handle an empty prefix and unknown prefixes
Key Topics to Cover
tries
prefix search
heaps
ranking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Cache the top k names at every trie node; what does an update cost now?
  • Match on any word in the name, so 'jo' finds 'Mary Jones'.
  • Decay counts so recent calls weigh more than old ones.
  • Fit the structure in a few hundred KB on Apple Watch.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Operations: add_call(name) increments that contact's count, inserting it if new. suggest(prefix, k) returns up to k names whose lowercase form starts with the lowercase prefix, highest count first...

High-Level Design

Each trie node holds a dict of children and, at terminal nodes, the original-cased name. Counts live in a dict keyed by lowercase name. suggest walks the prefix to its node in O(p), then DFS collect...


Submit Your Answer
Markdown supported