Flipkart Interview Question for Software Engineer / Developers






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

do the same as breadth first traversal using Queue while keeping track of levels. While popping from Queue,whenever the level changes, toggle between directly popping from Queue and inserting the popped element into a stack. When the level changes again, empty the stack.

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

I am thinking in the same lines.
Rather than adding it to a stack, why not try this..
We are keeping track of the levels anyways. So lets use it.

if(height%2==1)
{
push right child
push left child
}
else if(height%2==0)
{
push left child
push right child
}


This avoids the usage of the stack. please let me know what u guys think about my solution.

- Anonymous November 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude when you push (right,left) into the queue.Right will be at the front whereas for the next enqueue you need the left element first.

- Geek July 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use two stacks. When popping a node from stack 1, expand it from right child to left child, and the children should be pushed into stack 2; For stack 2, its the other way around. As init, A can be pushed in stack 1. Print the children when expending.

- careercuper October 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do Breadthfirstsearch and put the values in the queue by right child first and then the left child. (Meaning adding children from right to left)

- kannanvijay November 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The code is similar to the one in which a line has to be inserted doing bfs. Thinking on the same lines u require an additional stack and a variable level to keep track of the level of the tree..

void printNodes (Node *temp)
{
    Queue q;
    Stack s;
    int level = 0, current_level, next_level, next_level_temp;

    if (temp != 0)
    {
        q.enqueue (temp);
        level = 1;
        current_level = 1;
        next_level = 0;
        while (!q.isEmpty ())
        {
            if (current_level == 0)
            {
                if (level % 2 == 0)         //print the even levels backwards
                {
                    for (int i = 0; i < next_level_temp; i++)
                    {
                        temp = s.pop ();
                        cout<< temp->data;
                    }
                }
                if (level % 2)           
                  next_level_temp = next_level;  //No of nodes to print in next level
                
                current_level = next_level;
                next_level = 0;
                cout<<" ";
                level++;
            }
            temp = q.dequeue ();
            if (temp->left)
            {
                q.enqueue (temp->left);
                next_level++;
            }
            if (temp->right)
            {
                q.enqueue (temp->right);
                next_level++;
            }
            if (level % 2)
                cout<<temp->data;    //print if the level is odd
            else
                s.push (temp);       //push on the stack if the level is even
            current_level--;
        }
    }
}

- Siraj December 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using same algorithm we do for Level Order traversal using queue . here we will use an extra stack that get be filled in even case and then pop out so then we can get it printed backwards

#include<iostream>
#include<queue>
#include<stack>
using namespace std;

typedef struct nn {
    int data;
    struct nn *left;
    struct nn *right;    
}node;

node *insert(node *start, int data) {
     if(start == NULL) {
         node * p = new node;     
         p->data = data;
         p->left = NULL;
         p->right = NULL;
         start = p;     
     }
     else if(start->data > data)
         start->left = insert(start->left,data);
     else
         start->right = insert(start->right,data);
    return start;         
}

void printInOrder(node *start) {
     if(start) {
         printInOrder(start->left);
         cout << start->data << " ";
         printInOrder(start->right);      
     }
}

void printLevelOrderSpiralIterative(node *start) {
     queue<node*> levelOrder;
     stack<int> levelStack;
     levelOrder.push(start);
     levelOrder.push(NULL);
     int nullCount = 1;
     while(levelOrder.size() > 0) {
           node *start = levelOrder.front();
           levelOrder.pop();
           if(start != NULL) {
              if(nullCount % 2) cout << start->data << " ";
              else 
                 levelStack.push(start->data); 
           }
           else {
              if(levelOrder.front() != NULL) {
                 levelOrder.push(NULL);                        
                 nullCount++;
                 if(nullCount % 2) {
                      while(levelStack.size()) {
                           cout << levelStack.top()<< " ";
                           levelStack.pop();                 
                      }        
                 } 
                 cout << endl;
              }
              continue;
           }
           if(start->left) levelOrder.push(start->left); 
           if(start->right) levelOrder.push(start->right);
                
     } 
}

int main() {
    int n;
    cin >> n;
    node *start = NULL;
    while(n != -1) {
        start = insert(start,n);
        cin >> n;    
    }
    printLevelOrderSpiralIterative(start);
    system("pause");
}

- Manish Agarwal January 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You have to maintain a QUEQUE and STACK.
Starting from the ROOT node put it into QUEQUE and now delete element from the QUEQUE ,print them and put there neighbours into STACK till QUEQUE is empty. Again starting
from STACK take element from it and print them and put their neighbours into QUEQUE.
till STACK is empty.

DO it till we have both the QUEUE And STACK is empty

- kamal pushpad May 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

head = end =0;
h = height(root);
for i = 1 to h
CreateList(root, i, ((i%2)==0));

void CreateList(tree, d, front)
{
if (!tree) return;
if (d==1) {
AddToList(tree, front);
} else if (d>1) {
CreateList(root->left, d-1, front);
CreateList(root->right, d-1, front);
}
}
void AddToList(n, front) {
if (!head) head = n;
if (!end) {
end = n;
return;
}
if (front) {
n->next = head;
head = n;
} else {
end->next = n;
end = n;
}
}

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

head = end =0;
h = height(root);
for i = 1 to h
  CreateList(root, i, ((i%2)==0));

void CreateList(tree, d, front)
{
  if (!tree) return;
  if (d==1) {
    AddToList(tree, front);
  } else if (d>1) {
    CreateList(root->left, d-1, front);
    CreateList(root->right, d-1, front);
  }
}
void AddToList(n, front) {
  if (!head) head = n;
  if (!end) {
    end = n;
    return;
  }
  if (front) {
    n->next = head;
    head = n;
  } else {
    end->next = n;
    end = n;
  }
}

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

