## Flipkart Interview Question for Software Engineer / Developers

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

void Tree::getAllNodes(TreeNode *node, TreeNode *prev, int dist, int k){
if(dist == k){
kdistnodes.push_back(node->val);
return;
}
if(node->left != NULL && node->left != prev){
getAllNodes(node->left, node, dist+1, k);
}
if(node->right != NULL && node->right != prev){
getAllNodes(node->right, node, dist+1, k);
}
if(node->parent != NULL && node->parent != prev){
getAllNodes(node->parent, node, dist+1, k);
}

}

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

you should separate the two cases, i.e., searching downwards and searching upwards.
I think your current implementation will output all nodes at distances k, k-2, k-4...

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

If the parent pointer is given, then we can simply think of the tree as a graph and do a BFS till level - k.

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

None of these solution handle the problem where parent pointer is not given and you're supposed to handle the above nodes as well.

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

``````void create_stack(Tree *root, Tree *node, stack *st, int *done)
{
if (root == NULL)  return;
if (root == node) {
st.push(root);
done = 1;
return;
}
st.push(root)
crete_stack(root->left, node, st, done)
if(done)    return;
crete_stack(root->right, node, st, done)
if(done)    return;
st.pop(root)
}

void __findKnode(Tree *node, int k, List * ans)
{
if (node == NULL) return;
if( k == 0) {
append(ans, node);
return;
}

__findKnode(node->left, k-1,ans);
__findKnode(node->right, k-1,ans);

}
void findKnode(Tree *root, Tree *node, int k)
{
List *ans = NULL;
Tree * prev = NULL;
int dist = k
int done = 0
Stack *st;
create_Stack(root, node, st, &done)

while(top = st.pop()){
if (dist < 0)
break;
if(dist == 0){
append(ans, top);
break;
}
if(prev == NULL){
__findKnode(top, dist, ans);
} else if( top->left == prev)
__findKnode(top->right, dist-1, ans);
else
__findKnode(top->left, dist-1, ans);
dist -= 1
prev = top
}
return ans;
}``````

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

Sorry for the long and clumsy code :) did other problems too in the original code.
Cut and pasted relevant parts in ideone.com/4On0fK for easy demo.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

value = node.data;
min = value-k;
max = value+k;

Now do in/pre/post/leve order traversal of the BT and check if each node.value is in the [min,max] range.

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

Comment hidden because of low score. Click to expand.
-1
of 1 vote

void kdistancepath(node *node, int k)
{
if(node==NULL)
return;
if(k==0)
{
printf("%d ",node->a);
return;
}
kdistancepath(node->left,k-1);
kdistancepath(node->right,k-1);

}

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

What about the node above the given node in the tree??

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.

### 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.