Boomerang Commerce Interview Question for Developer Program Engineers


Country: India
Interview Type: In-Person




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

left view of a binary tree is the nodes which will be visible when tree is visited from left side. Its basically the first node (or leftmost node) at each level of the tree

#include<stdio.h>
	#include<stdlib.h> 
	struct node{
	    int data;
	    struct node *left, *right;
	};
 
	// A utility function to create a new Binary Tree node
	struct node *newNode(int item){
	    struct node *temp =  (struct node *)malloc(sizeof(struct node));
	    temp->data = item;
	    temp->left = temp->right = NULL;
	    return temp;
	}
 
	// Recursive function to print left view of a binary tree.
	void leftViewUtil(struct node *root, int level, int *max_level){
	    // Base Case
	    if (root==NULL)  return; 
	
	    // If this is the first node of its level
	    if (*max_level < level) {
	        printf("%d\t", root->data);
	        *max_level = level;
	    }

	    // Recur for left and right subtrees
	    leftViewUtil(root->left, level+1, max_level);
	    leftViewUtil(root->right, level+1, max_level);
	}
 
	// A wrapper over leftViewUtil()
	void leftView(struct node *root){
	    int max_level = 0;
	    leftViewUtil(root, 1, &max_level);
	}
	 
	// Driver Program to test above functions
	int main(){
	    struct node *root = newNode(12);
	    root->left = newNode(10);
	    root->right = newNode(30);
	    root->right->left = newNode(25);
	    root->right->right = newNode(40);
	 
	    leftView(root);
	 
	    return 0;
	}

Output 12 10 25

- aayush2k11 February 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you please elaborate the question. What does left view mean ? Example will be better.

- Anonymous February 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Explain the quesion with example plz

- Anonymous February 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def leftView(root, start=True):
     if root == None:
         return
     if start == True:
         print root.data     
     if root.left == None and root.right == None:
         return
     if root.left != None:
         print root.left.data

     leftView(root.left, False)
     leftView(root.right, False)

- TheSighing February 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We just want the left view, so what we can do is keep an array to hold the nodes we encounter in a traversal. We will traverse the right side before the left, ultimately overwriting nodes that are right-most with ones that are left most.

void printLeftSide(Node head) {
		
		Node[] left = new Node[getTreeHeight(head)];
		traverse(head, 0, left);
		
		print(left.toString);

	}

	void traverse(Node curr, int depth, Node[] arr) {

		if (curr == null) { return; }

		traverse(curr.right, depth+1, arr);

		arr[depth] = curr;

		traverse(curr.left, depth+1, arr);
	}

	int getTreeHeight(Node head) {
		
		if (head == null) { return 0; }
		return Max(getTreeHeight(head.left), getTreeHeight(head.right)) + 1;
	}

- Skor February 09, 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