Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Arrays:
Insert, delete and search are all O(n)
Hashmaps:
Insert and Delete O(1) whereas search is O(n)
Binary tree - all are O(lg n)
For Binary tree, delete and search not O(lg n) since it is not a BST and you have no hint to decide go to which branch
For Binary tree, delete and search are not O(lg n) since it is not a BST and you have no hint to decide go to which branch
Hash map:
- Chun-Fu Chao April 09, 2012On average Search and Delete are O( 1 + n/ # of bucket )
Because there might be more than one element have the same hash value.
And we have to do the search similar to the array inside that bucket.
Worse case for Search and Delete are O(n)
e.g. everything map to the same hash value.
Binary tree:
O(logn) "If balanced"
If the binary is not balanced, it can form a shape identical to array and take O(n)
To avoid that, some self-balancing algorithm is required.
Like RB-tree