Amazon Interview Question for Software Engineer / Developers


Team: Kindle-Periodicals
Country: India
Interview Type: In-Person




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

/*
A[] is array of size 2* max width of tree


VS(root,a[],width)

*/
void VS(node *root,int a[],int d)
{
if(!root)
return;

a[d]+=root->data;

VS(root->left,a,d-1);
VS(root->right,a,d+1);

}

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

Size of the array will be 2^(height of tree) it does not depends on width of the tree.

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

Size should be 2* max width ..... since the vertical sum will be spread across width
And of course not 2^height

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

Instead of using an array, Simple use a STL map.
map<int,int> soln; where the first int stores the node signature (ie. +1, -1, -2, +2 etc) and second stores the sum.
So instead of the a[d]+=root->data statement,
simply use soln[i]+=root-->data.

- Ananth March 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can somebody tell me what 'vertical sum array' means?
Is it the running sum from root to leaf, at every leaf node?

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

Can somebody tell me what 'vertical sum array' means?
Is it the running sum from root to leaf, at every leaf node?

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

search into google

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

The definition of the 'vertical sum' of a binary tree wasn't very intuitive - so I'm showing it here for reference.

Consider the following tree, with the vertical lines drawn:

| |  1   | |
         | | / | \ | |
         | 2  |  3 |
         |/| \ | / |\|
        4 | 5,6 | 7

Note that nodes 5,6 are considered to lie on the same vertical line. The vertical sum is now the sum all the nodes that lie on the same vertical line.

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

Width of the tree is again as non-intuitive. When the height is 1, that is, only the root is present, the width of the array is 1. With every increment in height there are two elements added to the array, one on extreme left and other on extreme right. Thus, size of array=2*height+1. This will ensure that the array is odd length and the first call should pass the center pointer.

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

Width of the tree is again as non-intuitive. When the height is 1, that is, only the root is present, the width of the array is 1. With every increment in height there are two elements added to the array, one on extreme left and other on extreme right. Thus, size of array=2*height+1. This will ensure that the array is odd length and the first call should pass the center pointer.

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

Examples:

1
/ \
2 3
/ \ / \
4 5 6 7

The tree has 5 vertical lines

Vertical-Line-1 has only one node 4 => vertical sum is 4
Vertical-Line-2: has only one node 2=> vertical sum is 2
Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12
Vertical-Line-4: has only one node 3 => vertical sum is 3
Vertical-Line-5: has only one node 7 => vertical sum is 7

So expected output is 4, 2, 12, 3 and 7

Solution:
We need to check the Horizontal Distances from root for all nodes. If two nodes have the same Horizontal Distance (HD), then they are on same vertical line. The idea of HD is simple. HD for root is 0, a right edge (edge connecting to right subtree) is considered as +1 horizontal distance and a left edge is considered as -1 horizontal distance. For example, in the above tree, HD for Node 4 is at -2, HD for Node 2 is -1, HD for 5 and 6 is 0 and HD for node 7 is +2.
We can do inorder traversal of the given Binary Tree. While traversing the tree, we can recursively calculate HDs. We initially pass the horizontal distance as 0 for root. For left subtree, we pass the Horizontal Distance as Horizontal distance of root minus 1. For right subtree, we pass the Horizontal Distance as Horizontal Distance of root plus 1.

Following is Java implementation for the same. HashMap is used to store the vertical sums for different horizontal distances. Thanks to Nages for suggesting this method.
import java.util.HashMap;

// Class for a tree node
class TreeNode {

// data members
private int key;
private TreeNode left;
private TreeNode right;

// Accessor methods
public int key() { return key; }
public TreeNode left() { return left; }
public TreeNode right() { return right; }

// Constructor
public TreeNode(int key) { this.key = key; left = null; right = null; }

// Methods to set left and right subtrees
public void setLeft(TreeNode left) { this.left = left; }
public void setRight(TreeNode right) { this.right = right; }
}

// Class for a Binary Tree
class Tree {

private TreeNode root;

// Constructors
public Tree() { root = null; }
public Tree(TreeNode n) { root = n; }

// Method to be called by the consumer classes like Main class
public void VerticalSumMain() { VerticalSum(root); }

// A wrapper over VerticalSumUtil()
private void VerticalSum(TreeNode root) {

// base case
if (root == null) { return; }

// Creates an empty hashMap hM
HashMap<Integer, Integer> hM = new HashMap<Integer, Integer>();

// Calls the VerticalSumUtil() to store the vertical sum values in hM
VerticalSumUtil(root, 0, hM);

// Prints the values stored by VerticalSumUtil()
if (hM != null) {
System.out.println(hM.entrySet());
}
}

// Traverses the tree in Inoorder form and builds a hashMap hM that
// contains the vertical sum
private void VerticalSumUtil(TreeNode root, int hD,
HashMap<Integer, Integer> hM) {

// base case
if (root == null) { return; }

// Store the values in hM for left subtree
VerticalSumUtil(root.left(), hD - 1, hM);

// Update vertical sum for hD of this node
int prevSum = (hM.get(hD) == null) ? 0 : hM.get(hD);
hM.put(hD, prevSum + root.key());

// Store the values in hM for right subtree
VerticalSumUtil(root.right(), hD + 1, hM);
}
}

// Driver class to test the verticalSum methods
public class Main {

public static void main(String[] args) {
/* Create following Binary Tree
1
/ \
2 3
/ \ / \
4 5 6 7

*/
TreeNode root = new TreeNode(1);
root.setLeft(new TreeNode(2));
root.setRight(new TreeNode(3));
root.left().setLeft(new TreeNode(4));
root.left().setRight(new TreeNode(5));
root.right().setLeft(new TreeNode(6));
root.right().setRight(new TreeNode(7));
Tree t = new Tree(root);

System.out.println("Following are the values of vertical sums with "
+ "the positions of the columns with respect to root ");
t.VerticalSumMain();
}
}

- Solution Provider April 19, 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