Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

To down approach. While visiting a node, check if the node is equal to any of the given input numbers. If yes terminate and return the node. If the node value is in between the given input numbers that is the LCA, return the node and terminate. Otherwise

if node value is less than lower number then recur in the right sub tree.
if node value is greater than the higher input then recur in the left sub tree.

Code below,

public static Node lca(Node n, int x, int y){
        if(n==null)
            return null;
        
        if(n.value == x || n.value == y){
            return n;
        }
        
        if(n.value>x && n.value<y)
            return n;
        
        Node lca = null;
        if(n.value>y){
            lca = lca(n.left, x, y);
        }else{
            lca = lca(n.right, x, y);
        }
        return lca;
        
    }

- GeeBee September 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice.

- Anonymous September 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This code fails when y<x in lca(Node n, int x, int y).

Therefore we need to add one more if-block i.e.

if(n.value<x && n.value>y)
return n;

- Fresh_man September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static node find( node *root)
{
if( root == null)
return null;
else
if(root->value > first && root->value<second)
return root;
else
if(root->value <first)
find(root->right);
else
if(root->value>right)
find(root->left);
}

- Anonymous September 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it should be if(root->value>second)

- guddu October 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you write a testing function for this?

- eulerrules2 April 10, 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