Goldman Sachs Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

import java.util.concurrent.atomic.AtomicInteger;


public class TrackCount {

private static AtomicInteger count = new AtomicInteger(0);

static synchronized void method1()
{
count.incrementAndGet();
}

synchronized void method2()
{
count.incrementAndGet();
}
}

- rajeshrp21 April 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Initialize counter to 1.

Method1 : counter = counter *2;
Method2: counter = counter *3;

When we have to get individual counts,
int i = 0;
while (count % 2 == 0) {
count = count /2;
i++;
}

final i is the number of times count was multiplied by 2.

- krishna December 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use two separate variables and add them. Using only one will result in race condition.

- King@Work March 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

since the method1 and method2 are synchronized, I don't think they will run in to a race condition. Because, it is not possible for two invocations of synchronized methods on same object to interleave.

correct me if I am wrong?

- Sr March 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

When you use synchronized method for static method you get lock on the class and not on class object. So in the above example you have two separate locks. On the the class and other on its object. No one blocks the other one and hence the race condition can happen.

Solution: Either use 2 variables or change static method to non-static.

- King@Work March 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@King@Work But in the question it is mentioned to use a single counter variable.

- Nomad July 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make another static synchronized method and call it in both?

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

Use increment of 10 for first method call and use increment of 11 for 2nd method call.For example if counter output is 105, that means 2nd method called 5 times as a result counter increased by 55 and first method called 5 times which increased 50 times. As a result counter = 55+50

- engineous.sumit February 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Any suggestions, something like this will work?

static Integer  counter = 0;
	static synchronized void method1(){
		synchronized (counter) {
			counter = counter+1;	
		}		
	}
	synchronized void method2(){
		synchronized (counter) {
			counter++;	
		}		
	}

- Andi November 26, 2012 | 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