Goldman Sachs Interview Question for Java Developers


Country: United States
Interview Type: Phone Interview




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

Double Checker Approach

- Prakash September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How can double checked lock help here? It is only used to make sure there is no race condition while initializing a singleton object. Constructor can still throw an exception.

- Chander September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Use double checker to create the instance while creation u handle the exception or u can throw the exception to the caller.

Lazy loading is the correct methodology to handle this type scenarios.

- Prakash September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Sample Program with single tons

<b>Please correct if i am wrong</b>

import java.io.IOException;

public class TestSingleTon {
    public TestSingleTon() {
        super();
    }

    public static void main(String[] args) {
        
        SingleTon1 one = SingleTon1.getInstance();
        SingleTon2 two = SingleTon2.getInstance();
        System.out.println(one == one.getInstance());
        System.out.println(two == two.getInstance());
    }

    static class SingleTon1 {
        private static SingleTon1 br;
        
        private SingleTon1()throws IOException {
            
        }
        static {
            try {
                br = new SingleTon1();
                System.out.println("Done With Instacnce1");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        public static SingleTon1 getInstance() {
            return br;
        }
    }
    
    static class SingleTon2 {
        private static SingleTon2 br;
        
        private SingleTon2()throws IOException {
            
        }
        public static SingleTon2 getInstance() {
            if(br==null) {
                synchronized(SingleTon2.class) {
                        if(br==null) {
                            try {
                                br = new SingleTon2();
                                System.out.println("Done With Instacnce2");
                            } catch (IOException e) {
                                e.printStackTrace();
                                //Either u can handle it here or u can thorw it back to the caller
                            }
                        }
                    }
            }
            return br;
        }
    }
}

- Prakash September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can achieve this Lazy loading with Exception handing in Constructor - For complete program u can refer to this link - javadiscover.com/2013/02/how-to-create-fully-singleton-design.html

- Anand September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

javadiscover forgot to make payments...is down :)

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

I think whoever is trying to get the SingleTon instance of that class should handle the exception. SingleTon class have to just throw it to the caller. It should be always caller's responsibility what action he have to take if some exception occurs.

class TestSingleTon{
	
	private TestSingleTon() throws IOException {
	}
	
	public static synchronized TestSingleTon  getInstance() throws IOException{
		return new TestSingleTon();
	}
}

- Harpal Singh September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry above code is not for SingleTon. It will always return a new instance.Exact code will be

class TestSingleTon{
	private static TestSingleTon INSTANCE=null;
	
	private TestSingleTon() throws IOException {
	}
	
	public static synchronized TestSingleTon  getInstance() throws IOException{
		if(INSTANCE==null){
			return new TestSingleTon();	
		}
		return INSTANCE;
	}
}

- Harpal Singh September 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

In case of multithreading application there are chances the above code will give 2 instace of singleton class.The best way to create singelton class is through enum.

- yogesh September 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question is not very clear. Its just to write a singleton class with the constructor throwing an IO Exception?

- nini October 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

One way would be to catch the exception in the constructor, log it and return a null instance.

- Chander September 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

How exactly do you return a NULL instance from a constructor? A constructor has no return type associated to it

- java noob January 28, 2014 | Flag


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