Goldman Sachs Interview Question for Software Engineer / Developers






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

1. Define a private static attribute in the “single instance” class.
2. Define a public static accessor function in the class.
3. Do “lazy initialization” (creation on first use) in the accessor function.
4. Define all constructors to be protected or private.
5. Clients may only use the accessor function to manipulate the Singleton.

public class Singleton {
private static final Singleton INSTANCE = new Singleton();

// Private constructor prevents instantiation from other classes
private Singleton() {}

public static Singleton getInstance() {
return INSTANCE;
}
}

- biggied88 March 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You might even want to synchronize the get instance method, so that it doesnt create 2 instaces when 2 threads are accessing simultaneously. This case is 100% possible though in a rare scenari0.

- OM March 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

private static final Singleton INSTANCE = new Singleton(); JVM will ensure that only one static instance so there will be not a case that 2 instaces when 2 threads are accessing

- Ashis Chanda March 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

OM is right,In a multi threaded environment,this method needs to be present within a synchronize block.Ex:

synchronize{
public static Singleton getInstance(){}
}

- Ran July 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

OM is correct only when we are implementing lazy initialization

- karthik May 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.sanjeeb.singleton;

/**
* Properties of a singletonFactory
* Private constructor
* Static getInstance Method
* static variable to return the instance
* clone method overriden
*
*/
public class SingletonFactory {
   private static final TestClass testClass = new TestClass();
   private SingletonFactory(){}
   public static synchronized TestClass getInstance() {
       return testClass;
   }
   
   @Override
   public Object clone() throws CloneNotSupportedException {
       throw new CloneNotSupportedException("Cloning not supported");
   }
}

class TestClass {
   private final int numberOfDaysInFebInALeapYear=28;

   public int getNumberOfDaysInFebInALeapYear() {
       return numberOfDaysInFebInALeapYear;
   }
}

- Anonymous October 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If a class does not implement Cloneable interface, doesn't java automatically throw CloneNotSupportedException?

- Anonymous May 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, Since Clonable is a Marker Interface and does not have any implementation and clone() method is in the Object class...you don't get Compile time exception but you will get Runtime Exception... when in the Clone method it will check for "instanceOf" clonable

- Geek July 01, 2011 | 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