Microsoft Interview | 14
My name is Ravi Chandra. Today I have attended Microsoft interview in Bangalore. I was referred by a consultant. Interviews were conducted for various positions in various teams in Hyderabad development center.
I was not able to clear the first round. But let me share the question I faced to the readers of geeksforgeeks.
All the phones stores contacts in some way. Primitive phones has less memory and less processing power. So design a data structure which stores the contacts. This should use as less memory as possible and support the algorithm to run efficiently. Basic operations on this data structure is given a number, how to lookup contact name and vice versa.
Initially I gave a hashmap solution. But he said hashmap operations are costly because of collisions etc… and asked me to provide a better solution. I proposed a linear search. But he wants a better search algorithm.
Unfortunately the better answer struck me when I came out of the interview room.
That solution is like this. Assume each contact record has unique index value. Let us maintain two sorted arrays of indices one based on phone numbers and one based on names. So the space used is less and search runs in O(log n).
Here are some thoughts by moderators:
Since the phone is of limited features, the number of contacts are also limited, typically 500 to 1000 contacts. And the contact length will be limited, assume it is 64 characters.
A phone contains atleast two types of memory, flash and RAM. Flash stores software and persistent data. RAM is used for processing.
There are various data structures to implement efficient searching. Quantitatively, as per our hypothetical feature phone of 1000 contacts, each of 64 characters long, we need [1000 * 64 * Nodesize] for the trie (an approximation). It is in the order of few kilo bytes. Where as we usually have RAM of few mega bytes.
If trie seems to be costly to the planned RAM, we can use ternary trie or compressed trie. Or even we can use radix based searching as someone pointed.
Since RAM is costly, we can allocate fixed size (say 1000) continuous blocks in flash memory to store these contacts. All we need is a dynamic search feature to access these contacts. Trie can be built in RAM whenever the user initiates search feature. Or even, since the data is constant, trie can be stored statically inside flash memory itself. During contacts update, bring that trie to RAM when search feature is called.
Note that a contact means set of data, not just phone number. It also include person name, contact number (one or more), group, assigned speed dial number, picture, etc… Be data centric while designing data structures. In the current case, search is simple feature wrapped over this data structure. Every end of successful search in our trie data structure will point to matched contact structure stored in the flash memory.
It is purely hypothetical thinking. Actual implementation will be even more complex based on required features. I hope it will be enough for interview discussion. Get sufficient details of question from the interviewer before jumping to answer. If you are on right track, the interviewer will help with some hints.
This article is compiled by Ravi Chandra. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks