Microsoft Interview Question for Software Engineer in Tests


Team: Server and tools in microsoft erp
Country: United States
Interview Type: In-Person




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

Use randomize selection to find the 2nd largest element i.e. element at N-1 th position.

solution 2.
int max1, max2 = 0;
for i = 1 to N
if (A[i] > max2)
max2 = A[i]
if (max2 > max1)
swap (max1, max2)

return max2

- Punit Jain April 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1) initialize max1 and max2 with int.minvalue
2) you need to handle cases when the largest number appears more than once in array.

- iman.goodarzi March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<stdio.h>

int secondlargest(int arr[] , int len)
{
int num1,num2,temp,i;
int max1 = 0, max2 = 0;
num1 = arr[0];
num2 = arr[1];

if(num1 > num2)
{
max1 = num1;
max2 = num2;
}
else
{
max1 = num1;
max2 = num2;
}

for(i=2;i<len;i++)
{
temp = arr[i];
if(temp > max1 && temp > max2)
{
max2 = max1;
max1 = temp;

}
else if(temp < max1 && temp > max2)
{
max2 = temp;
}
}
printf("1st max = %d, 2nd max= %d\n", max1,max2);
return max2;
}

int main(void)
{
const unsigned int x[] = { 5, 3, 8, 6, 0, 7 };


printf("Array: ");
int i;
const int nx = sizeof x/sizeof(int);
for (i = 0; i < nx; i++)
printf("%d ", x[i]);
printf("\nSecond largest: %d\n", secondlargest(x, nx));
return 0;
}

- shiv April 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
    Find the second largest integer in an array of integers.
*/

#include <stdio.h>

int
secondlargest(const unsigned int *x, int nx)
{
        /* Find the max, then go through the array one
           more time to find the second largest.
        */

        unsigned int max = x[0];
        int i;
        for (i = 1; i < nx; i++)
                if (x[i] > max)
                        max = x[i];

        /* Find an element other than the max to start
           with.  If none found, every element is the same.
        */
        for (i = 0; i < nx; i++)
                if (x[i] < max)
                        break;
        unsigned int secondmax;
        if (i == nx)
                return -1;
        else
                secondmax = x[i];

        /* Now compare to find the max but ignore max itself */
        for (i++; i < nx; i++)
                if (x[i] == max)
                        continue;
                else if (x[i] > secondmax)
                        secondmax = x[i];

        return secondmax;
}

int
main(void)
{
        const unsigned int x[] = { 5, 3, 8, 6, 0, 7 };

        printf("Array: ");
        int i;
        const int nx = sizeof x/sizeof(int);
        for (i = 0; i < nx; i++)
                printf("%d ", x[i]);
        printf("\nSecond largest: %d\n", secondlargest(x, nx));
        return 0;
}

- Event Horizon April 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int max1=a[0],max2=a[0];
for(int i=1;i<a.length;i++)
{//First Approach
/* if(a[i]>max1)
{
max2=max1;
max1=a[i];
}
else if(a[i]>max2)
{
max2=a[i];
}*/
//Second approach
if(a[i]>max2)
{
max2=a[i];
}
if(max2>max1)
{//swap max1 and max2
max2=max2^max1;
max1=max2^max1;
max2=max2^max1;

}
}
System.out.println("Largest number"+max1);
System.out.println("Second Largest Number"+max2);

- aditya April 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will not work when first element of the array is largest number. Because you are initializing max1 and max2 with a[0] which in this will be max number. Let's say array={115,10,6,80,15,9,12}, So max1=115 and max2=115 will be initialize in first step after that max2 will never be initialize with 80.

- Manoj April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can just initialize max2=0 then it will work in an array where first element is the largest.

- Manoj April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class secondMax {
  public static void main(String args[]) {
     //int arr[] = {7,5,2,7,9,8};
     //int arr[] = {8,5,2,7,7,9};
     int arr[] = {9,5,2,7,7,8};

     int max2 = 0;
     int max=arr[0];

     for(int i=1; i<arr.length; i++) {
       if (arr[i] > max) {
         max2 = max;
         max = arr[i];
       }
       else if (arr[i] > max2) {
         max2 = arr[i];
       }
     }

     System.out.println("Max     : " + max);
     System.out.println("2nd Max : " + max2);
  }
};

- Anonymous April 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code won't work on repeated occurrences of the largest number. Eg: {5,6,8,9,9,8,6}
Slight modification required while checking if the array element is larger than max2..:: else if(arr[i]>max2 && arr[i]!=max)

