Bloomberg LP Interview Question for Software Engineer / Developers






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

yes why cant we just do an in order traversal and check if the resulting string is sorted??

- vin2502 December 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you are correct. they are incorrect. isBST iff inorder is sorted.

- Anonymous January 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And it is the most efficient one.

- Anonymous July 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

IsBinarySearchTree(Node root)
{
if(root == null) return false

IsBinarySearchTree(node.Left) && IsBinarySearchTree(node.Right)

if(node.Right != null && node.Left !=null)
{
return (node.Right.Data > Node.Data && node.Left.Data < node.Data)
}
else if ( node.Right == null)
{
return (node.Left.Data < node.Data)

} else
{
return (node.Right.Data > node.Data)

}
}

- Paul October 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

compute inorder of the tree,
if it is sorted then its a BST

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

boolean isBinarySearchTree(Node root){
if(root==null) return true; //assume empty node is a binary search tree
else{
if(root.left!=null)
if(root.data<=root.left.data)
return false;
if(root.right!=null)
if(root.data>=root.right.data)
return false;
}
return isBinarySearchTree(root.left)& isBinarySearchTree(root.right);
}

- way October 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are all wrong.

- Anonymous November 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Discuss possible disadvantages of the proposed hash function.

- Anonymous April 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code will fail in the following scenario:

9 
  / \ 
4     10
      / \ 
    8     11

Inorder traversal (and then checking for sorted-ness) will work, but that is O(n). I do not know if there is a better solution, any ideas?

- Anonymous April 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous on April 27, 2011


you tree seems wrong. 8 should be on the other side of 9

- Anonymous October 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm not sure. But shouldn't we check both the lower bound and upper bound of a specific tree node?

- freaxer December 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey29109" class="run-this">boolean isBinarySearchTree(Node r){
if(r.isLeaf){
return true;
}
if(r.key>=r.left.key && r.key<r.right.key){
return (true && isBinaryTree(r.left) && isBinaryTree(r.right))
}
else{
return false;
}
}</pre><pre title="CodeMonkey29109" input="yes">
</pre>

- wangbingold December 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Start At root:
For each node in BST : minData < node.data < maxData
For root : minData = Negative Infinity maxData = Positive Infinity
For root.left : minData = Negative Infinity maxData = root.data
For root.right : minData = root.data maxData = Positive Infinity

Recurse till u find a node where BST condition is violated or node is null.

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

I think above algo works!!!

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

public class BSTChecker {

public static void main(String[] args) {
Node root = new Node(5);
Node l1 = new Node(4);
Node r1 = new Node(6);
root.left = l1;
root.right = r1;
Node l1L = new Node(3);
Node l1R = new Node(5);
l1.left = l1L;
l1.right = l1R;
System.out.println(isBST(root));
}

static class Node {
int data;
Node left;
Node right;

public Node(int data) {
this(data, null, null);
}

public Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}

public static boolean isBST(Node root) {
return isBST(root, Integer.MAX_VALUE, Integer.MIN_VALUE);
}

private static boolean isBST(Node node, int maxValue, int minValue) {
if (node == null) {
return true;
} else if (node.data > maxValue || node.data < minValue) {
return false;
} else {
return isBST(node.left, node.data, minValue)
&& isBST(node.right, maxValue, node.data);
}
}
}

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

bool validate(bst *node)
{
	if(node)
	{
		if(validate(node->left)) 
		{
			if(node->left) 
			{
				if(node->left->data < node->data)
				{
					if(validate(node->right)
					{
						if(node->right)
						{
							if(node->right->data > node->data)
								return TRUE;
							return FALSE;
						}
						return TRUE;
					}
					return FALSE;
				}
				return FALSE;
			}
			else
			{
				if(validate(node->right)
				{
					if(node->right)
					{
						if(node->right->data > node->data)
							return TRUE;
						return FALSE;
					}
					return TRUE;
				}
				return FALSE;
			}
		}
		return FALSE;
	}
	return TRUE;
}

- Sundar May 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0


typedef int bool;
typedef struct bst{
struct bst *left;
struct bst *right;
int data;
}bst;

bst * newNode(int data);
bool validate(bst *node, int MIN, int MAX);

int main(int argc, char **argv)
{
  bst *node;
  node = newNode(40);
  node->left        = newNode(20);
  node->right       = newNode(60);
  node->left->left  = newNode(10);
  node->left->right = newNode(30);
  node->right->left = newNode(35);


  if(validate(node,node->data,node->data))
    printf("Is BST\n");
  else
    printf("Not a BST\n");
    
  return 0;

}

bst * newNode(int data)
{
  bst *node = (bst *)malloc(sizeof(bst));
  if(!node)
  	return NULL;

  node->data = data;
  node->left = NULL;
  node->right = NULL;

  return node;
}

bool validate(bst *node, int MIN, int MAX) 
{
     if(node)
     {
		   if(node->data >= MIN && node->data <= MAX && validate(node->left,MIN,node->data) && validate(node->right,node->data,MAX))
		       return TRUE; 
		   return FALSE;
     }
     return TRUE;
}

- ksundara.badboyz May 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean isBST(Node n, int lower, int upper){
if(n == null)
return true;

if(n > upper || n < lower)
return false;

return isBST(n.left, lower, n.value) && isBST(n.right, n.value, upper);
}

- Anonymous October 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the perfect and elegant solution if you are looking for a recursive solution.
Each Node has a range of values it can be. It must be greater than all the nodes to its left side and greater than all of the values to its right side.

This solution encapsulates both of those points by using parameters to store the lower and upper bounds of the values of each node. Therefore, the node.value > left side and node.value < right side, so with each node, we update the lower and upper bounds.

boolean isBST(Node n, int lower, int upper){
if(n == null)
return true;

if(n > upper || n < lower)
return false;

return isBST(n.left, lower, n.value) && isBST(n.right, n.value, upper);
}

- Simon Zhu October 01, 2013 | Flag


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