Google Interview Question for Software Engineer / Developers






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

step 1: count the number of nodes in the tree ... in o(n) time.

step2: Make an inorder traversal for N/2 nodes.


time complexity O(n)
please correct me if some thing wrong...

- chandu February 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

if you augment the bst with the number of nodes on the left and right as you insert items into the bst, the median can be looked up in o(logn). The number of nodes can be maintained while insertion and deletion.

- Anonymous March 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one. Naive approach has complexity 2N and this one has complexity logN

- Mahesh April 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops! I dont think this works. What if we have a linked list like BST with all nodes on one side?

How is the median determined?

- Mahesh April 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

but considering you maintain the numbers of nodes on both sides, it you amortize, it's O(n), because you have already do n counter increments, right?

- peng April 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does an AVL tree work here?

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

anyway, the solution is O(n) complexity as we need to go through the whole tree to find how many nodes on left side and how many on the right side.

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

Another solution might be ... try simultaneous inorder and a reverse inorder traversals and do them one node at a time.
Median = point where they meet or avg of points where they cross.

- Aditya July 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess that works!

- gaurav August 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it will be extremely hard to code...
traverse alone needs recursion, doing two recursion at the same time and while the left and right subtrees are in different shapes, will be very difficult.

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

Order Statistics of Tree for Median of a BST

Step 1: Add another ‘size’ variable which is to store the size of the nodes in the sub trees.
Step 2: Create the tree and find the size. When completion of tree, return the size stored at Root, which will be the total number of nodes in the tree.
Step 3: If the total number of nodes is n and it is an odd number, then n/2th number is the median, else (n/2 th number +(n/2+1) th number )/2 is the median
Step 4: Use order statistics to find the n/2 the largest number for odd, n/2 and (n/2+(n/2+1) )/2 for even.
After creating the Tree with size variable, use the following to find the largest number.
public Node KThLargest(int k, Node t)
{
int rsize = 0;

if (t == null)
return null;

// size of right subtree
if (t.Right != null)
rsize = t.Right.Size;

if (k == rsize + 1)
return t;

if (k <= rsize)
return KThLargest(k, t.Right);
else
return KThLargest((k - rsize - 1), t.Left);
}
O(n) is the complexity.

- BVarghese June 20, 2011 | 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