Microsoft Interview Question: Given a triangle, determine i... | Glassdoor

## Interview Question

Software Development Engineer In Test (SDET) Interview Redmond, WA

# Given a triangle, determine if its a scalene, equilateral

, isosceles or neither... required knowledge of triangle properties, I learnt these properties about two decades ago so ofcourse I was fuzzy on the details, completely unexpected

0

testing use of ==, got a feeling he wanted to use bit level comparisons to compare sides lengths

Interview Candidate on Feb 19, 2013
0

If the sides are all integers, then compare the length^2 instead of length to avoid the floating comparison;

If the sides are floating numbers, then we need to set an epsilon to test.

Peter on Jul 21, 2013
0

BTW, if you are given the sides instead of points, the condition to make an triangle is:

a+b &gt; c &amp;&amp; b+c &gt; a &amp;&amp; c+a &gt; b

Peter on Jul 21, 2013
1

/* Q: Given a, b, c, determine if it can be the 3 sides of triangles, if yes
determine if it's equilateral, isosceles, or scalene. */

#include
#include
#include
using namespace std;

// Use this in real world
typedef enum
{
NONE = 0,
EQUILATERAL = 1,
ISOCELES = 2,
SCALENE = 3
} TriangleType;

double Epsilon = 1.0E-6;

string GetTriangleType(double a, double b, double c)
{
if (a+b &gt; c &amp;&amp; b+c &gt; a &amp;&amp; c+a &gt; b)
{
bool ab = (fabs(a-b) &lt; Epsilon);
bool bc = (fabs(b-c) &lt; Epsilon);
bool ca = (fabs(c-a) &lt; Epsilon);

if (ab &amp;&amp; bc &amp;&amp; ca)
return "Equilateral";

if (ab || bc || ca)
return "Isoceles";

return "Scalene";
}
else
{
return "None";
}
}

int main()
{
cout &lt;&lt; GetTriangleType(3, 3, 3) &lt;&lt; endl;
cout &lt;&lt; GetTriangleType(3, 3, 5) &lt;&lt; endl;
cout &lt;&lt; GetTriangleType(3, 4, 5) &lt;&lt; endl;
cout &lt;&lt; GetTriangleType(3, 4, 7) &lt;&lt; endl;

return 0;
}
/* Output:
Equilateral
Isoceles
Scalene
None
*/

Peter on Jul 21, 2013