Samsung Interview Question






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

class CSingleton
{
private:
CSingleton *mInstance;
...
public:
Static Csingleton* getInstance()
{
AcquireLock();
if(mInstance==NULL)
mInstance=new CSingleton();
ReleaseLock();
return mInstance;
}
}

AcquireLock/ReleaseLock can be implemented using various different lock semantics.

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

Below code will check for the instance before it acquires the lock.

class CSingleton 
{ 
private: 
CSingleton *mInstance; 
... 
public: 
Static Csingleton* getInstance() 
{ 
if(mInstance==NULL) 
{
AcquireLock(); 
if(mInstance==NULL) 
mInstance=new CSingleton(); 
ReleaseLock(); 
return mInstance; 
}
} 
}

- Sriramulu Appana April 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't know about other languages but in C#, a good idea would be to use an inner class to create an instance of the parent class.
Since locks on placed on classes our code will be thread safe. Copying code from C# 3.0 design patterns

public class Singleton{
Singleton(){}//private constructor
class SingletonCreator{
  static SingleTonCreator(){}
  internal static readonly 
   Singleton UniqueInstance= new Singleton();
}
public static Singleton UniqueInstance {
  get {return SingletonCreator.uniqueInstance;}
}}

- prolific.coder May 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
public class MyClass { {{{ private static MyClass ms_instance; private MyClass() {} public static MyClass getInstance() { if (ms_instance == null){ synchronized(this){ if (ms_instance == null) { ms_instance = new MyClass(); return ms_instance; } else return ms_instance; } } else return ms_instance; } - tetura May 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MyClass {

private static MyClass ms_instance;

  private MyClass() {}

  public static MyClass getInstance()
  {
    if (ms_instance == null){
        synchronized(this){
          if (ms_instance == null) {
             ms_instance = new MyClass();
             return ms_instance;
          }
          else
             return ms_instance;
        }
      }
      else
         return ms_instance;
}

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

The code given in first comment is missing a private constructor. You will have to define a private constructor else a default constructor will be given that will have default access. So i can create instances using that constructor in the same package.
SO ur class is not singleton.

The code by tetura is also missing 1 thing. It is not thread safe. Although chances of race condition is very less. The problem is that the method needs to be synchronized and not the block. Because a race condition can occur on the first line if (ms_instance == null). For example, a thread checks and finds it is not null and is sent to runnable state. The other thread comes and checks and finds ms_instance to be null also. SO now both will create instances.

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

public class MyClass {


private static MyClass ms_instance;


private MyClass() {}



public static synchronized MyClass getInstance()
{
if (ms_instance == null){
ms_instance = new MyClass();
return ms_instance;
}
else
return ms_instance;
}

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

There is one more big error in tetura code snippet.
He is calling synchronized on the current object. There is no current object there. The method will be called in a static context i.e MyClass.getInstance. SO a proper synchronize will be on the class object . synchronized(Myclass.class)

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

one last thing :: the synchronized(this) error will be caught by the compiler only

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

i agree on the object lock, lock should be obtained on the class object, but dont agree on other things.
see en.wikipedia.org/wiki/Singleton_pattern#Traditional_simple_way

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

Yeah my mistake.Didnt see you have two checks for null.

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

class A
{
	private:
		A(){};
		~A(){};

		A *instance;
		Mutex mutexLock;


	public:

		A *getSingleton()
		{
			if(instance == NULL)
			{
				mutexLock.lock();
				if(instance == NULL)
				{
					instance=new A();
				}
				mutexLock.unlock();
			}

			return instance;
		};
};

- Anonymous February 28, 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