Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
5
of 9 vote

single scan, logic start from end of arrays and compare

int[] top5LargestNumber(int[] A1,int[] A2,int[] A3)
{
int i,j,k,l=0;
int top5[];
if((A1.length + A2.length +A3.length) < 5)
 return 0;
i = A1.length -1;    // points to end
j = A2.length -1;
k = A3.length -1;

while(top5.length < 5) {
 large = Max(A1[i],A2[j],A3[k]) ;
 i-- if(large == A1[i])                  //decrement large pointer
 j-- if(large == A2[j])
 k-- if(large == A3[k])
 top5[l++] = large;
 }
return top5;
}

- pirate July 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

also need to check i , j, k does not below 0.
in case of array1 is having single element which is largest. we cant decrement the counter.

- Dheeraj July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Like @Apostle's solution below, I was thinking of using min-heap of size 5, but this solution is better. Good job! +1 from me.

- oOZz July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Suppose the arrays are:
A1 : {2,4,9}
A2: {1,5,9}
A3: {3,6,9}

The resultant top5 vector should be {9,9,9,6,5}, but your code results in {9,6,5,4,3}.

A trivial edit:
if(large == A1[i]) i--;
if(large == A2[j]) j--;
if(large == A3[k]) k--;

- ChaZ July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is essentially the "merge" step in merge sort (for the k-merge problem), but which starts from the end of the array instead of the head of the array, and which stops when there are 5 elements already. So the "officially suggested" solution uses a max heap of size 3, and in each step it extracts the maximum, then fill the blank with the next element in the array that extracted max belongs to.

- Chih.Chiu.19 July 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the arrays are not sorted, the best sorting algo will take O(nlogn) time. and then the linear scan approach posted by venkatesh will work. In all it will take O(nlogn) only.

- pratikrd July 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Use a min-heap of size 5. Insert the last 5 elements of first array into it.

Use the last 5 elements again from each of the remaining arrays. Insert the element into the heap if it is greater than the min-element of the heap by evicting the min-element.

- Murali Mohan July 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Apostle .. Let suppose arrays are not in sorted form. then what approach should we follow .

- sunny gupta July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sunny.gupta
For unsorted or sorted arrays just take out the six maximum elements from the three arrays two from each array. And then from those six take out the 5 maximum elements. Time complexity is O(n). I can also provide the code if you want.

- vgeek July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Vgeek
Please provide . that would be great help .
Thanks in advance.

- sunny gupta July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Vgeek
But there is a possibility to get the all 5 largest number in one array or two array also. picking up two elements from each array wont work here.

- sunny gupta July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sunny
Yes you are right that approach wont work here. We have to consider some other approach.

- vgeek July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the arrays are sorted then you just have to merge the three arrays and then match for the 5 largest numbers . Here is the code.

#include <stdio.h>
#include <conio.h>

int n;
int *topLargestElement(int a1[],int a2[],int a3[])
{
	int i=0,j=0,didx=n;
	int dest[3*n],top5ele[5];
	while(i<n&&j<n)
	{
		if(a1[i]<=a2[j])
		{
			dest[didx]=a1[i];
			++i;
			++didx;
		}
		else
		{
			dest[didx]=a2[j];
			++j;
			++didx;
		}
	}
	if(i<n)
	{
		while(i<n)
		{
			dest[didx]=a1[i];
			++didx;++i;
		}
	}
	else if(j<n)
	{
		while(j<n)
		{
			dest[didx]=a2[j];
			++didx;
			++j;
		}
	}
	i=0,j=n,didx=0;
	while(i<n&&j<3*n)
	{
		if(a3[i]<=dest[j])
		{
			dest[didx]=a3[i];
			++didx;++i;
		}
		else
		{
			dest[didx]=dest[j];
			++didx;++j;
		}
	}
	if(i<n)
	{
		while(i<n)
		{
			dest[didx]=a3[i];
			++didx;++i;
		}
	}
	j=0;
	for(i=didx-1;i>=didx-5;i--)
	{
		top5ele[j]=dest[i];
		j++;
	}
	return top5ele;
}
int main()
{
	int a1[]={2,4,9,16,19,68,78};
	int a2[]={4,8,67,106,109,115,120};
	int a3[]={9,15,19,108,119,130,190};
	n=sizeof(a1)/sizeof(a1[0]);
	int *arr=topLargestElement(a1,a2,a3);
	int i;
	printf(" %d %d %d %d %d ",arr[0],arr[1],arr[2],arr[3],arr[4]);
}

