Adobe Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

what is ancestor matrix?

- Anonymous September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

firstly number all nodes of tree as you have to form a n*n matrix where n is number of nodes in this given tree, now put all zeros in ith row of matrix where i is the number given to root node,now move onto childrens of this root node level order wise will be favoured now for the jth and kth row put all elements to zero except ith element as i is ancestor of both j and k.now let l be child of k ,then put all elements zero except where jth row is non zero and at position j.do this recursively .

- dev September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

please do not comment if you don't know English

- guest December 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Matrix[leftNode][rightNode]=rootNode...

Tail recursion:
public static Node populateAncestorMatrix(final Node root, final int level) {
        if (root == null) {
            return null;
        }

        Node nodeLeft = new Node(-1);
        Node nodeRight = new Node(-1);

        if (root.left != null) {
            nodeLeft = populateAncestorMatrix(root.left, level + 1);
        }

        if (root.right != null) {
            nodeRight = populateAncestorMatrix(root.right, level + 1);
        }

        if (!ancestorMatrix.containsKey(nodeLeft)) {
            ancestorMatrix.put(nodeLeft, new HashMap<Node, Node>());
        }

        ancestorMatrix.get(nodeLeft).put(nodeRight, root);

        return root;
    }

- Jack September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can treat each node as a vertex in a graph and then we can find path matrix for it....i think it will give ancestor matrix

- anonymous September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First count the total no. of nodes in the tree to allocate the matrix
We can do iterative inorder traversal using Queue.
At any time, if we go upwards, it means all the nodes on this path has been traced and we need to fill the ancestor matrix.
now we iterate all nodes in the queue and mark it as the ancestor of the deepest node.
Here we assume that every node is labelled with a positive integer.

Time Complexity :- O(N) [inorder traversal] + O(NlogN) [marking the elements of matrix]
Space Complexity :- O(LogN) [for Queue] + O(N^2) for matrix

- nk September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

will the pathmatrix cocept work???

- anonymous September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Head recursion:

public void populateAncestorMatrixHeadRecursion(final Node root) {
        if (root == null) {
            return;
        }

        if ((root.left != null) && (root.right != null)) {
            if (!ancestorMatrix.containsKey(root.left)) {
                ancestorMatrix.put(root.left, new HashMap<Node, Node>());
            }

            ancestorMatrix.get(root.left).put(root.right, root);
        } else if ((root.left != null) && (root.right == null)) {
            if (!ancestorMatrix.containsKey(root.left)) {
                ancestorMatrix.put(root.left, new HashMap<Node, Node>());
            }

            ancestorMatrix.get(root.left).put(new Node(-1), root);
        } else if ((root.left == null) && (root.right != null)) {
            Node nullNode = new Node(-1);

            if (!ancestorMatrix.containsKey(nullNode)) {
                ancestorMatrix.put(nullNode, new HashMap<Node, Node>());
            }

            ancestorMatrix.get(nullNode).put(root.right, root);
        }

        if (root.left != null) {
            populateAncestorMatrixHeadRecursion(root.left);
        }

        if (root.right != null) {
            populateAncestorMatrixHeadRecursion(root.right);
        }
    }

}

- Jack September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

class Node
{
int data;

public int Data
{
get { return data; }
set { data = value; }
}
public Node left;
public Node right;

public Node(int data)
{
this.data = data;
}
}

static void Main(string[] args)
{
Node t = new Node(3);
t.left = new Node(2);
t.left.left = new Node(1);
t.right = new Node(5);
t.right.left = new Node(4);
int[,] M = new int[5,5];

BuildAncestorTree(t, new List<Node>(),M);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write(M[i,j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.Read();
}

static void BuildAncestorTree(Node n, List<Node> nodes, int[,] M)
{
if (n != null)
{
nodes.Add(n);
BuildAncestorTree(n.left,nodes,M);
BuildAncestorTree(n.right,nodes,M);
Node cur = nodes[nodes.Count-1];
nodes.Remove(cur);
foreach (Node node in nodes)
{
int i = cur.Data -1;
int j = node.Data -1;
M[i,j] = 1;
}
}
}

- Megha September 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the meaning of ancestor matrix??

- Psycho September 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# define MAX 10

int a[MAX][MAX]={0};
void AMatrix(int a[][n], struct node* root, int temp[], int index){
if(root == NULL)
return ;
int i;

for(i=0;i< index;i++)
a[root->data][temp[i]]=1;
temp[index]=root->data;

AMatrix(a, root->left, temp, index+1);
AMatrix(a, root->right, temp, index+1);

}

POint out if anything seems incorrect.

Thanks

- Nishant Pandey May 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Explain your code. What is temp? How it will be constructed?

- Nitin Gupta May 29, 2013 | Flag


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