## 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)
{
return;
}
else
{
foreach (var n in buffer.ToList())
{
if (n.Contains(num - 1) || n.Contains(num + 1))
{
return;
}

}
}
}

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

}

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;
}
}``````

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)
{
return;
}

if(parent==null)
{
}
else if(Math.abs(root.val - parent.val)==1)
{
}
else if(Math.abs(root.val - parent.val)!=1)
{
l = new ArrayList<>();
}
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;
}
}``````

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.

### 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.