Okay .. I agree with multi-index, and this is the right way to do it. However, for an interview, they probably want you to think about it and explain something, and not just say that you need to use boost. If they ask about internal incentives, maybe it is embarrassing if you cannot explain it properly.
So here is a possible solution.
First of all, do not use a hash table for this. Phones and names can be easily sorted, and you should probably use some sort of balanced search tree or trie if you want an interactive search (http://en.wikipedia.org/wiki/Trie). IMHO, the hash table in this case is a big waste of space.
Suppose the names are unique and the numbers are unique. Then you can:
1 Data structure for storing your data
struct Phone {
2- Create two trees for the name and one for the phone
BalancedSearchTree<PhoneBookEntry> tree_by_name; BalancedSearchTree<PhoneBookEntry> tree_by_number;
3- This is it. Look at each tree for everything you need.
bool PhoneBook::getByName(const std::string &name, PhoneBookEntry &o) { tree_by_name.get(name, o); return !o.empty(); } bool PhoneBook::getByNumber(const Phone &p, PhoneBookEntry &o) { tree_by_number.get(p, o); return !o.empty(); }
Good luck.
source share