## Amazon Interview Question for SDE-2s

• 0

Country: United States
Interview Type: In-Person

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

I assume it can be any parent - child pair. I.e. for every node we find max diff (in value) with all of the nodes forming the subtree of the node.
My idea is to find the min and max values of the node's children. Max diff should be produced by either min or max of the children and the node's value.

``````#include <iostream>

using namespace std;

class Node {
public:
Node(int val)
{
val_ = val;
left_ = right_ = NULL;
}
int val_;
Node *left_, *right_;
};

pair<int, int> MaxDiff(Node *n, int &max_diff)
{
if (!n) {
return pair<int, int>(numeric_limits<int>::max(), numeric_limits<int>::min());
}
auto min_max_l = MaxDiff(n->left_, max_diff);
auto min_max_r = MaxDiff(n->right_, max_diff);
int children_min = min(min_max_l.first, min_max_r.first);
int children_max = max(min_max_l.second, min_max_r.second);
if (children_min != numeric_limits<int>::max()) {
max_diff = max(max_diff, abs(n->val_ - children_min));
}
if (children_max != numeric_limits<int>::min()) {
max_diff = max(max_diff, abs(n->val_ - children_max));
}
return pair<int, int>(min(n->val_, children_min), max(n->val_, children_max));
}

int main()
{
/*
(1)
/    \
(2)    (5)
/\     /\
(3)(4) (6)(7)
\
(8)
*/
Node n1(1), n2(2), n3(3), n4(4), n5(5), n6(6), n7(7), n8(8);
n1.left_ = &n2;
n1.right_ = &n5;
n2.left_ = &n3;
n2.right_ = &n4;
n5.left_ = &n6;
n5.right_ = &n7;
n6.right_ = &n8;

int max_diff = 0;
MaxDiff(&n1, max_diff);
cout << max_diff << "\n";

return 0;
}``````

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

The question isn't clear - Assuming
1. We are talking about node values, rather than length.
2. Parent is fixed
3. Find the absolute max difference

So, I would use level order traversal of the tree (Similar to BFS) and for every new node evaluate the difference of node with its parent (node.value - parent.value) and check update the max difference until the subtree is exhausted

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

``````import java.util.Set;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Comparator;

public class NodeMaxDifference
{
PriorityQueue<TreeNode> maxq = null;
PriorityQueue<TreeNode> minq = null;
int diff = Integer.MIN_VALUE;

public NodeMaxDifference()
{
this.maxq = new PriorityQueue<>(10, new Comparator<TreeNode>(){
public int compare(TreeNode a, TreeNode b)
{
return Integer.compare(b.val, a.val);
}
});

this.minq = new PriorityQueue<>(10, new Comparator<TreeNode>(){
public int compare(TreeNode a, TreeNode b)
{
return Integer.compare(a.val, b.val);
}
});
}

public static void main(String[] args)
{
NodeMaxDifference n = new NodeMaxDifference();

TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);

root.left.left = new TreeNode(9);
root.right.left = new TreeNode(7);

n.dfs(root);

System.out.println(n.diff);
}

public void dfs(TreeNode node)
{
helper(node);
}

private void helper(TreeNode root)
{
if(root==null)
return;

maxq.offer(root);
minq.offer(root);

if(maxq.peek().val - minq.peek().val > diff)
diff = maxq.peek().val - minq.peek().val;

helper(root.left);
helper(root.right);

maxq.remove(root);
minq.remove(root);

}
}

class TreeNode
{
int val;
TreeNode left;
TreeNode right;

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

@Override
public boolean equals(Object o)
{
if(o instanceof TreeNode)
{
TreeNode temp = (TreeNode)o;
if(this==null && temp == null)
return true;
if(this==null || temp== null)
return false;
return this.val == temp.val;
}
else
{
return false;
}
}

@Override
public int hashCode()
{
return (this.val+"").hashCode();
}

public String toString()
{
return this.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.