Microsoft Interview Question
Software Engineer / DevelopersI think there are two trade-offs between a BST/map or a Hash Table
1) when the number of object to be inserted are relatively small
2) when need to iterate over the items inserted into your data-structure
for searching purpose
Then map/bst is preferred, since has relatively lower memory overhead
However with minor modification you can have iterators over hash table
also, but that incurs more memory overhead.
Comments welcome. :)
IMHO, the most important consideration is collision in hashtable.
The worst case for collision is the whole hashtable becomes a linked list, and it is O(n) to find a value.
The 2nd consideration is the insert/delete operation. It is less expensive to insert/delete for hashtalbe than BST. So if there are a lot of insertion/delete operations, hashtable is better.
Hash Table is recommended over Binary Search Tree
- When Data is in ascending/descending order
- Insertion/Deletion are not frequently required.
- Sorted sequence is not required.
- Searching is a prevalent operation.
- When data are comparatively less. When Data is more, Hashing might require more extra location to avoid Collision.
For hashtable,the date store and retrieve time is O(1).
- freemail165 September 26, 2008while for BST,it is O(log2n).
But the BST is sorted while hash is not.
SO when you want to use sorted data and with little memory/time
to do it,you had better use BST