Flipkart Interview Question for Software Engineer / Developers






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

PrintPaths(node root, string curPath)
{
if(root == null)
print curPath;

PrintPaths(root.left, curPath + root.data.tostring());
PrintPaths(root.right, curPath + root.data.tostring());


}

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

won't work ... if root.left is null and root.right is not null ... then upon seeing root.left = null...it will print ..

- Anonymous April 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its recursive call. You haven't understood the code

- NEO September 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You will print each path twice. When you reach a leaf, you will print the path twice - once for each of its null child

- Vikas November 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Instead of

if(root == null)

it should be

if(root == null)
{
      return;
}

if(root != null && root->left == null && root->right == null)
{
print curPath;
return;
}
PrintPaths(root.left, curPath + root.data.tostring());
PrintPaths(root.right, curPath + root.data.tostring());

- naag December 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void PrintPath(node* root)
{
	int array[100];
	int depth=0;
	PathRootToLeave(root,array,&depth);
}

void PathRootToLeave(node* root, int* pArray, int* depth)
{
	if(root)
	{
		pArray[(*depth)++]=*((int*)root->pData);
		if(root->lChild)
		{
			PathRootToLeave (root->lChild,pArray,depth);
			(*depth)--;
		}
		if(root->rChild)
		{
			PathRootToLeave (root->rChild,pArray,depth);
			(*depth)--;
		}
		if((!(root->lChild))&&(!(root->rChild)))
		{
			int i = 0;
			for(i=0;i<(*depth);i++)
			{
				printf("[%d]", pArray[i]);
			}
			printf("\n");
		}
	}
}

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

printpaths(node root, string path)
{
if(node == NUll)
return;

if(node.left == NULL and node.right == NULL )
print path.
else
path = path + node.data;
printpaths(node.left,path);
printpaths(node.right,path);


}

- Anonymous April 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Although your solution is neater, I believe it will run in O(n squared) as you are passing a string each time and it is being copied. String length is linear in size of the input. Mind you that even in a language that allows pass by reference this won't work since you do need multiple copies for this to work correctly, so pass by value and copying is your only choice.

- Barcod April 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The order would be O(n). U won't copy it more than n times for n nodes in a tree.

- NEO September 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void PrintPaths(Node <T> * n, std::list<Node<T> *> & items)
	{
		items.push_back(n);
		if ( n ->lchild == NULL && n->rchild == NULL)
			Print(items);
		else
		{			
			if (n ->lchild != NULL)
				PrintPaths(n->lchild, items);
			if (n ->rchild != NULL)
				PrintPaths(n->rchild, items);		
		}
		items.pop_back();
	}

	void DisplayFromRootToLeaf()
	{
		std::list<Node <T> *> items ;
		PrintPaths(root, items);
	}

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

void printpath(struct node* root, int* path, int length)
{
if (root == NULL)
{printarray(path, length);
return;
}
if (root->left == NULL and root->right==NULL)
{
printarray(path,length);
return;
}
path[length] = root->value;
printpath(root->left, path, length+1);
printpath(root->right, path, length+1);
return;
}

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

<pre lang="" line="1" title="CodeMonkey95734" class="run-this">import java.util.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Node root = new Node(10);
root.left = new Node(8);
root.right = new Node(13);
root.left.left = new Node(4);
root.left.right = new Node(7);
root.left.left.left = new Node(2);
root.right.left = new Node(12);
root.right.right = new Node(18);
/*
10
/ \
8 13
/ \ / \
4 7 12 18
/
2
*/
printAllPaths(root,new LinkedList<Node>());
}

public static void printAllPaths(Node nd,LinkedList<Node> list){
if(nd == null){
return;
}
list.add(nd);
if(nd.left==null && nd.right==null){
System.out.println(list);
return;
}
if(nd.left!=null){
printAllPaths(nd.left,list);
list.removeLast();
}
if(nd.right!=null){
printAllPaths(nd.right,list);
list.removeLast();
}
}
}

class Node{
int data;
Node left;
Node right;

public Node(int data){
this.data = data;
}

public String toString(){
return data+"";
}
}

</pre>

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

Why dont we use stack instead of Linked List/List/String to store the path?

- Arne March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Something like this:
void BinaryTree::findPath(node* root, stack<node*> &stackPath)
{
if(root)
{
if(root->left == NULL && root->right==NULL)
{
printPath(stackPath);
}
else
{
stackPath.push(root->data);
findPath(root->left,stackPath);
findPath(root->right,stackPath);
stackPath.pop();
}
}
}

- Arne March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printpath(struct node* root, int* path, int length)
{
if (root == NULL)
{printarray(path, length);
return;
}
if (root->left == NULL and root->right==NULL)
{
printarray(path,length);
return;
}
path[length] = root->value;
printpath(root->left, path, length+1);
printpath(root->right, path, length+1);
return;
}

- Anonymous July 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct node
{
nw= (struct node *)malloc(sizeof(struct node));
}

- Anonymous July 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printTree(Node root)
	{
		if (root == null)
		{
			return;
		}
		
		out1.append(root.getData());
		
		if (root.getLeft() == null && root.getRight() == null)
		{
			System.out.println(out1);
		}
		
		if (root.getLeft() != null)
		{
			printTree(root.getLeft());
			out1.setLength(out1.length() - 1);
		}
		if (root.getRight() != null)
		{
			printTree(root.getRight());
			out1.setLength(out1.length() - 1);
		}
	}

- prabodh.prakash March 13, 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