Oracle Interview Question for Software Engineer / Developers


Country: United States




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

int visit(Node n, int add) {
    if (n == null) {
      return add;
    }
    int curVal = n.data;
    n.data = visit(n.right, add);
    return visit(n.left, n.data + curVal);
  }

   void visit(Node root) {
    visit(root, 0);
  }

- Alex February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what does this code is simply traverse the BST in backward order and collects sum of the elements as it goes.

- Alex February 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

output example:

5               
         / \       
        /   \      
       /     \     
      /       \    
     4       7       
    /         / \   
   /         /   \  
  2       6   8   
 / \             
1 3             
                                
          21               
           / \       
          /   \      
         /     \     
        /       \    
      26       8       
      /          / \   
     /          /   \  
  33       15   0   
   / \             
35 30

- Alex February 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Alex,

Your code works great... But I am not able to completely understand it. Can you give me a short explanation of how you thought of it?

Thanks

- Anonymous June 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sumTree(Tree root, int sum) {
	if (root == null)
		return sum;
	sum = sumTree(root.right,sum);
	root.value+=sum;
	sum = sumTree(root.left,root.value);
	return sum;
}

- Anonymous February 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey, tell me why I am wrong?

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> February 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is wrong because it also adds the node's value to the "sum of the values of all the nodes that are greater than that node". So if we have a tree where the root is 1 and it only has a right child with a value of 2, then it would change the root to 3 (when it should change it to 2) and the right child stays at 2 (when it should be 0).

- Zoli February 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Well.. Then I am just half wrong. look at the output

root = insert(root,1);
		insert(root,2);
		inorder(root);
		System.out.println();
		chSum(root);
		inorder(root);

output:

C:\javatest>java Trees
Tree questions
1 ,2 ,
2 ,2 ,

Root holds 2.

then I just need to change the case where the node is a leaf

public static int chSum(TNode node){
		if(node == null){
			return 0;
		}else if(node.left == null && node.right == null){
			return 0;  // return 0,since we dont have anything left
		}else{
			int  tmp = node.value;
			node.value = chSum(node.right);
			return tmp + node.value + chSum(node.left);
		}
	}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> February 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

C:\javatest>java Trees
Tree questions
1 ,2 ,
0 ,2 ,

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> February 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is a solution that takes O(n) time:
The idea is that we recurse the tree and we go updating every node's value. We pass a parameter called greater, which stores the sum of the values that are greater than the current node (and are coming from a different part of the tree). So when we call the recursion on a left subtree, we pass to it the sum of the nodes that a greater than it (but not part of this left subtree).
Also, the recursive function returns the sum of the nodes in the tree, which we use to calculate the new values of the nodes.

public void changeValues(Node root) {
	changeValues(root, 0);
}

public int changeValues(Node root, int greater) {
	if(root == null)
		return 0;
	tmp = root.value;
	int right = changeValues(root.right, greater);
	root.value = right + greater;
	int left = changeValues(root.left, root.value + tmp);
	return left + tmp + right;
}

- Zoli February 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect, small change put if right+greater > tmp then root.value=right+greater to avoid setting 0 as the node value to right most leave node. modified code as follows
public int changeValues(Node root, int greater) {
if(root == null)
return 0;
tmp = root.value;
int right = changeValues(root.right, greater);
if((right + greater) > tmp) {
root.value = right + greater;
}
int left = changeValues(root.left, root.value + tmp);
return left + tmp + right;
}

- Suresh Babu November 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since we are not allowed to create global or static variable, we can create hashmap(dictionary) datastructure, to maintain the key-value pair with key as nodevalue and value as sum of values of node greater than the node and while recurssive call, we will keep on sending this hashmap as an argument and using this hashmap, we will keep on updating the value.
Please let me know if this is right approach.

- Yo Yo Honey Singh February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// C program to find sum of all paths from root to leaves
#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *left, *right;
};

// function to allocate new node with given data
struct node* newNode(int data)
{
    struct node* node = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}

int sumOfRightsUtil(struct node* root, int inSum) {
	if (root == NULL)
		return 0;

	int rightSum = sumOfRightsUtil(root->right, inSum);
	int leftSum = sumOfRightsUtil(root->left, inSum + rightSum + root->data);

	int returnSum  = leftSum + rightSum + root->data;

	root->data = root->data + rightSum + inSum;

	return returnSum;
}

void printInorder(struct node* root) {
	if (NULL != root) {
		printInorder(root->left);
		printf("%d, ", root->data);
		printInorder(root->right);
	}
}

void sumOfRights(struct node* root) {
	int sum = sumOfRightsUtil(root, 0);
}

// Returns sum of all root to leaf paths. The first parameter is root
// of current subtree, the second parameter is value of the number formed
// by nodes from root to this node

// Driver function to test the above functions
int main()
{
    struct node *root = newNode(6);
    root->left        = newNode(3);
    root->right       = newNode(5);
    root->right->right= newNode(7);
    root->left->left  = newNode(2);
    root->left->right = newNode(5);
    root->right->right = newNode(4);
    root->left->right->left = newNode(7);
    root->left->right->right = newNode(4);
    printInorder(root);
    sumOfRights(root);
    printf("\n");
    printInorder(root);
    return 0;
}

- Naveen February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

