Microsoft Interview Question for Software Engineer / Developers


Team: Global Foundation Services
Country: United States
Interview Type: In-Person




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

1. Have a array [0-26]. This will initially have 0's
2. Traverse through Array A. And increment counters of the array we have, when we encounter respective values.
3. Similarly traverse Array B.
4. Now traverse the array and print the construct the new Array or print the output.

It will take O(n) time and O(n) space.

- Murali January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

count sort

- Anonymous January 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

//
//  main.cpp
//  MergeCharArrays
//
//  Created by Srikant Aggarwal on 09/01/12.
//  Copyright 2012 NSIT. All rights reserved.
//

#include <iostream>

using namespace std;

int main (int argc, const char * argv[])
{

    int n, m;
    int i = 0;
    
    cin >> n >> m;
    
    char *A = new char[n];
    char *B = new char[m];
    
    char temp[26] = {0};
    
    while(i < n)
    {
        cin >> A[i];
        temp[A[i] - 'a']++;
        i++;
        
    }
    
    i = 0;
    while(i < m)
    {
        cin >> B[i];
        temp[B[i] - 'a']++;
        i++;
    }
    
    char *C = new char[n+m];
    i = 0;
    int index = 0;
    
    while(i < 26)
    {
        int j = temp[i];
        
        while(j > 0)
        {
            C[index] = i+'a';
            index++;
            j--;
        }
        
        i++;
    }
    
    i = 0;
    while(i < (n+m))
    {
        cout << C[i] << " ";
        i++;
    }
    
    return 0;
}

- Srikant Aggarwal January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Coudn't have thought of a better solution!!! anyone who does +1 any other solution on this page don't deserve to be on this site. Perfect O(n).

- godzilaa January 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the merge step of merge sort can be used.

- Jester January 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

merge step of merge sort assumes, the two array's in sorted order. hence.. O(N)
there is nothing said about given arrays

- mrb January 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Unless the two arrays are sorted, how can this be done in O(n)?

- sajit.kunnumkal January 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If one of the arrays have space equal to 2N (size of first array + size of second array), then we can use the following algorithm:

1. Place a pointer at the end of both the arrays. Here 'end' refers to the last element in each array that has a character (remember that one of the arrays will have empty space since its 2N).

2. Place another pointer at the end of 2N (this will be the write pointer).

3. Then simply pick the character from the larger of the two characters from the array (using the pointer in step 1).

4. Whichever array you picked the character from, decrement the pointer in that array and also decrement the write pointer (from Step 2).

You are done in O(2N) = O(N) time.

If both the arrays are of size O(N) - that is no empty space, then simply use count sort, while scanning both arrays. You will need a temporary 'aux' array, so if the question demands O(1) space, then we will need some smart strategy.

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

how will you get a sorted array by using this technique untill the two arrays given are sorted??

- james January 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mergeAndSortTwoArrays(vector<char> &str1,
vector<char> &str2, vector<char> & str3)
{
int flag[26];
for (int i=0;i<26;i++)
{
flag[i] =0;
}
for (int i=0;i<str1.size();i++)
{
++flag[str1[i]-'a'];
}
for (int i=0;i<str2.size();i++)
{
++flag[str2[i]-'a'];
}
for (int i=0;i<26;i++)
{
for (int j=0;j<flag[i];j++)
{
str3.push_back('a'+i);
}
}
}

- bo hou January 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<stdlib.h>
int main()
{
char a[]={'a','v','s','w','a','v','n'};
char b[]={'q','r','y','i','s','f'};
int ar[26]={0};

for(int i=0;i<7;i++)
{

ar[a[i]-'a']++;


}

for(int i=0;i<6;i++)
{

ar[b[i]-'a']++;


}




cout<<"\n\n";
int k=0;
char c[6+7];
for(int i=0;i<26;i++)
{
if(ar[i]>0)
{
for(int j=0;j<ar[i];j++)
{
//char l=i;
c[k]=i+'a';
cout<<" [ "<<c[k]<<" ] ";
k++;
} }}
char c1;
cin>>c1;
}


a better solution

- Anonymous August 06, 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