Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

int isAnagram(char *s1,char *s2)
{
char characterCounter[256]={0};
int i;
for(;*s1;s1++)
{
 if(*s1==' ') continue;
 characterCounter[*s1]+=1;
}
for(;*s2;s2++)
{
 if(*s2==' ') continue;
   characterCounter[*s2]-=1;
   if(  characterCounter[*s2] <0)
      return 0; //false
}
for(i=0;i<256;i++)
    if(characterCounter[0]!=0)
      return 0; //false

return 1; // True, as both are anagram
}

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

if all the characters in the string are assumed to be ASCII charcters.

- raktunc January 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@yash.... one correction
for(;*s1;s1++)

the loop termination should be *s1="/0" (and same for s2 also) cause if they are string then the end of string will be on "/0" and not on NULL

- vik January 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
5
of 9 vote

solution 1:
use sorting and compare the sorted strings
solution 2:
Use buckets and then pop elements from buckets

- avikodak January 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

public static boolean isAnagrams(String str1, String str2)
	{

		if(str1 == null && str2 == null)
			return true;

		if(str1 == null || str2 == null)
			return false;

		char[] charArray1 = str1.toCharArray();
		char[] charArray2 = str2.toCharArray();

		Map<Character, Integer> charCount1 = new HashMap<Character, Integer>();

		for(int i = 0; i < charArray1.length; i++)
		{
			if(charArray1[i] != ' ')
			{
				Integer value = charCount1.get(charArray1[i]);	
				if(value == null)
					value = 0;
				charCount1.put(charArray1[i], ++value); 
			}
		}

		Map<Character, Integer> charCount2 = new HashMap<Character, Integer>();
		for(int i = 0; i < charArray2.length; i++)
		{
			if(charArray2[i] != ' ')
			{
				Integer value = charCount2.get(charArray2[i]);	
				if(value == null)
					value = 0;
				charCount2.put(charArray2[i], ++value); 
			}
		}
		
		int len1 = charCount1.keySet().size();
		int len2 = charCount2.keySet().size();
		if(len1 != len2)
			return false;
		Iterator<Character> it = charCount1.keySet().iterator();
		while(it.hasNext())
		{
			Character current = it.next();
			Integer count1 = charCount1.get(current);
			Integer count2 = charCount2.get(current);
			
			if(count1 != count2)	
				return false;
		}

		return true;

	}

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

this is a solution for this question

#include<stdio.h>
#include<string.h>
main()
{
char str1[20],str2[20];
printf("enter 2 strings..\n");
gets(str1);
gets(str2);
if(strlen(str1)==strlen(str2))
{
char ch;
int i=0;
for(i=0;str1[i];i++)
{
int j=0,k=0,l=0;
ch=str1[i];
for(j=0;str1[j];j++)
if(ch==str1[j])
k++;
for(j=0;str2[j];j++)
if(str2[j]==ch)
l++;
if(k!=l)
break;
}
if(i==strlen(str1))
printf("both strings are with same alphabets..\n");
}
}

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

Possible solution in python:

def is_anagram(x1,x2)
    x1 = ''".join(x1.split(' '))
    x2 = "".join(x2.split(' '))
    return sorted(x1) == sorted(x2)

- really January 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just add .lower() to the end of your calls to join and this will also work for comparing uppercase and lowercase letters.

- zattacks January 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main()
{
int i=0,j=0;
char *first = (char*)malloc(sizeof(50));
char *second = (char*)malloc(sizeof(50));
scanf("%[^\n]s\n",first);
scanf("%[^\n]s\n",second);
while(*first != '\0')
{
if(*first != ' ')
{
i = i + *first;
}
first++;
}
while(*second != '\0')
{
if(*second != ' ')
{
j = j + *second;
}
second++;
}
if(i == j)
{
printf("Anagram");
}
else
{
printf("not anagram");
}
return 0;
}

- csgeeg January 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This may not work---
Consider "AC" and "BB"
These add up to give same sum('A'+'C' = 'B'+'B')

- Sahil January 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
main()
{
char str1[20],str2[20];
printf("enter 2 strings..\n");
gets(str1);
gets(str2);
if(strlen(str1)==strlen(str2))
{
char ch;
int i=0;
for(i=0;str1[i];i++)
{
int j=0,k=0,l=0;
ch=str1[i];
for(j=0;str1[j];j++)
if(ch==str1[j])
k++;
for(j=0;str2[j];j++)
if(str2[j]==ch)
l++;
if(k!=l)
break;
}
if(i==strlen(str1))
printf("both strings are with same alphabets..\n");
}
}

- yaswanth January 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if the two strings have unequal number of white spaces same as given example in question ?

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

#include<stdio.h>
#include<string.h>
main()
{
char str1[20],str2[20];
printf("enter 2 strings..\n");
gets(str1);
gets(str2);
if(strlen(str1)==strlen(str2))
{
char ch;
int i=0;
for(i=0;str1[i];i++)
{
int j=0,k=0,l=0;
ch=str1[i];
for(j=0;str1[j];j++)
if(ch==str1[j])
k++;
for(j=0;str2[j];j++)
if(str2[j]==ch)
l++;
if(k!=l)
break;
}
if(i==strlen(str1))
printf("both strings are with same alphabets..\n");
}
}

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

short arr[26] = {0};
bool isAnagram(const char * str1, const char* str2){
	bool retVal = true;
	for(int ii = 0; ii < strlen(str1); ii++){
		if(str1[ii] == ' ') continue;
		arr[str1[ii] - 'a']++;
	}
	for(int ii = 0; ii < strlen(str2); ii++){
		if(str2[ii] == ' ') continue;
		arr[str2[ii] - 'a']--;
	}
	for(int ii = 0; ii < 26; ii++){
		if(arr[ii]) {
			retVal = false;
			break;
		}
	}
	return retVal;
}

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

#include <algorithm>

bool IsAnagram(string inString1, string inString2)
{ 
 //remove whitespace
 inString1.erase(remove(inString1.begin(), inString1.end(), ' '), inString1.end());
 inString2.erase(remove(inString2.begin(), inString2.end(), ' '), inString2.end());
 
 //sort
 sort(inString1.begin(), inString1.end());
 sort(inString2.begin(), inString2.end());
 
 return(inString1 == inString2);
}

- Tomstuchinski January 13, 2013 | 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