- vgeek July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i=size of first array
j=size of second array
k=size of third array
for(l=0;l<5;l++)
{
if(A1[i]>A2[j]&&A1[i]>A3[k])
{
ans[l]=A1[i];
i--;
}
else if(A2[j]>A1[i]&&A2[j]>A3[k])
{
ans[l]=A2[j];
j--;
}
else{
ans[l]=A3[k]
k--;
}
}

- Anonymous July 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Vgeek
Making one more array is not seems efficient because if all three arrays size are more . then after merging 4th array would be too large and occupy too much memory also.

Not have much idea but same logic i have used in Interview and they rejected by the same reason.

- sunny gupta July 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

private static int[] ReturnMaxFive(int[] arr1, int[] arr2, int[] arr3)
{
int []rArr = new int[5];

if (arr1.Length + arr2.Length + arr3.Length < 5)
{
throw new ArgumentException("Length is less than 5");
}

int iArr1 = arr1.Length - 1;
int iArr2 = arr2.Length - 1;
int iArr3 = arr3.Length - 1;

int irARR = 0;

while (irARR < 5)
{
int max = int.MinValue;
int ArrayNum = 0;

if (iArr1 > 0 && arr1[iArr1] > max)
{
max = arr1[iArr1];
ArrayNum = 1;
}
if (iArr2 > 0 && arr2[iArr2] > max)
{
max = arr2[iArr2];
ArrayNum = 2;
} if (iArr3 > 0 && arr3[iArr3] > max)
{
max = arr3[iArr3];
ArrayNum = 3;
}

switch (ArrayNum)
{
case 1:
rArr[irARR] = arr1[iArr1];
iArr1--;
break;
case 2:
rArr[irARR] = arr2[iArr2];
iArr2--;
break;
case 3:
rArr[irARR] = arr3[iArr3];
iArr3--;
break;
}
irARR++;
}
return rArr;
}

- really July 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

private static int[] ReturnMaxFive(int[] arr1, int[] arr2, int[] arr3)
        {
            int []rArr = new int[5];

            if (arr1.Length + arr2.Length + arr3.Length < 5)
            {
                throw new ArgumentException("Length is less than 5");
            }

            int iArr1 = arr1.Length - 1;
            int iArr2 = arr2.Length - 1;
            int iArr3 = arr3.Length - 1;

            int irARR = 0;

            while (irARR < 5)
            {
                int max = int.MinValue;
                int ArrayNum = 0;

                if (iArr1 > 0 && arr1[iArr1] > max)
                {
                    max = arr1[iArr1];
                    ArrayNum = 1;
                }
                if (iArr2 > 0 && arr2[iArr2] > max)
                {
                    max = arr2[iArr2];
                    ArrayNum = 2;
                } if (iArr3 > 0 && arr3[iArr3] > max)
                {
                    max = arr3[iArr3];
                    ArrayNum = 3;
                }

                switch (ArrayNum)
                {
                    case 1:
                        rArr[irARR] = arr1[iArr1];
                        iArr1--;
                        break;
                    case 2:
                        rArr[irARR] = arr2[iArr2];
                        iArr2--;
                        break;
                    case 3:
                        rArr[irARR] = arr3[iArr3];
                        iArr3--;
                        break;
                }
                irARR++;
            }
           return rArr;
        }

- really July 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <iostream>
#include <stdlib.h>

using namespace std;

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main(int argc, char** argv)
{
	int A1[7] = {2,4,9,16,19,68,78};
	int A2[7] = {4,8,67,106,109,115,120};
	int A3[7] = {9,15,19,108,119,130,190};

	int* tab = new int[21];
	for(unsigned int z=0; z<21; ++z)
	{
		if(z<7)
			tab[z] = A1[z];
		else if(z>=7 && z<14)
			tab[z] = A2[z-7];
		else if(z>=14 && z<21)
			tab[z] = A3[z-14];
	}
	qsort (tab, 21, sizeof(int), compare);

	for(int z=20; z>14; --z)
		cout << tab[z] << endl;
    return 0;
}

- Anonim July 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank u dude..But Why so err?..ur code is right ..Here is updAted prog in c++


#include <iostream.h>
#include <stdlib.h>
#include<conio.h>



int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

int main(int argc, char** argv)
{
int A1[7] = {2,4,9,16,19,68,78};
int A2[7] = {4,8,67,106,109,115,120};
int A3[7] = {9,15,19,108,119,130,190};

int* tab = new int[21];
for(unsigned int z=0; z<21; ++z)
{
if(z<7)
tab[z] = A1[z];
else if(z>=7 && z<14)
tab[z] = A2[z-7];
else if(z>=14 && z<21)
tab[z] = A3[z-14];
}
qsort (tab, 21, sizeof(int), compare);

for(z=20; z>14; --z)
cout << tab[z] << endl;
getch();
}

- subbu August 29, 2013 | 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