Microsoft Interview Question for SDE1s


Country: United States
Interview Type: In-Person




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

{{
if(x==0){
lock(x);
if(x==0){
x=newTime();
}
unlock(x);
}
}}

- Mohamed May 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I believe the function used to get the global variable x should use Double Checked Locking mechanism. (Multithreaded Singleton)

- Anonymous May 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Looks like a singletone to me also.

- Anonymous May 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class buffer resource
{
private Date d;
public synchorized void method1()
{
if (d == null) {
d = new Date();
}
}

- Manoj May 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static volatile YourObject instance;

public YourObject getInstance() {
YourObject r = instance;
if(r == null) {
synchronized(lock) { // while we were waiting for the lock, another
r = instance; // thread may have instantiated instance
if(r == null) {
r = new YourObject();
instance = r;
}
}
}
return r;
}

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

// Mark x as volatile to make sure we always read the latest update
	volatile int x = 0;

	void setX() {
		if (x != 0)
			return;
		synchronized (x) {
			// Need to check again in case x has been set
			// by another thread after our initial check 
			if (x == 0) {
				x = new_value;
			}
		}
	}

- Anonymous September 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use pthread_once_t and pthread_once.

- TV December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

numerous ways

while( x == 0 )
{
	lock() /* more than one thread will end up waiting here first time */
	if( x == 0 ) /* one of the thread grabs a lock and first one to do will find  x is 0 */
		x=1; /* only first thread will update it, others will simply unlock and exit loop */
	unlock()
}

Other than that, there are instruction like compare and swap which are atomic

/* compare x with 0, if x is 0 then set x as 1- do this atomically */
	/* processor has instruction called cmpxchg */
	__sync_val_compare_and_swap( &x, 0, 1 ); /* compare value of x with 0, if same only then update it to 1 */

- confused_banda December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <thread>
#include <mutex>
#include <vector>

int x = 0;
std::once_flag flg;
void setX(const int n) { 
	x = n; 
	std::cout << "setX: " << n << std::endl;
}

void wait_and_set(const int id) {
	std::this_thread::sleep_for(std::chrono::microseconds(100));
	std::call_once(flg, setX, id);
}

int main() {
	std::vector<std::thread> tLst;
	for (int i = 1; i < 10; ++i) {
		tLst.push_back(std::thread(wait_and_set, i));
	}
	for (auto& t : tLst) t.join();

	std::cout << "The final value of x is " << x << std::endl;
	std::cin.get();
	std::cout << "Press Enter to continue ..." << std::endl;
	return 0;
}

- Passerby_A March 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You should use Once_Flag with a lambda function containing the initialization, something like:
using namespace std;
once_flag _fl;

void process(int* x)
{
call_once(_fl, [](){*x=newTime(); });
//do whatever to x;
}

the once_flag implements a threadsafe singleton pattern

- Zaid March 02, 2015 | 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