Microsoft Interview Question for Software Engineer / Developers






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

this works for the trailing zeroes case

#include<iostream>
#define str(x) #x
using namespace std;
int main()
{
int count=0;
char* s=str(3.45000);
while(*s++)
{
if(*s++=='.')
{ while(*s++)
{
count++;
}
}

}

cout<<count;

return 0;
}

- suatman December 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong answer.

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

double no =3.44;

int count =0;

while(no!=((int)no))
{
count++;
no=no*10;
}
printf("%d",count);

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

This would work only when there are no trailing zeros.

- yeah December 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 0 votes

pls stop being so stupid

- prince January 21, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what a morn

- Anonymous September 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the answer given by backbencher is correct. Problem says

43.000 output=0 So in case of trailing zero back bencher solution will work. Please correct me if I am wrong

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

What should 4.301 output?

- lobatt December 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it should be 3

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

there are some numbers that can not be indicated by float type. for example, there is no 73.487 in float type, the number indicated by float in c is "73.486999999999995" to approximate it.

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

In the IEEE 754 Specifications, a 32 bit float is divided as 24+7+1 bits. The 7 bits indicate the mantissa.

Take a look at http://en.wikipedia.org/wiki/IEEE_754-1985

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

Only 1 problem in BackBencher's solution:

Lets say ur input number = 10.23456
once u r @ 10234.56 and multiply with 10, the next number comes as 102345.5999999998 instead 102345.6
so the loop will go on, never complete...

and thts the behavior of double and float.

best way to do is convert to string and do it.
any comment?

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

how abt this?
say n=1.25
the ans is:
x=(n-(int)n)*10
and now (int)x will be the ans..
correct if i am wrong

- ssn December 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you dint seem to understand problem using real numbers here.. I think best way is to convert the given number to string and then proceed with

int count=0,buffer=0;
while(num[i]!='\n'){
if(num[i]=='0'){
buffer++;
}else{
buffer++;
count = buffer;
buffer=0;
}
}

correct me if im wrong

- anonymous December 09, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code works.

I am pasting the same code with no errors. Just one thing, we have to convert a decimal number to its char * form.


int main(){
...int count=0,buffer=0;
...char* num ="1234.00001\n";
...int i =0;
...while(num[i]!='\n'){
......if(num[i]=='0'){
.........buffer++;
......}else{
.........buffer++;
.........count = buffer;
.........buffer=0;
......}
......i++;
...}
...cout <<"\n Count of decimals: " << count << endl;
...return 0;
}

- peace November 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dude.. do you really think this would work for input like - 223.101010
i think the correct output should be 5. Correct me ...

- neo February 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main()
{
int count=0;
char *s;
gets(s);

while(*s++)
{
if(*s++=='.')
while (*s++){
count++;
}}
cout<<count;
return 0;
}

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

In Backbencher's solution, the while loop will always execute as the condition != will be true. You cannot do this with floats in a reliable fashion
I would go with a string based solution

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

Convert the float to string using sprintf. Check how many characters are there are after period ".".

Doesn't this work very well ?

- Daniel Johnson January 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

double n=1.23456;
int count = 0;
while (!(n-(int)n>-0.0000001 && n-(int)n<0.0000001)){
  count++;
  n*=10;
}

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

double remainder = 9999;
int count = 0;
double input;
cin >> input;
while(remainder!=0)
{
input *= 10;
remainder = input % 10;
count++;
}
cout << --count;

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

#include<iostream>

#define str(x) #x

using namespace std;
int main()
{
	int count=0;
	int count2=0;
	bool count2Flag = false;
	char* s=str(3.45000);

	while(*s++)
	{
		if(*s == '.')
		{ 
			s++;
			while(*s)
			{
				cout<<s<<endl;

				if(*s == '0')
				{

					count2++;

					count2Flag=true;

				}
				else
				{
					if(count2Flag)
					{
						count+=count2;
						count2=0;
						count2Flag=false;

					}
					count++;
				}

			s++;
			}
			break;

		}
	}
	cout<<count;
	return 0;

}

- Hemant Jain February 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

float inputNumber = (float)2/3;
		int counter = 0;
		while(true) {
			inputNumber = inputNumber * 10;
			if(inputNumber % 10 == 0)
				break;
			counter++;
		}
		System.out.println("# of digits after decimal in number " + inputNumber + " = " + counter);

- Anonymous March 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about
- While the (number mod 1) is not 0
- Increment the counter by 1
- Multiply the number by 10
- number = number mod 1

For example,
3.0456 mod 1 = 0.0456
0.0456 * 10 = 0.456
0.456 mod 1 = 0.456
0.456 * 10 = 4.56
4.56 mod 1 = 0.56
.....

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

modulo operator does not work on floats or double (atleast in C++)...but with similar concept you can implement doing something like below. Although it has issues about float type...you just can't trust the behaviour... 3.123 can become 3.12999999998 etc...and you will end up in infinite loop..

EG.
while(num != 0)
{
num = num - (int)num;
count++;
num *= 10;
}

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

string to_string(float f)
{
stringstream str;string s;str<<f;str>>s;return s;
}
int no_of_zero(string s)
{
int i=0,l=s.length();bool dotpresent=false;
while(i<l && s[i]!='.'){i++;if(s[i]=='.')dotpresent=true;}
if(!dotpresent)return 0;
while(s[--l]=='0');
return l-i;
}
int main()
{
float f;cin>>f;//no to b converted
cout<<no_of_zero(to_string(f))<<endl;
system("pause");
return 0;
}
note that we can take any floating point no directly as input string...
In this case to_string will be redundant...

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

Suppose 89.0800 is the number.Convert it to string.

Start traversing from the back.
neglect the trailing zeroes and count the non zero values until u get a '.'.

correct me if i am wrong.

- adhi December 09, 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