Amazon Interview Question for Interns


Team: Web Service
Country: United States
Interview Type: Phone Interview




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

As output is success and null because of first time hashcode is 0; and we insert value in
bucket '0' but second time hashcode is 10 and bucket 10 does not have any value.

- Rohit March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

output will be success and success.. because the object was already created before changing the hashchode value ... so it doesnt matter u change it or not...

- kaushik April 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I wasn't sure if there would be a null pointer exception at runtime, so I put it into eclipse. New here, but I had to change a bit of the code to get it to compile and run. Here's the code I ran, the output was

success
null

So my initial assumption about a null pointer exception was wrong.

import java.util.*;


class Test { 
public int i=0; 
@Override 
public int hashCode() { 
return i; 
} 
} 

class HashPractice{ 
public static void main(String[] args){ 
HashMap <Test, String> hm = new HashMap<Test, String>(); 

Test t1 = new Test(); 

hm.put(t1,"success"); 

System.out.println(hm.get(t1)); //print success 

t1.i = 10; 

System.out.println(hm.get(t1)); //NULL 


} 
}

- Jonathan McKay September 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

Sytax error: can not find symbol 'psvm'

- HauntedGhost March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you're missing the core concept of this question.Focus not on syntax. Rather focus on the fundamentals of hashing.

- Frank March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

I lol'd

- erez.semaria@audaxhealth.com March 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the result you said is correct.
But I run this code in eclipse, I found the result is two lines of "success".
Why is that?

- Jerry April 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's my fault.
I have made a mistake in the code. The result is correct.

- Jerry April 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Basically it depends on how hashcode is adressed on buckets.

- M@rco March 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

output is same as
success
null

- Anonymous June 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Here the Key(of HashMap) is object itself not the value of i of that object.
So the statement "t1.i = 10;" is not changing the object it just assigning value to data member.
Finallu the output is "Success"

- Dinesh March 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

When you use an object as a key, its hashcode function is called to find the bucket index of the hashmap.

- fbati March 15, 2013 | 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