Interview Question
Software Mobile Engineering Interview
-
YelpIf you have less memory storage, what would you use - Hash Table or Tree? Why?
Interview Answers
3 Answers
I think the point is, hash tables are generally fairly empty, to increase search times so they take up a lot of memory space, but with a binary search tree, the information is very dense, so it would take up less space.
Anonymous on
Yes they ask you which takes up less memory and there are multiple answers due to the number of elements (as you've stated) as well as the type of the data being stored. A hash table would be better if there aren't many elements since you typically have more slots than you have actual elements so you aren't resizing on every insert. Similarly if you're storing larger object say of 64 bytes, then the extra pointer data stored in a basic tree doesn't amount to much in comparison. However if you're storing something like a character or even a 32-bit integer, a binary tree will take more space since each node (in a basic implementation) needs to store (assuming 32-bit pointer) 64-bits with of pointers per element. Point is the question is stupid without further information since the answer depends on information they don't give you. But they do expect you to use a binary tree.
Anonymous on
If minimizing memory footprint is the only requirement, I would use nothing, since nothing doesn't take up any memory and satisfies the functional requirement. Are you asking me which of these takes up less memory? That depends on the number of elements in the storage. But usually, a hash-table has less memory overhead Trees have overhead associated with every node.
the_coder on