Amazon Interview Question for SDE1s


Team: Kindle
Country: India
Interview Type: In-Person




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

Here is a possible solution :
{{
void populateInOrderSuccessor() {
Stack<Node> stack = new Stack<>();
stack.push(root);
Node current = root.left;
Node prev = null;
boolean finish = false;
while (!finish) {
if (current != null) {
stack.push(current);
current = current.left;
}
else {
if(!stack.isEmpty()) {
Node top = stack.pop();
if (prev != null){
prev.next = top;
}
prev = top;
if(top.right!=null) {
current = top.right;
}
}
else
finish = true;
}
}
}
}
}}

- EPavlova April 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a quick and dirty solution with a global variable. This is a recursive approach by modifying the inorder traversal algorithm.

Node *current = NULL;

void fillSuccessors(Node *root)
{
        if(root == NULL)
        {
                return;
        }
        else
        {
                fillSuccessors(root->left);
                if(!current)
                {
                        current = root;
                }
                }
                else
                {
                        current->next = root;
                        current = root;
                }
                fillSuccessors(root->right);
        }
}

- sarath April 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a quick and dirty solution with a global variable. This is a recursive approach by modifying the inorder traversal algorithm.

Node *current = NULL;

void fillSuccessors(Node *root)
{
        if(root == NULL)
        {
                return;
        }
        else
        {
                fillSuccessors(root->left);
                if(!current)
                {
                        current = root;
                }
                }
                else
                {
                        current->next = root;
                        current = root;
                }
                fillSuccessors(root->right);
        }
}

- sarath April 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think we would need to traverse the tree in reverse inorder.

- rams May 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assumption is that BST already contains val

public Integer inOrderSuccessor(Integer val) {
    return inOrderSuccessor(root, val);
  }

  public Integer inOrderSuccessor(Node n, Integer val) {
    if (n == null)
      return null;

    boolean left = false;
    boolean right = false;

    Integer ret = null;

    if (n.val == val) {
      left = true;
      right = true;
    } else if (val < n.val) {
      ret = inOrderSuccessor(n.left, val);
      right = true;
    } else if (val > n.val) {
      ret = inOrderSuccessor(n.right, val);
    }

    if (ret != null)
      return ret;

    if (left && n.left != null)
      return new Integer(n.left.val);

    if (right && n.right != null)
      return new Integer(n.right.val);

    return null;
  }

- CS_Stud April 24, 2015 | 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