Adobe Interview Question for SDE1s


Country: United States
Interview Type: In-Person




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

Answer is 3.
HashSet will not keep duplicate so it will take one out of s1 and s2. Also it will take both h1 and h2

How HashSet work is
It will use hashcode to find memory to store.
It will use equals method to find duplicate. s1.equals(s2) is true
but h1.equals(h2) if false.

that's the reason that it isrecommended to override equals and hashcode both.

- varunnitk.jain May 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

mostly agree. however, equals is not override, so I think s1.equals(s2) is false since the default equals is used which is only true when both are the same object.
when added to hashset, they are duplicated since their hashcode is the same (they share the same string literal).
The above behavior shows inconsistency since only hashCode while not equals overriden

- Jackie May 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The consequence would be s2 is not added since it has the same hashcode as s1 thus treated as duplicated even though they are not "equal"

- Jackie May 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

s1.equals(s2) will return true. String class has overriden equals method and therefore s1 ans s2 will not use Object class equals method. whearas equals is not overriden in HashTest that's the reason h1.equals(h2) will return false as it is using Object class return method.

- varunnitk.jain May 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I actually mean h1, h2 not s1, s2

- Jackie May 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ans is 3. Since h1 and h2 have same hashcode, they will both go to the same bucket as since equals method is not overridden in HashTest class. If along with hashcode(), equals method would have been overridden, only h1 will go in bucket and h2 will not go in bucket.

Since string overrides both equals and hashcode, so only s1 will go in bucket.

- Prashant Gupta May 30, 2013 | 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