Amazon Interview Question for Software Engineer / Developers


Country: India




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

int MaxVisitedIndex = 0;

TreeNode ConstructNodes(preOrderList, int index)
{
	TreeNode n = new TreeNode();
	n.data = preOrderList[index].data;
	
	if(MaxVisitedIndex < index)
		MaxVisitedIndex = index;
		
	if(preOrderList[index].NodeType == NodeType.Leaf)
	{
		n.Left = null;
		n.right = null;
	}
	else
	{
		n.Left = ConstructNodes(preOrderList, index + 1);
		n.Right = ConstructNodes(preOrderList, MaxVisitedIndex + 1);	
	}
	
	return n;
}

- Ran January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice!!

- pvs January 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why global variable, when you attempt Amazon online test, you don't have access to main function. So when their main function will run it twice, it will fail for second test, and onwards.

- Sachin January 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can use a static variable instead

- P January 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

shouldn't the recursion call values 2*i and 2*i+1

- scofield January 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"preOrderList" is a list storing preorder traversal of the tree. If the current root node is at 'i', left node is always stored at the 'i+1'th position.

We can pass the variable as reference parameter if global is not desired.

- Ran January 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You're not going to impress anybody at Amazon with a recursive function.

- Hal January 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(Especially one with no error checking.)

- Hal January 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Stack s1;
node nList[N];

for (i=0;i<N;i++){
        if (preorderList[i].type=='N' && nList[i].parent==NULL){
                nList[i].parent = pop(s1);
                s1.push(i);
                nList[i].left = i+1;
                nList[i+1].parent=i;
        }
        else if (preorderList[i].type=='S' && nList[i].parent==NULL){
                nList[i].parent = pop(s1);
                nList[i].parent.right = i;
        }

}

- coder123 January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution. Just one more case i guess. check this pre-order:

n1
/ \
l1 n2
/ \
n3 n4
/ \ / \
l2 l3 l4 l5

the preorder for this is n1,l1,n2,n3,l2,l3,n4,l4,l5
by your logic when the loop ends, the stack has still n2 and n0. so after the loop ends, should we connect n2 and n0 (i.e. remaining elements in the stack) as parents and children as well

- Jester January 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for pointing that out! Please check the code once again now.

- coder123 January 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Aren't you assuming that there is a parent pointer for each node ?

if it's not the case, the following soln works:

char pre_order[];
int sz = sizeof(pre_order);
stack<node *> S;

note *root, *parent = 0;
for(int idx = 0; idx < sz; idx++) {
    node *t = new node;
    if(parent != 0) {
        if(parent->left == 0)
            parent->left = t;
        else
            parent->right = t;
    } else {
        root = t;
    }
    if(pre_order[idx] == 'N') {
        parent = t;
        S.push(t);
    } else if(!S.empty()) 
        parent = S.pop();
    }
}

- 111 February 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

This is essentially like constructing an expression tree from the prefix expression. The L's are operands and the N's are operators. Parse the string from reverse and push the L's in a stack. For every N, pop two L's, make them the N's children and push it in the stack. Repeat until the string ends.

- Rajesh January 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good thinking

- P January 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good thinking but requires an extra stack.

- Laxmi Narsimha Rao Oruganti February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// getNextvalue is a pointer to a function which reads the next value if present or returns -1 if not present
typedef struct node * link ;
link make_tree(link_to_func getNextvalue )
{  int t;
	if((t=getNextvalue())==-1)
		return NULL;
link head=new node(t,NULL,NULL);
head->left=make_tree(getNextptr);
head->right=make_tree(getNextptr);
return head;

}

//we cannot get the exact tree we can get one of the possible trees which gives the inorder as the given sequence

- Anonymous January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please note, that pre-order is given, not in-order.

And we can get the exact tree, considering the other constraint given. (0 and 2 children)

- P January 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it is correct for pre-order . when i am printing the tree using pre-order i am getting the initial input sequence

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

void insert(node **root, char aa[], int &cnt) // cnt starts from zero
{
   *root = new node;
   (*root)->data = aa[cnt];
   (*root)->left = (*root)->right = NULL;
   if(aa[cnt] == 'N')
   {
      insert(&(*root)->left, aa, ++cnt);
      insert(&(*root)->right, aa, ++cnt);
   }
}

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