When we do reverse inorder traverse of BST, that is right--root--left, the nodes visited before current node are those having greater value. By keeping a record of the sum of all nodes visited, we can easily fix the problem.
Following is C code:

void replaceWithSumOfAllGreater(TreeNode* root, int* pSum)
{
	if(root == NULL) return;
	replaceWithSumOfAllGreater(root->right, pSum);
	int tmp = *pSum + root->val;
	root->val = *pSum;
	*pSum = tmp;
	replaceWithSumOfAllGreater(root->left, pSum);
}
void replaceWithSumOfAllGreater(TreeNode* root)
{
	int sum = 0;
	replaceWithSumOfAllGreater(root, &sum);
}

- uuuouou February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(root != null) {
		updateNodeValue(root, 0);
	}
	
	void updateNodeValue(Node n, int val) {
		n.data = n.data + val;
		if(n.left != null) {
			updateNodeValue(n.left, n.data);
		}
		if(n.right != null) {
			updateNodeValue(n.right, n.data);
		}
	}

- Sunil February 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ignore this. i interpreted the question wrong. will come with proper solution shortly.

- Sunil February 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if(root != null) {
		updateNodeValue(root);
	}

int updateNodeValue(Node n) {
		if(n.right != null) {
			n.data = n.data + updateNodeValue(n.right);
		}
		if(n.left != null) {
			n.left.data = n.left.data + n.data;	
			int temp1 = 0, temp2 = 0;
			if(n.left.right != null && n.left.right.right == null && n.data > n.left.right.data) {
				temp1 = n.left.right.data + n.data;
			}
			temp2 = updateNodeValue(n.left);
			if(temp1 != 0) {
				n.left.right.data = temp1;
			}
			return temp2;
		}
		return n.data;
	}

I came up with this above solution. it worked with different examples. Please try and let me know if it has any issues.

- Sunil February 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about we do in-order traversal iteratively and store the node's value in a array list and at the same time using a hash map store the value-node. After the traversal, update the array list by calculating the sum and then update the node using hash map?

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

I think this would work fine:
1. Do inorder traversal and push each node into a stack
2. set sum=0
3. pop the Nodes from stack and do
temp = node.data;
node.data = sum;
sum += temp;

This takes O(n) time and space.

- Vamsi Krishna March 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a twisted inorder traversal question:

public int inOrder(Node node, int sum)
{
	if(node == null)
	{
		return 0;
	}
	sum += inOrder(node.right, sum);
	int t = node.data;
	node.data = sum;
	sum += t;
	sum += inOrder(node.left, sum);
	return sum;
}

From main, just call inOrder(root,0);

- Vamsi Krishna M March 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverseorder(tNode *n, int& sum) {
  if (n == NULL)
      return;

  reverseorder(n->right, sum);
  int tmp = sum;
  sum += n->data;
  n->data = tmp;
  reverseorder(n->left, sum);
}

Its bascially reverse inorder traversal. But we also need to clarify, in case there are duplicate nodes, whether to consider those while adding up .. This code does not care of duplicate nodes

- iwanna April 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int add(Node root,int sum){
	if(root.right != null) sum = add(root.right,sum);
	if(root != null){
		root.data += sum;
		sum = root.data;
	}
	if(root.left != null) sum = add(root.left,sum);
	return sum;
}

- ajayv October 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

:) Inorder traversal gives the tree in sorted order: L(D) R ... if we reverse the calls to right and left, we can get the same in reverse sorted order ... so now ... just before printing, one can keep count of how already printed. O(n) solution. Very simple.

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

That is

Inorder (root, &count)
{
if (root == NULL) return;
Inorder(root->Right);
root->Sum = count;
count++;
Inorder(Root->Left);
}

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

public static int ModifyNodeData(Node node,int data)
{
int res=0;

if(node!=null)
{
int r = ModifyNodeData(node.right,data);

if(r>0)
res =node.data+r;
else
res=node.data+data;
node.data =res;
int l =ModifyNodeData(node.left,res);
if(l>0)
return l;
else
return res;
}
return res;

}

- Ram Pratap Singh January 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int ModifyNodeData(Node node,int data)
	{		int res=0;
			if(node!=null)
		{
			int r = ModifyNodeData(node.right,data);
				if(r>0)
			res =node.data+r;
			else
				res=node.data+data;
			node.data =res;
		    int l =ModifyNodeData(node.left,res);
			if(l>0)
				return l;
			else
			return res;
		}
		return res;
	}

- Anonymous January 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int ModifyNodeData(Node node,int data)
{ int res=0;
if(node!=null)
{
int r = ModifyNodeData(node.right,data);
if(r>0)
res =node.data+r;
else
res=node.data+data;
node.data =res;
int l =ModifyNodeData(node.left,res);
if(l>0)
return l;
else
return res;
}
return res;
}

- singh.ram25327 January 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static int chSum(TNode node){
		if(node == null){
			return 0;
		}else if(node.left == null && node.right == null){
			return node.value;
		}else{
			int  tmp = node.value;
			node.value = chSum(node.right);
			return tmp + node.value + chSum(node.left);
		}
	}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> February 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is also incorrect, see my above comment. With a tree where the root is 1 and its right child is 2, this would leave the right child at the value of 2, when it should be changed to 0.

- Zoli February 19, 2014 | 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