Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

I suspect a lot of these questions posted here are posted by people that suck at math. Why? Because definitions are important. I can't reconcile any sensible definition of 'vertical' between the tree you drew up and the o/p you posted. So why not define what vertical means here for your nodes. Make sure it is rigorous too. There isn't anything interesting about wasting time trying to figure out what you are trying to say. Communication is important so don't kid yourself by thinking otherwise.

- Godel October 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Imagine drawing the edges at 90degree angles. (i.e. 45 to horizontal).

- Anonymous October 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

haha, the author should draw a more clear graph, but anyway it is easy to understand

- haha October 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you understand the difficulties with text based figures and varying fonts, right?

- Anonymous October 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Godel sry abt the bad tree diagram but I think its not very hard to understand the question without the diagram. Is it?

Forget about the example given in the question, just imagine any tree and try to print the nodes you encounter when you move from left to right. If there are more than one nodes that are available in a given vertical line then print it from top to bottom.

- kaushikdev9 October 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ kaush: that was only part of my issue. The other part is that what you are describing isn't really vertical unless you orient your view accordingly. Otherwise, if you are just facing the tree as it is present on your screen, then the most appropriate word you would want to use would be: diagonal.

That might all seem like a silly thing to harp on but in fact, it isn't. Before you can solve a problem, you need to understand the problem. If you want people to solve it, then you need to be rigorous in your problem statement. Problem statements in mathematics are rigorous, they follow from definitions so that everything is clearly understood, there are no assumptions. You should model your problem statements after mathematical problem statements.

- Godel October 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ kaush:
I fully agree with Godel - pls be rigorous about your post.
Additionally, you've mentioned about moving left to right in a tree and this to me is a horizontal movement rather than a verticat one.

- buckCherry November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The example is a little bit misleading but it does not really matter. This is easy enough to understand. Rigorous definition of vertical is both unnecessary and impractical in this situation.

- 3.14159265358979323846264338327950288 November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

I am assuming that we have to print going from the left lines to the right lines, and top to bottom on a given line.

Each node is on a vertical line and can be assigned a number. The line corresponding to the root being zero. If a node has the vertical line number v, then its left child has number v-1, and right child has v+1.

So we can do a breadth first search, collecting all nodes with the same vertical line in a list.

Once we are done, we can print the lists, in sorted order of the vertical number.

Pseudo code might look something like this

void printVerticals <T> (BinTree<T> tree) {
    if (tree == null) throw...
    
     int n = tree.Count;

    // vertical line numbers are -n,-n+1, -n+2, ..., 0, 1, 2, .., n+1
    // and correspond to array locations 0,1,2, ..., 2n+1    
    List <T> verticals[2*n+1];
        
    Queue <BinTree<T>, int> q;
  
    q.enQ(tree, 0);
   
    while(q.hasElements()) {
        // Python like syntax? Being lazy here.
        BinTree<T> curNode, int lineNumber = q.deQ();
    
        // Add to corresponding list.
        verticals[lineNumber+n].Add(curNode);
       
       if (curNode.Left) {
            q.enQ(curNode.Left, lineNumber-1);
        }
    
        if (curNode.Right) {
            q.enQ(curNode.Right, lineNumber+1);
        }
    }
    // Assuming this iteration does in the order 0,1,2,...
    foreach(List <T> vertical in verticals) {
        // Assuming this iteration does in the order of insertion.
        foreach (T data in vertical) {
            // Print.
        }
    }
}

- Loler October 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sry abt the bad tree structure

Root Node: 1
Left Child of 1: 2
Right Child of 1: 3
Left Child of 2: 4
Left Child of 3: 5
Right Child of 5: 6

- kaushikdev9 October 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks for update.. using horizantal distance will work..

- nikhil March 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

General algorithm:

You are going to iterate through a hashtable depending on what group the node belongs and then print it in order from top to bottom.

- rahvii October 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can use an array instead of a hashtable. See Loler's answer. In fact what he does is like the hash function: h(x) = x+n. Only that it is a 'perfect' hash, with no collisions.

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

1. Assign 2D coordinates to nodes
                                   1( 0,0 )
                                 /            \
                             2(-1,1)      3 (1,1)
                             /                  /      \
                           4(-2,2)    5(0,2)  6(2,2) 
2. Sort the coordinates
-2,2
-1,1
0,0
0,2
1,1
2,2

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

