Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




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

I will post the code momentarily, but this is essentially checking if you know Horner's rule. Let me explain the rule below and converting it to code should be extremely simple:

Say, you are reading a string of numbers left to right: 2 3 6. The problem with this model of reading is that we cannot tell if 2 is a "hundred" or a "ten" or a "thousand". We cannot glean this information, without reading the whole string and using some data structure (or variable(s)) to store the position, which is needlessly inefficient.

So this Horner guy really nailed the problem (no pun intended). What you do is that you read the first number "2" and store it in a variable, say "num".

Then you read "3" and you simply multiply "num" by 10 and add 3 = (2*10)+3=23

Then you read "6" and you multiply "num" by 10 again and add 6 = (23*10)+6=236

Viola! Suddenly you have the correct number.

Now how to do this with string. Well we can make a simple assumption and that is that the string is ASCII so every time we read a character, we simply subtract '0' or 47 from it and we transform it to a valid number
{{

Code in C#/Psuedo (the code is essentially so simple, that I am just going to use a combo of C# and English)

int ConvertStringToInt(string str)
{
    num=0;
    isNegative= false;
    foreach (char c in str)
    {
          if(c == '-')             // Final number will be negative
          {
                isNegative = true;       
                continue;
          }
  
          num *= 10;
          num += c - '0';       // Works correctly in C/C++
    }
    if(isNegative) num *= -1;
    return num;
}

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

I am surprised to see why we need to know any rule.. its just simple mathematics....

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

Yes its simple mathematics. And this simple observation was made by Horner. Its good to know what the actual mathematical rule is called.

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

Yep..I didn't know this simple logic too has a name. Thnx for enlighting

- Varun January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'd like to contribute to this discussion by saying that I really hate it when absolutely trivial results are called rules or theorems and have a name attached to them. I see you, DeMorgan, I see you.

- eugene.yarovoi January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What I mean to say is: didn't we all kind of invent DeMorgan's Laws? It's ridiculous for someone's name to be attached to something so obvious and basic.

- eugene.yarovoi January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. Valid inputs are "123", "+123","-123"
2. Invalids inputs are "1+23","12-3","*123"
3. Integer overflow case for large string ("21323333333333333332"), my code returns so far calculated integer value

long StringToNum( char *str, int len )
{
	if( str == NULL ||  len < 1  )
	{
		printf(" Invalid input ");
		return -1 ; // error code -1 
	}
	else
	{
		long num = 0, num1 ;

		while( len )
		{
			if( '0' <= str[len-1] && str[len-1] >= '9' )
			{
				num1 = num;
				num = num * 10 + (int) (str[len] - '0' );
				if(num1 > num )
				{
					printf("\n integer overflow " );
					return num1;
				}
			}
			else if( (len == 1 ) )
			{
				if(str[len-1] == '-' )
				{
					num *= -1 ;
				}
				else if (str[len-1] != '+' )
				{
					printf(" Invalid input ");
					return -1; // invalid input
				}
			}
			else
			{
				printf(" Invalid input ");
				return -1;
			}

			len -- ;
		}

		return num;
	
	}
}

Please letme know if you notice any mistake in my code

- siva.sai.2020 January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think error checking for decimal point , special characters , integer overflow , +/- etc is important .

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

it is mentioned to conver tit into integer so no question of decimal

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

Perhaps your exact suggestions weren't quite right, but on a relatively easy question like this, the interviewer would in fact be looking at how careful you're being.

- eugene.yarovoi January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you are totally right. I support you.

- john January 30, 2012 | Flag


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