Amazon Interview Question for Software Engineers


Country: United States




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

Basically you need to find two nodes which are same to start with. And then call the isDuplicate(root1, root2) method on both. This exhaustive traversal is worst case matching of each node with each node and then isIdentical is O(n). Hence the overall complexity is O(n3)
Now any two subtrees can be duplicate only if one is not ancestor of other.

- Yoda May 05, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;

namespace test
{
    public class TreeNode
    {
        public int val { get; set; }
        public TreeNode left { get; set; }
        public TreeNode right { get; set; }
        public TreeNode(int x)
        {
            val = x;
        }
    }

    
    public class FindDupNodes
    { 
        public FindDupNodes()
        {
            List<TreeNode> tree = new List<TreeNode>();
            tree = ConstructTree();

            TreeNode node = FindMaxDups(tree);
            Console.WriteLine($"{node.val} -- {node?.left?.val} -- {node?.right?.val}");


        }
        public List<TreeNode> ConstructTree()
        {
            List<TreeNode> nodes = new List<TreeNode>();
            TreeNode t1 = new TreeNode(1);
            TreeNode t2 = new TreeNode(2);
            TreeNode t3 = new TreeNode(3);
            TreeNode t4 = new TreeNode(4);

            t1.left = t2; t1.right = t3;
            t2.left = t4;
            t3.left = t2;
            t3.right = t4;

            nodes.Add(t1);
            nodes.Add(t2);
            nodes.Add(t3);
            nodes.Add(t4);
            return nodes;
        }
        
        public TreeNode FindMaxDups(List<TreeNode> tree)
        {
            //get the root
            TreeNode rootNode = tree.Where(t => t.val == 1).FirstOrDefault();
            //iterate over in recursion and see which one appears more than once
            IterateTree(rootNode, tree);
            int maxValue = nodeCounts.Max(n => n.Value); //.Select(nd => nd.Key);
            List<TreeNode> maxNodes = nodeCounts.Where(n => n.Value == maxValue).Select(n => n.Key).ToList();
            return maxNodes.FirstOrDefault();
        }

        public Dictionary<TreeNode, int> nodeCounts = new Dictionary<TreeNode, int>();
        public void IterateTree(TreeNode currentNode, List<TreeNode> tree)
        {
            if (currentNode == null)
                return;
           
            if (nodeCounts.ContainsKey(currentNode))
                nodeCounts[currentNode] = nodeCounts[currentNode] + 1;
            else
                nodeCounts.Add(currentNode, 1);

            if (currentNode.left == null && currentNode.right == null)
                return;

            IterateTree(currentNode.left, tree);
            IterateTree(currentNode.right, tree);

        }
    }
}

- GeekLion May 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
class Main {

  private static String max="";
  public static void main(String[] args) {
    System.out.println("Hello world!");
    TreeNode root = new TreeNode(1);
    root.left = new TreeNode(2);
    root.left.left = new TreeNode(4);
    root.left.right = new TreeNode(6);
    root.right = new TreeNode(10);
    root.right.left = new TreeNode(2);
    root.right.left.left = new TreeNode(4);
    root.right.left.right = new TreeNode(6);
    getRepeatTree(root);
  }

  
  private static TreeNode getRepeatTree(TreeNode root)
  {
    Map<String,TreeNode> map = new HashMap<>();
    createMap(map,root);
    return map.get(max);
  }

  private static String createMap(Map<String,TreeNode> map,TreeNode root)
  {
    if(root==null)
      return "";
    String left = createMap(map,root.left);
    String right = createMap(map,root.right);
    String curr = left+right+root.val;
    if(map.containsKey(curr))
    {
      if(max.length()<curr.length())  
      {
        max=curr;
      } 
    }
    else
    {
      map.put(curr,root);
    }
    return curr;

  }

  
}
class TreeNode
  {
    TreeNode left,right;
    int val;
    TreeNode(int val)
    {
      this.val=val;
    }
  }

- parth June 02, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static TreeNode getRepeatTree(TreeNode root)
  {
    Map<String,TreeNode> map = new HashMap<>();
    createMap(map,root);
    return map.get(max);
  }

  private static String createMap(Map<String,TreeNode> map,TreeNode root)
  {
    if(root==null)
      return "";
    String left = createMap(map,root.left);
    String right = createMap(map,root.right);
    String curr = left+right+root.val;
    if(map.containsKey(curr))
    {
      if(max.length()<curr.length())  
      {
        max=curr;
      } 
    }
    else
    {
      map.put(curr,root);
    }
    return curr;

  }

- parth81091 June 02, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

static class TreeNode {
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((children == null) ? 0 : children.hashCode());
			result = prime * result + data;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			TreeNode other = (TreeNode) obj;
			if (children == null) {
				if (other.children != null)
					return false;
			} else if (!children.equals(other.children))
				return false;
			if (data != other.data)
				return false;
			return true;
		}

		TreeNode(int data) {
			this.data = data;
		}

		@Override
		public String toString() {
			return this.data + "";
		}

		int data;
		List<TreeNode> children;

		public int getData() {
			return data;
		}

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

		public List<TreeNode> getChildren() {
			return children;
		}

		public void setChildren(List<TreeNode> children) {
			this.children = children;
		}
	}


	public static void main(String args[]) throws Exception {
          Node root=new Node(2);
          root.left=new Node(1);
          root.right=new Node(1);
          List<TreeNode> temp=findDuplicateSubtrees(root);
	}

	public static List<TreeNode> findDuplicateSubtrees(Node root) {
		String inOrder = inOrderTraversal(root);
		String preOrder = preOrderTraversal(root);
		List<TreeNode> list = new ArrayList<TreeNode>();
		for (int i = 0; i < inOrder.length(); i++) {
			for (int j = i; j < inOrder.length(); j++) {
				String subStr = inOrder.substring(i, j);
				if (inOrder.indexOf(subStr)  != inOrder.lastIndexOf(subStr)
				 && preOrder.indexOf(subStr) != preOrder.lastIndexOf(subStr) && subStr.length()>0) {
					list.add(new TreeNode(Character.getNumericValue(subStr.charAt(0)))) ; 
				}
			}
		}
		return list;
	}

	private static String preOrderTraversal(Node root) {
		if (root == null)
			return "";
		return root.data + preOrderTraversal(root.left) + preOrderTraversal(root.right);
	}

	private static String inOrderTraversal(Node root) {
		if (root == null)
			return "";
		return inOrderTraversal(root.left) + root.data + inOrderTraversal(root.right);
	}

- koustav.adorable May 06, 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