Amazon Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Looking for interview experience sharing and coaching?

Visit AONECODE.COM for ONE-ON-ONE private lessons by FB, Google and Uber engineers!

SYSTEM DESIGN
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algorithms, Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Get hired from G, U, FB, Amazon, LinkedIn, Yahoo and other top-tier companies after weeks of training.

Email us aonecoding@gmail.com with any questions. Thanks!

Solution:

public TreeNode closestLeaf(TreeNode root, TreeNode target) {
        if(root == null || target == null) {
            return null;
        }
        Map<TreeNode, List<TreeNode>> mapping = new HashMap();
        buildAdjList(mapping, root, null);

        Queue<TreeNode> queue = new LinkedList();
        Set<TreeNode> visited = new HashSet();

        for (TreeNode node: mapping.keySet()) {
            if (node != null && node == target) {
                queue.add(node);
                visited.add(node);
                break;
            }
        }

        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node != null) {
                if (mapping.get(node).size() <= 1) {
                    return node;
                }
                for (TreeNode n: mapping.get(node)) {
                    if (!visited.contains(n)) {
                        visited.add(n);
                        queue.add(n);
                    }
                }
            }
        }
        return null;
    }

    public void buildAdjList(Map<TreeNode, List<TreeNode>> mapping, TreeNode node, TreeNode p) {
        if (!mapping.containsKey(node)) {
            mapping.put(node, new ArrayList<>());
        }
        if (!mapping.containsKey(p)) {
            mapping.put(p, new ArrayList<>());
        }
        if (node != null) {
            mapping.get(p).add(node);
            mapping.get(node).add(p);
            if (node.left != null) {
                buildAdjList(mapping, node.left, node);
            }
            if (node.right != null) {
                buildAdjList(mapping, node.right, node);
            }
        }
    }

- aonecoding August 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below code can be replaced

for (TreeNode node: mapping.keySet()) {
            if (node != null && node == target) {
                queue.add(node);
                visited.add(node);
                break;
            }
        }

With

queue.add(target);
visited.add(target);

- Krishna September 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class MyNode {
    constructor( node, prev ) {
        this.node = node;
        this.prev = prev;
        this.next = null;
    }
}
class NodeList {
    constructor() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }
    push( node ) {
        if( this.tail ) {
            this.tail = this.tail.next = new MyNode(node,this.tail);
        }
        else {
            this.tail = this.head = new MyNode(node,null);
        }
        this.size++;
    }
    pop() {
        if( this.tail ) {
            this.tail = this.tail.prev;
            if( !this.tail ) {
                this.head = null;
            }
            this.size--;
        }
    }
    clone() {
        // TODO
    }
    count_common_nodes( list ) {
        let result = 0;
        let list_node = list.head;
        for( let mynode=this.head; mynode; mynode=mynode.next ) {
            if( mynode.node!=list_node.node )
                break;
            result++;
            list_node = list_node.next;
            if( !list_node )
                break;
        }
        return result;
    }
}
class Context {
   constructor( target ) {
      this.target = target;
      this.path_to_target = new NodeList();
      this.leaves = new NodeList();
   }
   task( node, path_from_root ) {
       if( !node )
           return;
       path_from_root.push(node);
       if( node==target ) {
           this.path_to_target = path_from_root.clone();
       }
       if( !node.left && !node.right ) {
           this.leaves.push({'node':node,'path_from_root':path_from_root.clone()});
       }
       else {
           this.task(node.left,path_from_root);
           this.task(node.right,path_from_root);
       }
       path_from_root.pop();
   }
}

let context = new Context(target);
context.task(tree,new NodeList()); // This operation is going to take O(n)
if( !context.path_to_target ) {
  throw Error("Target is not found in the tree");
}

let min_distance_to_target = Number.POSITIVE_INFINITY;
let closest_leaf = undefined;
for( let mynode=context.leaves.head; mynode; mynode=mynode.next ) {
  let leaf = mynode.node;
  let size_of_common_prefix = context.path_to_target.count_common_nodes(leaf.path_from_root);
  let distance_to_target    = leaf.path_from_root.size+context.path_to_target.size-2*size_of_common_prefix;
  if( distance_to_target<min_distance_to_target ) {
     min_distance_to_target = distance_to_target;
     closest_leaf = leaf;
  }
}
console.log(closest_leaf.toString());

- fatcat September 23, 2018 | 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