NetApp Interview Question


Country: India
Interview Type: In-Person




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

Here is my understanding of the question:
Given a binary tree - where each node has three pointers namely parent, left Child, right Child. Find the 'right' (as in right direction) sibling of any given node.

Answer:
0.1) Assumption: Assume that the actual node pointer is given
1) Integer variable: 'hop count' initialized to zero
2) Walk-up the tree using parent pointer (keep track of the actual node - say prev)
2.1) Increment hop count
2.2) If 'prev' is node's left child and node's right is not null, stop walk-up and quit the loop
2.3) continue the walk-up
3) Initialize tempHops = hopCount - 1, subTree = node's right
4) Do depth-first traversal on sub-Tree (i.e. prefer left child over right child if exists)
4.1) For every walk down, decrement tempHops
4.2) For every walk (back up), increment tempHops
4.2) When tempHops is 'zero', that node is right sibling
5) If we exhausted the subtree and did not hit tempHops zero
5.1) It means the right sub-tree is not of same height at this node level
5.2) Repeat step (2)-(4) with continuation of hopCount (without resetting it to zero)
5.3) If we reached the root node and never could find tempHops zero, it means that there is no right sibling at the level of a given node

Thanks,
Laxmi

- Laxmi Narsimha Rao Oruganti December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 vote

1. if node->root->left = node then node->root->right is answer
2. if node->root->right = node then node->root->root->right->left is answer

if both condition are false (NULL) then node is not having any right sibling

- Bhagat Singh December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think this is right solution

- prafullchauhan03 September 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

false if it is not complete tree

- rajat December 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

First find the root node using the parent pointer. Then do BFS level by level. After the BFS encounters the node we are interested in, start printing the rest of nodes in the same level. Stop traversing after the current level is done.

- freshboy December 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

You can have something like this:   
         O
     /      \
   O          O
  /  \      /   \
X     P1  P2    P3

X is the current node. We are interested to find the right node for X.
P1 , P2 , P3 and null are the possibilities.
so I think you should keep in mind the level on which you are interested to find the "right" node. Use another variable to keep track of the current level. Go test each of the three possibilities and if non of them is !null then return null which means that you have no "right" node.

- Kamy December 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not understanding the question, can you please provide an example?

- Andi December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Run BFS and Store the value with its level in a hashmap.

Read the value which is right next to the given value.

- hprem991 December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But this requires a 'Queue' (memory). In my version, no extra memory required.

- Laxmi Narsimha Rao Oruganti December 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

1. if root->left = node then root->right is answer
2. if root->right = node then root->root->left is answer

if both condition is false then node is not having any right sibling

- Bhagat Singh December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is the code i wrote based on Laxmi's assumption.

public Node func(Node n)
{
Node p= n;
    if(p.getParent().getRight() != null)
    {
        p = p.getParent().getRight();
        return p;
    }
    else
    {
        while(p.getParent().getRight() == null || p.getParent() == null)
        {
            p = p.getParent();
            x++;
        }
       if(p.getParent() != null)
       {
        Node y = p.getParent().getRight();
        while( x!=0)
        {
            if(y.getLeft() != null)
            {
                y = y.getLeft();
                x--;
            }
            else
            {
            y = y.getRight();
            x--;
            }
        }
      return y;
     }
     }
    else
     {
        print("no sibling");
     }
}

- Anonymous April 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

1. if node->root->left = node then node->root->right is answer
2. if node->root->right = node then

if(node->root->root->right->left != node->root->left )
then node->root->root->right->left is answer

if above condition are false (NULL) then node is not having any right sibling

- Putta June 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