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);
}

}

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

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

- Anonymous September 04, 2010 | Flag
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.

- kislay.nsit March 01, 2013 | Flag Reply
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.

- Anonymous March 13, 2013 | Flag Reply
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;
}

- jigs July 14, 2013 | Flag Reply
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.

- Anonymous December 17, 2013 | Flag Reply
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.

- bawet October 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

try to read question properly and test your answer before answering

- anurag.gupta108 December 20, 2012 | Flag
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);

}

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

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

- anurag.gupta108 December 20, 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