Amazon Interview Question for Software Engineers

Country: United States
Interview Type: In-Person

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

class Node:
    def __init__(self, ID, parent):
        self.ID = ID
        self.parentID = parent
        self.left = None
        self.right = None

#function to identify if given is preorder
Create a stack to store nodes in the current path when traversing.
Push node[i] into stack once node[i] is verified to be valid (valid only when parent of node[i] is in stack. 
In preorder a parent must show up earlier than its child)
Whenever stack top is not the parent of node[i], pop until parent of node[i] is at stack top. Push node[i].
If all nodes popped but parent of node[i] still not found, then node[i] is not in preorder sequence.
def isPreorder(nodes):
    if not nodes:
        return True
    st = [nodes[0].ID]
    i = 1
    while i < len(nodes):
        if not st:
            return False
        if st[-1] is nodes[i].parentID:
            i += 1
    return True

Looking for interview experience sharing and mentors?
Visit A++ Coding Bootcamp at

Taught by experienced engineers/interviewers from FB, Google and Uber,
our ONE TO ONE courses cover everything in an interview including
latest interview questions sorted by companies,
SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG)
ALGORITHMS (conquer DP, Graph, Greed and other advanced algo problems),
and mock interviews.

Our students got offers from Google, Uber, Facebook, Amazon and other top companies after a few weeks of training.

Welcome to email us with any questions. Thanks for reading.

- acoding167 May 07, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 votes

@acoding, suppose the input is : ( parent_id, node_id) format :

( null, 0 ), ( null, 2 ) , ( null, 4 ) ,   (0,1) , ( 2,3), ( 4,5)

what would happen? Of course we are giving wrong input -- but..

- NoOne May 08, 2019 | Flag
Comment hidden because of low score. Click to expand.
of 1 vote

Solution in java, memory is constant and time is linear (amortized)

public class Solution {

    // Node class
    private class Node {
        int ID;
        Node parent;

    public boolean isPreOrder(List<Node> nodes) {

        // Assume that empty list represents correct tree
        if (nodes.size() == 0) {
            return true;

        Node prev = nodes.get(0);

        // Root has no parent otherwise not pre-order
        if (prev.parent != null) {
            return false;

        for (int i = 1; i < nodes.size(); i++) {
            Node current = nodes.get(i);

            // There is only one root node
            if (current.parent == null) {
                return false;

            // Find parent in current list of actively exploring
            while (prev != null && current.parent.ID != prev.ID) {
                prev = prev.parent;

            // It means that we have not visited such element or we visited but we already marked subtree as done
            if (prev == null) {
                return false;

            // Explore current's subtree
            prev = current;

    return true;

- andy May 13, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
of 1 vote

I think the keep it simple, stupid works better here.
1. Construct a graph from the list ( it is a type of adj list anyways )
2. Check if the graph is a tree first.
2.1 In that case, there must be no cycle
2.2 and one clear node with null parent.
3. Do a pre-order traversal on that "tree" and check at any point if there is any discrepancy
from the already given traversal.

- NoOne May 08, 2019 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


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


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