Microsoft Interview Question for Software Engineer in Tests






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

compare(char *str1 , char *str2)
{
while(*str1 == *str2 && str1!=NULL && str2!=NULL)
{
str1++;
str2++;
}

if(((str1 == NULL) && (str2 == NULL)) || (*str1 == *str2) )
return 0; // equal

else if ( str1 == NULL || *str1<*str2 )
return -1; // less

else if
return 1; //greater
}

- P December 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>
using namespace std;

int compareStrings(string &s1, string &s2)
{
	int i = 0;
	for(i=0;(s1[i]&&s2[i])&&(s1[i]==s2[i]);i++);
	
	if((s1[i] == '\0') && (s2[i] == '\0') )
		return 0; //cout <<"Strings are equal" <<endl;
	else if(s1[0] == '\0' || s1[0] < s2[0])
		return -1;
	else 
		return 1;
}

int main()
{
	string s1("Gajanan"),s2("Gajanan");
	//if(s1 == s2)
		//cout<<"Strings are equal" <<endl;
	//cout<<s1[0];
	//cout<<(s1);
	cout<<"Enter string1 = ";
	cin >>s1;
	cout<<"Enter string1 = ";
	cin >>s2;
	int i = compareStrings(s1,s2);
	if(i == 0)
		cout <<"Strings are equal"<<endl;
	if(i == -1)
		cout <<"First string is smaller" <<endl;
	if(i == 1)
		cout <<"Second string is smaller" <<endl;
	return 0;
}

- Gajanan December 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int strcmp(const char* s1, const char* s2)
{
int t;

if( !*s1 && !*s2)
return 0;
else if( !s1 )
return -*s2;
else if( !s2 )
return *s1;

do
{
t = *s1 - *s2;
} while(*s1++ && *s2++ && !t);
return t;
}

- T December 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>

using namespace std;

int compare (string str1, string str2)
{
int i = 0;
while (i < str1.length() && i < str2.length())
{
if (str1[i] > str2[i])
return 1;
else if (str2[i] > str1s[i])
return -1;

i++;
}
if (str1.length() > i)
return 1;
else if (str2.length() > i)
return -1;

return 0;
}

int main(int argc, char argv[])
{
int compare (string str1, string str2);
string s1 = "bhushan", s2 = "bhushan";
int ret = compare(s1, s2);

cout << ret << endl;

return 0;
}

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

test cases can be:
1. passing equal and unequal strings
2. passing empty strings
3. passing same string with different cases

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

int strcmp(char *str1,char *str2)
   {
      for(;*str1==*str2;str1++,str2++)
         if(str1=='\0')
            return 0;
       return *str1-*str2;
    }

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

You need to add null pointer check to handle that case


int strcmp(char *str1,char *str2)
{
if(str1 == NULL || str2 == NULL)
return str1 - str2;

for(;*str1==*str2;str1++,str2++)
if(str1=='\0')
return 0;
return *str1-*str2;
}

- Murat Ozturk January 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public bool String_Compare(string string1, string string2)
{
bool equal = false;



for (int i = 0; i < string2.Length; i++)
{

if (string1[i] != '\0' && string2[2] != '\0')
{
if (string1[i] == string2[i])
{
equal = true;
}
}

}

return equal;



}

Test Cases:- Equal String, Uequal String and One string will be empty

- Sneha March 04, 2010 | 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