Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

These methods are usually overridden when you want to use objects of that type as keys in data structures like hashtables.
Whenever overriding the equals method is recommended to also override the hashCode method. That is because there must be a relationship between these two methods: equal objects must have equal hash codes, but non-equal objects can have the same hash code.

If the methods are not overridden , then using them as keys in a hashtable is practically impossible (inefficient). By default equals returns true if the two reference variables refer to the same object , and hashCode returns different hash codes for different objects.

- remus.sinorchian November 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1. Nice explanation!!

- Stupid Developer November 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Failure to override one of them while overriding other will result in inconsistent working of hash-based collections..

@Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj == null || obj.getClass() != this.getClass()) {
            return false;
        }

        Person guest = (Person) obj;
        return id == guest.id;
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((firstName == null) ? 0 : firstName.hashCode());
        result = prime * result + id;
        result = prime * result
                + ((lastName == null) ? 0 : lastName.hashCode());
        return result;
    }

- techpanja November 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

you should not use fields in hashCode() which you don't use in equals(). Otherwise you could end up with two objects which are equal but have different hashCodes.

- myk November 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean equals(Object object) {
return this == object;
}

public int hashCode() {
return VMMemoryManager.getIdentityHashCode(this);
}

- Gaurav November 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@techpanja: I think you should add instance of check before casting

Person guest = (Person) obj;

- AJ November 13, 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