Bloomberg LP Interview Question for Java Developers


Country: United States
Interview Type: In-Person




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

public static void traverse(Node node) {
	if (node == null) return;
	List<Node> thisLevel = new ArrayList<Node>();
	thisLevel.add(node);
	while (!thisLevel.isEmpty()) {
		List<Node> nextLevel = new ArrayList<Node>();
		for (Node n : thisLevel) {
			System.out.print(n.key() + " ");
			if (n.left() != null) nextLevel.add(n.left());
			if (n.right() != null) nextLevel.add(n.right());
		}
		thisLevel = nextLevel;
		System.out.println();
	}
}

- Zhengyang.Feng2011 March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

very bright answer

- roshan January 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

BFS traversal using Two Queues for line breaks.

- Dev March 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

it can be done by the use of only one queue also...if the end of each level is marked by the null in the queue....

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

I have tried this and failed, can you please post some code for your logic.

- Dev March 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is the pseudo code....
levelorder(struct node *root)
{
struct queue *q=createqueue();
if(root==NULL)
return;
levelorderutil(q,root);
}
levelorderutil(struct queue *q,struct node *root,int k)
{
if(root!=NULL)
enqueue(q,root);
if(k==0)
enqueue(q,NULL);
levelorderutil(q,root->left,k+1);
levelorderutil(q,root->right,k+1);
enqueue(q,NULL);
}

crkt me if i'm wrong.....

- codinglearner March 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

void Level(root)
{
int h=height(root);

for(i=1;i<=h;i++)
{
LEVELORDER(root,i);
printf("\n");
}
}


void LEVELORDER(root,level)
{
if(!root)
return NULL;

if(level==1)
printf("%d ",root->data);

else if(level>1)
{
LEVELORDER(root->left,level-1)
LEVELORDER(root->right,level-1)
}

}

- NaiveCoder March 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

void printtree(Node* p,int level) {

vector<Node*> av;
av.push_back(p);
map<int,vector<Node*> > amap;

amap[0]=av;
while(true) {
vector<Node*> nodev;
for(int i=0;i<amap[level].size();i++) {
Node* an = amap[level][i];
cout << an->num << endl;
if (an->left!=NULL) {
nodev.push_back(an->left);
}
if (an->right!=NULL) {
nodev.push_back(an->right);
}

}

if (nodev.size()>0) {
amap[++level]=nodev;
}
else
break;
}
}
void print(Node* p) {
printtree(p,0);
}

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

Cant you just get the pre-order of the tree. Iterate through a look (0...n; n being height of the tree) and keep printing 2^n objects from the pre-order list each time?

- Vishal April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Start traversing the tree after putting Root and new line in list. and then append new line whenever see new line in the list.
pseudo code:::

Tree *node=root;
list.push_back(node);
list.push_back(new_line_node);
while(node != null)
{
if (node->left != null)
list.push_back(node->left)
if (node->right != null)
list.push_back(node->right)

print (node->value)

if (list.pop_front == new_line_node)
{
print "\n"
list.push_back(new_line_node)
}
else
node=list.pop_front();
}

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

Couldn't we maintain a single queue and push every element in along with it's level number (which is level number of parent + 1). We also maintain a variable for the current level. Whenever the element popped out has a level number greater than the current level, we go to a new line and increment the current level number before continuing as before.

- Nivedita November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Couldn't we maintain a single queue and push the left and right children of each node popped out along as a new object with an associated level number (which is level number of parent + 1). We also maintain a variable for the current level. Whenever the element popped out has a level number greater than the current level, we go to a new line and increment the current level number before continuing as before.

- Niha November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

typedef struct TreeNode 
{
  int element;
  struct TreeNode *left, *right;
} TreeNode;

TreeNode *displayTree(TreeNode *node)
{
  //display the full tree
  if(node==NULL)
  {
    return;
  }
  displayTree(node->left);
  printf("| %d ", node->element); 
  displayTree(node->right);
}

- CheckThisResume.com March 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