Amazon Interview Question for SDE1s


Country: United States




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

C# implementation

class BrowseNodeTree
        {
            private Dictionary<int, Node> _items = new Dictionary<int, Node>();

            public BrowseNodeTree()
            {
                /** Read data from file here **/
                /** Sample **/
                List<NodeData> DataReadFromFile = new List<NodeData>()
                {
                    new NodeData() { nodeid = 10, parent_node_id= 1, nodename="Comedy Books" },
                    new NodeData() { nodeid = 20, parent_node_id= 2, nodename="Tablets" },
                    new NodeData() { nodeid = 1, parent_node_id= -1, nodename="Books" },
                    new NodeData() { nodeid = 11, parent_node_id= 1, nodename="Novels" },
                    new NodeData() { nodeid = 12, parent_node_id= 11, nodename="Terror Novels" },
                    new NodeData() { nodeid = 2, parent_node_id= -1, nodename="Electronics" },
                    new NodeData() { nodeid = -1, parent_node_id= 0, nodename="GlobalRoot" }
                };

                foreach(NodeData nd in DataReadFromFile)
                {
                    _items.Add(nd.nodeid, new Node() { NodeID = nd.nodeid, NodeName = nd.nodename, Children =  new Lazy<List<int>>()  });
                }

                foreach(NodeData nd in DataReadFromFile)
                {
                    _items[nd.parent_node_id].Children.Value.Add(nd.nodeid);
                }
            }

            public Node GetRoot()
            {
                Node root;
                _items.TryGetValue(-1, out root);
                return root;
            }

            public List<Node> GetChildren(Node node)
            {
                List<Node> result = new List<Node>();
                Node NodeData;
                _items.TryGetValue(node.NodeID, out NodeData);
                if (NodeData.Children.IsValueCreated) {
                    foreach (int ChildID in NodeData.Children.Value)
                    {
                        Node ChildNode;
                        _items.TryGetValue(ChildID, out ChildNode);
                        result.Add(ChildNode);
                    }
                }
                return result;
            }
        }

- PayamRad February 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could you explain why you used

new Lazy<List<int>>()

also the time complexities of both the methods?

- pragramticProgrammer March 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