Microsoft Interview Question


Country: United States
Interview Type: In-Person




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

int main()
{
    int n,m,N;
    cin>>N>>m;
    int A[N],B[m];
    n=N-m; //given elements of first array
    for(int i=0;i<n;i++) cin>>A[i];
    for(int i=0;i<m;i++) cin>>B[i];
    int k=N-1,i,j;
    for(i=n-1,j=m-1;i>=0 && j>=0;){
        if(A[i]>B[j]) A[k--]=A[i--];
        else A[k--]=B[j--];
    }
    while(j>=0) A[k--]=B[j--];
    for(int i=0;i<N;i++) cout<<A[i]<<" ";
    return 0;
}

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

public static int[] merge(int[] a, int[] b, int n, int m)
        {

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

            return a;
        }

- soni vashisht September 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but you should include a condition when either i or j is equal to -1 and while loop breaks. In this condition it will simply copy the elements of another array to a.

- Nishant Pandey September 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Something like  :    int []a = new int [7]{1,3,5,7 ,0 , 0,0};
            int[] b = new int[3] { -3, 4, 6 };

- soni vashisht September 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a lot of clarification is needed before providing an answer. this will closely affect your test plan.

- earthflying September 19, 2012 | 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