Amazon Interview Question for Software Engineer / Developers






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

pseudocode:
void f(treenode *root){
if(root==NULL) return;
queue q;
q.enqueue(root);
treenode *p=NULL
q.enqueue(p);
while( !q.empty()){
p=q.dequeue();
if(p==NULL&& p.front()!=NULL){
q.enqueue(p);
continue;
}
else{
if (p.left!=NULL) q.enqueue(p.left);
if (p.right!=NULL) q.enqueue(p.right);
}
p.next=q.front();
}
}

treenode *pn;

}

}

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

pseudocode:
void f(treenode *root){
if(root==NULL) return;
queue q;
q.enqueue(root);
treenode *p=NULL
q.enqueue(p);
while( !q.empty()){
p=q.dequeue();
if(p==NULL&& p.front()!=NULL){
q.enqueue(p);
continue;
}
else{
if (p.left!=NULL) q.enqueue(p.left);
if (p.right!=NULL) q.enqueue(p.right);
}
p.next=q.front();
}
}

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

could smone explain the logic

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

algorithm:
1. Maintain a queue of node pointers at the same level.
while(q is not empty)
{
2. Iterate through the queue and set the next pointers to point to the next node in the queue. The last node in the queue will point to NULL.
3. Iterate through the queue, dequeue a node and enqueue its left and right children if they exist.
}

- anonymous February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

algorithm:
1. Maintain a queue of node pointers at the same level.
while(q is not empty)
{
2. Iterate through the queue and set the next pointers to point to the next node in the queue. The last node in the queue will point to NULL.
3. Iterate through the queue, dequeue a node and enqueue its left and right children if they exist.
}

Here is the code...
void link(node* root)
{
queue q;
if(root==NULL) return;
root->next=NULL;
if(root->left)
q.enqueue(root->left);
if(root->right)
q.enqueue(root->right);
while(!q.empty())
{
node* prev = NULL;
Iter iter(q);
for(iter.begin(); !iter.end(); iter++)
{

- anonymous February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
void link(node* root) {{{{ queue q; if(root==NULL) return; root->next=NULL; if(root->left) q.enqueue(root->left); if(root->right) q.enqueue(root->right); while(!q.empty()) { node* prev = NULL; Iter iter(q); for(iter.begin(); !iter.end(); iter++) { if(!prev) prev = *iter; else{ prev->next = *iter; prev = prev->next; } } prev->next=NULL; Iter iter(q); for(iter.begin();!iter.end();iter++) { q.dequeue(); if(*iter->left) q.enqueue(*iter->left); if(*iter->right) q.enqueue(*iter->right); - anonymous February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void link(node* root)

{
    queue q;
    if(root==NULL) return;
    root->next=NULL;
    if(root->left)
      q.enqueue(root->left);
    if(root->right)
      q.enqueue(root->right);
    while(!q.empty())
    { 
      node* prev = NULL;
      Iter iter(q);
      for(iter.begin(); !iter.end(); iter++)
      {
        if(!prev)   
           prev = *iter;
        else{
           prev->next = *iter;
           prev = prev->next;
        }
      }
      prev->next=NULL;
      Iter iter(q);
      for(iter.begin();!iter.end();iter++)
      {
        q.dequeue();
        if(*iter->left) 
          q.enqueue(*iter->left);
        if(*iter->right)
          q.enqueue(*iter->right);
      }

}

- anonymous February 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
	 * The idea is to use to use 2 queues.
	 * One queue will hold all Node references at one level while the other one will hold Node references at the next level
	 * Of course, it is less efficient that other programs. But the code looks elegant
	 */
	public void populateNext(Node root){
		Queue<Node> q1 = new LinkedList<Node>();
		Queue<Node> q2 = new LinkedList<Node>();
		q1.add(root);
		while ( !q1.isEmpty()) {
			Node temp = q1.remove();
			temp.next = q1.peek();
			if ( temp.left != null)
				q2.add(temp.left);
			if ( temp.right != null)
				q2.add(temp.right);
			if ( q1.isEmpty()) {
				while (!q2.isEmpty()) {
					q1.add(q2.remove());
				}
			}
		}
	}

- translator February 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a Breadth First Traversal, the BFT algo uses a Queue then just have an element in the Q point to the next one, or better yet use an implementation of Q that uses a linked list under the covers.

- aquila.25 February 21, 2010 | 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