Amazon Interview Question for Developer Program Engineers


Country: United States
Interview Type: Phone Interview




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

Traverse the binary tree whichever way you want (preorder, postorder, inorder, level order, etc.). When you visit the node, check if the node exists in the hash set (or hash table), if it does then add it to the list, otherwise add the node to the hash set.

O(n) time and O(n) space

- oOZz August 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the numbers are repeated more than once, then you need to use a hashtable with elements being the key and the element count is the value. Only add the elements to the list, if the element exists in the hash table AND the count is 1.

- oOZz August 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Assuming integers to be size 32-bit, this can be improved with having a bit vector of size (2^32) -1 bits instead of hash table. When traversing the tree, set the bit corresponding to the number traversed.

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

Super cool solution!!!

- Hello world August 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes indeed!!

- I am a programmer August 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Alright point taken!

- oOZz August 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

you dont need to maintain count. instead of hashtable, you could use a set which just has keys.

for each node in tree traversal:
if key is present in set:
insert into list
else:
insert into hash

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

A sample program in java

package oracle.prakash.investmentbank.test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;


public class AmazonTree<T extends Comparable> {
    public AmazonTree() {
        super();
    }
    public AmazonTree(T data) {
        this.setData(data);
    }
    private AmazonTree<T> left;
    private AmazonTree<T> right;
    private T data;
    private AmazonTree<T> root;
    private ArrayList<AmazonTree<T>> alist = new ArrayList<AmazonTree<T>>();
    
    public static void main(String[] args) {
        AmazonTree<Integer> amazonTree = new AmazonTree<Integer>();
        int[] ints = {10,20,30,40,50,60,60};
        for(int i:ints) {
            amazonTree.insert(i);
        }
        System.out.println(amazonTree.mostDuplicate());
    }
    
    public void insert(T data) {
        AmazonTree<T> root = this.getRoot();
        AmazonTree<T> item = new AmazonTree<T>(data);
        LinkedList<AmazonTree<T>> list = new LinkedList<AmazonTree<T>>();//since in java we have LinkedList which implements queue
        
        if(root==null) {
            this.setRoot(item);
            return;
        }
        list.add(root);
        while(!list.isEmpty()) {
            root = list.poll();
            if(root.getLeft()!=null) list.add(root.getLeft());
            else {
                root.setLeft(item);
                return;
            }
            if(root.getRight()!=null) list.add(root.getRight());
            else {
                root.setRight(item);
                return;
            }
        }
    }
    
    public  ArrayList<AmazonTree<T>> mostDuplicate() 
    {
        
        AmazonTree<T> root = this.getRoot();
        HashSet<T> set = new HashSet<T>();//Set holds unique values
        if(root==null) return  alist;
        LinkedList<AmazonTree<T>> list = new LinkedList<AmazonTree<T>>();
        list.add(root);
        while(!list.isEmpty())
        {
            root = list.poll();
            if(root.getData()!=null) if(!set.add(root.getData())) alist.add(root);
            if(root.getLeft()!=null) list.add(root.getLeft());
            if(root.getRight()!=null) list.add(root.getRight());
        }
        return alist;
    }
    
    

    public void setLeft(AmazonTree<T> left) {
        this.left = left;
    }

    public AmazonTree<T> getLeft() {
        return left;
    }

    public void setRight(AmazonTree<T> right) {
        this.right = right;
    }

    public AmazonTree<T> getRight() {
        return right;
    }

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

    public T getData() {
        return data;
    }

    public void setRoot(AmazonTree<T> root) {
        this.root = root;
    }

    public AmazonTree<T> getRoot() {
        return root;
    }
    
    public String toString() {
        return this.getData()!=null ? this.getData().toString():null;
    }
}

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

I didn't do anything except posing the interview question. Why my reputation this low?!

- Cystal August 10, 2013 | 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