Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

#include<iostream>

bool checkIsPermutation(const char* a, const char* b)
{
	if (strlen(a) != strlen(b))
		return false;

	char arr[256];
	memset(arr, 0, sizeof(arr));

	for (int i = 0; i < strlen(a); i++)
	{
		arr[a[i]]++;
		arr[b[i]]--;
	}

	for (int i = 0; i < 256; i++)
	{
		if (arr[i] != 0)
			return false;
	}

	return true;
};

int main()
{
	char* a = "microsoft";
	char* b = "softmciro";

	bool res = checkIsPermutation(a, b);

	return 0;

}

- hao.liu0708 August 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ascii range is from 0-127. Why have you taken 256?

- neowin October 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

asc ii extended has larger range

- hao.liu0708 July 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

1.take an array of 256 size,(for ascii charctrs),initialise with 0.
2.iterate through the string,increment the value of array by 1,on position pointd by ascii value of char obtained.
3.
do this for both string.
4.iterate through array of both strings,if same. than true else false..
time complexity o(n)
space complexity o(1){since int array is independent of string size}.

- rockstar August 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can as well decrease the value in the first array while traversing the second string, whenever we get a negative value we know they are not permutations of each other. And in the end we have to verify if the array has all zeros. This way you will save half of the space. Moreover we do not know if only characters are allowed, and also characters could be case sensitive.

- Naveen Reddy Mandadi August 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yar,how come we will recieve negative values?? nd yes there cn be integers instead of charcters,hence i hav taken array of 256 rather than 26.. pls do xplain me your concept.. i feel u r sayng right,bt nt able to get it..

- rockstar August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, your 256 array will cover the digits as well, i was mistaken in this regard.
Related to the negative values consider the example given box and obxx.
Based on the first string you get counts as 1 for b, 1 for o and 1 for x.
When we are passing through the second string we could decrease the count we got in first string.
When we first take o in obxx we decrease the count of o to 0.
When we take b in obxx we decrease the count of b to 0.
When we take first x in obxx we decrease the count of x to 0.
When we take second x (last character) in obxx we decrease the count of x to -1.
We got negative value here that means there is one x more in second string than in the first string.
We can terminate here without checking further characters if at all they exist.

- Naveen Reddy Mandadi August 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup... good goin naveen... ur rite abt this...

- rockstar August 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

one solution is to sort those strings and compare.

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

Not needed. That will take O(n log n) time. Just use an array of 26 elements and keep increasing the indexes for each character. This will work in O(n) time.

- androidify August 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPermutation(string s1, string s2)
{
if(s1.length != s2.length)
return false;

int []arr = new int[26];

for(int i=0;i<s1.length;i++)
{
int index = s1[i] - 'a';
arr[index]++;

index = s2[i] - 'a';
arr[index]++;

}
for(int i=0;i<arr.length;i++)
if(arr[i] != 0)
return false;

returnt true;
}

- calculator.fcis August 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess there will be "arr[index]--" after "index = s2[i] - 'a';" and not "arr[index]++".. kindly let me know if I am wrong..

- riteshkyal August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use built-in HashMap as <character, count> and check for both the strings.

- Hitesh Sajnani August 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

This could also be done by traversing both the list and adding the values of each character,if the final answer is same then the strings are permutations of one another otherwise not.

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

Adding the values of characters wont work because "ZZZ" will be same as "AAAK"

- Anonymous August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

but here the length is different

- Anonymous August 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Consider BC and AD. A+D = B + C because B=A+1 and C=D-1. This will fail.

- Learner August 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string.h>

using namespace std;

int main()
{
	char a[]="hello", b[]="oheee";
	if(strlen(a)!=strlen(b))
	{
		cout<<"\nNO\n";		
		return 0;	
	}				
	int count[53];
	for(int i=0;i<52;++i)
	count[i]=0;
	
	int k,i;
	k=strlen(a);
	i=0;
	while(i<k)
	{	
		count[(int)a[i]-65]+=1;		
		count[(int)b[i]-65]-=1;		
		i++;
	}
	
	i=0;	
	while(i<52)
	{
		if(count[i]!=0)
		{
			cout<<"\nNO\n";
			return 0;
		}	
		i++;
	}
	cout<<"\n YeS\n";
	return 0;
}

- rohan August 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

#include<string.h>
#include<conio.h>
#include<malloc.h>
#include<stdio.h>
#include<iostream.h>
#include<iostream>
void check(char *,char *);
void check(char *a,char *b)
{
if(strlen(a)!=strlen(b))
{
printf("It is not permutation");
}
else
{
int x=0,y=0;
for(int i=0;i<strlen(a);i++)
{
x=x+*(a+i);
y=y+*(b+i);
}
if(x==y)
{
printf("It is permutation");
}
else
{
printf("It is not a permutation");
}
}
}
int main()
{
char* a=(char *)malloc(100) ;
char* b=(char *)malloc(100) ;
gets(a);
gets(b);


check(a,b);

getch();

return 0;

}

- Shubham Gupta August 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Method1: traverse two strings, one from its head and the other from the end. compare every elements from both string.
Method2: first get two strings length( which will cost o(n) time); compare the lengths if the are not equal, return false; else traverse first string by increasing variable i, and compare str1[i] with str2[len-i-1] if one of them are not equal return false.
...

- Bin December 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

con cat both string original and permutation and check whether original one is sub string of concated one

- Anonymous January 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
int main()
{
	char str1[MAX],str2[MAX];
	int i,j,count=0;
	scanf("%s%s",str1,str2);
	if(strlen(str1)!=strlen(str2))
	printf("%s and %s are not permutations of each other",str1,str2);
	else
	{
		for(i=0;i<strlen(str1);i++)
		{
			for(j=0;j<strlen(str2);j++)
			{
				if(str1[i]==str2[j])
				{
				count++;
				break;
			}}
		}
		if(count==strlen(str1))
		printf("%s and %s are permutations of each other",str1,str2);
		else
		printf("%s and %s are not permutations of each other",str1,str2);
	}
	return 0;
}

- apoorva September 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>
#include<string.h>
int main()
{
        char str1[10000];
        char str2[10000];
        scanf("%s",str1);
        scanf("%s",str2);
        int i,k1,k2,sum1=0,sum2=0;
        k1=strlen(str1);
        k2=strlen(str2);
        if(k1!=k2)
                printf("FALSE");
        else{
                for(i=0;i<k1;i++)
                {
                        sum1+=str1[i];
                        sum2+=str2[i];
                }
                if(sum1==sum2)
                        printf("TRUE");
                else
                        printf("FALSE");
        }
        return 0;
}

- mrmastikhorchandan September 01, 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