Amazon Interview Question
Senior Software Development EngineersTeam: AWS
Country: United States
Interview Type: Phone Interview
I'm not quite sure I get this question. A Hash Table itself is a data structure. Why would we use another data structure to make this data structure?
The question is basically asking if you have to implement your own hashTable how will you do .
1 simple approach to implement hashtable is using an array of fixed sized ( initial capacity = c) . If we consider load factor to be x, then everytime when hashtable is x% full, we will need to copy all the elements in our array a to another array with double size.
There are several variations.
1) as 'champion' said, create array of given data structure and use open addressing
2) use array of pointers to given data structure and use separate chaining. (in this case we have to insert a pointer to the data structure within the structure)
Deciding hash function depends on the content of the data structure.
for 1st method we need to have collision resolving techniques like linear probing, quadratic probing. (for more details read Data structures and algorithms by Mark Allen Weiss). And as 'champion' said when the array is full we need to increase the size and do rehashing.
I was also asked a similar question once.
- swati4agrawal April 24, 2013We can use array to implement hashtable
and for collisions we can use self balancing trees like AVL or red black trees so that the searching time is logN