Microsoft Interview Question


Team: Office
Country: United States
Interview Type: In-Person




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

if(a+b <= c || b+c<=a || a+c<=b )
		return 0;
	else if(a == b && b== c)
		return 1;
	else if(a==b || b==c || c==a)
		return 2;
	else
		return 3;

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

If one or more of the values in zero or negative, need to return 0.

One is also asked to provide test cases. If the method has a signature:
int testTriangle(int a, int b, int c);

Positive TC's:
testTriangle(3, 3, 3);
testTriangle(3, 3, 4);
testTriangle(3, 4, 5);

Negative TC's:
testTriangle(3, 4, 8);
testTriangle(0, 0, 0);
testTriangle(-3, -3, -3);

- aap67 May 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int test(int a, int b, int c){
	if(a < 0 || b < 0 || c < 0)
		return 0;
	if(a+b <= c || a+c <= b|| b+c <= a)
		return 0;
	if(a == b && b == c){
		return 1;
	}else if(a == b || b == c){
		return 2;
	}else{
		return 3;
	}
}

- Kevin February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Input : a , b, c :

cnt  = 0;
if(a == b) cnt++;
if(a == c) cnt++;
if(b == c) cnt++;

if(a+b <= c || b+c <= a || a+c <= b) return 0; //thanks vik (I changed && in || and added <=)
if(cnt >= 2) return 1;
if(cnt == 1) return 2;
if(cnt == 0) return 3;

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

@Kamy in the if condition

if(a+b < c && b+c < a && a+c < b) return 0;

can this be ever true?

I dont think for 3 +ve real numbers a,b,c they can ever satisfy your condition

However you can fix the code by putting
if(a+b = c || b+c = a || a+c = b) return 0;

which is the case when the 3 points lie on a line

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

You can check here the rules for three sides(not points) to form a triangle wikihow.com/Determine-if-Three-Side-Lengths-Are-a-Triangle

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

After this code ... I was asked to provide the inputs to test if this code is working or note.

main stress was on conditions for Isoceles triangle.

- Anonymous April 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't forget guys, for any triangle, any two sides (a, b for instance) must add together to a distance longer than the third side (a+b > c), otherwise it is not a triangle...

- Anonymous April 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Provide negative values then conditions like (a+b <= c) will fail to check
2) Provide floating values also a=2.3,b=2.8 then if you took as integer then a==b, but logically wrong

- Gaurav Khurana May 11, 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