Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

Using DFS in C#

public void maxConsecutiveSequenceOfBTree(BinaryTree<int> tree)
        {
            if (tree == null) return;
            if (!tree.IsVisited)
            {
                Console.WriteLine(tree.Node);
                CollectionMethod(tree.Node);
                tree.IsVisited = true;
                maxConsecutiveSequenceOfBTree(tree.Left);
                maxConsecutiveSequenceOfBTree(tree.Right);
            }
        }

        private List<List<int>> buffer = new List<List<int>>();


        private void CollectionMethod(int num)
        {
            if (buffer.Count == 0)
            {
                buffer.Add(new List<int>(num));
                return;
            }
            else
            {
                foreach (var n in buffer.ToList())
                {
                    if (n.Contains(num - 1) || n.Contains(num + 1))
                    {
                        n.Add(num);
                        return;
                    }

                }
            }
            buffer.Add(new List<int> { num });
        }

        public int getMaxSize()
        {
            int sum = 0;
            foreach(var n in buffer)
            {
                if(sum < n.Count)
                {
                    sum = n.Count;
                }
            }
            return sum;

}

- maksymas March 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C#

public class Main
{
    private int getMax(Node n, int max)
    {
        int lMax = max;
        int rMax = max;
        
        if (n==null)
        return max;
        
        if (n.left!=null)
            lMax = getMax(n.left, (n.value+1 == n.left.value ? max + 1 : max));
        if (n.right!=null)
            rMax = getMax(n.right, (n.value+1 == n.right.value ? max + 1 : max));
        return Math.Max(lMax, rMax);
    }
    
    public Node root{get;set;}

    public Main()
    {
        Node l = new Node(99, null, null);
        Node r = new Node(100, null, null);
        l = new Node(5, l, r);
        r = new Node(4, null, null);
        Node l1 = new Node(68, null, null);
        l = new Node(2, l, r);
        r = new Node(67, l1, null);
        l1 = new Node(1, null,null);
        r = new Node(66, l, r);
        this.root = new Node(90, l1, r);
        
    }
    public int GetMax()
    {
        return getMax(this.root, 0);   
    }
}

public class Node{
 public int value {get; set;}
 public Node left {get; set;}
 public Node right {get; set;}
 
 public Node(int v, Node l, Node r)
 {
  this.value = v;
  this.right = r;
  this.left = l; 
 }
}

- Babaika March 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;

public class MaxConsecutiveNumbersinBinaryTree
{
	public static void main(String args[])
	{
		MaxConsecutiveNumbersinBinaryTree m = new MaxConsecutiveNumbersinBinaryTree();
		TreeNode root = new TreeNode(90);
		root.left = new TreeNode(1);
		root.left.left = new TreeNode(2);
		root.left.left.left = new TreeNode(5);
		root.left.left.right = new TreeNode(4);
		root.left.left.left.left = new TreeNode(99);
		root.left.left.left.right = new TreeNode(100);

		root.right = new TreeNode(66);
		root.right.left = new TreeNode(67);
		root.right.left.left = new TreeNode(68);

		Set<List<Integer>> list = new HashSet<>();

		m.dfs(root, null, new ArrayList<Integer>(), list);

		System.out.println(list);

	}

	public void dfs(TreeNode root, TreeNode parent, List<Integer> l, Set<List<Integer>> list)
	{
		if(root == null)
		{
			list.add(l);
			return;
		}

		if(parent==null)
		{
			l.add(root.val);
		}
		else if(Math.abs(root.val - parent.val)==1)
		{
			l.add(root.val);
		}
		else if(Math.abs(root.val - parent.val)!=1)
		{
			list.add(l);
			l = new ArrayList<>();
			l.add(root.val);
		}
		dfs(root.left, root, l, list);
		dfs(root.right, root, l, list);
	}
}

class TreeNode
{
	TreeNode left;
	TreeNode right;

	int val;

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

- noob October 09, 2017 | 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