Qualcomm Interview Question
Software DevelopersCountry: United States
Interview Type: Phone Interview
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
#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;
}
#include <pthread.h>
- anaghakr89 April 19, 2018#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);
}