Amazon Interview Question for Software Engineer in Tests






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

where will the root go?

anyways, may be we can maintain 3 arrays and while BFS, fill those arrays and after BFS print those.

- verma.tech December 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i guess root will come at the last I guess... so if we have:
1
/ \
2 3
/ \ /
4 5 6

@Tim .. Then u mean we should print: 2 4 5 6 3 1 ?

- Rj January 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Probably considering a bigger tree is better to analyse this question:
{{
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ \
8 9 10 11 13
\
12

}}}
Then order shud be: 2 4 8 9 10 12 5 11 3 6 13 7 1
Is my ordering correct ? (or 4 shud cum b4 2 ? :o)

- Rj January 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Plz see my diagram below... this 1 got messed up due to a missing '}' !

- Rj January 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i mean missing '{'.. :D

- Rj January 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

(oops.. ignore previous drawing..)

Probably considering a bigger tree is better to analyse this question:

1
                            /               \
			   2	             3
                        /      \           /   \
		       4        5	  6     7
		      /  \     /  \        \
 		     8	  9  10   11        13
				   \
				    12

Then order shud be: 2 4 8 9 10 12 5 11 3 6 13 7 1
Is my ordering correct ? (or 4 shud cum b4 2 ? :o)

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

Shouldn't the order be : 2 4 8 9 10 12 5 11 6 13 7 3 1 ..??

- Annonymous January 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Algo for above o/p: hope i m right

func(root)
{
if(root!=NULL)
{
if(root->left!=NULL)
left[i++]=root->left;
if(root==leaf(node))
{
leaf[j++]=root;
return;
}
else
{
func(root->left);
if(left!=NULL && left[i]=leaf[j])
i--;
if(root->right!=leaf(node))
right[k++]=root->right;
func(root->right);
while(i>=0)
print left and i--;
while(j>=0)
print leaf and j--;
while(right[k-1]==traversed)
print right and k--;
}
}
else
return;
}

- d_mak January 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

guys..here is a good solution and fair one.I copied frm id = 3747687

void printOuterTree(Node root){
if(root != null){
System.out.println(root.element);
printOuterTree(root.left,1,1);
printOuterTree(root.right,0,0);
}
}

void printOuterTree(Node root,int urFlag, int parentFlag){
if(root == null)
return;
if(root.left == null && root.right == null)
System.out.println(root.element);
else{

if(urFlag==1){//do a preorder for left subtree

if(urFlag == parentFlag)
System.out.println(root.element);
printOuterTree(root.left,1,urFlag);
printOuterTree(root.right,0,urFlag);
}
else{//do a post order for right sub tree
printOuterTree(root.left,1,urFlag);
printOuterTree(root.right,0,urFlag);
if(urFlag == parentFlag)
System.out.println(root.element);
}
}
}

- Newbie January 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not able to get what the question is ?

Will DFS doesn't work for this ?

- genthu January 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tried to take care of most of the cases. Here's the bfs algo

void bfs (Node *root)
{
    Queue q;
    int next_level;
    int current_level;
    int i = 0; j = 0; k = 0, current_level_holder;
    Node left[MAX], right[MAX], leaf[MAX];
    if (root != NULL)
    {
        q.enqueue (root);
        current_level = 1;   //No of nodes left at the current level
        next_level = 0;      //keeps count of no of nodes at the next level;
        current_level_holder = 1;   //Used for comparison and meet certain conditions
        left[i++] = root;
        while (!q.isEmpty ())
        {
            if (current_level == 0)
            {
                current_level = next_level;
                current_level_holder = next_level;
                next_level = 0;
            }
            root = q.dequeue;
            if (root->left)
            {
                if (next_level == 0 && current_level == current_level_holder && (root->left->left
                != 0 || root->left->right != 0))
                    left[i++] = root->left;
                next_level++;
                q.enqueue (root->left);
            }
            current_level_holder--;
            if (root->right)
            {
                if (current_level == 1 && (next_level != 1 || root == head))
                    right[j++] = root->right;
                q.enqueue (root->right);
                next_level++;
            }
            current_level_holder--;
            if (root->left == NULL && root->right == NULL)
                leaf[k++] = root;
            current_level--;
        }
    }
}
Print the left and leaf arrays as is and print the right array backwards

- Siraj January 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Just print the root first.
- Then pass left child to your bfs method.
- Then pass right child to your bfs method.

- Big-O January 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't this postorder traversal?

- Messi February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is not Post order Traversal. I am guessing that we should print all the left nodes first, right nodes second, the leaf nodes third and then at last the root.

My solution is to maintain 3 queues that allocates left, right and leaf nodes.

- Karthik February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. print root
2. print pre-order of the left subtree with:
a. a node is printed if it reached only by traversing left children (if the traversal never makes a right move),
b. or if it is a leaf
3. print post-order of the right subtree with:
a. a node is printed if it reached only by traversing right children(if the traversal never makes a left move),
b. or if it is a leaf

- Vinay Thotakura March 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printLeftSide( tree* root, bool toPrint )
{
if(root==NULL) return ;
if((root->left==NULL && root->right==NULL)|| toPrint) printf("%d ",root->data);
printLeftSide(root->left,toPrint);
printLeftSide(root->right,false);
}
void printRightSide( tree* root, bool toPrint )
{
if(root==NULL) return ;
printRightSide(root->left,false);
printRightSide(root->right,toPrint);
if((root->left==NULL && root->right==NULL)|| toPrint) printf("%d ",root->data);
}
void printBoundary( tree* root )
{
if(!root) printf("%d ",root->data);
printLeftSide(root->left,true);
printRightSide(root->right,true);
}
int main()
{
tree *T = newNode(26);
T->right = newNode(3);
T->right->right= newNode(3);
T->left = newNode(10);
T->left->left = newNode(4);
T->left->left->right = newNode(30);
T->left->right = newNode(6);
printBoundary(T);
puts("");
return 0;
}

- Chuchu November 03, 2011 | 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