Storing 1 million phone numbers

What is the most efficient way, memory-wise, to store 1 million phone numbers?  Apparently this is an interview question at Google, although this seems like its a bit too easy.

💡Strategies for Solving This Problem

System Design Question

Got this at Google. It's not about writing code - it's about discussing data structures, storage, and trade-offs. This is testing your systems thinking.

What They're Really Asking

  • How would you store them?
  • How would you search them?
  • What about memory constraints?
  • Need to support prefix search? ("Find all numbers starting with 415")

Approach 1: Hash Table

If you just need fast lookup by exact number: O(1) search, O(n) space.

Pros: Super fast exact matches
Cons: Can't do prefix search, can't get sorted order

Approach 2: Trie (Prefix Tree)

If you need prefix search ("all numbers starting with 650"):

Pros: Efficient prefix search, O(k) where k is number length
Cons: More memory than hash table

This is what my interviewer wanted to hear about.

Approach 3: Database

In reality, you'd use a database with indexes. Discuss:

  • B-tree index for range queries
  • Partitioning by area code
  • Replication for availability

Phone Number Specifics

  • Fixed length (10 digits in US)
  • Can represent as integer (but watch out for leading zeros!)
  • Area code provides natural partitioning

My Interview Discussion

I talked about Trie for prefix search. Interviewer asked:

  • "What's the space complexity?" (O(n × k) where k=10)
  • "Can you compress it?" (Yes, path compression)
  • "What if numbers don't fit in memory?" (Disk-based B-tree)

Be ready to dive deep into whichever structure you choose.

Scroll to Top