Oracle Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

This is what solution I came up with:

void populate(NODE *root){
    if (root == NULL) 
        return;
    populate(root->left);
    populate(root->right);
    if(root->left)
        root->lc = root->left->lc + root->left->rc + 1;
    else
        root->lc = 0;
    if(root->right)
        root->rc = root->right->lc + root->right->rc + 1;
    else
        root->rc = 0;
}

- rising April 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is the solution to your question, plus it returns the total count of nodes for the given node. Hope that the code is self explaining. :)

struct Node {
    Node *left;
    Node *right;
    int leftcount;
    int rightcount;
};

int node_count(Node *pNode)
{
    int l = 0;
    int r = 0;
    
    if (NULL == pNode)
        return 0;
    
    if (pNode->left) {
        l = 1 + node_count(pNode->left);
        pNode->leftcount = l;
    }
    else {
        pNode->leftcount = 0;
    }
        
    if (pNode->right) {
        r = 1 + node_count(pNode->right);
        pNode->rightcount = r;
    }
    else {
        pNode->rightcount = 0;
    }
    
    return l + r;
}

- ashot madatyan April 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

DFS

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

//  please tell me mistake in this algo..
int nodes(struct *node)
{
    if(node==NULL)
    return 0;
    
    ans=nodes(node->left)+nodes(node->right)+1;
    
    
    return ans;
}

- mac April 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

will give total number of nodes in the tree...but actual question is something different :

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

//  please tell me mistake in this algo..
int nodes(struct *node)
{
    if(node==NULL)
    return 0;
    
    ans=nodes(node->left)+nodes(node->right)+1;
    
    
    return ans;
}

- mac April 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you need to set the value in left and right data field.
instead of ans=nodes(node->left).... line

you change it to
int ans = nodes(node->left); node->lval=ans;
ans=nodes(node->right); node->rval=ans;
return (ans+1);

- Nandhini June 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//  please tell me mistake in this algo..
int nodes(struct *node)
{
    if(node==NULL)
    return 0;
    
    ans=nodes(node->left)+nodes(node->right)+1;
    
    
    return ans;
}

- mac April 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the question mentioned above, we have to populate the no,of nodes in LHS and RHS of the binary tree. But in your algorithm you are simply returning the total count of the nodes. The algorithm is not able to distinguish the no,of nodes present in the RHS or LHS neither is it storing it in the data field.

- dv April 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the problem can be removed by using

int update_parameters(tree* node)
{
    if(node==NULL)
        return 0;
    node->left_child=update_parameters(node->child[0]);
    node->right_child=update_parameters(node->child[1]);
    return node->left_child+node->right_child+1;
}

- apoorv May 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

node->child[0] is the pointer to left node , node->child[1] is pointer to right node and node->left_child means the number of left nodes similarly for node>right_child

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

My answer

#include "stdafx.h"

struct Tree
{
int data;
int lCount;
int rCount;
Tree *left;
Tree *right;
};

Tree * CreateTree(Tree *root, int value)
{
if(root == NULL)
{
root = new Tree();
root->data = value;
root->left = NULL;
root->right = NULL;
root->lCount = 0;
root->rCount = 0;
}
else if(value < root->data)
root->left = CreateTree(root->left, value);
else
root->right = CreateTree(root->right, value);

return root;
}

Tree * CountLR(Tree *root)
{
Tree *node = NULL;
if(root != NULL)
{
if(root->left == NULL && root->right == NULL)
{
root->lCount = 0;
root->rCount = 0;
}
else if(root->left == NULL)
{
root->lCount = 0;
node = CountLR(root->right);
root->rCount = node->rCount + node->lCount + 1;
}
else if(root->right == NULL)
{
root->rCount = 0;
node = CountLR(root->left);
root->lCount = node->lCount + node->rCount + 1;
}
else
{
node = CountLR(root->left);
root->lCount = node->lCount + node->rCount + 1;

node = CountLR(root->right);
root->rCount = node->rCount + node->lCount + 1;
}

}

return root;
}


int _tmain(int argc, _TCHAR* argv[])
{
int arr[] = {30,15,10,66,51,53,12};
Tree *root = NULL;

for(int i=0; i< 7; i++)
root = CreateTree(root, arr[i]);

CountLR(root);
return 0;
}

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

Tree* Count(Tree *root)
{
if(root != NULL)
{
if(root->left == NULL)
root->lCount = 0;
if(root->right == NULL)
root->rCount = 0;
if(root->left)
root->lCount = Count(root->left)->lCount + Count(root->left)->rCount + 1;
if(root->right)
root->rCount = 1 + Count(root->right)->lCount + Count(root->right)->rCount;
}

return root;
}

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

this might also be one of the answers
struct node
{
int lcount;
int rcount;
struct node * left;
struct node * right;
};
typedef struct node NODE;

NODE * populate( NODE * root)
{
if(!root)
return 0;
root->lcount=populate(root->left);
root->rcount=populate(root->right);
return(root->lcount+root->rcount+1);
}

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

how does having count as data field helping here ?
Just do any of the tree traversal techniques.

- krutik January 31, 2014 | 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