Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

1. sort the array A => 1,2,3,4,5,6,7,8,9
2. Include the max element in the set S i.e S = {9} and min = 9, i = 8 (index of max)
3. now for all i.
   If (A[i] + min >= K) then add A[i] into set S.
   if (A[i] < min) then min=A[i]
   --i

- Anonymous November 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very clean, correct solution. Checking A[i] < min is unnecessary because it's always true unless they're equal (since your array is sorted). Better to skip that if statement. Also no need to maintain a set. Try just:

i = A.length-1;
while ( (i>=1) && (A[i-1] + A[i] >= K) )
{
--i;
}
return A.subArrayStarting(i);

Hell, you could even do a binary search. Won't really matter because your algo is O(N logN) because of the sort. There's an O(N) algo.

- eugene.yarovoi November 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain the O(n) algo (without sorting)?

- Anonymous November 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n) algo ( without sorting )
1. max = maximum element seen still yet. Initialize with A[0]
1. start from second element of A.
2. stop if next visited element i.e A[i] + max is >= K.
3. Now we have got our first element in the set. i.e maximum of A[i] and max. Store the min of these two in the min. (min is min element in the set)
4. next check for all element whose when added with min then the result >= K. update the min and continue.

std::vector<int> numSet;
int i = 1;
int max = A[0];
int min = 0;
for (i = 1; i < n; ++i) {
if (max + A[i] < K) {
    if (max < A[i]) max = A[i];
} else {
    min = (max < A[i]? max: A[i]);
    if (min == A[i])
        numSet.push_back(max);
    else
        numSet.push_back(A[i]);
}

for (j=i+1; j <n; ++j) {
    if (min+A[j] >= K) {
        if (min < A[j]) {
            numSet.push_back(A[j]);
        } else {
            numSet.push_back(min);
            min = A[j];
        }
    } 
}
numSet.push_back(min);
}

- Anonymous November 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

function(A)
{
min = A[0];
for i = 1 to n
{
if(A[i] + min >= k)
{
set.add(max(A[i], min));
min = min(A[i], min);
}
else
{
min = max(A[i], min);
}
}
if(set.length() > 0 || min >= k)
set.add(min)
output set
}

- jackass November 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

First we sort the array. Assume A is sorted.

Let B be a subset of A whose minimum element is b. Now every element x in B must be at least K-b.

So, here is the algorithm: for every element in A, see how many elements are at least K minus that element. Take the maximum over all.

This takes O(n logn) in total

- KIan November 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didn't get your solution. Could you please explain more elaborately, may be with some pseudo code ?

- ShadowFax November 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sort(A)
max = 0;
max_index = 0;
for (int i=0; i < A.size(); i++){
int x = {the minimum index such that} A[x] >= K-A[i]
if (n-x >= max){
max = n-x;
max_index = i;
}
}

for (int i=0; i < A.size(); i++)
if (A[i] >= A[max_index])
cout << A[i] << " ";

- Kian November 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution...

- King@Work November 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

will it work if a[i] > k and also if the array has negative elements.. using binary search for finding the arr[i] [{the minimum index such that} A[x] >= K-A[i]] may have to be modified if it has duplicates.

- King@Work November 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Please let me know what extra information is needed. In the above example output = {8,6,7,9} because all pair in it, <8+6>, <7+8>, <8+9>, <6+7>, <6+9>, <7+9> greater than 12.

- sonali.kapor007 November 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Either the question is wrong or example or its poorly explained

- aladin November 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why do u think that the que is wrong?

- Anonymous November 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the example in the question is what is required then here's an O(n) solution. all the elements >= k/2 should be in the result set.

- Algo Visa November 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

won't really work though. let me think a little more

- Algo Visa November 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is almost correct. The only thing is that you might include one element < k/2 depending on the parameters of the set >= k/2

- eugene.yarovoi November 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

void printMaxSubsetWithAllPairSumMoreThanK(int[] a, int k) {
    int minSubsetElement = Integer.MAX;
    for (int i = 0; i < n; ++i) {
        if (a[i] > (k >> 1)) {
            System.out.println(a[i]);
            if (minSubsetElement > a[i]) {
                minSubsetElement = a[i];
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        if (minSubsetElement > a[i] && minSubsetElement + a[i] > k) {
            System.out.println(a[i]);
            break;
        }
    }
}

- kartikaditya December 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do we assume input: |A|>1?
what is the output for A={4}, K=3?

- duchino November 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The subset {5, 7, 8, 9} is incorrect as the (5 + 7) = 12 ! > 12

- Mukesh Kumar November 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A refined solution of 'Anonymous' :)
It is O(n) solution and only requires 2 passes over the array. Would like to see if there are any cases breaking the code. Code follows:

int A[] = {1,2,3,4,10,5,6,7,8,9} ;
    int array_length = sizeof (A)/sizeof(int) ;
    int B[100], b_index = 0 ;

    int max_elem = 0, max_index = 0, K = 14;
    max_elem = get_max_value(max_index, A, array_length) ;
    std::cout<<"Max Elem:"<<max_elem<<endl;
    std::cout<<"Max Pos :"<<max_index<<endl;

    B[b_index++] = max_elem ;
    for (int index = array_length - 1; index >=0 ; --index)
    {
        if (index == max_index)
            continue ;
        if ((A[index] + max_elem) > K)
        {
            B[b_index++] = A[index] ;
            if (A[index] < max_elem)
            {
                max_elem = A[index] ;
            }
        }
    }

max_elem is the size of longest such set and B[] contains the set itself.

- Anonymous November 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And ohh..i forgot the name too. The above post is by me : Mukesh Kumar

- Mukesh Kumar November 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

u preassumed the sorted array , so if array is not sorted, this solution is nlgn

- Anonymous December 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

u preassumed the sorted array , so if array is not sorted, this solution is nlgn

- Anonymous December 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It needs O(n log n). Your O(n) solution requires the array to be sorted.
One the array is sorted, the rest could be done in O(n)

- Kian November 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1, loop over array
       if (element >= k/2){
           store it in result array
       }

2, find the smallest one in result array

3, loop over array
        if(element + smallest one > k){
            add element in result array
        }

4, return result array

step 3 can be optimized cause the elements in result array can be skipped

- Tomato November 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is almost correct except for a minor bug. We should not add all elements satisfying the if() condition of the third step, because then two elements added in this manner will have a sum less than k. Instead, if there are say 'm' number of elements satisfying the if() condition of the 3rd step, then we have 'm' different subsets with the same length satisfying the all pair sum criterion of the question. All of these should be displayed...

- anoopananth December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n)
1)Find in o(N) the largest number <= k/2 ( lets call it Min)
2)Find in o(N) the smallest number >= k/2 and count the elements larger the k/2 (lets call it Max, and cnt the number of elements)
if Min + Max >= k:
result = cnt + 1
else:
result = cnt

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

private static Set printMax(int[] a, int length, int k) {
	
		int diff = k- a[0];
		int i=1; 
		Set<Integer> result = new HashSet<Integer>();
		result.add(a[0]);
		while (i <length){
			if (a[i] > diff){
				result.add(a[i]);
				int tempDiff = k-a[i];
				if (tempDiff > diff){
					diff = tempDiff;
				}
			}
			i++;
		}
		return result;
	}

- Engy ali November 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the algorithm is O(n).
depending on the concept that for any two elements in the list, X+Y > K => K-X < Y.
for any newly added element Y, it must satisfy the above inequality, then after adding new element Y we must update the K-X

- Anonymous November 23, 2012 | 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