/*
struct Node
{
char val;
Node* left;
Node* right;
};
*/

// p/P -non-leaf node and L/l - leaf node
Node* createBinaryTree(string str)
{
stack<Node*> stk;
size_t length = str.length();
Node * root = new Node();
if(str.empty())
{
cout<<"Error: invalid Input"<<endl;
return NULL;
}
root->left = NULL;
root->right = NULL;
root->val = str[0];

stk.push(root);
for(int i = 1; i < length; i++)
{
Node * newNode = new Node();
newNode->left = NULL;
newNode->right = NULL;
newNode->val = str[i];
if(stk.empty())
{
cout<<"Error: invalid Input"<<endl;
return NULL;
}
Node* tmpNode = stk.top();
while(!stk.empty())
{
if (tmpNode->left == NULL)
{
tmpNode->left = newNode;
break;
}

if (tmpNode->right == NULL)
{
tmpNode->right = newNode;
stk.pop();
break;
}
tmpNode = stk.top();
}
if(str[i] == 'P' || str[i] == 'p')
stk.push(newNode);
}
return root;
}

- sheldon January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

1. Calculate number of Leafs (i.e. L) in preorder say it is nL
2. if nL =1, there is only one node i.e root
3. if nL>1 && nL is even , left and right subtree is of height nL/2
4. if nL>1 && nL is odd, one subtree is of height 1 and other is of height (nL-1)/2 (There are two trees possible in this case)

Once we have height of both subtrees we can construct trees.

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

One can construct multiple trees if we have to satisfy just pre-order traversal.

Example: Pre-Order Traversal Result: A B D E C F G

A
         /     \
        B       C
      /   \   /   \
      D   E   F    G

A
         /   \
        B     D
            /   \
            E    C
               /   \
               F   G

It is also not clear what other nodes are possible than N & L in a binary tree?

To conclude, question is incomplete or the interviewer is expecting you to prove that question leads to multiple answers :).

Thanks,
Laxmi

- Laxmi Narsimha Rao Oruganti January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

By N and L, I mean, node names can be only N or L
{ N
/ \
N L
/ \
L L
}

Pre-Order will be NNLLL

- P January 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for clarification. Now I read it again, I can see how my reading comprehension is low.

Thanks,
Laxmi

- Laxmi Narsimha Rao Oruganti January 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Because for a tree with 0 or 2 children, number of leaf = Number of Non-leaf + 1.

we can separate left and right tree with this fact.

I have the following code

Node *BuildTree(char A[ ], int size)
  {
     if (size == 1){
          if (A[0] == 'L') {
                Node *root = new Node(A[0]);
                root ->left = NULL;
                root->right = NULL;
                return root;
         }else
                return NULL;
       }

      Node *root = new Node(A[0]);
      root ->left = NULL;
      root->right = NULL;
      int N_count = 0;
      int L_count = 0;
      int i = 1;
      while (L_count <=N_count){
             if(A[i] == 'N')  N_number ++;
             else L_Number ++;
             i++;
       }
       root ->left = BuildTree(A+1, i-1);
       root->right = BuildTree(A+i+1, size - i- 1);
       return root;
}

- hanks January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Here is the working code link: h t t p://codepad.org/qqd48QUT

Logic:
0) The length of the string (or count of nodes) with given restrictions will always be odd
1) For every 'N' visited, call the create function to populate left sub-tree and right sub-tree.

In more detailed manner:
1) Let us assume CreateTree is a function that takes the input string, string length, parent node reference, and an index into the string about current node
2) When the index surpasses the string length, it means we are expecting more nodes as per restrictions but the input given does not have. Which means, invalid input. Bail out
3) Create a node with current index data
4) If current node data is 'L', no more children for this node. just return success
5) If current node data is 'N', it means we have two children for this node.
5.1) Increment the current node index, call the CreateTree function to populate left sub-tree
5.2) Increment the current node index, call the CreateTree function to populate right sub-tree
6) Set the root at this level to this node and return success

