Amazon Interview Question for Software Engineer / Developers






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

What was your answer ?

- amit.h1b August 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hint: Create your own custom map (extending from HashMap)

- Anonymous August 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we extend a HashMap, we will have to use our custom HashMap in places where we would use the original HashMap. If we do that ,its easy. Declare a counter and override the put method modify it so that the counter is incremented each time it is called.

- Chetan August 30, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actaully I heard a presentation on Javassist and it could be used to modify the bytecode of a class at runtime. So we could actually modify the byte code of put method of Hashmap at runtime to insert the counting logic. More about Javassist; http://www.csg.is.titech.ac.jp/~chiba/javassist/

- Chetan August 30, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

overriding.. hashcode() method and putting static variable inside it.

every time put() and get() call java will call hashcode() method to find hashcode()

- Ravi Savaliya September 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can't declare static inside a method. That is not allowed. Static variables are part of the class but not method.

- master January 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

override hashcode() method of class whose object is used as key.

- Ravi Savaliya September 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if you have more than one class whose objects are used as keys?
Can we not use Decorator pattern to add responsibility to hashmap to count the get and put method calls at runtime?

- Anonymous October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hashcode() doesn't seem to be a good idea. Put and Get method of hashmap will call the key objects hashCode(); Hence it an overhead to override HashCode function in every key objects class. Imagine key being String class.

Cleaner way is to extend HashMap class and overide get, put call..and call super.put() and get method. Use get/put count per MyHashMap object.

When it comes to passing the MyHashMap to another method, reference would be created to same hashMap object. Hence continue to use the same get and put method.

Hence at the end . when you query MyHashmap objects getPutCount()/getGetCount() you should be getting the exact number of time the call was made to get/put method.

- MasterSolution June 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

solution:

1) In case if map allows null keys then hashcode() of key doesn't get called. So hashcode() of key class can't be used for counting purpose. Hence Override will work as explained above. May be use decorator pattern.

2) How about implementing custom collection class called as MyHashMap which is very well supported by Java Collection Framework with the help of template such as AbstractMap etc.

- master January 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dude how about java Observer Class? Exploiting the observer design pattern.

- Anonymous January 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hint : Add such behaviour to Map using dynamic proxies.

- Anonymous January 24, 2010 | 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