Amazon Interview Question for Software Engineer / Developers


Country: India




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

Even I was asked this code recently in 1st round of Amazon Interview:
Logic is :

1. Solve it by recurssion.
2. The left most child of a tree is the head node
3. The right most child of a tree is the last node of a DLL.
4. If only one node exists . Then the root->left=root and root->right=root;

static Node BSTtoDLL(Node root)
{ Node head,tail;
if(root==NULL) return NULL;

head=BSTtoDLL(root->left);
tail=BSTtoDLL(root->right);

// code for single element
root->left=root;
root->right=root;

head=append(head,root);
head=append(head,tail);

return(head);

}

void createDLLpointers(Node a,Node b)
{
a-> left=b;
b->right=a;
}

Node append(Node a, Node b)
{
if(a==NULL) return b;
if (b=NULL) return a;

afirst=a->left; // pointers to the first elements of each list.
bfirst=b-left;

createDLLpointers(afirst,b);
createDLLpointers(bfirst,a);

return a;
}

- anjum February 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey can you explain your code more. you have taken different function where it is using not getting your point

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

Can anybody explain how this code is running

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

//Let T be the root node of bst tree.


main(){
start=createNew(-Infinity);
end=createNew(+Infinity);
start->prev=null;
start->next=end;
end->next=null;
end->prev=start;
bstToLLRight(T,start);
}


bstToLLRight(T,L){
if(T!=null){
node=createNew(T.value);
node->prev=L;
node->next=L->next;
L->next->prev=node;
L->next=node;
bstToLLLeft(T->left,node);
bstToLLRight(T->right,node);
}
}


bstToLLLeft(T,L){
if(T!=null){
node=createNew(T.value);
node->next=L;
node->prev=L->prev;
L->prev->next = node;
L->prev = node;
bstToLLLeft(T->left,node);
bstToLLRight(T->right,node);
}
}

- Sid February 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even recursion is not considered as constant space(becuase of space taken in stack for each function call), I guess we have to use recursion to iterate through a tree.

- Sid February 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess constant space means you have to modify the tree, not to traverse and build a DLL. This question can be solved using inorder traversal(as in above codes) or post order traversal of tree.

- shashank.neo February 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void BSTtoDLL(node* root, node* &left, node* &right){
      if ( root ){
           node *left1=0,*right1=0,*left2=0,*right2=0;
           BSTtoDLL(root->left,left1,right1);
           BSTtoDLL(root->right,left2,right2);
           if(right1){
                     right1->right=root;
                     root->left=right1;
           }
           if(left2){
                     root->right=left2;
                     left2->left=root;
           }
           if (left1)
           left=left1;
           else 
           left = root;
           if (right2)
           right=right2;
           else
           right=root;
      }
}

- shashank.neo February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i have done it by using an array with two indices (like queue). can i make use of that as it is stated to use constant space.
Algo i had used...
1. make an inorder traversal...
2. store all the addresses of tree's node in an array (queue)..
3. assign
q[i]->left=q[i+1];
q[i]->right=q[i-1];
4. head of dll = q[0]

i only want to know that can i use extra array here on not....??

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

Here is my solution. It may not be efficient with time complexity

private void convertToDLLinkedList(TreeNode node) {
		if (node != null ) {
			if (node.getLeftNode() != null) {
				convertToDLLinkedList(node.getLeftNode());
				TreeNode temp = node.getLeftNode(); 
				while (temp.getRightNode() != null) {
					temp = temp.getRightNode();
				}
				temp.setRightNode(node);
				node.setLeftNode(temp);
			}
			if (node.getRightNode() != null) {
				convertToDLLinkedList(node.getRightNode());
				TreeNode temp = node.getRightNode(); 
				while (temp.getLeftNode() != null) {
					temp = temp.getLeftNode();
				}
				temp.setLeftNode(node);
				node.setRightNode(temp);
			} 
		}
	}

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

It can be done in O(n) time and using constant space by using two functions: find_successor(node * ptr) and find_predecessor(node * ptr). Both these functions run using constant space.I am assuming that nodes of the tree are modifiable into nodes of a DLL.
BST_to_DLL(node * root)
{
node *head,*tail,ptr;
head=tail=NULL;
if(root!=NULL){head=tail=root;}
while((ptr=find_predecessor(head))!=NULL)
{
ptr->next=head;
head->prev=ptr;
head=ptr;
}
while((ptr=find_successor(tail))!=NULL)
{
tail->next=ptr;
ptr->prev=tail;
tail=ptr;
}
return head;
}

- Manohar Singh February 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Morris traversal (inorder traversal of BST without stack or recursion),

- RV February 12, 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