HCL Interview Question for Interns


Team: 1
Country: India




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

Not sure I understand the 4 cases, but here's my interpretation and solution.
- a triangle is a square block which contains a triangle in it, it can be of any size, (2x2, 3x3, 4x4...).
- a triangle is a block which has ones on one corner and zeros on the other corner... for example

[1,1,1
 1,1,0
 1,0,0]

is a triangle
- I excluded the triangles that have other stuff in the square like

[1,1,1
 1,1,0
 1,0,1]

Not a triangle. But not sure if it should be... from the question. Would need clarifications on this. If this is an acceptable triangle, we'd have the template be
[1,1,1
1,1,0
1,0,-1] where -1 is a don't care, shown later in the template making function. We really just need a solid triangle of 1s, and a line of 0 which is a boundary of the triangle, and the rest of the square, we don't really care.
This is the solution presented here as I find it's a bit more flexible and intuitive to someone looking for triangles.

My code uses somewhat of an image processing, template matching approach. I make 4 templates of triangles and convolve them on the matrix, counting everytime we find a match. I then increase the size of the triangle and convolve again, until the triangle is the size of the matrix, which I assumed to be square as stated in the question, but is not the case in the example.

public static int[,] MakeTriangleH(int n, int typeTriangle)
        {
            int[,] H = new int[n,n];

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {// change the elseif to else if want strict triangles (remove else)
                    if (typeTriangle == 0)
                    {// top left triangle
                        if ((n - i) > j)
                            H[i, j] = 1;
                        else if ((n - i) == j)
                            H[i, j] = 0;
                        else
                            H[i, j] = -1;
                    }
                    if (typeTriangle == 1)
                    {// top right triangle
                        if (i <= j)
                            H[i, j] = 1;
                        else if (i == j+1)
                            H[i, j] = 0;
                        else
                            H[i, j] = -1;
                    }
                    if (typeTriangle == 2)
                    {// bottom right triangle
                        if (i >= n - j - 1)
                            H[i, j] = 1;
                        else if (i == n - j-2)
                            H[i, j] = 0;
                        else
                            H[i, j] = -1;
                    }
                    if (typeTriangle == 3)
                    {// bottom left triangle
                        H[i, j] = i >= j ? 1 : 0;

                        if (i >= j)
                            H[i, j] = 1;
                        else if (i == j-1)
                            H[i, j] = 0;
                        else
                            H[i, j] = -1;
                    }
                }
            }
            return H;
        }

        public static bool PatternMatch(int [,] A, int iy, int ix, int [,] H)
        {
            for (int i = 0;i<H.GetLength(0);i++)
            {
                for (int j = 0;j<H.GetLength(0);j++)
                {// this check for -1 is in case the template contains a don't care
                    if (H[i,j] != -1 && A[i+iy,j+ix] != H[i,j]) 
                    {
                        return false;
                    }
                }
            }
            return true;

        }

        public static int Convolve(int[,] A, int[,] H)
        {
            int count = 0;
            for (int i = 0; i <= A.GetLength(0) - H.GetLength(0); i++)
            {
                for (int j = 0; j <= A.GetLength(0) - H.GetLength(0); j++)
                {
                    count += PatternMatch(A, i, j, H)?1:0;
                }
            }
            return count;
        }

        public static int CountTriangles(int[,] A)
        {
            int count = 0;
            int[,] H;
            for (int sizeTriangle = 2; sizeTriangle <= A.GetLength(0); sizeTriangle++)
            {
                H = MakeTriangleH(sizeTriangle, 0);
                count += Convolve(A, H);
                H = MakeTriangleH(sizeTriangle, 1);
                count += Convolve(A, H);
                H = MakeTriangleH(sizeTriangle, 2);
                count += Convolve(A, H);
                H = MakeTriangleH(sizeTriangle, 3);
                count += Convolve(A, H);
            }
            return count;
        }

[1,1,1,1,1,1
 1,1,1,1,1,0
 1,1,1,1,0,0
 1,1,1,0,0,1
 1,1,0,0,1,1
 1,0,0,1,1,1]

gives us 18 triangles

- djtrance January 31, 2016 | 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