Interview Question


Country: United States




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

Here the code for Ancestor Matrix in C#

public class BinaryTreeNode
    {
        public int Data { get; set; }
        public BinaryTreeNode LeftNode { get; set; }
        public BinaryTreeNode RightNode { get; set; }
    }

    public class BinaryTree
    {
        public BinaryTreeNode RootNode { get; set; }
        public void CreateBinaryTree(BinaryTreeNode node)
        {
            if (RootNode == null)
            {
                RootNode = node;
            }
            else
            {
                var current = RootNode;
                while (true)
                {
                    var previousNode = current;
                    if (current.Data < node.Data)
                    {
                        current = current.RightNode;
                        if (current != null) continue;
                        previousNode.RightNode = node;
                        break;
                    }
                    else
                    {
                        current = current.LeftNode;
                        if (current != null) continue;
                        previousNode.LeftNode = node;
                        break;
                    }
                }
            }
        }

        public int PostOrderTravese(BinaryTreeNode node)
        {
            if (node.LeftNode != null) PostOrderTravese(node.LeftNode);
            if (node.RightNode != null) PostOrderTravese(node.RightNode);
            Console.WriteLine(node.Data);
            return node.Data;
        }
    }


    public class AncestorMatrix
    {
        public int[,] AMatrix { get; set; }

        public void CreateArray(int n)
        {
            AMatrix=new int[n,n];
            for (var i = 0; i < n; i++)
            {
                for (var j = 0; j < n; j++)
                {
                    AMatrix[i, j] = 0;
                }
            }
        }

        private void FillMatrix(string data, int parentId)
        {
            if (data == string.Empty) return;

            string[] valueStrings = data.Split(' ');
            foreach (var valueString in valueStrings)
            {
                if((valueString!=string.Empty) && valueString!=parentId.ToString(CultureInfo.InvariantCulture)) AMatrix[parentId-1, Convert.ToInt16(valueString)-1] = 1;
            }
        }

        public string PostOrderTravese(BinaryTreeNode node)
        {
            string str=String.Empty;
            if (node != null)
            {
                str += " " + PostOrderTravese(node.LeftNode);
                str += " " + PostOrderTravese(node.RightNode);
                str += " " + node.Data;

                FillMatrix(str, node.Data);
            }
            else
            {
                str = "";
            }
            return str;
        }

        public void ShowMatrix(int n)
        {
            for (int i = 0; i < n; i++)
            {
                string displayData = "Row " + (i+1) + " ";

                for (int j = 0; j < n; j++)
                {
                    displayData += AMatrix[i,j] + " ";
                }
                Console.WriteLine(displayData);
            }
        }
    }


        static void Main(string[] args)
        {
            var tree=new BinaryTree();
            var node = new BinaryTreeNode() {Data = 6};
            tree.CreateBinaryTree(node);
            tree.CreateBinaryTree(new BinaryTreeNode() { Data = 2 });
            tree.CreateBinaryTree(new BinaryTreeNode() { Data = 8 });
            tree.CreateBinaryTree(new BinaryTreeNode() { Data = 1 });
            tree.CreateBinaryTree(new BinaryTreeNode() { Data = 4 });
            tree.CreateBinaryTree(new BinaryTreeNode() { Data = 7 });
            tree.CreateBinaryTree(new BinaryTreeNode() { Data = 9 });
            tree.CreateBinaryTree(new BinaryTreeNode() { Data = 3 });
            tree.CreateBinaryTree(new BinaryTreeNode() { Data = 5 });
            tree.CreateBinaryTree(new BinaryTreeNode() { Data = 10 });
            tree.PostOrderTravese(node);

            var matrix=new AncestorMatrix();

            matrix.CreateArray(10);
            matrix.PostOrderTravese(node);
            matrix.ShowMatrix(10);
            Console.ReadLine();
        }

- Ashish April 28, 2015 | 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