Amazon Interview Question


Country: United States
Interview Type: In-Person




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

1. Create a BST from the numbers. Print the N/2 node of the BST.

2. Create a min-heap of size N/2. Populate with first N/2 numbers.For each new number, if it is larger than the head value, replace & update the heap. At the end, head will indicate the N/2 node.

- Aashish July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I like the second one.

- Daru July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. creating a BST and then printing the N/2 nodes similar to doing sorting (i.e. inorder traversal of the nodes) to figure out the median. But the question requires no sorting.
So to me this looks like a sol but not what the qus demands exactly. Comments?
2. the second approach seems to work but not for both the cases when n is odd/even. Coz according to this we are returning a single median which is the root of min-heap.

The better/complete solution would be to use 2 heap 1 max-heap containing the lowest n/2 elements and the other min-heap containing the rest elements. These lists can differ in size by +-1 factor at the end.

But the approaches are definately in the rite direciton.

- mr July 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How you are getting the N ?

I thought its a open-ended problem ..

- Ani September 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

create a max heap and min heap.. delete each element from root of each heap one by one...
when both the elements are equal .. it is the median..

- cobra July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do U think Ur soln is O(NLogN), which is the expection.

- SRB July 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This approach is O(n^2Log(n))

- codemonkeyusa July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, somehow I feel this is an (Nlog N) solution.
For building each heap( min and max) u require (Nlog N) time, so 2Nlog N is equivalent to Nlog N.

Now you are deleting an element from each heap, which is a log N operation . This delete operation is performed n/2 times for each heap , so that again sums up to Nlog N.

so what we have is 2(N log N) + (N/2)log N + (N/2)log N ...which is Nlog N.

- perlscar July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesn't the heap creation take O(n) time? Extracting min from the min-heap roughly n/2 times can give the n/2-th smallest (as root) number which is the median, almost like the heap sort.

- tanvir.zaman@csebuet.org July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn't work when the size of input data is even. In this case, median is the average of the two middle items.

- Aashish July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

one don't need (n log n) time to calculate the complexity, it only require o(n). Here are two ways.
1) use optimize version of partition methods used in quicksort.
2) divide the array into n/5 small arrays each of size at max 5, calculate median of each of them, store it into a another array . this new array size is n/5. now we calculate median of this array too. lets suppose i calculated median of this, then i divide my array based upon this median of median array. in my division the largest array is of size at max 7n/10. and smallest array is of size at least 3n/10.
so the time complexity equations is :
T(n) = T(7n/10) + T(n/5) + 0(n)
this equation give 0(n) solution.
ya but space requirement is 0(n) too.

- sonesh October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hints: Quick Sort Approach, i.e. select pivot num.
Sol:
1. Take a random pivot number from the list.
2. Put the pivot number to it's exact location in the list. T(N) = O(N)
3. If position of the pivot num is the middle of list then retun the pivot as median.
else if position of the pivot num id less than middle num position then select a random pivot number from the right group and goto setp 2(above).
else select a random pivot number from the left group and goto setp 2(above).

The complexity of above approach depends on how pivot choosen. So its comlexity will be less than (N*LogN) or it will above (N*LogN).

- SRB July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but worst case complexity of your solution is O(n^2)

- Anonymous July 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Learn Quick sort, then complain about the worst performance.

- florin314 August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is called selection algorithm

- Vincent August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

No sort operation should be performed. What does that mean exactly?

- Anonymous July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no sorting means that no sorting should be done on the input array because then its straight forward.
What the previous approach is depicting is a modifaction of partition to find the ith order statistics by traversing only a part of the array depending on the index of the pivot found. Hence it doesn't sort the array rather just uses it to recurse either to the left or the right of the array.

- mr July 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can I sort part of the array? Like say sqrt(n) elements?

Or split into two halves and sort each separately?

Can I sort two numbers? (i.e. just compare them?)

This is a very ambiguous and pointless constraint.

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

Is it Middle elemnet in the array or (max-min)/2

- Arul July 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't understand. Median could be found in O(n). Why O(nlgn)?

- LoocalVinci September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

tag

- jiangok2006 September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Find the sum of all elements in one scanning of the array.
Then do sum/no of elements
Then in the second scan, check which element has the least difference with this sum. Then that element is the median.
Solution obtained in 2n
But i am doubtful if this is right. Can some one comment

- Mani July 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is completely wrong. It just gives the number closest to mean

- Mani July 28, 2012 | 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