Coding Interview Question: Median of Arrays

Question

Find the median of two sorted arrays.

eg.

1

2

3

4

arr1=[1,3,5]

arr2=[2,4,6]

median(arr1,arr2)=3.5

Once you think that you’ve solved the problem, click below to see the solution.

As always, remember that practicing coding interview questions is as much about how you practice as the question itself. Make sure that you give the question a solid go before skipping to the solution. Ideally if you have time, write out the solution first by hand and then only type it into your computer to verify your work once you've verified it manually. To learn more about how to practice, check out this blog post.

Solution

How was that problem? You can check out the solution in the video below.

Post navigation

joel

Given an array containing integers, zero is considered an invalid number and rest all other numbers are valid. If two nearest valid numbers are equal then double the value of the first one and make the second number as 0.At last move all the valid numbers on the left.

// Now all non-zero elements have been shifted to
// front and 'count' is set as index of first 0.
// Make all elements 0 from count to end.
while (count < n)
a[count++] = 0;
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}

Here in the else loop why cant we write i++ instead of i=j-1;
please help

Search for:

Dynamic programming crash course for non-geniuses

Download my free guide to learn:

How to finally “get” what Dynamic Programming really is – no Ph.D required

The not-so-obvious way you can solve any dynamic programming problem fast – and not freeze up during your interview

The only 10% of information you need to know to ace your interview – forget all the useless fluff