Automated Traders Desk Interview Question for Software Engineer / Developers






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

exception safe and thread-safe

template <typename T>
class Singleton
{
public:
T* Instance()
{
if(!instance)
{
Lock lock(m_dataMutex); // RAII object which aquires mutex lock

if(!instance) // Double check
{
try
{
instance = new T;
}
catch(...)
{
cout<<"Exception encountered in Singleton::MyInstance"<<endl;
}
}
}

return instance;
}
private:
Singleton(){}
Singleton(const Singleton&){}
Singleton& operator=(const Singleton&){}
~Singleton(){}
static T *instance;
static Mutex m_dataMutex;
};

- ac November 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think they expect a double checking lock

- contactjey June 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes that's true.
Please refer to the solution provided by Venkat below.

- yemre_ank October 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Vinit
would you mind explaining your code a bit...would be nice. thank you.

- assHo August 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

template <typename T>
class Singleton
{
public:
T* Instance()
{
if(!instance)
{
Lock mutex; // RAII object which aquires mutex lock

if(!instance) // Double check
{
instance = new T;
}
}

return instance;
}
private:
Singleton(){}
~Singleton(){}
static T *instance;
};

In .cpp

static Singleton<T>::instance = 0

- Venkat September 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry... the Instance() function should be static

- Venkat September 27, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think Lock has to be static otherwise other threads will have their own private copy of the Lock;
Indeed the Lock should be lock and unlocked with an automatically destroyed class - RAII.

- kykydyk March 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

and you should also make the operator= private for this class.

- ac November 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't understand, why do you need a double check?

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

Efficiency :) Think hard

- M August 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

template<class C>
class Singleton
{
private:
static C * obj;
Mutex lock;

public:
static C * getSingleton()
{
if(C==NULL)
{
lock.lock();
if(C==NULL)
{
obj=new C();
}
lock.unlock();
}

return obj;
};
};

- Anonymous February 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

class foo
{
public:
static foo* instance();
private:
foo() {}
~foo() {}
static foo* smInstance;
static Lock smMutex;
};



foo* foo::smInstance = NULL;
Lock foo::smMutex;

foo* foo::instance()
{
LockLocker lock(&smMutex);

if (smInstance == NULL)
smInstance = new foo();

return smInstance;
}

- Vinit June 03, 2009 | 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