Bloomberg LP Interview Question for Financial Software Developers






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

1. singleton : a design pattern that is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. (from Wikipedia) In a multithread program, we have to pay attention on the process of singleton class instantiation because multiple threads are in progress of intatiating the class.

2. thread-safety: For making singleton design pattern thread-safe, we can make use of mutex. In a nutshell, thread-safety means that we can make sure the correctness of execution result even if any routine or program is executed by multiple threads at the same time. In the meantime, one can address re-entrant if the routine can be executed by multiple threads simultaneously and if the correctness of execution result can be guaranteed.

- drwolf February 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Singeton
{
static Singleton* instance;
Singleton();
Singleton(const Singleton& copy);
public:
static Singleton* getInstance();
}

Singleton* Singleton::instance = NULL;

Singleton* Singelton::getInstance()
{
MutexLock(mutex);
// Use RAII - Resource Acquisition is Instantiation
if(instance) {
return instance;
} else {
instance = new Singleton();
}
}
class MutexLock{ Mutex* aMutex;
public:
MutexLock(Mutex* mutex){
pthread_mutex_lock(mutex);
aMutex= mutex;
}
~MutexLock() { pthread_mutex_unlock(aMutex);
}
}

- baski March 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I love this question: when you think that you've nailed it, you find problems, when you solve those, you discover more and so on (I had 6 iterations and couldn't finish!), so drwolf's mouthful of vagueness is useful to distract attention of the interviewer, but provides no real answer.

Your singleton's destructor isn't thread safe. Try to make it thread safe now :)

- Anonymous December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh, actually, I wrote too fast -- BEFORE trying to think about thread safety of the destructor, solve your memory leak problem (the leak isn't that important, I understand that, but the fact that your destructor isn't called at all is quite bad, if you hold some system resources locked). So, first solve the bug in your code, then try to deal with the thread safety. It's fun, I promise.

- Anonymous December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can use active singleton

private static Singleton * instance = new Singleton();

public Singleton* Singelton::getInstance(){
    return instance;
}

you don't need a mutex then. I might be wrong, any comments?

- Ryan Zhang March 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Objective of singleton pattern is to have only one instance of the class being present at any time, but if such a class exists in a multi-threaded environment, 2 threads might simul try to create a obj of the class considering no instance exists so far.So each thread should check if there exists one before it creates and that block of code should be mutually exclusive

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

Objective of singleton pattern is to have only one instance of the class being present at any time, but if such a class exists in a multi-threaded environment, 2 threads might simul try to create a obj of the class considering no instance exists so far.So each thread should check if there exists one before it creates and that block of code should be mutually exclusive

- divyaC April 24, 2010 | 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