Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Just for simplicity assume X =2 , that mean we have 2 vectors.


Case 1: when 2 vectors sorted, we can simply use medians of median algorithm to find medain of combined vector.

Time complexity : O(logn)
Space complexity : O(1)

note: here n = n1+n2 ( n1 1st vector size , and n2 second vector size )
-----------------------------------
Case 2: when 2 vectors not sorted, then we better to use two Heaps (min & max).

1. if n is even, max and min heap sizes n/2 .
2. if n is odd, max heap size n/2 +1 , and min heap size n/2 .

3. add small elements of vector into max heap , simlarly add max elements into min heap.
4. After inserting n elemets into min & max heaps. if n is odd, max element in max heap is the medain.
5. if n is even, get max elm for max heap, and get min elm from min heap, avg of both elms is the median.

==================================================

if X is big number , I mean X > 2 . then use following algoriths

case 1: when all X vectors sorted.

step1: take MIN heap of size X , add first elemets of X vectors , now heap has x elemtns.
Step2: now delete min elemnt from heap, and add next element of vector which contains previously dleted min element.

step 3: repeat step 2 , till we get n/2 th min element. n/2 th element is median

TC = O(n);
SC = O(X) ;// heap size
-----------------------------
Case 2: x vectors are unsorted

We can use selection algorithm. this algorithm bit difficult to explain. you better to refer Coreman book . below I mentioned basic steps.
step1 : this algothm divides n elements into n/5 groups,
step2: find median of each group .
step3: find median of mediands.
step4: do some elemts reduction based on current median value.
step5: becuase of reduction , we can find median in O(n) TC.

- siva.sai.2020 April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the algorithm in the Coreman book is better suited for sorted vectors. The first step in that algorithm is to sort the groups of 5 and get the middle element

- yossee April 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The algo is called median of medians and is there on wiki. The size of the group need not necessarily be 5. However, in the recursive passes, it might be better to do it with odd numbered groups of elements

- yossee April 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

This question makes no sense. Read about median on Wikipedia. It say its defined only on 1D data/scalar. I don't know what it means to find median of a vector

- axecapone July 08, 2012 | 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