Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
5
of 7 vote

/* last is a global/static variable*/

void inOrder(node *root){
    if (root==NULL) return;
    inOrder(root->left);
    if(last!=NULL)last->next = root;
    last = root;
    inOrder(root->right);
}

- Crazy Tesla January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it should be

{
void inOrder(node *root){
    if (root==NULL) return;
    inOrder(root->left);
    if(last!=NULL){
    last->next = root;
   last= last->next;   
}
    last = root;
    inOrder(root->right);
}

}

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

if(last!=NULL)
{
last->next = root;
last= last->next; // this step isn't necessary. the same job is done by next step
}
last = root;

- Anonymous March 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node *end=NULL;
Node *beg=NULL;
void Inorder(Node *head)
{
if(head == NULL)
return ;
if(head->left)
Inorder(head->left);

if(beg==NULL)
{
beg=head;
end=head;
}
else
{
end->next=head;
end=head;
}


if(head->right)
Inorder(head->right);
}

- peng January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Plese re write the question agian....Which pointer do we need to change..left or right....or is it just a new linked list we can create....like the one done above...

- A January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that the Tree structure could be changed. We can use a modified version of the Threaded binary tree. Here we are changing the 'right' TreeNode reference of a TreeNode. We pass on the last visited TreeNode through the recursion stack, and return the last visited TreeNode to the previous call of the recursion.

class TreeNode{
    TreeNode left = null;
    TreeNode right = null;
}

    private TreeNode inOrderLLTransf(TreeNode root, TreeNode predec){
        if(root == null){
            return null;
        }
        
        if(root.left!= null){
            predec = inOrderLLTransf(root.left, predec);
        }
        if(predec != null){
            predec.right = root;
        }
        predec = root;
        
        if(root.right != null){
            predec = inOrderLLTransf(root.right,predec);
        }               
        return predec;        
    }
    
    public void transformTree(TreeNode root){
        TreeNode lastVisited = inOrderLLTransf(root.right, null);
        if(lastVisited == null){
            //ERROR
        }
    }

- nik January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for java

class PreNode
{
   TreeNode node = null;
}

public void addNext(TreeNode root, PreNode pre)
{
   if(root == null)
      return;
   addNext(root.left, pre);
   if(pre.node != null)
      pre.node.next = root;
   pre.node = root;
   addNext(root.right, pre)
}

- xin January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the iterative solution using Stack in JAVA
public void flatten(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if(root == null){
return;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
TreeNode previous = null;
while(!stack.empty()){
TreeNode current = stack.pop();
if (current.right!=null) {
stack.push(current.right);
}
if (current.left!=null) {
stack.push(current.left);
}
current.left = null;
current.right = null;

if (current == root) {
previous = current;
}
else{
previous.right = current;
previous = current;
}
}
}

- Rocky February 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Didn't understandthe question completely. Do we need to introduce an additional node to point to the inorder successor or we should change the existing pointers?

- Pradeep March 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* InorderList(node *root)
{
	if(root->right)
	{
		root->right->next = root->next;
		root->next = InorderList(root->right);
	}
	if(root->left)
	{
		root->left->next = root;
		return InorderList(root->left);
	}
	return root;
}

- sid April 22, 2013 | 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