Bloomberg LP Interview Question for Financial Software Developers






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

/* Pointer array to node the list at a depth */
struct node *buff[hight_of_bin_tree] ;

void binJoin(struct node * curr_node,int depth)
{
   /* Assign the buff[depth] to the current node when its left most node at hight 'depth' */
   if(buff[depth] == 0)
        buff[depth] = curr_node;
   /* Set the current node as the next of previous buff[depth] node */
   /* And mark current node as buff[depth] node */
   else
   {
      buff[depth]->next = curr_node;
      buff[depth] = curr_node;
   }
   /* Traverse the tree in left direction */
   if(curr_node->left)
      binJoin(curr_node->left,depth+1);
   /* Traverse the tree in right direction */
   if(curr_node->right)
      binJoin(curr_node->right,depth+1);
   return;
}

- Ashutosh February 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if(buff[depth] == 0)

Do you mean

if(buff[depth] == NULL)

- orangetime23 May 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Asutosh, when you do
buff[depth]->next = curr_node;
buff[depth] = curr_node;
Are you losing the pointer to the original buff[depth] pointer? i.e., you append a node the the tail of a list, but you then throw away the pointer to the previous tail... consequently, you can only retrieve the last node.

I think it should be
curr_node->next = buff[depth];
buff[depth] = curr_node;

i.e., insert to the head, and then you can traverse the list.

- Haoju July 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can make use of the breadth first search

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

Breadth First Trtaversal

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

Let us say you tree is defined like this, where n is the variable that should point to its right neighbour

public static class Tree{
		Tree left;
		Tree right;
		Tree n;
		int value;
	}

Then your code would be

public static void linkTreeLevels(Tree root){
		Tree temp;
		Tree head = null;
		Tree node = null;
		for(temp = root;temp!=null;temp = temp.n){
			if(temp.left!=null){
				if(head==null){
					head = temp.left;
					node = head;
				}
				else{
					node.n = temp.left;
					node = node.n;
				}
			}
			if(temp.right!=null){
				if(head == null){
					head = temp.right;
					node = head;
				}
				else{
					node.n = temp.right;
					node = node.n;
				}
			}
		}
		if(head!=null){
			node.n = null;
			linkTreeLevels(head);
		}
	}

- Abdul Malik Mansoor February 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain your idea in words? I have no idea how you could link all nodes at a level using recursion.

To Clarify: All nodes at a level should be linked left to right, without forming a leaf i.e. the last node in a level pointing to the first node in the level.

- Mahesh February 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ashutosh solution is great!

A less efficient way would be to take two queues.
- Push root into the first queue
- Remove elements from the first queue, chaining them together and pushing their children into the second queue as they are being removed.
- Now remove elements from the second queue, chaining them together and pushing their children into first queue.
- So on until queues are empty.

- Mahesh February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Mahesh:
Not even two queues. Just one queue. That is the breadth first traversal the first person mentioned.

- Rakshit February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

two understandings:
1. link the nodes level by level
2. link all nodes in the tree, going from top to down, from left to right in each level, but the result is one linked list

Mahesh's solution works for #1; other solutions work for #2....

- beyondfalcon August 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

simply refers to "how do u distinguish the levels of tree node"

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

ArrayList<LinkedList<BinaryNode>> findlevelbylevel(BinaryNode root)
{
ArrayList<LinkedList<BinaryNode>> result = new ArrayList<LinkedList<BinaryNode>>();
LinkedList<BinaryNode> list = new LinkedList<BinaryNode>();
list.Add(root);
result.Add(list);
int level=0;
While (true)
{
list = new LinkedList<BinaryNode>();
for (int i=0;i<result.get(level).size();i++
{
BinaryNode node = result.get(level).get(i);
if (node.left!=null)
list.Add(node.left);
if (node.right!=null)
list.Add(node.right);
}
if (list.size()>0)
result.Add(list);
else
break;
level++;
}
return result;
}

- Jie November 01, 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