Gluster Interview Question for Software Engineer / Developers






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

Seems like a DFS.

- Mike February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

That is not spiral..

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

Pseudo code:

Visit(node){
if (node== null) {return ;}
visit a node
if( node.left !=null || node.right !=null){
push its right child and then left child in a queue
}
}

serialize(node)
{
  if(node=q.pop()){
    s.push(node);
    Visit(node);
    serialize(node);
    
  }else{
     return;
  }
}

main()
{
  Queue q = new Queue();
 serialize(root);
 printStack();
}

- ninjaCoder February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done using a BFS pattern. The tree levels can be printed from bottom to top (root). Direction of printing needs to be adjusted (forward/backward). Here is a C# implementation

class Node<T>
{
public T data;
public Node<T> left, right;
}

static void PrintReverseSpiral<T>(Node<T> root)
{
List<Node<T>> current = new List<Node<T>>();
List<Node<T>> next = new List<Node<T>>();
List<List<Node<T>>> levels = new List<List<Node<T>>>();

if (root != default(Node<T>))
    current.Add(root);

// build a list of all the levels
while (current.Count > 0)
{
    foreach (Node<T> n in current)
    {
        if (n.left != null) next.Add(n.left);
        if (n.right != null) next.Add(n.right);
    }

    levels.Add(new List<Node<T>>(current));
    current = next;
    next = new List<Node<T>>();
}

// print in spiral order
for (int i = levels.Count - 1; i >= 0; i--)
{
    if (i % 2 == 0)
    {
        // even levels printed forward
        for (int j = 0; j < levels[i].Count; j++)
            Console.Write("{0}, ", levels[i][j].data);
    }
    else
    {
        // odd levels printed backward
        for (int j = levels[i].Count - 1; j >= 0 ; j--)
            Console.Write("{0}, ", levels[i][j].data);
    }
}

Console.WriteLine();
}

- saifullah.baig March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// C++ implementation of a O(n) time method for spiral order traversal
#include <iostream>
#include <stack>
using namespace std;
 
// Binary Tree node
struct node
{
    int data;
    struct node *left, *right;
};

// A utility functiont to create a new node
struct node* newNode(int data)
{
    struct node* node = new struct node;
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return(node);
}
 
void printSpiral(struct node *root)
{
    if (root == NULL)  return;   // NULL check
	struct node *temp;
    // Create two stacks to store alternate levels
	// Here stack stores tree node pointers
    stack<struct node*> s1;  // For levels to be printed from right to left
    stack<struct node*> s2;  // For levels to be printed from left to right
	stack<struct node*> s3;  // To store the end result, and print it in reverse order 
    // Push first level to first stack 's1'
    s1.push(root);
 
    // Keep ptinting while any of the stacks has some nodes
    while (!s1.empty() || !s2.empty())
    {
        // Print nodes of current level from s1 and push nodes of
        // next level to s2
        while (!s1.empty())
        {
            temp = s1.top(); 
            s1.pop();
            //cout << temp->data << " ";
			s3.push(temp);
            // Note that is right is pushed before left
            if (temp->right)
                s2.push(temp->right);
            if (temp->left)
                s2.push(temp->left);
        }
 
        // Print nodes of current level from s2 and push nodes of
        // next level to s1
        while (!s2.empty())
        {
            temp = s2.top();
            s2.pop();
            //cout << temp->data << " ";
			s3.push(temp);
            // Note that is left is pushed before right
            if (temp->left)
                s1.push(temp->left);
            if (temp->right)
                s1.push(temp->right);
        }
    }
	// Print tree elements in reverse order
	while(!s3.empty())
	{	
		temp = s3.top(); // retrun top element
		s3.pop(); // pop doesn't return anyhting , just removes top element.   
		cout<<temp->data<<" ";
	}
}
 
 
int main()
{
    struct node *root = newNode(1);
    root->left        = newNode(2);
    root->right       = newNode(3);
    root->left->left  = newNode(7);
    root->left->right = newNode(6);
    root->right->left  = newNode(5);
    root->right->right = newNode(4);
    cout << "Spiral Order traversal of binary tree is \n";
    printSpiral(root);
 
    return 0;
}

- siva.sai.2020 March 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java code for reverse spiral order traversal :
Node is as following

public class Node {

	private int key;
	private Node left;
	private Node right;

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

	public int getKey() {
		return key;
	}

	public void setKey(int key) {
		this.key = key;
	}

	public Node getLeft() {
		return left;
	}

	public void setLeft(Node left) {
		this.left = left;
	}

	public Node getRight() {
		return right;
	}

	public void setRight(Node right) {
		this.right = right;
	}

	@Override
	public String toString() {
		return "Node [key=" + key + ", left=" + left + ", right=" + right + "]";
	}

}

reverseSpiralLevelOrder is given below :

public static void reverseSpiralLevelOrder(Node root) {
		boolean isLeftToRightTraversal = true;
		int height = height(root);
		for (int i = height; i >= 1; i--) {
			spiralLevelOrder(root, i, isLeftToRightTraversal);
			isLeftToRightTraversal = !isLeftToRightTraversal;
		}
	}

	private static void spiralLevelOrder(Node root, int level, boolean isLeftToRightTraversal) {
		if (root == null) {
			return;
		}
		if (level == 1) {
			System.out.println(root.getKey() + " ");
		} else {
			if (isLeftToRightTraversal) {
				spiralLevelOrder(root.getLeft(), level - 1, isLeftToRightTraversal);
				spiralLevelOrder(root.getRight(), level - 1, isLeftToRightTraversal);
			} else {
				spiralLevelOrder(root.getRight(), level - 1, isLeftToRightTraversal);
				spiralLevelOrder(root.getLeft(), level - 1, isLeftToRightTraversal);
			}
		}

}

- Deepak October 19, 2018 | 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