Amazon Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

private static boolean isThereSum(Node root, int sum) {
		return sum(root.left, root.data, sum) || sum(root.right, root.data, sum);
	}

	private static boolean sum(Node node, int data, int sum) {
		sum = sum - data;
		if (node == null)
			return sum == 0;

		return sum(node.left, node.data, sum) || sum(node.right, node.data, sum);
	}

- Mikhail August 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node:
    def __init__(self):
        self.left = None
        self.right = None
        self.val = 0


def hasSum(root, target_sum):
    _, found = getSum(root, target_sum)
    return found


def getSum(root, target_sum):
    if root is None:
        return 0, False

    left_sum, found = getSum(root.left, target_sum)
    if found:
        return 0, found

    right_sum, found = getSum(root.right, target_sum)
    if found:
        return 0, found

    s = root.val + left_sum + right_sum
    found = bool(s == target_sum)
    return s, found

- Alex July 20, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean hasPathSum(Node node, int sum) {
	// recursion base case is when the node is empty (aka null)
	if(node == null) {
		return node.data == 0;
	}
	sum -= node.data;
	return hasPathSum(node.left, sum) || hasPathSum(node.right, sum); 
}

- Anonymous November 30, 2019 | 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