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


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