Amazon Interview Question for SDE-2s


Country: United States




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

{
int data;
struct node *left;
struct node *right;
};

struct node *Node(int data)
{
    struct node *tmp = (struct node *)malloc(sizeof(struct node));
    tmp->data = data;
    tmp->left = tmp->right = NULL;
}

int printlevel(struct node *tree, int level)
{
    if ( tree == NULL)
        return 0;

    if (level == 1)
    {
        printf("%d  ", tree->data);
        return 1;
    }

    int left = printlevel(tree->left,level-1);
    int right = printlevel(tree->right, level-1);

    return ( left || right );
}
int main()
{

    struct node *tree=Node(15);
    tree->left = Node(10);
    tree->right=Node(20);
    tree->left->left=Node(8);
    tree->left->right=Node(12);
    tree->right->left=Node(16);
    tree->right->right=Node(25);

    int level = 1;
    while(printlevel(tree,level))
    level++;

    printf("\n");
    return 0;
}

- prathap July 25, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node:
    def __init__(self, key=None, left=None, right=None):
        self.key = key
        self.left = left
        self.right = right

def printLevel(root, level):
    # base case
    if root is None:
        return False

    if level == 1:
        print(root.key, end=' ')

        return True

    left = printLevel(root.left, level - 1)
    right = printLevel(root.right, level - 1)

    return left or right

def levelOrderTraversal(root):
    level = 1

    while printLevel(root, level):
        level = level + 1


if __name__ == '__main__':
    root = Node(15)
    root.left = Node(10)
    root.right = Node(20)
    root.left.left = Node(8)
    root.left.right = Node(12)
    root.right.left = Node(16)
    root.right.right = Node(25)

    levelOrderTraversal(root)

- Manasa August 09, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node:
    def __init__(self, key=None, left=None, right=None):
        self.key = key
        self.left = left
        self.right = right

def printLevel(root, level):

    if root is None:
        return False

    if level == 1:
        print(root.key, end=' ')
        return True

    left = printLevel(root.left, level - 1)
    right = printLevel(root.right, level - 1)

    return left or right

def levelOrderTraversal(root):
    level = 1
    while printLevel(root, level):
        level = level + 1


if __name__ == '__main__':
    root = Node(15)
    root.left = Node(10)
    root.right = Node(20)
    root.left.left = Node(8)
    root.left.right = Node(12)
    root.right.left = Node(16)
    root.right.right = Node(25)

    levelOrderTraversal(root)

- Manasa August 09, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

level order using dfs
recursive : recursively call left child first and right child next while adding visited nodes to result.
time O(n)
space O(n)
*/

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        
        levelOrderUtil(root, 0, result);
        
        return result;
    }
    
    private static void levelOrderUtil(TreeNode node, int level, List<List<Integer>> result) {
        if (node == null) {
            return;
        }
        
        if (result.size() < level + 1) {
            result.add(new LinkedList<Integer>());
        }
        
        result.get(level).add(node.val);
        
        levelOrderUtil(node.left, level + 1, result);
        levelOrderUtil(node.right, level + 1, result);
        
        return;
    }
}

- Euihoon Seol October 17, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
iterative : use a stack
time O(n)
space O(n)
*/

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        
        levelOrderUtil(root, result);
        
        return result;
    }
    
    private void levelOrderUtil(TreeNode node, List<List<Integer>> result) {
        if (node == null) {
            return;
        }
        
        Stack<Node> s = new Stack<>();
        Node temp = new Node(node, 0);
        s.push(temp);
        
        while (!s.empty()) {
            temp = s.pop();
            
            if (result.size() < temp.level + 1) {
                result.add(new LinkedList<Integer>());
            }
            
            result.get(temp.level).add(temp.tn.val);
            
            if (temp.tn.right != null) {
                s.push(new Node(temp.tn.right, temp.level + 1));
            }
            if (temp.tn.left != null) {
                s.push(new Node(temp.tn.left, temp.level + 1));
            }
        }
        
        return;
    }
    
    class Node {
        TreeNode tn;
        int level;
        
        public Node(TreeNode tn, int level) {
            this.tn = tn;
            this.level = level;
        }
    }
}

- Euihoon Seol October 17, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
levelHelper(res, root, 0);
return res;
}

public void levelHelper(List<List<Integer>> res, TreeNode root, int height) {
if (root == null) return;
if (height >= res.size()) {
res.add(new LinkedList<Integer>());
}
res.get(height).add(root.val);
levelHelper(res, root.left, height+1);
levelHelper(res, root.right, height+1);
}

- somyasaxena23 December 24, 2020 | 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