Amazon Interview Question for Interns


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
2
of 2 vote

Hash map will give O(1) lookup. Treemap requires O(logn) .
Hash map requires space equal to the number of possible keys.
But Treemap ionly requires space equal to the amount of keys currently stored.

- isandesh7 January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

HashMap is for unordered keys; TreeMap is for sorted keys.

- Godel October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

HashMap's lookup is faster O(1).
TreeMap internally uses a balanced binary tree to store keys in sorted order. Lookup is O(logn)

- Zero October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

HashMap also uses more space, and can have degenerate cases (the lookup isn't strictly guaranteed to be O(1), and can be slower than a TreeSet for some degenerate cases). A TreeSet, as currently implemented in Java, gives you a hard guarantee of O(log n) performance.

- Anonymous October 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also what Godel said. A TreeSet keeps the keys in sorted order, according to some ordering that you would specify when creating the TreeSet (so you must be able to define some criteria, based on which the keys can be ordered). You might not care about the sortedness of the data for some applications, but it means that 1) the order of iteration when iterating through the data structure with an iterator (such as for (Type element: myTreeSet){....}) is well-defined (you cannot rely on any such thing for HashSets), and 2) if you need to, you can get the map sorted by key in O(n) time (once you already have the TreeSet).

- Anonymous October 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

HashMap is an unordered dictionary. average running time of all operations in HashMap is constant. TreeMap is an Ordered dictionary where all keys are ordered. They can be implemented using balanced binary search trees. So, all operations worst case running time would be O(logn).

- Eswar October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Worst case of HashMap is O(n). All elements have same hash key...

- kds December 13, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More