above was incorrect. correct one is:

head = 0;
h = height(root);
for i = 1 to h
  CreateList(root, i, ((i%2)==0));

void CreateList(tree, d, even)
{
 if (!tree) return;
 if (d==1) {
  AddToList(tree, front);
 } else if (d>1) {
  if (even) {
   CreateList(root->left, d-1, even);
   CreateList(root->right, d-1, even);
  } else {
   CreateList(root->right, d-1, even);
   CreateList(root->left, d-1, even);
  }
 }
}
void AddToList(n) {
  if (!head) head = nextNode = n;
  else {
   NODE* nn = new NODE(n->data); 
   nextNode->next = nn;
   nextNode = nn;
  }   
}

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

Please check the pseudo code and comment
while(nxtelement)
if(level==even)
{
if(first_instance)
{
push all elements from stack to Q;//using stackpointer
stackpointer=quepointer+1;
}
push in stack;//left & right
}
push in queue;//left & right

nxtelement=Deque;
}

- Ankur May 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class ZigZagTraversal {

	class BinaryTree {
		public Character value;
		public BinaryTree left = null, right = null;

		public BinaryTree(Character value) {
			this.value = value;
		}

	}

	public void doZigZagTraversal(BinaryTree root) {
		if (root == null)
			return;
		Boolean pushInReverseOrder = false;
		Stack<BinaryTree> currentLevelVisitStore = new Stack<>();
		Stack<BinaryTree> nextLevelVisitStore = new Stack<>();
		currentLevelVisitStore.push(root);
		while (!currentLevelVisitStore.isEmpty()) {
			BinaryTree node = currentLevelVisitStore.pop();
			if (node != null) {
				System.out.print(node.value + " ");
				if (pushInReverseOrder) {
					if (node.right != null)
						nextLevelVisitStore.push(node.right);
					if (node.left != null)
						nextLevelVisitStore.push(node.left);
				} else {
					if (node.left != null)
						nextLevelVisitStore.push(node.left);
					if (node.right != null)
						nextLevelVisitStore.push(node.right);
				}
			}
			if (currentLevelVisitStore.isEmpty()) {
				Stack<BinaryTree> levelChangeSwapPointer = currentLevelVisitStore;
				currentLevelVisitStore = nextLevelVisitStore;
				nextLevelVisitStore = levelChangeSwapPointer;
				pushInReverseOrder = !pushInReverseOrder;
				System.out.print(" "); // just to highlight the change in order of printing. 
			}
		}
	}

	public static void main(String args[]) {
		ZigZagTraversal zzt = new ZigZagTraversal();
		BinaryTree bt = zzt.new BinaryTree('A');

		bt.left = zzt.new BinaryTree('B');
		bt.right = zzt.new BinaryTree('C');

		bt.left.left = zzt.new BinaryTree('D');
		bt.left.right = zzt.new BinaryTree('E');

		bt.right.left = zzt.new BinaryTree('F');
		bt.right.right = zzt.new BinaryTree('G');

		bt.left.left.left = zzt.new BinaryTree('H');
		bt.left.left.right = zzt.new BinaryTree('I');

		bt.left.right.left = zzt.new BinaryTree('J');

		zzt.doZigZagTraversal(bt);
	}

}

- CyberJockey June 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above code was having some compilation error,removed that

import java.util.Stack;

class BinaryTree {
public Character value;
public BinaryTree left = null, right = null;

public BinaryTree(Character value) {
this.value = value;
}

}

public class ZigZagTra1 {

public static void doZigZagTraversal(BinaryTree root) {
if (root == null)
return;
Boolean pushInReverseOrder = false;
Stack<BinaryTree> currentLevelVisitStore = new Stack<BinaryTree>();
Stack<BinaryTree> nextLevelVisitStore = new Stack<BinaryTree>();
currentLevelVisitStore.push(root);
while (!currentLevelVisitStore.isEmpty()) {
BinaryTree node = currentLevelVisitStore.pop();
if (node != null) {
System.out.print(node.value + " ");
if (pushInReverseOrder) {
if (node.right != null)
nextLevelVisitStore.push(node.right);
if (node.left != null)
nextLevelVisitStore.push(node.left);
} else {
if (node.left != null)
nextLevelVisitStore.push(node.left);
if (node.right != null)
nextLevelVisitStore.push(node.right);
}
}
if (currentLevelVisitStore.isEmpty()) {
Stack<BinaryTree> levelChangeSwapPointer = currentLevelVisitStore;
currentLevelVisitStore = nextLevelVisitStore;
nextLevelVisitStore = levelChangeSwapPointer;
pushInReverseOrder = !pushInReverseOrder;
System.out.print(" "); // just to highlight the change in order
// of printing.
}
}
}

public static void main(String args[]) {
ZigZagTraversal zzt = new ZigZagTraversal();
BinaryTree bt = new BinaryTree('A');

bt.left = new BinaryTree('B');
bt.right = new BinaryTree('C');

bt.left.left = new BinaryTree('D');
bt.left.right = new BinaryTree('E');

bt.right.left = new BinaryTree('F');
bt.right.right = new BinaryTree('G');

bt.left.left.left = new BinaryTree('H');
bt.left.left.right = new BinaryTree('I');

bt.left.right.left = new BinaryTree('J');

doZigZagTraversal(bt);
}

}

- justCoder October 17, 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