can it be done with recursion?

- Anonymous October 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, a depth first search can be used instead of a breadth first search.

- Anonymous October 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

use preorder traversal of the binary tree

class Node{
     int data;
     Node leftChild;
     Node rightChild;
 }
public void preOrder(Node localRoot){
          preOrder(localRoot.leftChild);
          System.out.print(localRoot.data+ " ");
          preOrder(localRoot.rightChild);
}

That should do it

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

What is node 2 had a right child?

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

yes, it looks like the preorder recursion works

- vladimir.veselov October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isnt it the inorder traversal logic ?

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

Hi ,
What if the two nodes are at the same level .... do you want their sum to be printed .... ??
If yes, just create a Hash data structure and index using hash levels (-1,0,1) and iterate over the hash and print the sum

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

When there are more than one nodes at a level, print from top to bottom

- kaushikdev9 October 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if 2 had a right child. Then its coordinates and coordinates of 5 would be the same (0,2).
In that case what is the correct order ??

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

printVertical(Node node){

if(node==null) return;

Stack<Node> stack = new Stack<Node>();
List<Node> queue = new ArrayList<Node>();
Node temp = node;

while(temp!=null){
stack.push(temp);
temp = temp.left;
}

while(!stack.isEmpty()){

temp = stack.pop();
if(temp.right!=null){
queue.add(temp.right);
}
PRINT(temp.data);
}

while(!list.isEmpty())
printVertical(list.remove(0));

}

- Wolverine October 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is correct

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

package trees;

public class TreeNode {

	private int data;
	private TreeNode left;
	private TreeNode right;
	
	public TreeNode(int d){
		this.data = d;
	}
	
	public TreeNode appendRight(int d){
		this.right = new TreeNode(d);
		return this.right;
	}

	public TreeNode appendLeft(int d){
		this.left = new TreeNode(d);
		return this.left;
	}

	public int getData() {
		return data;
	}

	public void setData(int data) {
		this.data = data;
	}

	public TreeNode getLeft() {
		return left;
	}

	public void setLeft(TreeNode left) {
		this.left = left;
	}

	public TreeNode getRight() {
		return right;
	}

	public void setRight(TreeNode right) {
		this.right = right;
	}
	
	public static void print(TreeNode node){		
		
		if(node!= null){
			print(node.left);
			System.out.print(node.data+ " ");
			print(node.right);
		}
	}
}

package trees;

