Given a mobile phone with only a numeric keypad, we need to store the contacts in a way that will make the search fast.
User will punch in numbers and we have to surface all the contacts in the address book that begin with the letters corresponding to those numbers.
I was asked this in an interview and I suggested creating a trie. For each name in the address book, I suggested adding the corresponding number to the trie.
So, if the address book has the following contacts:
bob
boby
mat
mav
I would create tries using the corresponding numbers. In this case, the trie will contain:
262 (At the 2nd node 2, keep a pointer to bob)
2629 (At the node 9, keep a pointer to boby)
628 (At the node 8, keep 2 pointers, one to each of mat & mav)
Are there any better approaches?
Update: This trie is used in T9 technology described here Data structure behind T9 type of dictionary