Amazon Interview Question for SDE1s


Country: United States




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

Looking for coaching on interview preparation?
Visit AONECODE.COM for ONE-TO-ONE private lessons by FB, Google and Uber engineers!

System Design (for candidates of FB, LinkedIn, AMZ, Google and Uber etc)
Algorithms (DP, Greedy, Graph etc. advanced algorithms and clean coding)
Interview questions sorted by companies
Mock Interviews

Ace G, U, FB, Amazon, LinkedIn, MS and other top-tier interviews in weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

SOLUTION:

import java.util.List;
import java.util.Map;
import java.util.HashMap;
class TreeNode {
    int val;
    List<TreeNode> kids;
}
public class HouseRobTree {
    public int houseRob(TreeNode root) {
        Map<TreeNode, Integer> money_in_tree = new HashMap<>();
        return houseRobHelper(root, money_in_tree);
    }
    
    private int houseRobHelper(TreeNode root, Map<TreeNode, Integer> money_in_tree) {
        if(root == null) return 0;
        if(root.kids == null || root.kids.isEmpty()) return root.val;
        if(money_in_tree.containsKey(root)) return money_in_tree.get(root);
        
        int max = 0;
        for(TreeNode kid: root.kids) {
            max = Math.max(max, houseRobHelper(kid, money_in_tree));
            if(kid.kids != null) {
                for(TreeNode grandKid: kid.kids) {
                    max = Math.max(max, houseRobHelper(grandKid, money_in_tree) + root.val);
                }
            }
        }
        money_in_tree.put(root, max);
        return max;
    }
}

- aonecoding April 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this would be easier... Note: don forget to write code to populate the tree

import java.util.List;

public class RobNTree
{

    static int max;

    static class TreeNode
    {
        int val;
        List<TreeNode> kids;
    }

    public static void main(String[] args)
    {
        max = 0;
        TreeNode root = null;
        // populate tree
        if (root != null)
            traverse(root, 0);

        System.out.println("The max possible loot is " + max);
    }

    private static void traverse(TreeNode node, int sum)
    {
        sum += node.val;
        if (node.kids.isEmpty())
            max = (sum > max) ? sum : max;
        else
            for (int i = 0; i < node.kids.size(); i++)
                traverse(node.kids.get(i), sum);
    }

}

- PeyarTheriyaa April 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this wont give right answer. Question says two directly linked houses/nodes can't be broken on same night. Meaning we cant add values of 2nodes in sum if they are connected. Node and its kid node is directly connected.

- thriver April 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Howdy Mate,

Amazon Interview Question for SDE1s being contrived to exist for many projects simply so it can be run will be the first to hit the wall, but those projects where the functions to make existing transactions cheaper in real world applications will find the elusive real world demand.

I recently attended the AWS Summit London 2018. During the afternoon session about "Open Source at AWS" there were some resources mentioned for persuading your company that open source can be a great benefit to them.
Unfortunately this wasn't one of the slide sets which has been made available after the event.

Follow my new blog if you interested in just tag along me in any social media platforms!

Cheers,
Radhey

- Radhey June 01, 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