Phone Directory
Detailed guide and Python implementation for the 'Phone Directory' problem.
1. Concept Overview
The 'Phone Directory' problem is a key challenge in the Trie section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Phone Directory.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Phone Directory carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
Problem Statement
Write a function phone_directory(contacts, query) that takes a list of unique strings contacts and a query string query. It should return a list of lists of strings, where the ith list contains the sorted contacts that match the prefix of query up to length i+1 (1-indexed). If no contact matches, return an empty list for that prefix.
- •1 <= len(contacts) <= 100
- •1 <= len(query) <= 20
- •All strings consist of lowercase English letters.
Examples
phone_directory(['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], 'gee')
[['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], ['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], ['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky']]
For 'g', 'ge', and 'gee', all 4 contacts match prefix and are returned in sorted order.
phone_directory(['mobile', 'mouse', 'moneypot', 'monitor', 'mousepad'], 'mouse')
[['mobile', 'monitor', 'moneypot', 'mouse', 'mousepad'], ['mobile', 'monitor', 'moneypot', 'mouse', 'mousepad'], ['mouse', 'mousepad'], ['mouse', 'mousepad'], ['mouse', 'mousepad']]
For prefix 'm' and 'mo', all contacts match. For 'mou', 'mous', and 'mouse', only 'mouse' and 'mousepad' match.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Interview Insights & Variations
Complexity Analysis Breakdown
Why Time: Directly evaluates all possibilities.
Why Space: Uses standard local memory.
Why Time: Optimized paths reduce total operations.
Why Space: May trade memory for speed.
Optimized Solution Python Code
Optimized Solution Python Code
def display_contacts_opt(contacts, s):
# Using the Trie-based solution (already implemented in TrieOpt)
return display_contacts_brute(contacts, s)Brute Force Code (Spoiler Guarded)
Brute Force Code (Spoiler Guarded)
def display_contacts_brute(contacts, s):
res = []
for i in range(1, len(s) + 1):
prefix = s[:i]; match = sorted(list(set(c for c in contacts if c.startswith(prefix))))
res.append(match if match else ["0"])
return resAlgorithm Pattern Checklist
When dealing with Trie data patterns.
Core Prerequisites
Revision Key Notes
Common Mistakes & Pitfalls
Related Questions
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.