Goldman Sachs Interview Question for Software Engineer / Developers






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

public int hashCode() {
int hash = 0;
for (int i = 0; i < length(); ++i) {
hash = 31 * hash + charAt(i);
}
return hash;
}

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

If two objects are equal as given by "equals()" then their hash codes should also be same, but if two objects have same hash code they don't necessarily need to be equal.

public class User {
	private String firstName;
	private String lastName;
	
	public boolean equals(Object obj){
		if(obj == null){return false;}
		if (obj instanceof User){
			User other = (User) obj;
			if(this.firstName.equals(other.firstName) && this.lastName.equals(other.lastName)){
				return true;
			}
		}
		return false;
	}
	public int hashCode(){
		return (firstName.hashCode() + lastName.hashCode());
	}
}

- Abdul Malik Mansoor February 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

firstName is private, how can u other.firstName)

- Anonymous February 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Since the code is in the same class, so you can use that. Only missing thing seems to be null check.

- Pravin March 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

other.firstName must b replaced wid other.getFirstName()......also instanceOf shud b replaced wid this.getClass().getName().equals(other.getClass().getName()) ......dis ll prevent comaprison between parent-subclass objects.

- chandan.here4u May 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

don't think if(obj == null){return false;} test is required.
If obj is null, instanceof operator returns false.

- tetura February 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FullName {
private String firstname;
private String lastname;

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result
+ ((lastname == null) ? 0 : lastname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof FullName)) {
return false;
}
final FullName other = (FullName) obj;
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (lastname == null) {
if (other.lastname != null)
return false;
} else if (!lastname.equals(other.lastname))
return false;
return true;
}
}

- brijpal.ism March 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Better to not use instanceof when overriding equals method ( be careful for subclass)

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

We need to check and compare the classes of both the objects in equals()

- Anonymous September 25, 2011 | 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