Amazon Interview Question for Software Engineer / Developers






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

The clue here is every node contains 0 or 2 childrens. So using this condition we can be sure that we can construct a tree using the given preorder.

node* constructTree( char *str) 
{
        static int index =0 ;

        if ( str[index] == '\0' )
               return NULL;
        else
        {

                  node * ptr = new node;
                  ptr->left = ptr->right = NULL;
 
                  if ( str[index] == 'N' )
                  {

                       index++;
                       ptr->data = 'N'
                       ptr->left  = constructTree(str) ;
                       ptr->right = constructTree(str);
                  }
                  else if ( str[index] == 'L' )
                  {
                         index++;
                         ptr->data = 'L'; 
                  }
                  return ptr;
        }
}

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

Its really awsome.
Most simplified solution for this problem...... :)
thnx a lot

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

can you please explain the question in detail?

and please post some more questions for AMAZON bangalore..which u gave on 5'th feb

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

Hi Cracker(Who Have Cleard The Written Test & 3 Interview round)

Can You Please share your interview experience with us here
plz plz plz..?? because i also have interview with amzone blore so plz do it fats..help me..

Thanks & Regards

- Algoseekar March 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how can 1 construct a tree..with only preorder given...we also need the inorder for that

if only preorder is given then there can be many such trees.not a single unique 1..

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

I think in this particular case we can get a unique tree, but I m not sure.
1. root=NULL;
call MakeTree (&root, root, str, 0)

node* MakeTree (node** root, node* tempRoot, char* str, int* index)
{
if (str[*index])
{
tempRoot = makeNewNode ();/*some function which creates new
node and initialises node to 0*/
if (str [*index] == 'N')
{

if (*index == 0)
{
*root = tempRoot;
}
tempRoot->left = MakeTree (root, tempRoot->left, str, &((*index) ++));
tempRoot->right = MakeTree (root, tempRoot->right, str, &((*index) ++));
}
else
{
return tempRoot;
}
}
}

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

try this i assume traversal is in an array a.Call wud be Construct(new Node(),0)

int Construct(Node n,int i){
   if(a[i]=='N'){
      n.right=new Node();n.left=new Node();
      return Construct(n.right,Construct(n.left,i+1));
   }
   else
      return i+1;
}

- Sathya February 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Condition is every node will have 0 children or 2 children... If you encounter L then definitely it will have both child and verify to left

For preorder sequence LLNLNNLLNNLNN

                           L
                     L            L 
                  N     L      L     L
                       N  N   N N   N N 
They are just asking to construct the tree by seeing the sequence. not write the progam i think

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

you have to write a program to construct tree

- siva.sai.2020 February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdafx.h"
#include <iostream>
using namespace std;

struct node
{
	node *left;
	char data;
	node *right;
};

void insert(node **root, char d, int&);
void preorder(node *root);

int _tmain(int argc, _TCHAR* argv[])
{
	node *root= NULL;

	char a[] = {'L', 'N', 'L', 'L', 'L', 'N', 'N', 'N', 'L', 'N', 'L', 'L', 'N', 'N', 'L', 'N', 'N',};
	int n = sizeof(a) / sizeof(char);
	
	for(int i=0; i<n; i++)
	{
		int temp=0;
		insert(&root, a[i], temp);
	}

	preorder(root);cout << endl;
	cout << endl;

	return 0;
}

void insert(node **root, char d, int &flag)
{
	if(*root == NULL)
	{
		*root = new node;
		(*root)->data = d;
		(*root)->left = (*root)->right = NULL;
		flag = 1;
		return;
	}
	else
	{
		if((*root)->data == 'N')
			return;
		if((*root)->data == 'L')
		{
			insert(&(*root)->left, d, flag);
			if(flag == 0)
				insert(&(*root)->right, d, flag);
		}
	}
}

void preorder(node *root)
{
	if(root == NULL)
		return;

	cout << root->data << " ";
	preorder(root->left);
	preorder(root->right);
}

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

Can you please tell us the question for the program you have written?

