Microsoft Interview Question for Software Engineer in Tests






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

Some general testcases for this would be:

Basic tests:
("abc","abc")
("abc","aBc")
("abc","ABC")
("abc","def")
("abc","aec")

Null tests:
(NULL,"abc")
("abc",NULL)
(NULL,NULL)

Boundary tests:
("abc",abd")
("dab","cab")
("Abc","abc")
("abc","abC")
("","abc")
("abc","")
("","")

Non alphabets:
("Aa-#Bb", "aa-#bb")
("*&^","!@$")
("*&^","*&^")

For each loop or condition, prepare a testcase that
meets one, two,.. all of the conditions
("$ABC",">abc") // If difference of 26 is used for equalizing upper & lowercases, then it should still see $,> as different
...
....

- acoader November 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Check the case ("abc", "MNO")

Here then sum of ascii values of "abc" will be greater than "MNO" but it is not lexicographically greater.

- NG November 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Compare_Strings(char* str1, char* str2)
{
while( *str1 && *str2)
{
int diff=*str1-*str2;

if(*str1 >='A' && *str1 <='Z') diff+=32;
if(*str2 >='A' && *str2 <='Z') diff-=32;
if(diff >0) return 1;
else if(diff < 0) return -1;

str1++;
str2++;
}
return ((*str1 - *str2)>0?1:((*str1 - *str2)<0)?-1:0);
}

- Ankush Bindlish November 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you try to explain the algo in words as well or add comments ? I see that 32 is the correct difference between an uc & an lc version of the same alphabet.

But your implementation will return 1 for "A" & "a". We need to return a 0..

- acoader November 05, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int (strcmp)(const char *s1, const char *s2)
{
unsigned char uc1, uc2;
/* Move s1 and s2 to the first differing characters
in each string, or the ends of the strings if they
are identical. */
while (*s1 != '\0' && *s1 == *s2) {
s1++;
s2++;
}
/* Compare the characters as unsigned char and
return the difference. */
uc1 = (*(unsigned char *) s1);
uc2 = (*(unsigned char *) s2);
if (uc1 != uc2)
return ((uc1 < uc2) ? -1 : (uc1 > uc2));
else
return 0;
}

- Anonymous November 06, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its from wiki

- Anonymous November 06, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char toLower(char c){
..if ((c>='A')&&(c<='Z'))
....return (c-'A'+'a');
}

int compare(char* a, char*b)
{
..if (a==b==NULL)
....return 0;
..if (a==NULL)
....return -1;
..if (b==NULL)
....return 1;
..while (*a&&*b){
....if (toLower(*a)>toLower(*b))
......return 1;
....else if (toLower(*a)<toLower(*b))
......return -1;
....else{
......a++;
......b++;
....}
..}
..if ((*a==NULL)&&(*b==NULL))
....return 0;
..else if (*a==NULL)
....return -1;
..else //(*b==NULL)
....return 1;
}

void main()
{
..cout<<compare("AB", "Ab");
}

- XYZ November 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//c# way. Here is the code with the explaination.

//First convert to char
char[] c1 = s1.ToCharArray();
char[] c2 = s2.ToCharArray();

//Check the length is same or not
if (c1.Length != c2.Length) throw new Exception("Length is not same. Lenght shoulb be same");

//Convert to lower case
if (c1[0] <= 'Z')
{
c1[0] = (char)((int)c1[0] + 32);
}
if (c2[0] <= 'Z')
{
c2[0] = (char)((int)c2[0] + 32);
}

//Now compare the first character
//One assumption is:
//1. The string is always sorted that means it is like abc, bcd, def, mno, etc...but not like CBG, GAB, etc..
if (c1[0] > c2[0]) return 1;
else if (c1[0] < c2[0]) return -1;
else return 0;

Any comment?

- Raj December 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int strcmp11 (const char * src,const char * dst)
{
int ret = 0 ;
if(src == NULL || dst == NULL)
{
std::cout << "NULL Detected";
return -3;
}
while( ! (ret =(unsigned char) ::toupper(*src) - (unsigned char)::toupper(*dst)) && *dst)
++src, ++dst;

if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;

return( ret );
}

- Arjun December 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another solution..which to me seemed elegent. Its from a cookbook By Herb's

int stringcmp_ign_case(const char* str1,const char* str2)
{
  while(*str1 && *str2){
       if(tolower(*str1)!=tolower(*str2))
          break;
      
       *str1++;*str2++;
      }
     //this elegantly handles the three cases
     //NULL-NULL=0
     //Somevalue-NUll>1
     //NULL-somevalue<1
     return tolower(*str1)-tolower(*str2);
}
Pretty similar to Ankush's solution

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

For the above algorithm, one needs to check for all the characters present in both the strings if the ASCII value of any one of the characters in string 1 is less than we must return -1 if both the strings have equal length and both are equal then return 0 else return 1.

Implementation:

#include<bits/stdc++.h>
using namespace std;
int findstrings(string str1, string str2){
int count = 0;
transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
int n = str1.length();
int m = str2.length();
if(n > m)
return -1;
for(int i = 0; i < n; i++){
if(str1[i] == str2[i])
count++;
else if((int)str1[i] > (int)str2[i])
return +1;
else
return -1;
}
if(count == n)
return 0;
}
int main(){
string str1 = "abc";
string str2 = "mno";
cout<<findstrings(str1, str2)<<endl;
return 0;
}

- swapnilkant11 June 03, 2019 | 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