Challenge: Find the Median of Two Sorted Arrays

Let's write a function to find the median of two sorted arrays that can be the same or of variable length.

Problem statement

Implement a function that takes two sorted arrays, which may have different lengths and finds the median of the two arrays.

💡 Do you know?

The median is the central value of a Set Sorted in ascending order. If there are an even number of items in the set, then the median is equal to the mean (average) of the 2 middle-most values.


Two sorted arrays.


The median of two arrays.

Sample input

A = {100};
B = {1, 5, 8, 10, 20}

Sample output

9.0                 // Take mean of 2 central values
                    // (8+10)/2.0 = 18/2.0 = 9.0

Coding exercise

Take a close look and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the hint and solution provided in the code tab. Good Luck!

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.