Qualcomm Interview Question for Software Engineer / Developers






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

int threadCount=20;
Semaphore barrierSem(0);


void barrier()
{
	barrierSem.post();
	while(threadCount)
	{
		barrierSem.wait();
		threadCount--;
	}
}


Thread1
{
	//some code
	barrier();
}

Thread2
{
	//some code;
	barrier();
}

...

Thread20
{
	//some code
	barrier();
}

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

Sounds like barrier, never used though

- Sharat February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

call pthread_join at that point to make it wait till the other finishes. I am confused what do they mean by reaching there. If its as simple then you can even use a semaphore check at that point which this earlier reached thread shall call with a sem_wait and the latter with sem_post.

- hary February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

t1.start();
t2.start();
In run method check whats the running thread if its not the one which you wanted to run first(Lets say t1).Put t2.join stmt meaning you can only run the remaining block once t2 will end the critical section.

Or just put a semaphore check.

Please correct me If i am wrong.

- kapoor February 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It seems like he is asking you to design a barrier. Here's one solution.

Initialize a semaphore called barrier with value 0. Also, have a variable num_threads, which is a count of the number of threads to be waited at the barrier. So, when a thread reaches the particular synchronization/barrier point in the code it does:

num_threads++;
if(num_threads == 2) // 2 in this case
{
for(i=0;i<2; i++)
V(barrier); // wakes up all threads blocked on the semaphore barrier..
// in this case 2
}
else
P(barrier); // block thread at this point if all threads have not reached

- Bandicoot March 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well that should work if you wrap num_threads in a mutex; but otherwise you can get your threads incrementing it at the same time, and writing back a value of 1.

Also, you give too many wakeups. You'd want to give num_threads - 1 wakeups (V(barrier)s), because the last thread doesn't need to wait. For that same reason it also doesn't need to increment num_threads to know whether it's the last thread (unless there are other threads coming along behind it, ie you're releasing 'batches' of N threads at a time).

- JeffD October 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How abt making all threads block on a condition variables and increment a counter. If the counter is equal to #threads spawned, notify all threads. This way the last thread increments the counter and notifies all threads. Till then they will be blocked.

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

This isn't safe. One of the threads might have seen and incremented the count, decided it has to wait, but gets suspended by the OS before it gets to wait on the condition. The 'last' thread comes through and broadcasts the wakeup to everyone on the condition. The 'slow' thread then finally gets there, but the signal has missed it - signals apply only to threads that are already there. So it waits forever. You have to use a semaphore, which will safely, atomically etc, keep a count.

- JeffD October 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

we can use semaphore to achieve such synchronization
initialize the semaphore value to zero
Process 1
do operations,
//sync point
signal semaphore

Process 2
do operations
wait for semaphore

- anna for May 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

simple and clean solution

- Anonymous August 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If process one starts first, where is the code that will wait for process 2?

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

sema task1;
sema task2;

in task1: in task2:
=========== ===========
wait(task2); wait(task1);

- Anonymous October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use pthread conditian wait and condition signaling mechanism this might be the better approach.

- harishonmail October 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

semaphore t1(0),t2(0)

T1()
{
....
t2.signal()
t1.wait()
}

T2()
{
....
t1.signal()
t2.wait()
}

- dontulakapil November 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If t2 is not waiting t2.signal is lost?
Alterante solution. Start a semaphore in taken state. When the common function enters, wait on the semaphore. When 2nd thread reaches the function give the semaphore so that previous thread wakesup and continous.
int threadCount=0;
Semaphore barrierSem(0);
Mutex mu;

void barrier()
{
Mutex.lock( );
threadcount++;
Mutex.unlock( );
if(threadcount ==2)
{
barrieSem.post( );
}else if (threadcount ==1)
{
barrierSem.wait(); //1st entered thread waits here
}
}


Thread1
{
//some code
barrier();
}

Thread2
{
//some code;
barrier();
}

- Reema July 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If t2 is not waiting t2.signal is lost?
Alterante solution. Start a semaphore in taken state. When the common function enters, wait on the semaphore. When 2nd thread reaches the function give the semaphore so that previous thread wakesup and continous.
int threadCount=0;
Semaphore barrierSem(0);
Mutex mu;

void barrier()
{
Mutex.lock( );
threadcount++;
Mutex.unlock( );
if(threadcount ==2)
{
barrieSem.post( );
}else if (threadcount ==1)
{
barrierSem.wait(); //1st entered thread waits here
}
}


Thread1
{
//some code
barrier();
}

Thread2
{
//some code;
barrier();
}

- Reema July 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If t2 is not waiting t2.signal is lost?
Alterante solution. Start a semaphore in taken state. When the common function enters, wait on the semaphore. When 2nd thread reaches the function give the semaphore so that previous thread wakesup and continous.
int threadCount=0;
Semaphore barrierSem(0);
Mutex mu;

void barrier()
{
Mutex.lock( );
threadcount++;
Mutex.unlock( );
if(threadcount ==2)
{
barrieSem.post( );
}else if (threadcount ==1)
{
barrierSem.wait(); //1st entered thread waits here
}
}


Thread1
{
//some code
barrier();
}

Thread2
{
//some code;
barrier();
}

- Reema July 22, 2014 | 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