Microsoft Interview Question for Software Engineer / Developers






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

Approach:
1. Set the iterator in both the array
2. Compare the item and do just increment, or increment with FirstArray right shift by one position.
3. Augment Second array to FirstArray if it still remains after first iterator finishes.

int [] MergeSortedArray(int FirstArray[], int elementCountInFirstArray, int SecondArray[], int sizeofSecondArray){
   int i=0,j=0;
   while(i<elementCountInFirstArray){
      if(FirstArray[i] >= SecondArray[j]){
          if(FirstArray[i] == SecondArray[j]){
              i++;
          }
          int k= i;
          while(k<elementCountInFirstArray){
              FirstArray[k+1] = FirstArray[k];
              k++;
          }
          FirstArray[i]=SecondArray[j];
          j++;
          elementCountInFirstArray++;
     }
          i++;
   }
   while(j<sizeofSecondArray){
    FirstArray[i++] = SecondArray[j++];
   }
   return FirstArray;
}

Please feel free to comment.

Thanks
Ankush

- Ankush Bindlish November 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algorithm:-

Let the two arrays be a[n], b[m+n+c]//m elements & c >=0
Let i be the iterator for a[]
Let j be the iterator for b[]
Let k be the position that needs to be filled

i = index for last element of a[] = n-1
j = index for last element of b[] = m-1
k = m+n-1;

while(i>=0 && j>=0)
{
    if(a[i]>b[j])
        b[k--] = a[i--]
    else
        b[k--] = b[j--]
}
while(i>=0)
{
    b[k--] = a[i--];
}

- Amit Grover November 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int[] MergeSortedArray(int firstArray[],int secondArrady[], int firstSize,int secondSize){
int k = firstSize + secondSize-1;
while(firstSize>=0 && secondSize>=0)
{
if(a[firstSize]>b[secondSize])
{
b[k--] = a[firstSize--];
}
else
{
b[k--] = b[secondSize--];
}
}
while(i>0)
{
b[k--] = a[firstSize--];
}
}

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

nice algo! one more thing to consider: one array in ascending order while the other in descending orde.

- Anonymous February 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Copy all the items into the large array and perform a sort again. O(n log n) if you compare each item and push into the large array from the small array it will require moving items in the large array which is boring and useless.

- Anonymous December 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let i & j point to element in arrays A & B
and k points to position to be filled in B(assuming B is larger one)

k = m+n-1
i = m-1
j = n-1

instead of merging from the beginning, start merging from the end(in decreasing order).

- Phani Deepak October 27, 2010 | 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