Flipkart Interview Question for Software Engineer / Developers






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

Do inorder traversal and compare if the successor is no less than the predecessor. O(n).

- Anonymous June 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int check_binarytree(tree *t)
{
if(!t)
return true;
if(t->left&&(maxvalue(t->left)>t->element))
return  false;
if(t->right&&(minvalue(t->right)<=t->element))
return  false;
if(!check_binarytree(t->left)||!check_binarytree(t->right))
return false;
return true;
}

comments on this are welcome

- sukumar June 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Complexity?

- Loony June 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since we don't know that it's a binary tree, it's presumed that its children nodes are stored in some enumerable structure. As long as no more than 2 children are non-null, and that the first non-null child has value less than and second non-null child has value greater than, then it's a binary tree.

public bool isBinaryTree(Node n, bool isLeftChild, Node Parent)
{
   int count=0;
   int[] childIndexes = new int{-1,-1};

   for (int s=0; s < n.Children.Count; s++)
   {
       if ( n.Children[0] != null )
       {   
           if (count==2) return false;
           childIndexes[count++] = s;
       }
   }

   if (parent != null && isLeftChild)
   {
       if (n.Value > parent.Value)
           return false;

 
   }
   else if (parent != null && !isLeftChild)
   {
        if (n.Value < Parent.Value) return false;
   }
   bool isLeft = true;
   for (int r=0; r< count; r++)
    {
        if (! isBinaryTree(n.Children[childIndexes[r]], isLeft, n) ) return false;
        isLeft = false;
    }
    return true;
}

- SomeGuy July 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if ( n.Children[0] != null )
should be
if ( n.Children[s] != null )

- SomeGuy July 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Node{
int data;
Node[] children ;
}

public void isBST(Node root){
if(root == null){
return true;
}
int numChild = 0;
Node left = null;
Node right = null;
for(int i =0;i<root.children.size();i++){
if(root.children[i] != null){
numChild++;
if(left == null){
left = root.children[i];
}else{
right = root.children[i];
}
}
}
if(numChild > 2){
return false;
}
boolean leftOk = false;
boolean rightOk = false;
if(left == null || (left.data <= root.data && isBST(left))){
leftOk = true;
}
if(right == null || (right.data > root.data && isBST(right)){
rightOk = true;
}
return leftOk && rightOk ;
}

- iamnotiam April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Node{
int data;
Node[] children ;
}

public void isBST(Node root){
if(root == null){
return true;
}
int numChild = 0;
Node left = null;
Node right = null;
for(int i =0;i<root.children.size();i++){
if(root.children[i] != null){
numChild++;
if(left == null){
left = root.children[i];
}else{
right = root.children[i];
}
} 
}
if(numChild > 2){
return false;
}
boolean leftOk = false;
boolean rightOk = false;
if(left == null || (left.data <= root.data && isBST(left))){
leftOk = true;
}
if(right == null || (right.data > root.data && isBST(right)){
rightOk = true;
}	
return leftOk && rightOk ;
}

- iamnotiam April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

You should also make sure that each node has no more than two children

- Anonymous June 29, 2010 | 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