Amazon Interview Question for Software Engineer / Developers


Country: India




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

2 passes:
- Once to replace root when the root > left + right
- else to replace child node

void ReplaceRoot(TreeNode r)
        {
            if (r == null)
                return;
            ReplaceRoot(r.Left);
            ReplaceRoot(r.Right);

            int left = 0;
            int right = 0;
            if (r.Left != null)
                left = r.Left.Data;
            if (r.Right != null)
                right = r.Right.Data;

              if ((r.Data > (left + right)) && ((left > 0) || (right > 0)))
                r.Data = left + right;
        }


       void ReplaceChild(TreeNode r)
        {
            if (r == null)
                return;
            int left = 0;
            int right = 0;
            if (r.Left != null)
                left = r.Left.Data;
            if (r.Right != null)
                right = r.Right.Data;

            if ((r.Data != (left + right)) && ((left > 0) || (right > 0)))
            {
                if (r.Right != null)
                    r.Right.Data = r.Data - left;
                else if (r.Left != null)
                    r.Left.Data = - r.Data;
            }

            ReplaceChild(r.Left);
            ReplaceChild(r.Right);           
        }

       public static void Main()
       {
           ReplaceRoot(r);
           ReplaceChild(r);

       }

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

In the ReplaceChild in else if condition why did u put the negative sign
r.Left.Data = - r.Data;

- pefull February 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Root node is always less then the right child and so the replacing root to sum of the values will cause a B-tree where right child is less than root
But in Case if left child is negative than only this task feasible resulting in a BST

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

How is root node always less than right node. It is a binary tree not a BST.

- cooldaa January 30, 2012 | Flag


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