/**
 * Imagine a vertical line sweeping the tree from Left to right and we will print
 *  nodes at each vertical level in an ascending order of the depth of the node 
 *  from the root.
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PrintTreeVertically {

	public static void main(String[] args) {
		TreeNode root = new TreeNode(1);

		root.appendLeft(2).appendLeft(4);
		root.getLeft().appendRight(5).appendLeft(8);

		root.appendRight(3).appendLeft(6);
		root.getRight().appendRight(7);

		Map<Integer, ArrayList<TreeNode>> nodes = new HashMap<Integer, ArrayList<TreeNode>>();
		organizeNodesVertically(root, 0, nodes);

		List<Integer> verticalIndices = new ArrayList<Integer>(nodes.keySet());
		Collections.sort(verticalIndices);

		for (Integer i : verticalIndices) {
			ArrayList<TreeNode> list = nodes.get(i);
			for (TreeNode treeNode : list) {
				System.out.print(treeNode.getData() + " ");
			}
		}

	}

	private static void organizeNodesVertically(TreeNode root,
			int verticalIndex, Map<Integer, ArrayList<TreeNode>> nodes) {
		if (root == null) {
			return;
		}

		if (nodes.get(verticalIndex) == null) {
			nodes.put(verticalIndex, new ArrayList<TreeNode>());
		}

		nodes.get(verticalIndex).add(root);

		organizeNodesVertically(root.getLeft(), verticalIndex - 1, nodes);
		organizeNodesVertically(root.getRight(), verticalIndex + 1, nodes);

	}

}

- sriniatiisc October 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
Algoritham: {{{ stack.push(root) while (!stack.empty()) { while(stack.top().->leftchild!=NULL) Stack.push(stack.top()->leftchild()); temp=stack.pop(); print(temp) stack.push(temp->rightchild) } - Dhari October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algorithm:

stack.push(root)
while (!stack.empty())
{       while(stack.top().->leftchild!=NULL) 
                    Stack.push(stack.top()->leftchild());
        temp=stack.pop();
        print(temp)
        stack.push(temp->rightchild)
}

- Dhari October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this doesnot work for the node with value 6.Node 6 will be printed b4 node 3.It shud be other way round

- pm April 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void BST::printVertical(Node *root,int index,map<int,vector<int>>& list) {
    
    if ( root == NULL)
        return;
    
    vector<int> l = list[index];
    l.push_back(root->value);
    list[index] = l;
    
    printVertical(root->leftChild,index-1,list);
    printVertical(root->rightChild,index+1,list);
    
}

iterate through the map and you'll get sorted list of indexes and vectors containing vertical elements.

- Anonymous September 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void BST::printVertical(Node *root,int index,map<int,vector<int>>& list) {
    
    if ( root == NULL)
        return;
    
    vector<int> l = list[index];
    l.push_back(root->value);
    list[index] = l;
    
    printVertical(root->leftChild,index-1,list);
    printVertical(root->rightChild,index+1,list);
    
}

- the-awakened-1 September 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's a simple in-order traversal of the tree.

- S.Green.Warren November 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Here's my two cents with o(1) space with recursion!!
//printing algo. Take any root and combine its left->right and right->left value
//ofcourse excluding those values from printing again by this condition
//if it is not root node or if it's on the root left and it is left node itself, then print it.
//if it is not root node and if it's on the root right and it is right node itself, then print it.

	private static void printTreeVOrder(Node node,boolean root,boolean left,boolean rootLeft){
		if(node == null) return;
		if(root) {
			rootLeft = true;
		}
		if(node.left!=null) printTreeVOrder(node.left,false,true,rootLeft);
		if(root || ((left && rootLeft ) || (!left && !rootLeft)))
			System.out.print(node.value);
		if(node.left!=null && node.left.right!=null) System.out.print(" " + node.left.right.value);
		if(node.right!=null && node.right.left!=null) System.out.print(" " + node.right.left.value);
		if(root || ((left && rootLeft ) || (!left && !rootLeft)))
			System.out.println();
		if(node.left!=null) printTreeVOrder(node.right,false,false,(root?!rootLeft:rootLeft));
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Node root = add(null, 8, false);
		//level 1 nodes
		Node l11 = add(root,6,true);
		Node l12 = add(root,10,false);
		//level 2 nodes
		Node l21 = add(l11,4,true);
		Node l22 = add(l11,7,false);
		Node l23 = add(l12,9,true);
		Node l24 = add(l12,12,false);
		//level 3 nodes
		Node l31 = add(l21,3,true);
		Node l32 = add(l21,5,false);
		printTreeVOrder(root,true,false,false);
		
		
	}
	private static Node add(Node parentNode,int value,boolean left) {
		Node returnNode = new Node();
		if(parentNode != null) {
			if(left) parentNode.left = returnNode;
			else parentNode.right = returnNode;
		}
		returnNode.value = value;
		return returnNode;
		
	}

- naren November 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node
{

    public $info;
    public $left;
    public $right;
    public $hd; // horizontal distance

    public function __construct($info)
    {
        $this->info     = $info;
        $this->left     = null;
        $this->right    = null;
        $this->hd       = 0;
    }

    public function __toString()
    {
        return "$this->info";
    }

}

$root = new Node(5);
$root->left = new Node(4);
$root->right = new Node(3);
$root->left->left = new Node(6);
$root->left->right = new Node(7);
$root->right->left = new Node(8);
$root->right->right = new Node(9);

function bfs(Node $node)
{
    $queue          = array($node);
    $node->hd       = 0;
    $output         = array();

    while (count($queue) > 0) {
        $current_node = array_shift($queue);
        $output[$current_node->hd][] = $current_node->info;

        if ($current_node->left != null) {
            $current_node->left->hd = $current_node->hd + 1;
            array_push($queue, $current_node->left);
        }

        if ($current_node->right != NULL) {
            $current_node->right->hd = $current_node->hd - 1;
            array_push($queue, $current_node->right);
        }
    }
    return $output;

}

$result = bfs($root);
$keys = array_keys($result);
arsort($keys);
foreach ($keys as $key) {
    echo implode(' ', $result[$key]), "\n";
}

- Tormoz November 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This comment has been deleted.

- Administrator October 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Huh?

- Anonymous October 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Disregard this code. It is incorrect.

- Anonymous October 12, 2012 | 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