NVIDIA Interview Question for Software Engineer Interns


Team: Driver Development
Country: United States
Interview Type: Phone Interview




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

Implementation using mutex and condition var.

//
//  main.c
//  EvenOddCreat
//

#include <stdio.h>
#include <pthread.h>

pthread_t odd_thread, even_thread;
pthread_mutex_t lock;
pthread_cond_t cond;
int turn;
int max;

void* even_run(void* data)
{
	int* max = data;
	
	for(int i=0; i<*max; i+=2)
	{
		pthread_mutex_lock(&lock);
		while (turn != 0)
		{
			pthread_cond_wait(&cond, &lock);
		}
		turn = 1;
		pthread_mutex_unlock(&lock);
		printf("%d,", i);
		pthread_cond_signal(&cond);
	}
	return NULL;
}

void* odd_run(void* data)
{
	int* max = data;
	
	for(int i=1; i<*max; i+=2)
	{
		pthread_mutex_lock(&lock);
		while (turn != 1)
		{
			pthread_cond_wait(&cond, &lock);
		}
		turn = 0;
		pthread_mutex_unlock(&lock);
		printf("%d,", i);
		pthread_cond_signal(&cond);
	}
	return NULL;
}

int main(int argc, const char * argv[])
{
	turn = 0;
	max = 10;
	pthread_cond_init(&cond, NULL);
	pthread_mutex_init(&lock, NULL);
	
	pthread_create(&even_thread, NULL, &even_run, &max);
	pthread_create(&odd_thread, NULL, &odd_run, &max);
	
	pthread_join(odd_thread, NULL);
	pthread_join(even_thread, NULL);

	pthread_mutex_destroy(&lock);
	pthread_cond_destroy(&cond);
 
    return 0;
}

- Tony L February 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The mutex and cond vars should be destroyed at the end of main func. And I edited my submit to reflect that change. Apologize for my carelessness.

- Tony L February 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is the while loop necessary, can't it be just an if condition?

- Paul January 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

- (void)runNumbersTest {
    NumbersWorker *worker = [NumbersWorker new];
    worker.limit = 10000;
    
    NSThread *evenThread = [[NSThread alloc] initWithTarget:worker selector:@selector(printEvenNumbers) object:nil];
    evenThread.name = @"Even Thread";
    
    NSThread *oddThread = [[NSThread alloc] initWithTarget:worker selector:@selector(printOddNumbers) object:nil];
    oddThread.name = @"Odd Thread";
    
    [evenThread start];
    [oddThread start];
}


@interface NumbersWorker ()
@property (assign) NSUInteger limit;
@property (strong) NSConditionLock *workerLock;
@property (atomic) NSUInteger curr;
@end

@implementation NumbersWorker

static const NSInteger conditionEven = 0;
static const NSInteger conditionOdd = 1;

- (id)init {
    self = [super init];
    if(self) {
        self.workerLock = [[NSConditionLock alloc] initWithCondition:conditionEven];
        self.workerLock.name = @"NumbersLock";
    }
    return self;
}

- (void)printEvenNumbers {
    NSString *threadName = [NSThread currentThread].name;
    while (self.curr < self.limit) {
        [self.workerLock lockWhenCondition:conditionEven];
        NSLog(@"%d - %@ ", self.curr, threadName);
        self.curr++;
        [self.workerLock unlockWithCondition:conditionOdd];
    }
}
- (void)printOddNumbers {
    NSString *threadName = [NSThread currentThread].name;
    while (self.curr < self.limit) {
        [self.workerLock lockWhenCondition:conditionOdd];
        NSLog(@"%d - %@ ", self.curr, threadName);
        self.curr++;
        [self.workerLock unlockWithCondition:conditionEven];
    }
}

- Edward February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Working on linux:

#include <pthread.h>
#include <stdio.h>

pthread_mutex_t lock;
pthread_cond_t cv;
int turn = 0;

void *print_num(void *arg)
{
    int my_turn = arg ? 1 : 0;

    for (int i = my_turn; i < 100; i += 2) {
        pthread_mutex_lock(&lock);
        while (my_turn != turn)
           pthread_cond_wait (&cv, &lock);
        turn = (turn + 1) % 2;
        printf ("%d ", i);
        pthread_cond_signal (&cv);
        pthread_mutex_unlock(&lock);
    }

    printf ("\n"); 
    pthread_exit(0);
}

int main (int argc, char *argv[])
{
    pthread_mutex_init(&lock, NULL);
    pthread_cond_init (&cv, NULL);

    pthread_t odd, even;

    pthread_create(&even, NULL, print_num, (void *)0);
    pthread_create(&odd, NULL, print_num, (void *)1);

    pthread_join(even, NULL);
    pthread_join(odd, NULL);

    pthread_mutex_destroy(&lock);        
    pthread_cond_destroy(&cv);         
    return 0;
}

- Westlake February 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>       
#include <thread>         
#include <chrono>         
#include <mutex>          
#include <condition_variable>

using namespace std;

mutex mtx;
condition_variable cv;

int nGoEven = 1;
int nGoOdd = 0;

void print_even(const int nMax) {
	for (int i = 0; i < nMax; ++i) {
		unique_lock<mutex> lck(mtx);
		while (nGoEven == 0) cv.wait(lck);
		cout << "   " << i * 2 << endl;
		nGoEven = 0;
		nGoOdd = 1;
		this_thread::sleep_for(chrono::microseconds(1));
		cv.notify_one();		
	}
}

void print_odd(const int nMax) {
	for (int i = 0; i < nMax; ++i) {
		unique_lock<mutex> lck(mtx);
		while (nGoOdd == 0) cv.wait(lck);
		cout << "   " << i * 2 + 1 << endl;
		nGoEven = 1;
		nGoOdd = 0;
		this_thread::sleep_for(chrono::microseconds(1));
		cv.notify_one();
	}
}

int main(int argc, char *argv[])
{
	int nMax = 20;
	
	thread runner1(print_even, nMax);
	thread runner2(print_odd,  nMax);

	runner1.join();
	runner2.join();

	getchar(); 
	return 0;
}

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

gurumanblogs.wordpress.com/2014/03/22/multi-threading/

- guruman March 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>
#include <pthread.h>
void printOdd(void *max){
int limit;
int i = 1;
limit = (int)max;
while(i <= limit){
        printf("%d\n",i);
        i = i+2;
        sched_yield();
}
}

void printEven(void * max){
int limit;
int i = 0;
limit = (int)max;
while(i <= limit){
        printf("%d\n",i);
        i = i+2;
        sched_yield();
}

}

int main(){
int limit;
printf("Enter the maximum value till which you would like to print\n");
scanf("%d",&limit);
printf("\nPrinting\n");
pthread_t evenThread, oddThread;
pthread_create(&evenThread, NULL, printEven, (void *)limit);
pthread_create(&oddThread, NULL, printOdd, (void *)limit);
pthread_join(evenThread, NULL);
pthread_join(oddThread, NULL);
return 0;
}

- nivi1991 February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work. You need to have an explicit synchronization mechanism.

- Anonymous February 14, 2014 | Flag


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