Goldman Sachs Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

if job thread executes first and calls notifyAll() method before main thread calls wait() then it will be in infinite waiting ..
but it is not guranteed which thread will excute first so when you are running locally you are getting output...
if you want put main() in infinite loop then call sleep(10 secs) method in main thread.

- TD June 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with you. I think this is the right answer.

- Anon June 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agree, outcome is not sure, will depend on scheduling sequence by scheduler on each run.

- tarunk August 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

If main acquires the monitor first, it will start waiting (don't forger that wait releases the monitor).
When the thread acquires the monitor, it will notify main and release him from the wait.

On the other hand, if the Thread acquires the monitor first, then it will call notifyAll() before main even starts wait()ing. When the thread releases the monitor, main will start wait()ing forever and never get to print the result.

try inserting the code

Thread.currentThread().sleep(10000);

right after the line job.start() in main to demonstrate this.

- Anon June 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

synchronized(job) {
            job.wait();
        }

The above code in main method is effectively as good as nothing since main function is not re entrant . It is called only once per process.

- Nbob June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why it should wait infinitely???

And Nbob is right! main() is not reentrant method.

- coding.arya June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What does main() being non rentrant got to do with this? the lock is on Job class.

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

Why should it go into infinite waiting?

- Subhajit June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try debugging the code with value changed from 100000 to a lesser value like say 10 or 20 and see what happens.

- ASD June 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey the given snippet is not valid.
It gives lot of compile error.

Also, i agree with nboob

- Avinash June 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Avinash the code is very much valid, just put the code inside one class.
This program actually does not go into infinite waiting if you run in your IDE.

But try to debug it by putting break point in the 1st line of main method and try to step over each line. It stops at job.wait(); and goes infinite loops.

Then if you suspend the thread and resume again, it will execute the run method and prints the output with the below error message.

ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:838]

- Ashu June 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The parenthesis were missing , The correct running code is below , and it does print the output : 100000

class Job extends Thread {
        private int counter;

        @Override
        public void run() {
            synchronized(this) {
                for(int i = 0; i < 100000; i++)
                    counter++;
                
                this.notifyAll();
            }
        }
    
    
    public static void main(String[] args) throws InterruptedException {
        Job job = new Job();
        job.start();
        synchronized(job) {
            job.wait();
        }
        
        System.out.println(job.counter);
    }
}

- hululu July 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Agreed with Anon & TD...
Try to execute below code , it will go in infinite wait..
class Job extends Thread {
private int counter;

@Override
public void run() {
synchronized(this) {
for(int i = 0; i < 100000; i++)
counter++;

this.notifyAll();
System.out.println("Completed Counting......");
}
}
public static void main(String[] args) throws InterruptedException {
Job job = new Job();
job.start();
Thread.sleep(10000);
System.out.println("Waiting to get End.....");
synchronized(job) {
job.wait();
}

System.out.println(job.counter);
}
}

- manish.94u July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Will get compilation error, because static modifier is not applicable at class level

- Laxminarayana August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Illegal modifier for the class Job; only public, abstract & final are permitted

- lucky August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The interesting thing is you can kind of reproduce the random behavior if you put a Thread.sleep(1). This will cause it to sometimes go into an infinite wait OR sometimes print out the result.

- petko.ivanov September 09, 2014 | 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