Interview Questions

Write a function that takes three integers corresponding to the lengths of sides ....

Microsoft Interview Questions and Answers


(Continued from previous question...)

11. Write a function that takes three integers corresponding to the lengths of sides ....

Question:
Write a function that takes three integers corresponding to the lengths of sides and returns what kind of triangle can be made out of those 3 sides. (Equilateral, etc)

You must also handle the case where no triangle may be formed from those 3 lengths.


maybe an answer:

answer1:
Keep a count such that count = 2 if two values are the same, and count=3 if all the same.
then count=3 implies equi
=2 - isos
=1 - scalene


answer2:
def ValidateTraingle(side1, side2, side3)
triangleSides = [side1, side2, side3]
triangleSides = triangleSides.sort
if (triangleSides[2] >= triangleSides[0] + triangleSides[1]) then
# Error in the traingle dimensions
return 4
elsif (triangleSides[0] == triangleSides[1]) and (triangleSides[1] == triangleSides[2]) then
# Equilateral traingle
return 3
elsif (triangleSides[0] == triangleSides[1]) or (triangleSides[1] == triangleSides[2]) or (triangleSides[0] == triangleSides[2]) then # Isosceles traingle
return 2
elsif (triangleSides[0] != triangleSides[1]) and (triangleSides[1] != triangleSides[2]) then # Scalene traingle
return 1
end

end


answer3:
int isTriangle (int x1, int x2, int x3) {

if ((x1 >= x2 + x3) || (x2 >= x1 + x3)
|| (x3 >= x1 + x2)) {
return 0; // no triangle possible
}

if (x1 == x2 == x3) return 1; // Equilateral

if (x1 == x2 || x2 == x3
|| x1 == x3) {
return 2; // Isoceles
}

return 3; //Scalene
}


answer4:
1) The three integers are a, b and c
2) If not (a+b > c and a+c > b and b+c > a) then input is not valid
3) if a= b =c then return equilateral
4) If ( a= b or a=c or b=c) then return isosceles
5) Arrange variables such that a > b > c and
if sqr(a) = srq(b) + sqr(c) then return right angle triangle
6) Else scalene triangle


answer4:
enum RectangleType { Scalene, Isosceles, Equilqteral, NotARectangle};
template
struct RectComaparator {
RectComparator(){}
bool compare(const T&lhs, const T& rhs) {
return (lhs > rhs);
}
};
RectangleType
template
detectRectangleType (cosnt T& a, const T&& b, const T&& c) throws std::runtime_error {
//validate in parameters
if (!a || !b || !c) {
//throw exception
}
//store and sort in array
T store[3]= {a, b, c};
RectComparator comp;
std::sort(store, comp); //in desc order
//store[0] has the greatest value
if (store[0] > store[1] + store [2]) {
return NotARectangle;
}
if ((store[0] == store[1] == store[2])
return Equilateral;
else if (store[0] == store[1] || store[1] == store[2]) return Isosceles;
else
return Scalene;
}
}
alternatively, store the values in a set
Set stores unique sorted values, so if the size of the set is 1 then it is equilateral etc…


answer5:
function typeOfTriangle($x, $y, $z){

$a=array($x,$y,$z);
sort($a);

echo “
Type of triangle that can be drawn with sides with respective lengths as $x, $y, $z : “.($a[2]
“;

}

typeOfTriangle(11,10,17);
typeOfTriangle(11,10,11);
typeOfTriangle(11,11,11);
typeOfTriangle(11,110,11);
typeOfTriangle(11,0,11);
—- —- —- —- —- —- —- —- —-
//output :

Type of triangle that can be drawn with sides with respective lengths as 11, 10, 17 : Scalene

Type of triangle that can be drawn with sides with respective lengths as 11, 10, 11 : Isosceles

Type of triangle that can be drawn with sides with respective lengths as 11, 11, 11 : Equilqteral

Type of triangle that can be drawn with sides with respective lengths as 11, 110, 11 : None

Type of triangle that can be drawn with sides with respective lengths as 11, 0, 11 : None

—- —- —- —- —- —- —- —- —-
[Note : Here assuming following factors : sides’ lengths are numbers (signed/unsigned int/floats) and type of triangle is to be decided on sides and not on angles, in the resultant triangle. ]

answer6:
enum TRIANGLE_TYPES
{
TT_NONE = 0Χ00,
TT_CONFLUENT = 0Χ01,
TT_COMMON = 0Χ02,
TT_EQUILATERAL = 0Χ04,
TT_ISOSCELES = 0Χ08,
TT_RECTANGULAR = 0Χ10,
TT_CONFLUENT_ISOSCELES = TT_CONFLUENT | TT_ISOSCELES,
TT_RECTANGULAR_EQUILATERAL = TT_RECTANGULAR | TT_EQUILATERAL,
TT_RECTANGULAR_ISOSCELES = TT_RECTANGULAR | TT_ISOSCELES
};

TRIANGLE_TYPES triangle_kind(int a, int b, int c)
{
TRIANGLE_TYPES type = TT_NONE;

if ( a
Validate(sides[3])
{
sides.sort; //asc order
if(sides[2] > side[0] + side[1])
{
return 0;
}
else
{
if(side[0] == side [2])
{
return equi;
}
elseif(side[0] == side[1])
{
return isoc;
}
return scalene;
}
}

(Continued on next question...)

Other Interview Questions