Amazon Interview Question for Software Engineer / Developers


Team: RCX
Country: United States
Interview Type: In-Person




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

C#:

private void PopulateNextRight(Node node, Node parent)
{
      if(node==null) return;
      if(parent.leftChild==node) node.nextRight=parent.rightChild;
      PopulateNextRight(node.leftChild, node);
      PopulateNextRight(node.rightChild, node);
}

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

This code will create a nextRight link for all left nodes. I am guessing the question is assuming that the right child needs to be connected to the left most child on the level below?

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

Good point. If that is what you want, we can do a BFS search, push everything on to a stack and then pop one by one and assign pointers. Here is the code in C#:

private void PopulateAllNextRight(Node node)
{
    Stack<Node> allNodes=new Stack<Node>();
    Queue<Node> bfs=new Queue<Node>();

    bfs.Enqueue(node);
    while(bfs.Count>0)
    {
        node=bfs.Dequeue();
        allNodes.Push(node);
        
        // Process children
        if(node.leftChild!=null) bfs.Enqueue(node.leftChild);
        if(node.rightChild!=null) bfs.Enqueue(node.rightChild);
    }

    if(allNodes.Count>0)
    {
	Node temp=allNodes.Pop();
	while(allNodes.Count>0)
	{
		node=allNodes.Pop();
		node.nextRight=temp;
		temp=node;
	}
    }
}

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

Very neatly written. Nice work

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

agreed with the previous comment

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

Consider this Binary Tree;

1
              2                         3
        4          5             6          7

We can observe that for a given r, r.left, and r.right. then a) r.left.nextR = r.right.left and b) r.right.nextR = r.nextR.left. So if we traverse the tree in preOrder we can effectively propagate the nextR, hence;

void propagate(Node r){ //begin with r = root
           if(r.left != null){
                if(r.right != null)  r.left.nextR = r.right.left;
               propagate(r.left);
           }
           if(r.right != null) {
                if(r.nextR != null) r.right.nextR = r.nextR.left;
                propagate(r.right);
           }
   }//end

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

void SetNextRightNode(node* root)
{
if(root)
{
node* dummy = new node(100000);
if(!dummy)
return;

queue<node*,100> q;
q.enqueue(root);
q.enqueue(dummy);

while(q.isEmpty() == false)
{
node* cur = NULL;
node* prev = NULL;
while((cur = q.dequeue()) != dummy)
{
if(cur->left)
q.enqueue(cur->left);
if(cur->right)
q.enqueue(cur->right);

if(prev)
{
prev->nextRight = cur;
}
prev = cur;
}
q.enqueue(dummy);
}
delete dummy;
}
}

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

void SetNextRightNode(node* root)
{
if(root)
{
node* dummy = new node(100000);
if(!dummy)
return;

queue<node*,100> q;
q.enqueue(root);
q.enqueue(dummy);

while(q.isEmpty() == false)
{
node* cur = NULL;
node* prev = NULL;
while((cur = q.dequeue()) != dummy)
{
if(cur->left)
q.enqueue(cur->left);
if(cur->right)
q.enqueue(cur->right);

if(prev)
{
prev->nextRight = cur;
}
prev = cur;
}
q.enqueue(dummy);
}
delete dummy;
}
}

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

We can do this with a single depth first traversal and O(log(n)) space. Important thing is to traverse the right subtrees before the left subtrees. For each level of the tree, we keep a pointer to the last visited node in that level of the tree. In the helper function, we just set the rightChild of the current node to the corresponding element in the array and update the element in the array to be the current node so that next time we are visiting a node in that level, we set the rightChild of that node to the correct Node.

void populate(Node* n) {
    vector<Node*> R;
    populateHelper(n, R, 0);
}
void populateHelper(Node* n, vector<Node*> & R, int h) {
    if ( n == NULL ) return; // obvious
    // if we are at this level for the first time, initialize the
    // corresponding element of R with NULL
    if ( h == R.size() ) R.push_back(NULL); 
    n->rightChild = R[h];  // get last visited Node in that level from R
    R[h++] = n;            // update R and increment h
    populateHelper(n->rightChild, R, h); // traverse right subtree first
    populateHelper(n->leftChild, R, h);
}

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

void SetSibling(treenode* root)
{
if(root == NULL)
return;
else
{
// Set the siblings
treenode* candidate = NULL;
if(root->left == NULL)
{
if(root->right == NULL)
candidate = NULL;
else
candidate = root->right;
}
else
{
if(root->right == NULL)
candidate = root->left;
else
{
root->left->sibling = root->right;
candidate = root->right;
}
}
treenode* ptr = root->sibling;
treenode* result = NULL;
if(candidate)
{
while(ptr!=NULL)
{
if(ptr->left != NULL)
{
result = ptr->left;
break;
}
else if(ptr->right != NULL)
{
result = ptr->right;
break;
}
ptr = ptr->sibling;
}
candidate->sibling = result;
}
SetSibling(root->right);
SetSibling(root->left);
}
}

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

@ashish can u plzz explain what u r doing

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

i am not getting what you want ,please give an example

- uddeshyakumar.kumar February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void connect(struct node *p)
   {
      if(p == NULL)
         return;
   
      if(p -> left)
         p -> left -> nextright = p -> right;
   
      if(p -> right)
         p -> right -> nextright = (p -> nextright) ? p -> nextright -> left : NULL;
   
      connect(p -> left);
      connect(p -> right);
   }

- Anonymous June 04, 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