Flipkart Interview Question for Software Engineer / Developers






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

maximum(node n):
  if n==null:
    return INTEGER.MINIMUM
  return max(n->data, maximum(n->left), maximum(n->right)

- Anonymous December 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

looks perfect!!

- rhino December 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

right !
simple and sleek

- shoonya.mohit July 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Sorry !
didn't read the question properly...

- shoonya.mohit July 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

if its a BST, then we can move to the rightmost element and find the max value...otherwise i guess it takes O(n) complexity to find the max value in a binary tree(simply traverse the entire tree and find the max value)

- T December 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(log n) to find the max element in BST !
remember you are skipping the left nodes so it is not O(n).

- Sunil January 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the code I had written

node *max=root; //make it global;
findmax(root);
printf("Maximum val:%d",max->value);

void findmax(node *cur)
{
 if(cur->val>max->val)
   max=cur;
 findmax(cur->left);
 findmax(cur->right);
}

- Vaishnavi December 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Missed one more condition

void findmax(node *cur)
{
if(cur)
{
if(cur->val>max->val)
max=cur;
findmax(cur->left);
findmax(cur->right);
}
}

- Vaishnavi December 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about if we kept a seperate list which contains the max values. before you add or remove an element from the tree compare it to the first element on the list.

- caferrick December 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think it will work when you are removing the element. In case the element to be deleted is the max itself you need to find NEW_MAX again taking O(N)

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since it is binary tree (and not binary search tree), pick you favourite traversal, and modify it to keep track of max etc.

- Anonymous January 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int max(struct node* node) {
if(node==NULL) return -1;
int max=node->data, temp=-1;
if(node->left != NULL) { if(max < (temp=max(node->left))) max=temp;}
if(node->right != NULL) { if(max < (temp=max(node->right))) max=temp;}
return max; }

- Naga Samrat Chowdary, Narla May 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int findMax(Node start){
if(start == null)
return 0 ;
if(start.left == null && start.right == null)
return start.value;
if(start.left == null)
return Max(start.value , findMax(start.right));
if(start.right == null)
return Max(start.value , findMax(start.left));
return Max(start.value , Max(findMax(start.left), findMax(start.right)));

}


static int Max(int val1 , int val2){
if(val1 > val2)
return val1 ;
return val2;
}

- soumitra April 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findmax (Node *root, int *maxm)
{
if (root)
{
findmax(root->left, maxm);
*maxm = max(root->data, *maxm);
findmax(root->right, maxm);
}
}

- A August 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I ain't sure but Java doesn't have pointers, how do you plan to build a Binary tree in java?

- Mohit December 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

java has references that can be used for this

- anon December 05, 2009 | Flag


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