Amazon Interview Question for Software Engineer / Developers






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

Not sure what Rider is talking about.

Lookup = Search for an element I thought. Where did cache/most recently used come into the picture?

Two example structures that can be used:

1. Linked List
   1.1 Insert is O(1)
   1.2 Delete is O(1)
   1.3 Lookup is O(n)
   1.4 Pros: Fast inserts and deletes, can use for any data type.
   1.4 Cons: Slow lookups.

2. Balanced Search Tree
   2.1 Insert is O(logn)
   2.2 Delete is O(logn)
   2.3 Lookup is O(logn)
   2.4 Pros: Reasonably fast inserts/deletes and lookups.
   2.5 Cons: Data needs to have order defined on it.

- T March 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sounds reasonable. But don't you think interviewer expect one solution that uses hash_table ie. provides O(1) look up ? I think intention is to know if we know how map is actually implemented.

- sonu March 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

He asked for two, he gets two :-) If he is not satisfied, can give the third.

- T March 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

T missed a very important reason to use BST, which has nothing to do with speed. For some reason he put it as a con

- JD March 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you mean Data needing to have an order defined? If not, care to clarify?

- T March 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

btw,
why didn't anyone point out that the Delete running time I gave for Linked List is wrong?

- T March 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hashtable: given a key, calculate a hashcode for the key. hashcode mod size_of_table gives you a bucket where item is placed.

Why I would not use a linked list: hashtable requires random access, which is O(n) for each insert/delete/lookup with a linked list.

It is true, that linked list is dynamic which makes it better then an array, but, in this case its not an advantage since every time the size increases every item has to be re-inserted. So inserting n items will take O(n*n) operations.

I'd use an array. Average case for insert/delete/lookup is O(1)

You're spot on for a BST. Advantage: avoid worst case O(n) operations. If you ever want to iterate over the items you get them in order thanks to BST's order property.

The last point - ordered traversal of items is really the only reason I'd used a BST backed hashtable.

Disadvantage: sacrifice best-case lookup O(1)

- JD April 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

when using Linked List, both Insert and Delete would take O(n), instead of O(1), since you need to search for duplicate for Insert and existence for Delete

- Anonymous April 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have a look at Java collections and perhaps the book from Algorithms course. Java Collection Maps come in 2 flavors - array backed and BST backed. Never (ever) use a linked list.

- JD March 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Saying "Never ever" is a sign of ignorance, IMO. There are always exceptions (or, in order not to fall in the same trap, there are likely to be exceptions).

Based on the usage patterns, there are situations where using a Linked List could be better.

btw, I was in no way suggesting that you use Linked List as a map for your daily use.

- T March 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Linked List for a map... bad bad bad solution !!!!

- Anonymous June 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Comments like this are quite idiotic.

Suppose the usage pattern was only to lookup recently inserted elements and very few deletions. Linked list works perfectly fine for this.

btw, read the question. It just asked for 2 data structures which can be used and their comparison. It does not ask for the best ones, which btw, depends on the usage... and it is ignorant to claim one is always better than the other without knowing the context of usage.

- T June 14, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The disadvantage of using hashing is that it takes up lot of space as opposed to say linked lists

- abhimanipal April 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about the keys are not numbers?

- A April 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 Hash table:
2 balance BST(red-black tree)

hash versus bst
hash take constant time for lookup/insertion , whereas bst take O(lgn)

bst versus hash
tree support range search , whereas hash don't support any search involved order
and tree is space efficient than hash

- iatbst February 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

array and linkedlist, use modulo to act as hashcode(), use linkedlist to store elements, and help solve the collision issues.

- Anonymous May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Lookup:It is like a cache, the most recent will be present in the lookup.
lookup is used to reduce the access time of most recently used elements.

Implementation:
---------------
suppose lookup size is K, then it is implemented with a hash table.

and all the elements will be stored in a BST, so insertion will take o(log n) time.

If an element is found in the lookup, its search time is 0(1), if not found search time is o(log n) to search in the BST.

- Rider March 19, 2009 | Flag Reply


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