Interview Question for Software Engineer / Developers






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

get the height of the tree , h, and max width of tree will be (2^h) -1.

public int height(Node root){
if(root.val != NULL)
return 1+max(height(root.getLeft()), height(root.getRight()) ;
return 0;

}

public int findWidth(Node root){
int h = height(root);
return ((2^h)-1);

}

- tito May 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry, there is a correction. New findWidth function is :
public int findWidth(Node root){
int h = height(root);
if(h!=0)
return (2^(h-1) );
return 0;

}

- Anonymous May 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

C'mon dude, at least step through your code by hand on <i>the example given in the OP</i>.

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

Traverse the tree; Use an extra int array to count width of each level;
Finally find the largest width.
Is there a way without extra space usage?

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

step 1: find the height of the tree
step 2: do the level order traversal and keep a count of number of nodes at each level
step 3: print the max node count after whole traversal tree is over

- dns May 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this would mean you are visiting the tree twice, once for the height and then you will be touching each node one more time. Is there a better way to do it?

- Anonymous May 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why does one need to calculate height?
in level order traversal , maintain an array L where L[i] represents the no of nodes at level i.

- A May 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Should we use Breadth First Search(BFS) for this case with little modification? In this case, we will visit each node exactly once.

- Deepak May 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can, but we will have to remember "level" of each node that you place in the queue.

- Larrr May 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Enqueue root node
set count=1
while queue not empty
do
tmp_cnt =0
while I<count
   Do
     Dequeue node
     I++
     Increment tmp_cnt
     Enqueue the child nodes
  Done
if tmp_cnt > max_cnt 
   Max_cnt = tmp_cnt
count = tmp_cnt
done

return max_cnt

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

Use BFS, keep track of number of nodes on each level

1. set maximum = 1
2. Push(1, Root node)
3. Pop(number of nodes on next level, node list)
4. Sum up the number of child nodes for each node in the node list.
5. if sum > maximum, set maximum to sum
6. Push(sum, child nodes)
7. Go to 3.

- BrokenGlass May 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a struct NL that stores a node pointer and a level (int)
Use a queue of NLs

Struct NL {
Node* n, int level }

Int MaxWidth (Node* root)
{
Queue q = new queue();
NL nl1 { n=root, level =1)
NL prev, curr;
q.enqueue(nl1);
Int currcount, MAX;
MAX = 0;

While (!q.isEmpty())
{
Curr = q.dequeue();
If (prev!=null && curr.level == prev.level){
currcount ++;
}
Else
{
Currcount = 1;
}
If (currcount > MAX) MAX = currcount;
If (curr.n->l)
{
NL nl2 { n = curr.n->l, n.level+1 };
q.enqueue(nl2);
}
If (curr.n->r)
{
NL nl2 { n = curr.n->r, n.level+1 };
q.enqueue(nl2);
}
Prev = curr;
}
Return MAX;
}

- Rohit July 11, 2010 | 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