- Laxmi Narsimha Rao Oruganti January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ class Node { Node* left; Node* right; char value; Node() : left(NULL), right(NULL) {} public: void set_value(char c) { value = c } static create_node( char c ) { Node* n = new Node(); n->set_value(c); } }; main () { char array[] = "NNLNLLNNLLL"; int i = 0; Node* current_node = NULL, *node = NULL; while( array[i++] != '\0' ) { if (current_node == NULL) { current_node = Node::create_node( array[i] ); } else if( current_node->value == 'N' ) { if( current_node->left != NULL) { node = Node::create_node(array[i] ); current_node->left = node; if ( node->value == 'N' ) { push(current_node); current_node = node; } } else if (current_node->right == NULL ) { node = Node::create_node(array[i]); current_node->right == node; if( node->value == 'N') { push(current_node); current_node = node; } } else { current_node = pop(); } } // current_node->value == 'N' } // while() } - coder January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;


public class PreOrderConstruction {

public static void main(String[] args) {

char[] preOrderList = new char[]{'N','N','L','L','L'};
Stack<Node> stck = new Stack<Node>();

Node inNode = null;
for(int i=preOrderList.length-1; i>=0; i--){
if(preOrderList[i]=='L'){//If leaf node
stck.push(new Node(preOrderList[i]));
}else{
Node left = stck.pop();
Node right = stck.pop();
inNode = new Node(preOrderList[i]);
inNode.setLeftNode(left);
inNode.setRightNode(right);
stck.push(inNode);
}
}
inNode = stck.pop();
}

}

class Node{

Node left = null;
Node right = null;
char data;

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

public void setLeftNode(Node n){
this.left = n;
}

public void setRightNode(Node n){
this.right = n;
}

}

- Panda January 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* GenerateTree(char *input, int stIndex, int endIndex)
{
	if(stIndex > endIndex) return null;
	if(input[stIndex] == 'L')
	{
		node *tempnode =(node*)malloc(sizeof(node));
		tempnode->data = 'L'; // I have just put here some data
		return tempnode;
	}
	else if (input[stIndex] == 'N' && stIndex +2 <= endIndex)
	{
		node *tempnode = (node *)malloc(sizeof(node));
		tempnode->data = 'N';
		tempnode->left = GenerateTree(input,stIndex + 1, endIndex);
		tempnode->right = GenerateTree(input, stIndex+ 2, endIndex);
		if(tempnode->left == null || tempnode->right == null)
		{
			return null;
		}
		else
		{
			return tempnode;
		}
	}
	else
	{
		return null; // Invalid format
	}
}

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

node* GenerateTree(char *input, int stIndex, int endIndex)
{
	if(stIndex > endIndex) return null;
	if(input[stIndex] == 'L')
	{
		node *tempnode =(node*)malloc(sizeof(node));
		tempnode->data = 'L'; // I have just put here some data
		return tempnode;
	}
	else if (input[stIndex] == 'N' && stIndex +2 <= endIndex)
	{
		node *tempnode = (node *)malloc(sizeof(node));
		tempnode->data = 'N';
		tempnode->left = GenerateTree(input,stIndex + 1, endIndex);
		tempnode->right = GenerateTree(input, stIndex+ 2, endIndex);
		if(tempnode->left == null || tempnode->right == null)
		{
			return null;
		}
		else
		{
			return tempnode;
		}
	}
	else
	{
		return null; // Invalid format
	}
}

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

int count = 0;

treeNode * createNode(char val){
	treeNode * node = malloc(sizeof(treeNode));
	node->value = val;
	node->lChild = NULL;
	node->rChild = NULL;
	return node;
}

//treeString is the pre-order representation of tree,i.e.-"NNLLL"
treeNode * makeTree(char * treeString, int *count){
treeNode * temp = NULL;
if(*(treeString + *count) != '\0'){
	temp = createNode(*(treeString + *count));
	if(*(treeString+ *count) == 'N'){
		*count += 1;
		temp->lChild = makeTree(treeString,count);
		*count += 1;
		temp->rChild = makeTree(treeString,count);
	}
}
return temp;
}

So, the function is called as:
int main(){
	char * treeString = "NNLLNNLLL";
	treeNode * root = NULL;
	root = makeTree(treeString,&count);
}

- TheGhost January 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

string str="NNLLL";
int i = 0;
typedef struct node
{
char val;
struct node *left;
struct node *right;
}*tree;
tree root;
void construct_tree(tree current)
{
if(i < (int)str.length() - 1 && current->val=='N')
{
tree temp_l = new node;
temp_l->left=temp_l->right=NULL;
temp_l->val= str[++i];
current->left = temp_l;
construct_tree(temp_l);
tree temp_r = new node;
temp_r->left=temp_r->right=NULL;
temp_r->val = str[++i];
current->right = temp_r;
construct_tree(temp_r);
}
return;
}

void print(tree current)
{
if(current != NULL)
{
cout<<current->val<<" ";
print(current->left);
print(current->right);
}
}
int main()
{
tree tmp = new node;
tmp->left=tmp->right=NULL;
root = tmp;
if(str.length()-1 > 0)
{
tmp->val = str[0];
construct_tree(tmp);
}
print(root);
return 0;
}

- kk.nitrkl January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it works fine but is recursive .. need to think about non-recursive implementation

- kk.nitrkl January 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Node buildTree(Node[] preorder) {
Stack s = new Stack();
Node lastNode = null;
for(int i=0;i<preorder.length;i++) {
Node node = preorder[i];
if(lastNode == null) {

} else if (lastNode.type.equals("N")) {
s.push(lastNode);
lastNode.left = node;
} else {//node type if L
s.pop().right = node;
}

lastNode = node;
}
return preorder[0];
}

- Ming January 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Node buildTree(Node[] preorder) {
		Stack s = new Stack();
		Node lastNode = null;
		for(int i=0;i<preorder.length;i++) {
			Node node = preorder[i];
			if(lastNode == null) {
				
			} else if (lastNode.type.equals("N")) {
				s.push(lastNode);
				lastNode.left = node;
			} else {//node type if L
				s.pop().right = node;
			}
			
			lastNode = node;
		}
		return preorder[0];
	}

- Ming January 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* create_from_preorder(char *s)
{
static int count=0;
node * temp=(node*)malloc(sizeof(node));
temp->left=NULL;
temp->right=NULL;
printf("enter here");

if(s[count]== '1')
{
printf("got 1\n");
temp->data=1;
count++;
temp->left=create_from_preorder(s);
count++;
temp->right=create_from_preorder(s);

}
else if(s[count]== '0')
{
printf("got 0\n");
temp->data =0;

}
else
{
printf("not a valid input");
return NULL;
}
return temp;

}

- kunal gupta January 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* create_from_preorder(char *s)
{
static int count=0;
node * temp=(node*)malloc(sizeof(node));
temp->left=NULL;
temp->right=NULL;
printf("enter here");

if(s[count]== '1')
{
printf("got 1\n");
temp->data=1;
count++;
temp->left=create_from_preorder(s);
count++;
temp->right=create_from_preorder(s);

}
else if(s[count]== '0')
{
printf("got 0\n");
temp->data =0;

}
else
{
printf("not a valid input");
return NULL;
}
return temp;

}

- kunalgupta January 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pair<TreeNode<Character>, Integer> preorderToTree(char[] a, int index) {
    if (index < a.length) {
      TreeNode<Character> n = new TreeNode<>();
      n.value = a[index];
      if (a[index] == 'L') { // leaf
        return new Pair<>(n, index+1);
      }
      else {
        n.value = 'N';
        Pair<TreeNode<Character>, Integer> lp = preorderToTree(a, index+1);
        if (lp != null) {
          Pair<TreeNode<Character>, Integer> rp = preorderToTree(a, lp.snd);
          n.left = lp.fst;
          n.right = rp.fst;
          return new Pair<>(n, rp.snd);
        }
        return new Pair<>(n, index+1);
      }
    }
    return null;
  }
  
  @Test
  public void testPreorderToTree() {
    Pair<TreeNode<Character>, Integer> p = preorderToTree(
        "NNLLL".toCharArray(), 0);
    System.out.println(p.fst.toString());
  }

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

stuct node *constructTree(char *preorder, int *index)
{
// Return null if the array is empty
if (!preorder || *index == n)
    return NULL;

// Create a node from the array at that index
struct node *new = NewNode(preorder[*index];

// If the node is a leaf node, terminate the funtion and return a pointer to that node
if (new->data == ā€˜Lā€™) 
    return node;

// Move the index pointer ahead by one cell
*index = *index + 1;

// Construct the node's left pointer
new->left = constructTree(preorder, index);

// Move the index pointer ahead by one cell
*index = *index + 1;

// Construct the node's right pointer
new->right = constructTree(preorder, index);

// Return the current node's pointer to its caller funtion
return node;
}

Time Complexity O(n)

- Dragon March 08, 2012 | 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