National Instruments Interview Question for Software Engineer / Developers






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

typedef _Tree{
int color;
bool clump; //indicate if it is already a clump or not
TreeNode *lchild;
TreeNode *rchild;
}TreeNode, pTree;

static int clumps;
void BiTreeClumps(pTree T){ //Postorder traversal
if(!T) return;
else{
BiTreeClumps(T->lchild);
BiTreeClumps(T->rchild);
if(T->lchild->color == T->rchild->color && T->lchild->color == T->color && T->lchild->clump != true && T->rchild->clump != true){
T->clump == true;
clumps++;
}
else if(T->lchild->color == T->color && T->lchild->clump == true)
T->clump = true; //doesn't count
else if(T->rchild->color == T->color && T->rchild->clump == true)
T->clump = true; //doesn't count
}
}

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

this will also give segmentation fault as for leaf node also you are T->lchild->color. i.e trying to access NULL pointer

- thinker August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int colorCode (struct node* root) {
	if (root == NULL)
		return 0;
		
	if (root->left != NULL && root->right != NULL) {
		if (root->left->color == root->color && root->right->color == root->color)
			return (colorCode(root->left) + 1 + colorCode(root->right));
			
		else
			return 0;
	}
	
	else
		return 0;
}

- Anonymous May 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int colorCode (struct node* root) {
	if (root == NULL)
		return 0;
		
	if (root->left != NULL && root->right != NULL) {
		if (root->left->color == root->color && root->right->color == root->color)
			return (colorCode(root->left) + 1 + colorCode(root->right));
			
		else
			return (colorCode(root->left) + colorCode(root->right));
	}
	
	else
		return 0;
}

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

say u r at node x. add the left subtree's number of clumps, try to find out if x forms a new clump or not with left or right adjacent nodes, if yes increment total clump count. else not. add right subtree's number of clumps.
Do that recursively for all nodes till base case.

- arnabdas November 27, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could you clarify your solution? Maybe provide some pseudo-code. Are you returning the sum or the 'colors' of the child subtrees?

- kryptoknite October 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is a psuedo code:

int ReturnClumps(List* head)
{
  if (head == null )   
   {
     if(FormsClumps(head))
       return 1;
     else return 0;
    }
  else
   {
      if(FormsClump(head))
        return  1 +  ReturnClumps(right.child) + ReturnClumps(left.child);
      else 
        return  ReturnClumps(right.child) + ReturnClumps(left.child);
   } 
              
}//End ReturnClumps()

boolean FormsClump(List* head)
{
  if(head.RightAdjacent.color == head.color && head.LeftAdjacent.color == head.color)
      return 1;//forms clump
  else 
      return 0;
    
}//End FormsClumps()

PS: Please correct if wrong.:)

- shivamshukla749 August 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

head being NULL will cause segmentation fault in FormsClump. Here we are trying to access NULL pointer.

- RobinHood May 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

head being NULL will cause segmentation fault in FormsClump. Here we are trying to access NULL pointer.

- RobinHood May 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are checking only the left and the right nodes and comparing that with the root node. However, the question clearly states that a clump is formed when you have more than 3 different colors as adjacent nodes.

- mitshubh October 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could you please clarify the question ? More than 3 nodes being adjacent in a binary tree ?

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

sorry donno how to do whitespace

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

could u explain the ques plz? bcoz u r saying that more than three colors are adjacent then a clump is formed, and everybody only checking the color of root and their left and right child, it is not more than 3 they are checking for 3 colors, how we check for more than 3?

- Saaksham August 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

use a stack to keep track of the last node. Not move down (to the left/right) and check if the colors of the current node's left and right children AND the color of the node int the TOP of the stack are different. Call the function recursively, incrementing the value if you find a clump.

- mitshubh October 26, 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