Bloomberg LP Interview Question for Financial Software Developers


Country: United States
Interview Type: In-Person




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

struct node* mirror(struct node*root)
{
struct node*temp;
if(root!=NULL)
{
mirror(root->left);
mirror(root->right);
temp=root->left;
root->left=root->right;
root->right=temp;
}
return root
}

- Ali_BABA January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class ReverseBinTree {

	/**
	 * @param args
          * @author sandeep 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		BinaryTree seven = new BinaryTree(7, null, null);
		BinaryTree six = new BinaryTree(6, null, null);

		BinaryTree three = new BinaryTree(3, six, seven);

		BinaryTree five = new BinaryTree(5, null, null);
		BinaryTree four = new BinaryTree(4, null, null);

		BinaryTree two = new BinaryTree(2, four, five);

		BinaryTree root = new BinaryTree(1, two, three);

		revBinTree(root);
		System.out.println("The reversed tree is " + root.toString());

	}

	private static void revBinTree(BinaryTree root) {
		// TODO Auto-generated method stub
		if(root != null){
			BinaryTree temp = root.left;
			root.left = root.right;
			root.right = temp;


			revBinTree(root.left);
			revBinTree(root.right);
		}
	}
}

- Sandeep N L January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

void MirrorImage(Node * root)
{
  if(root){
    Node * temp = root->left
    root->left = root->right;
    root->right = temp;

    MirrorImage(root->left);
    MirrorImage(root->right);
  }
}

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

you have to do it first for the children before you swap the nodes.

- hello world January 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why so, hello world?

- rajanvictor January 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not think you have to do the children first.

- Sean February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the change to the root is not returned properly as in another solution

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

Do the children first would save some stack space, that is it.

- xicheng March 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

NODE* mirror(NODE* aHeadNode){

NODE* myTempNode = NULL;

if(aHeadNode) {
myTempNode = aHeadNode->left;
aHeadNode->left = aHeadNode->right;
aHeadNode->right = myTempNode;

if(aHeadNode->left)
mirror(aHeadNode->left);

if(aHeadNode->right)
mirror(aHeadNode->right);
}
}

- abhimanipal January 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

to create a mirror image we need to traverse the tree in ordrer: root->right->left
function mirror(root,mirror){
if(root==NULL)return;
mirror.val = root.val;
mirror( root.right,mirror.left)
mirror( root.left, mirror.right)
}
OR you can swap the right node with left node to change the same tree
function mirror(root){
if(root==NULL)return;
swap(root.left,root.right);
mirror(root.left)
mirror(root.right)
}

- virat February 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is not a correct question.
Binary tree is not a geometrical object to create mirror images of it.

Binary tree is a node-based binary tree data structure which has the following properties:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.

If you call "mirror" function that people give here on an existing binary tree, the resulting object will not be a binary tree at all, it will have wrong ordering of the nodes.

I think they wanted to ask such a question:
"Print binary tree level by level so elements of each level will be in the reverse order".

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

The question is fine. It doesn't state that the mirror image needs to be a proper binary tree.

- Anonymous April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void mirror(Node root) {
    if (root.left() == null && root.right() == null) return;
    exchange(root, root.left(), root.right());
    mirror(root.left());
    mirror(root.right());
  }
  
  private static void exchange(Node root, Node left, Node right) {
    Node temp = root.left();
    root.left(root.right());
    root.right(temp);
  }

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

Traverse the BT in Right-Root-Left

- Yev August 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mirror(node** root)
{
if(*root != NULL)
{
mirror(&(*root)->left);
mirror(&(*root)->right);
node* temp = (*root)->left;
(*root)->left = (*root)->right;
(*root)->right = temp;
}
}

- kumar.mramit January 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct node* createMirror(struct node* root){
	     if(root==NULL){
		    return NULL; 
  	     }
	     struct node* temp = createMirror(root->left);
	     root->left = createMirror(root->right);
	     root->right = temp;
	     return root;
         }

- DigitalFire April 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think for this question it requires to "create" a mirror image , so may we should create them a new tree

node *mirror(struct node *root){

if(root!=NULL){
node *result=new node;
(*result)->leftch=NULL;
(*result)->rightch=NULL; //initialization

(*result).data=root->data;
(*result)->rightch=mirror(root->rightch);
(*result)->leftch=mirror(root->leftch);

return result;
}

else{
return NULL;
}

}

- code.cracker1991 December 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

public static void main(String[] args){

revBinTree(root);

}

revBinTree(TreeNode node, TreeNode left, TreeNode right){
if(node != null){
TreeNode temp = node.temp;
node.left = node.right;
node.right = temp;

revBinTree(node.left);
revBinTree(node.right);
}
}

- Sandeep N L January 21, 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