Citigroup Interview Question for Analysts


Country: United States




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

//tree implementation

//Tree Nodes

#include<iostream>
#include<string>

using namespace std;


//tree node
struct TreeNode{
	string data;
	TreeNode *leftNode;
	TreeNode *rightNode;
	TreeNode(){}
	TreeNode(string data){this->data=data;}
};


//binary tree
class BinaryTree{

	TreeNode* rootNode;
	//create binary tree
	void createTree(TreeNode*& rootNode,string data);
	void showTree(TreeNode*& root);

public:
	BinaryTree(){rootNode=NULL;}

	//starting point for creating tree, root is accessed from this func
	void insert(string data)
	{
		createTree(rootNode,data);
	}
	//show tree
	void BinaryTree::show()
	{
		showTree(rootNode);
	}

};

void BinaryTree::createTree(TreeNode*& root,string data)
{
	//check if root is NULL, this is called recursively for left or right side,for every new node
	if(root==NULL)
	{
		root=new TreeNode(data);
		root->leftNode=NULL;
		root->rightNode=NULL;
		return;
	}
	if(root->data[0]<data[0])
	{
		createTree(root->leftNode,data);
	}
	else
	{
		createTree(root->rightNode,data);
	}

}

void BinaryTree::showTree(TreeNode*& root)
{
	//check if root is NULL, this is called recursively for left or right side,for every new node
	if(root==NULL)
	{
		return;
	}
	showTree(root->leftNode);
	cout<<root->data<<endl;
	showTree(root->rightNode);
}




int main()
{
	BinaryTree tree;
	tree.insert("cd");
	tree.insert("b");
	tree.insert("c");
	tree.insert("f");
	tree.insert("z");
	tree.insert("zaas");
	tree.insert("avs");
	tree.show();

	getchar();
	return 0;
}

- Nik March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you elaborate the question.
Binary Tree can be implemented through double linked list with Left and Right pointers. You can have few operations such as Insert, Delete, Find, Compare...

To build tree you can ask for Insert with data.

- howaboutthis March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that question is about binary search tree, because there can't be a fixed strategy to add an element in a normal binary tree.

Basic Java implementation (wwithout delete operation):

public class BST {
private class Node {
private int value;
private Node left;
private Node right;

public Node(int value) {
this.value = value;
this.left = null;
this.right = null;
}
}

private Node root; // root of BST

public void insert(int item) {
root = insert(root, item);
}

private Node insert(Node x, int item) {
if (x == null) {
return new Node(item);
}

if (item < x.value) {
x.left = insert(x.left, item);
} else if (item > x.value) {
x.right = insert(x.right, item);
}
return x;
}

public void inOrder() {
inOrder(root);
System.out.println();
}

private void inOrder(Node x) {
if (x == null) {
return;
}

inOrder(x.left);
System.out.print(x.value + " ");
inOrder(x.right);
}

public static void main(String[] args) {
BST t = new BST();
t.insert(5);
t.insert(2);
t.insert(1);
t.insert(3);
t.insert(6);
t.insert(7);
t.insert(4);
t.inOrder();
}
}

- singhSahab October 06, 2012 | 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