- Anonymous February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its Very Tough Program..This Man..Makes it Very Easy..i Can Imagine that this man who hav written d program is really awesome..he/she can what he wants....he/she written very neat & lean code..gud luck man..keep it up.. iahve tested it for nearly 15 test cases & it running perfect some them are
//{'N'};
//{'N','L','L'};
//{'N','N','L','L','N','L','L'};
//{'N','N','N','L','L','L','L'};
//{'N','L','N','L','N','L','L'};
//{'N','N','N','L','L','N','L','L'};
//{'N','L','N','N','L','L','N','L','L'};
//{'N','N','N','L','L','N','L','L','N','L','L'};
//{'N','N','L','L','N','N','L','L','N','L','L'};
//{'N','N','N','L','L','N','L','L','N','N','L','L','N','L','L'};
//{'L', 'N', 'L', 'L', 'L', 'N', 'N', 'N', 'L', 'N', 'L', 'L', 'N', 'N', 'L', 'N', 'N',};

Best of Luck Man

- WgpShashank March 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you please tell us the question for the program you have written?

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

My solution is to use recursion as you traverse the Pre-Order once.
1. First node is the Root as everyone know.
2. Reading the rest of the array.
if the character is N; keep it as left link if available or else right link of it.
if both are filled go to its parent and follow the same.
if the character is L; keep it as left link if available or else right link of it.
if both are filled go to its parent and follow the same.

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

Replace all instance of pattern NXY in the tree with the tree built as N.left = X and N.right = Y
where X,Y can be L or a previously built NXY.

public static void makeTree(){
		String preOrder = "NNNLLLL";
		Stack<Treenode> stck = new Stack<Treenode>();
		for(int i=0; i<preOrder.length(); i++){
			if(preOrder.charAt(i) == 'N'){
				Treenode tnode1 = new Treenode('N', false);
				stck.push(tnode1);
			}
			else{
				Treenode tnode = new Treenode('L', true);
				boolean flag = false;
				while(true){
						if(!(stck.isEmpty())){
							if(flag){
								tnode = stck.pop();
							}
							else{
								flag = true;
							}
							Treenode stcktop = stck.pop();
							if(stcktop.nodetype){
								if(!(stck.isEmpty())){
									Treenode nnode = stck.pop();
									nnode.left = stcktop;
									nnode.right = tnode;
									nnode.nodetype = true;
									stck.push(nnode);
									if(stck.size()==1)
										break;
								}
								else{
									stck.push(stcktop);
									stck.push(tnode);
									break;
								}
							}
							else{
								stck.push(stcktop);
								stck.push(tnode);
								break;
							}
						}
						else{
							stck.push(tnode);
							break;
						}
					}	
			}
		}
	}

where the constructor for Treenode is

class Treenode{
	Treenode left;
	Treenode right;
	char value;
	boolean nodetype;
	
	public Treenode(char val, boolean nodetype){
		this.value = val;
		this.nodetype = nodetype;
		this.left = null;
		this.right = null;
	}
}

- Abhishek April 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@WgpShashank/anyone :- can u pls tell us the logic of the program.

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

Hi .. siva.sai .. did u cleared the amazon interview?

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

Hi .. siva.sai .. did u cleared the amazon interview?

- Arpit February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi .. siva.sai .. did u cleared the amazon interview?

- Arpit February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct tree* construct_tree(char* A, int *i)
   {
    //Boundary Condition
      if (A == NULL){
         return NULL;
      }
   
      struct tree *node = newnode(A[*i]);
    //On reaching leaf node, return
      if (A[*i] == 'L'){
         return node;
      }
   
    //Populate left sub tree
      *i = *i + 1;
      node->left = construct_tree(A, i);
   
    //Populate right sub tree
      *i = *i + 1;
      node->right = construct_tree(A, i);
   
      return node;
   }

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

struct tree * constructTree(char *inputArray, int arraySize, int *index)
{
             /* validating input */
	if( input == NULL || *index >= arraySize )
	{
		return NULL;
	}	
	else
	{
		struct tree newNode = (struct tree *) malloc ( sizeof(struct tree));
		newNode->data = inputArray[*index];
		newNode->left = newNode->right = NULL;
		
		*index = *index+1;
		
		if(inputArray[*index] == 'N')
		{
			newNode->left = constructTree(inputArray, arraySize, index);
			newNode->right = constructTree(inputArray, arraySize, index);
		}
		return newNode;
	}
}

- siva.sai.2020 February 14, 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