Bloomberg LP Interview Question


Country: United States




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

search for 1008019 at stackoverflow. Make sure you click all the links in the article, it is quite a bit of information, but worth reading. I sent you c++ article because I think Bloomberg prefers c++ over other languages

- animageofmine June 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Something.h
#include <mutex>

class Something
{
public:
	static Something *singleton()
	{
		if (!val)
			std::call_once(val_flag, singleton_once);
		return val;
	}

private:
	Something();

	static Something *val;
	static std::once_flag val_flag;
	static void singleton_once()
	{
		static Something ret;
		val = &ret;
	}
};

//Something.cpp
Something *Something::val = NULL;
std::once_flag Something::val_flag;

Also, to add up, according to C++11 static declarations are thread safe, no need for call_once there... and call_once is C++11 feature :)

- pavelp May 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.doubble.checked.singleton;

public class DoubleCheckedSingleton {

public static void main(String[] args) {
MySingleton instnace = MySingleton.getInstance();
System.out.println(" "+instnace.hashCode());

instnace = MySingleton.getInstance();
System.out.println(" "+instnace.hashCode());
}

}

class MySingleton {

private static volatile MySingleton instance;

private MySingleton() {
// TODO Auto-generated constructor stub
}

//double locking check
public static MySingleton getInstance(){
if(instance==null) // 1st locking check without threading
synchronized (MySingleton.class) {
if(instance==null) // 2nd locking check with threading
instance = new MySingleton();
}
return instance;
}
}

- thesadanand November 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using pthread_once() method?

class Singleton
{
};

pthread_once_t once = PTHREAD_ONCE_INIT;

Singleton *obj = nullptr;

void Initilize()
{
    obj = new Singleton();
    assert(obj);
}

int main()
{
    pthread_once(&once, Initilize);
}

- Anand December 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using pthread_once

class Singleton
{
};

pthread_once_t once = PTHREAD_ONCE_INIT;

Singleton *obj = nullptr;

void Initilize()
{
    obj = new Singleton();
    assert(obj);
}

int main()
{
    pthread_once(&once, Initilize);
}

- Anand December 07, 2018 | 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