- nidhi June 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SecondLargestInIntArray {

public static void main(String[] args) {

int a[] = {9,1,9,2,8,4,1,9,0,0};
int mMax,mSecMax;

mMax = a[0];
mSecMax = 0;

for (int i=1;i<a.length;i++){
//if the current int in array is > mMax, set mMax to max value
//set mSecMAx to the previous value of mMax as that will the second largest so far
if(a[i]> mMax){
mSecMax = mMax;
mMax = a[i];
}//if the first element of the array is the largest,above if condition will never be enetered
//So,mSecMax will remain as 0 which is not the desired result.
//any value that is < mMax and > current mSecMax will be assigned to mSecMax
else if((a[i]> mSecMax) && (a[i] != mMax)){
mSecMax = a[i];
}
}

System.out.println("Second Largest value : " + mSecMax);
}

}

- sid_tintin May 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
*
* @param input the input array, which should contains more than 1 element
* @return the index in the array which is the second largest element. -1 means an error
*/
public static int findSecondLarge(int[] input){
int max=0,max2=1;
int temp;
if(input==null || input.length< 2){
return -1;
}
if(input[max]<input[max2]){
temp=max;
max=max2;
max2=temp;
}
for(int i=2;i<input.length;i++){
if(input[i]>input[max]){
max2=max;
max=i;
}else if(input[i]>input[max2]){
max2=i;
}
}
return max2;
}

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

private void getSecondHighestNumber(int [] arr) {
// TODO Auto-generated method stub

int []temp = new int[2];

temp[0] = arr[0];
temp[1]=arr[1];

for (int i = 2; i < arr.length; i++) {



if(arr[i]>temp[0] && arr[i]>temp[1])
{
int k = arr[i] - temp[0];
int l = arr[i] - temp[1];

System.out.println("l="+l+"=k="+k);

if(k>l)
temp[0] = arr[i];
if(k<l)
temp[1] = arr[i];
}else if(arr[i]>temp[0] && arr[i]<temp[1]){
temp[1] = arr[i];

}else if(arr[i]<temp[0] && arr[i]>temp[1]){
temp[0] = arr[i];

}


}


System.out.println("=========="+temp[0]+"======"+temp[1]);



}

- suvrokroy May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done by Tournament selection algorithm..

Compare 2 elements at a time. And get the maximum.
Now amongst the elements which were compared with maximum, find the largest element. That will be the second maximum element.

But i am not sure how to efficiently implement this algo
Any thoughts on this?

- Ragavenderan May 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int returnSecondLargestFromArray(int a[],int size) {
   int indx;
   int max = a[0];
   int secondMax = a[0];

   if (size == 1)
      return 0;

   for (indx = 1 ; indx < size; indx++) {
      if (a[indx] > max) {
         secondMax = max;
         max = a[indx];
      } else if ((secondMax == max && a[indx] < secondMax) || a[indx] > secondMax) {
         secondMax = a[indx];
      }
   }
   return 0;
}

- yomero May 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just realized that I submitted an incomplete version, this one should work:

void returnSecondLargestFromArray(int a[],int size,int &max, int &secondMax) {
	int indx;

	if (size <= 1)
		return;

	max = a[0];
	secondMax = a[0];

	for (indx = 1 ; indx < size; indx++) {
		if (a[indx] > max) {
			secondMax = max;
			max = a[indx];
		} else if ((secondMax == max && a[indx] < secondMax) || a[indx] > secondMax) {
			secondMax = a[indx];
		}
	}
	return;
}

- yomero May 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

max1=a[0]
max2=0
for i=1 to n
       if a[i] > max1
              max2=max1
              max1=a[i]
       else if a[i] > max2 && a[i] != max1
              max2=a[i]
return max2

- Sachin June 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dear sachin,
Please do not upvote your own comment..let others do that...

- topcoder June 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@superffeng:

Is the array sorted?Are there duplicates

- Begineer June 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Test cases
1. The array is sorted.
2. The array contain all duplicates.[second largest doesn't exist]
3. Array contain both positive & negative numbers.
4. A part of the array is sorted suc that both largest & second largest exists in this sorted part.

- Aashish June 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] ar = {-4,3,7,-1,10};
		
		int max = -1;
		int Smax = -1;
		for ( int i = 0; i < ar.length ; i++){
			if (ar[i] > max){
				Smax = max;
				max = ar[i];
			}
			
			
			if(Smax < ar[i] && ar[i]!= max){
				Smax = ar[i];
			}
		
		}
		System.out.println("The second largest element is" + Smax + max);

- Pratty March 19, 2013 | 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