Microsoft Interview Question Software Engineer / Developers

  • microsoft-interview-questions
    0
    of 0 votes
    6
    Answers

    Given two sorted arrays where the size of second array is large enough to hold the first array, write code to merge them (in sorted order). Write test cases

    - Anonymous on November 26, 2009 Report Duplicate | Flag
    Microsoft Software Engineer / Developer Arrays



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 on 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 on 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 on 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 on 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 on 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 on October 27, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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