Qualcomm Interview Question for Software Developers


Country: United States
Interview Type: Phone Interview




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

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

int average;
int size = 0;

void *calcAvg(void *arg);
int numbers[] = {0,1,2,3,4,5,6,7};

pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t even = PTHREAD_COND_INITIALIZER;
pthread_cond_t odd = PTHREAD_COND_INITIALIZER;

void *print_even();
void *print_odd();
int count = 0;

// This is useful when the elements are not provided via array
#define COUNT_DONE 200


int main(int argc, char *argv[])
{
if(argc < 1)
exit(0);

//initialize an array of the integers to be passed
int numSize = atoi(argv[1]);
srand(time(NULL));
int *nums = (int*)malloc((numSize)*sizeof(int));
int i ;
for(i = 0; i < numSize ; i++)
{
nums[i] = (rand() % 100) + i ;
printf("%d " , nums[i]);
size++;
}
printf("\n");
//Thread Identifier
//pthread_t avgThread;
//pthread_create(&avgThread, NULL, calcAvg, (void*)nums);
pthread_t even_thread;
pthread_t odd_thread;
pthread_create(&even_thread, NULL, print_even , (void*)nums);
pthread_create(&odd_thread, NULL, print_odd, (void*)nums);

//pthread_join(avgThread, NULL);
//printf("average = %d \n",average);
pthread_join(even_thread, NULL);
pthread_join(odd_thread, NULL);
pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&even);
pthread_cond_destroy(&odd);
free(nums);

return 0;

}



void *calcAvg(void *arg)
{
int *val_p = (int *) arg;
int sum = 0;
int i = 0;
for( i = 0; i < size; i++){
sum += val_p[i];
}
average = sum / (size);
pthread_exit(0);
}


// Print even numbers
void *print_even(void *arg)
{
int *arr_p = (int *)arg;
while(count < size)
{
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &count_mutex );
//Keep looping till the condition becomes even
while( arr_p[count] % 2 == 1 )
pthread_cond_wait( &even, &count_mutex );
printf("Array value print_even: %d\n",arr_p[count]);
count++;
pthread_cond_signal( &odd );
pthread_mutex_unlock(&count_mutex);
}
pthread_exit(0);
}



// print odd numbers
void *print_odd(void *arg)
{
int *arr_p = (int*)arg;
while(count < size)
{
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &count_mutex );
//Keep looping till the condition becomes odd
while( arr_p[count] % 2 == 0 )
pthread_cond_wait( &odd, &count_mutex );
printf("Array Value print_odd: %d\n", arr_p[count]);
count++;
pthread_cond_signal(&even);
pthread_mutex_unlock(&count_mutex);
}
pthread_exit(0);
}

- anaghakr89 April 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

class odd extends Thread
{
    int[] number;
    odd(int[] num){
       this.number = num;
    }
    @Override
    public void run(){
        System.out.println("Odd numbers are:");
        for (int i=0;i<number.length;i++)
        {
            if (number[i]%2!=0)
            {
                System.out.println(i);
            }
        }
    }
}

class even extends Thread{
    int[] number;
    even(int[] num){
       this.number = num;
    }
    @Override
    public void run()
    {
        System.out.println("Even Numbers are");
        for (int i=0; i<number.length; i++)
        {
            if (number[i]%2==0)
            {
                System.out.println(i);
            }
        }
     }

 }


public class EvenOdNumberThread { 
        public static void main(String[] args) throws InterruptedException
        {
            int[] arr = {0,1,2,3,4,5,6,7,8,9,10};

            odd odd1=new odd(arr);
            even even1=new even(arr);

            odd1.start();

            Thread.sleep(5000);
            even1.start();

        }
}

Odd numbers are:
1
3
5
7
9
Even Numbers are
0
2
4
6
8
10

- silvimasss April 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <pthread.h>
int arr[] = {3,1 ,2, 5, 6, 7, 8, 10, 9};
int i;
int lengthArray;
pthread_mutex_t mutex;

void * PrintEven(void)
{

while(i < lengthArray)
{
pthread_mutex_lock(&mutex);

if( i < lengthArray && arr[i]%2 == 0)
{
printf("Even : %d \n ",arr[i]);
i++;
}

pthread_mutex_unlock(&mutex);
}
}
void * PrintOdd(void)
{

while(i < lengthArray)
{
pthread_mutex_lock(&mutex);
if(i < lengthArray && arr[i]%2 != 0)
{
printf("odd : %d \n ",arr[i]);

i++;
}

pthread_mutex_unlock(&mutex);
}


}
int main()
{

int rc = -1;
pthread_t thread_id_even,thread_id_odd;

lengthArray = sizeof(arr)/sizeof(arr[0]);

rc = pthread_create(&thread_id_even, NULL, PrintEven, (void*)0);
if(rc) /* could not create thread */
{
printf("\n ERROR: return code from pthread_create even is %d \n", rc);
exit(1);
}

rc = pthread_create(&thread_id_odd, NULL, PrintOdd, (void*)0);
if(rc) /* could not create thread */
{
printf("\n ERROR: return code from pthread_create odd is %d \n", rc);
exit(1);
}

pthread_join(thread_id_even,NULL);
pthread_join(thread_id_odd,NULL);
return 0;
}

- nandviv April 11, 2021 | 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