Google Interview Question
1,069 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
* Describe a balanced binary tree. * When would you want to use a balanced tree rather than a hashmap?
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (6)
1 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
A balanced binary search tree is a tree-based data structure where each node has at most two children and each level is complete except possibly the deepest. The left descendants of a given node have keys less than the node’s key and the right descendants have keys greater than the node’s key. Finding an item in a balanced binary search tree runs in O(log n). Insertion and deletion run in O(log n). Finding an item in a hashmap runs in constant time, but one would want to use a balanced binary tree if the hashmap function is imperfect and many key collisions are expected to occur. With a balanced binary tree, there will never be more than one value per node. One may also want to use a balanced binary search tree if the hash function is particularly slow.
Helpful Answer?
Yes |
No
Inappropriate?
0 of 1 people found this helpful
The reason you want balanced tree is to have all the operations be O(logn) where an unbalanced tree would have O(n) for all operations.
A hash has constant insert and O(n) search. It does have average constant time search but if you need to minimize O(), a balanced tree gets you a better search time (though worse on average)
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
1 of 1 people found this helpful
by neakor:
When ordering of elements in the data structure matters. for instance, keep the root node as the node with the largest or smallest value for fast retrieval. Also when the hierarchical parent children relationship is important for a complete traversal. for instance when a parent is marked as invisible, all of its children are also considered invisible, e.g. scene graph bounding tree.