Flipkart Interview Question for SDE1s


Country: India
Interview Type: Phone Interview




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

I'm going to assume that your tree is supposed to be the following because I can't read yours:

_________1
_______/___\
_____2______3
___/___\____/__\
__4____5__6___7
_/__\___/______/
8___9_10_____11
___/
__12

public static void printBottom(TreeNode node){
    Worker worker = new Worker(node);
    worker.execute();
    System.out.println(worker.getResults());
}

private static class Worker{
    TreeNode head;
    StringBuilder resultHolder;
    
    private Worker(TreeNode head){
        this.head = head;
    }

    private void execute(){
        this.resultHolder = new StringBuilder();
        this.executeRecur(this.head);
    }

    private void executeRecur(TreeNode node){
        if(node.left != null){
            printBottom(node.left);
        }
        if(node.left == null || node.right == null){
            this.resultHolder.append(node.value);
            this.resultHolder.append(' ');
        }
        if(node.right != null){
            printBottom(node.right);
        }
    }

    private String getResults(){
        if(this.resultHolder != null){
            return this.resultHolder.toString();
        }
        return null;
    }
}

- zortlord May 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is a standard DFS traversal and print the leaf nodes that do not have any children.

- vj May 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is a standard DFS traversal of the tree and print the nodes that do not have any child nodes.

- VJ May 20, 2015 | 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