Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

If the key's match then the value is overwritten. In this case you have Integers as keys and since the key's match, during the second put() operation, "tom" would overwrite the previous value (which incidentally was tom) for key = 1

- Anonymous December 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct! and it does this in O(1)

- Kamy December 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is it specific to java? or does it happen in C# also ?

- juny December 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@juny: i don't think there's a concept of HashMap in C#. Dictionary is a close match in which if you try to add two entries with the same key, it throws an exception. you need to first check the dictionary using the ContainsKey() method

- The Artist December 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you use the indexer and assignment operator instead of Add for a C# Dictionary, it will add if it's not there and replace if it is.

- kunikos January 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here second Tom is overridding previous tom. Is there any way to retreive First Object (Tom)??

- Uday November 05, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

"What will happen if two different objects have same hashcode?”
call keys.equals() method to identify correct node in LinkedList and return associated value object for that key in Java HashMap
"Since hashcode is same, bucket location would be same and collision will occur in HashMap, Since HashMap use LinkedList to store object, this entry (object of Map.Entry comprise key and value ) will be stored in LinkedList.

- Minnu December 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why LinkedList? coz LinkedList doesnt maintain unquieness so if they maintain like Set then may be no collision.. so can u clarify it..

- chiranjib December 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If two different objects have the same hashCode, what happens depends on the output of equals. If key2.equals(key1), then (key1, val1) is replaced with (key2, val2), else a new entry (key2, val2) is added to the same bucket of the hash map.
Please note that hashCode determines which bucket the key-val pair goes to while equals determines whether the object exists on not.
A more interesting question would be "What happens if two objects that are équal' returns different hash codes". In that case, they would hash to different buckets and hence will result in duplicate entries.

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

1. If the key is a duplicate, then the value is overwritten. Here is code to demonstrate that:

import java.util.Map;
import java.util.HashMap;

public class DuplicateEntriesAreNotAllowed {
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap();

        map.put(1, "a");
        map.put(1, "b");

         System.out.println(map.entrySet());
     }
}

2. If 2 objects have the same hashCode and they are not equal, then that key value pair will be put in the hash table.

- Anonymous May 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is there any way we can retrieve the overridden object in hashmap or HashSet as the two objects have same hashcode and they are equal by overridden equals() method??

- Uday November 05, 2018 | 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