Amazon Interview Question for SDE1s


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
3
of 5 vote

You can use Morris traversal and traverse the trees iteratively using no stack (www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/)

- oOZz June 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

but using inorder traversal you cannot decide whether two trees are equal or not.

- Algorithmist June 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can't?

Why not just traverse the tree in order and comparing current, left child and right child with the other tree's current/leftchild/rightchild?

- bunnybare June 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@AnonymousMe, You can compare both trees using inorder traversal

1. Pass both root nodes to the function and iterate them at the same time, compare the nodes, just like @bunnybare mentioned above. (recommended)

2. Alternatively, you can traverse the trees one by one and save the null children as nodes too. e.g., if this is your tree

3
/ \
1 2
/
4

Your in-order will be: null, 4, null, 1, null, 3, null, 2, null

Then compare these two lists.

- oOZz June 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution, I support that you can traverse both trees at the same time, and it should be synchronism, if the trees are same, at every steps, the value are equal, else they are not same.

- yingsun1228 June 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I ask these questions! Yay!

- Idiotic Question asking Interviewer. June 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

By comparing both trees together at each step of inorder

#include<stdio.h>
struct node
{
    int data;
    struct node* left;
    struct node* right;
};
struct stack
{
    int top;
    int capacity;
    struct node **array;
};

struct stack * CreateStack()
{
    struct stack *s=malloc(sizeof(struct stack));
    if(!s)
        return 0;
    s->top=-1;
    s->capacity=10;
    s->array=malloc(s->capacity*sizeof(struct node *));
    if(!s->array)
        return 0;

    return s;
}
int isEmptyStack(struct stack *s)
{
    return(s->top==-1);
}
void DoubleStack(struct stack *s)
{
    s->capacity*=2;
    s->array=realloc(s->array,s->capacity);
}
int isFullStack(struct stack *s)
{
     return(s->top==s->capacity-1);
}
void push(struct stack *s, struct node *root)
{
    if(isFullStack(s))
        DoubleStack(s);
    s->array[++s->top]=root;
}
struct node * pop(struct stack *s)
{
    if(isEmptyStack(s))
        return 0;
    else
        return s->array[s->top--];

}

struct node * newNode(int data)
{
        struct node *root;
        root=(struct node*)malloc(sizeof(struct node));
        root->data=data;
        root->left=NULL;
        root->right=NULL;
        return root;
}
int equalTrees(struct node *root1,struct node *root2)
{
    int count1=0,count2=0;
    struct stack *s1=CreateStack();
    struct stack *s2=CreateStack();
   int x=3,y=3;

    while(1)
    {
        while(root2)
    {
                push(s2,root2);
                root2=root2->left;
                count2++;
    }
    while(root1)
    {
                push(s1,root1);
                root1=root1->left;
                count1++;
    }
    if(isEmptyStack(s1) && isEmptyStack(s2))
    {
            return 1;
    }
    if(isEmptyStack(s1)|| isEmptyStack(s1) )
    {
              return 0;
    }
    if(count1!=count2)
        return 0;
               root1=pop(s1);
                root2=pop(s2);
                if(root1->data!=root2->data)
                    return 0;
                root1=root1->right;
                root2=root2->right;
    }


}
int main()
{
    struct node *root1 =newNode(1);
    root1->left        = newNode(3);
    root1->left->right  = newNode(2);
    struct node *root2 = newNode(1);
    root2->left        = newNode(2);
    root2->left->left  = newNode(3);
    printf("%d",equalTrees(root1,root2));
    return 0;
}

- Joey June 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No Extra Space is to be used.

- Anonymous June 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ohhh sorry then i guess ur answer seems correct :/

- Joey June 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your equalTrees function already uses 2 stack objects. How is this answering the question?

- pedropenduko June 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think perform both Inorder and pre order using Morris tree traversal as both are possible. If trees are identical than both will yield the same answer.

- Nascent June 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Forget about Morris Traversals. No one expects you to know that.

This is likely not an interview question. If it is, either the candidate was stellar, or the interviewer was an idiot.

- Anonymous June 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I thought for a while and said it is not possible without extra space for iterative solution.

- Algorithmist June 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I believe you can. If both Binary tree implementation is using an adjacency matrix then you can use iteration to traverse the tree. The matrix is NxN multi-dimensional array. So for a Binary Tree with 10 nodes you get a A[10][10] to represent the relationship of the nodes, all nodes with a value (say non-negative value) represents relationship from source node to target node. You also need an array which contains the values say V[N].

Note that my assumption here is the binary tree has already allocated the arrays as a representation of the trees and it node values. The problem we are trying to solve is if our comparison require additional space, and my answer is we don't need additional space as long as its represented this way as a tree. Of course you need the iteration variables.

So for example if you have an entry A[1][2] = 1 and A[1][3] = 1 then Node 1 has two children Node 2 and Node 3. Then lookup the values array say V[1], V[2] and V[3] for the values. Just compare these indexes on both the binary trees. If at least one is not equal then exit your iteration and the trees are not equal.

- pedropenduko June 20, 2013 | 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