Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Looks like the given example tree is wrong, because leaf node 1 should have 
one node right? this should be like below with input 3221001000

         3
       / | \
      2  0  0
     / \  
    2   1  
   / \   \
  1   0   0
 /
0

Please correct me if I am wrong.

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

you mean to say Pre-order is given ??

- Avinash October 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah. you could paraphrase it like that :)

- msramachandran October 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node 
{
	int data;
	vector<Node*> children;
};

Node* _insert(Node *root, int n)
{
	if( root==NULL )
	{
		root = new Node;
		root->data = n;
		for( int i=0; i<n; i++ )
			children.push_back(NULL);
		return root;
	}
	
	for( int i=0; i<root->children.size(); i++ )
	{
		Node *x = _insert(children[i], n);
		if( x==NULL )	// Could not isert under children[i], try to insert as/under next child
			continue;
		if( x==children[i] )	// Inserted under children[i]
			return root;
		else 
		{
			children[i] = x;
			return root;
		}
	}
	return NULL;
}


Node* insert(Node* root, int n)
{
	Node* new_root = _insert(root, n);
	return ( new_root == NULL )?root:new_root;
}

- Sani October 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Added running code

#include<malloc.h>
#define N 4
struct node{
        int data;
        int number;
        struct node* (child[N]);
};
struct node* construct(int arr[],int start, int end)
{
        struct node* root=(struct node*)malloc(sizeof(struct node));
        if(arr[start]==0)
        {
                root->data=0;
                printf("build node %d\n",root->data);
                return root;
        }
        int i;
        static int index=0;
        root->data=arr[start];
        printf("build node %d\n",root->data);
        for(i=0;i<arr[start];i++){
                printf("called (arr,%d,%d)\n",index+1,end);
                root->child[i]=construct(arr,++index,end);}
        printf("finally returning root:%d\n",root->data);
        return root;
}

- tp December 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

exactly the same code :)

- sk007 January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node
        {
            public int Value;
            public Node[] children;
            public Node(int c)
            {
                Value = c;
                children = new Node[c];
            }

        }
        static void Main(string[] args)
        {
            Node node = FormTree( "3221001000");
        }
        static Node FormTree(string str)
        {
            int i = 0;
            return FormTree(str, ref i);
        }

        static Node FormTree(string str, ref int i)
        {
            int c = str[i++] - '0';

            Node node = new Node(c);
            for (int a = 0; a < c && i < str.Length; ++a)
            {
                node.children[a] = FormTree(str, ref i);
            }
            return node;
        }

- sva November 13, 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