Amazon Interview Question for Software Engineer / Developers






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

I have an answer. Not sure if it is bug free

void treeList(struct Node * root)
{
	struct Node *lastNode = root; // End of the imaginary Queue
	struct Node *currentNode = root; // Start of the imaginary Queue
	
	while(currentNode)
	{		
		cout << currentNode->data << ' ';
		if(currentNode->left)
		{
			lastNode->next = currentNode->left;
			currentNode->left->previous = lastNode;
			lastNode = currentNode->left;
		}
		if(currentNode->right)
		{
			lastNode->next = currentNode->right;
			currentNode->right->previous = lastNode;
			lastNode = currentNode->right;
		}
		currentNode = currentNode->next;
	}
}

- codebuddy November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this should work. thanks codebuddy

- blueskin.neo November 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this should not work as you are not going to link level wise nodes. Please check once again.Make me correct if i am wrong

- SK November 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I executed this code and compared the output with the output of breadth first traversal (using queue) and I am getting same output consistently. That means the above code is correct though there could be boundary conditions which need to be tested.

If you need more clarification please let me know.

- Codebuddy November 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code works.(takes the list till the end from root).
Only the filling of prev pointers is problematic.
it can be removed from both the if's and can be replaced as follows, (before cur=cur->next)
cur->next->prev=cur;

- Anonymous December 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

great sol dude

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

this code works

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

the boundary condition can be attained by adding a line before

currentNode = currentNode->next;

just a single line would be

if(currentNode->next==null)break;

and outside the loop just have

currentNode->next = root;
root->prev = currentNode;

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

NULL <-------1 -------> [Node2]
                 /              \
     [Node1]<-  2 <--------------> 3 -> [Node4]
              /   \             /     \
 [Node3]<-  4<---->5<--------->6<------>7 -> [Node8]
          /  \    /   \      /   \    /   \
[Node7]<-8<-->9<->10<->11<->12<->13<->14<->15->NULL

Are we looking at something like this? if not feel free to use the above and explain.
If this is the case then we might need a pointer to go back to the parent, do we have that?

- blueskin.neo November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, exactly.

- codebuddy November 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void ConvertBSTtoLL(Node *root)
  {
       queue <Node *> treeQ1;
       queue <Node *> treeQ2;
       Node *prevNode = NULL;
       if (root == NULL ) {return ; }
       treeQ1.push(root);        
       while (!treeQ1.empty()|| !treeQ2.empty())
       {
           Node *tempNode == NULL;
           while (!treeQ1.empty())
           {
               tempNode = treeQ1.pop();
               tempNode->prev = prevNode;
               if (tempNode->left  != NULL) { treeQ2.push(tempNode->left); }
               if (tempNode->right != NULL) { treeQ2.push(tempNode->right); }
               prevNode = tempNode;
           }
           if (tempNode != NULL) {
               if (treeQ2.empty()) { tempNode->next = NULL; }
               else { tempNode->next = treeQ2.front(); }
           }
           while (!treeQ2.empty())
           {
               tempNode = treeQ2.pop();
               tempNode->prev = prevNode;
               if (tempNode->left  != NULL) { treeQ1.push(tempNode->left); }
               if (tempNode->right != NULL) { treeQ1.push(tempNode->right); }        
               prevNode = tempNode;
           }
           if (tempNode != NULL) {
               if (treeQ1.empty()) { tempNode->next = NULL; }
               else { tempNode->next = treeQ1.front(); }
           }                               
       }  
  }

- Ramkumar November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

should be without queue or any others

- Anonymous November 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"The algorithm should have space complexity O(1). i.e., should be done without using extra data structures"

- Anonymous November 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void TreeToList (Node* root, Node* next)
{
if (!root) return;
if (!next) next = root;
if (root->left)
{
next->next = root->left;
root->left->previous = next;
next = r->lchild;
}
if (root->right)
{
next->next = root->right;
root->right->previous = next;
next = root->right;
}
TreeToList(root->left, next);
TreeToList(root->right, next);
}

- Worldlet November 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice idea. But I guess it's not going to work once the tree has height > 1.

This can be done using a queue and using bfs algorithm. Not sure if that can be counted as O(1) space.

- vrb November 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this code does not work for level 2 onwards.

- Anonymous November 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This question can be done without using the next and previous pointers. This has been discussed on the forum itself if I remember. i will post the code once it is ready...

- DashDash November 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a duplicate of question #6070872.
Solution is posted there already which is very simple and working with no need of recursion (hence no need of stack memory space)

- Unknown November 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the algorithm

1) Make root->next = root->right
In recursion up to leaf nodes from root
2) if(this->right!=null) this->right->next=this->left ; apply recursion to this->right
if(this->left!=null)this->left->next=this->next->right ; apply recursion to this->left

this will give a singly linked list which can be used to make doubly linked list.

- Anonymous November 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This one will work
Complexity O(n) and Space complexity O(1)

pair<node*,node*> bstTodll(node *root){
if(root==null)
return make_pair(null,null);
else{
pair<node *,node*> p1=bstTodll(root->left);
pair<node *,node*> p2=bstTodll(root->right);
if(p1.second!=null){
p1.second->next=root;
root->previous=pi.second;

}
else{
p1.second=p1.first=root;
}
if(p2.first!=null){
root->next=p2.first;
p2.first->previous=root;

}
else{
p2.first=p2.second=root;
}
return make_pair(p1.first,p2.second);

}
}

- nitin November 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Codebuddy has already solved it. The only missing part is that root->prev should be NULL and the end of the list should be NULL. That's all.

- Rayden November 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nvm they are all NULL at the start anyway.

- Rayden November 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think first we have to do level order traversal in place means without using queue and then we can do that work simply.

code is shown below

levelorder(tree, level)
begin if tree is null, return;
if level is 1, then
if(connection != null)
{
connection->next = tree.root && tree.root -> previous = connection;
}
connection = tree.root;
}

else if(level > 1)
{
levelorder(tree.leftsubtree, level - 1);
levelorder(tree.rightsubtree, level-1);
}
}


levelorder(tree)
for d=1 to heightree
levelorder(tree,d);

- skagrawal November 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution posted by codebuddy, on November 21, 2010 seems to be correct.

- Anonymous December 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

isn't that wrong coz for each node we are making its 'left' child as its 'next' node while for the last node at a level,the 'next' node should be the first node at the next level.
or may be i misunderstood the code.

- baghel April 11, 2011 | Flag


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