What is the median of two sorted arrays?

In this article we will find out how to solve the common coding interview problem:‘What is the median of two sorted arrays?’ This guide explains how to efficiently find the median in sorted datasets, an important skill for jobs in software engineering and data analysis. Whether you’re getting ready for an interview or improving your programming skills, this article gives you the knowledge to handle this problem. Learn about data manipulation and analysis, and understand how to quickly work with data - a key skill in today’s tech industry.

Problem Statement

Given two sorted arrays nums1 and nums2 of size m and n respectively, find the median of the two sorted arrays. The overall runtime complexity should be O(log(m+n)).

Examples

  • Input: nums1 = [1, 3], nums2 = [2]
    Output: 2.0
    Explanation: The arrays merge into [1, 2, 3] and the middle value is 2.0.

  • Input: nums1 = [1, 2], nums2 = [3, 4]
    Output: 2.5
    Explanation: The arrays merge into [1, 2, 3, 4] and the median is the average of the two middle numbers (2 and 3) which is 2.5._

Constraints

  • nums1 and nums2 cannot be both empty.
  • The total number of elements in both arrays will not exceed 2,000.

Approaches to Finding the Median of Two Sorted Arrays

Direct Merge and Find Method

Method: Merge the two sorted arrays into one and then calculate the median from the combined array.

Python3
Python3

. . . .

Explanation: This method involves combining both arrays into a single sorted array from which the median can be directly calculated. It’s a straightforward approach but might not be the most efficient for large arrays due to its reliance on sorting.

Time Complexity: O((m + n) log (m + n)), due to the sorting step involved in merging the arrays.

Space Complexity: O(m + n), as additional space is required to store the merged array.

Binary Search Approach

Python3
Python3

. . . .

Method: Use binary search to find the median without fully merging the arrays.

Explanation: This approach leverages binary search techniques to locate the median by considering elements from both arrays without merging them. It's particularly effective because it significantly reduces the time complexity by avoiding the merge step, making it scalable for larger datasets.

Time Complexity: O(log(min(m, n))), since the search is conducted on the shorter of the two arrays.

Space Complexity: O(1), as it does not require additional space beyond the input arrays. n text here.

Application

Finding the median of two sorted arrays is essential for statistical analysis in data science, real-time data monitoring, and other applications where it is crucial to quickly determine the central tendency of combined data sets.

Conclusion

Mastering the techniques to efficiently find the median of two sorted arrays is vital for coding interviews and practical software development, particularly for applications that involve large data sets and require high-performance solutions.

TAGS
Coding Interview Questions
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Which company has the hardest coding interview?
1768. Merge Strings Alternately - Detailed Explanation
A two pointer walk that interleaves word1 and word2 one character at a time, then appends the tail of the longer string.
72. Edit Distance - Detailed Explanation
How a bottom-up table over the prefixes of two strings turns insert, delete, and replace into a single minimum per cell.
338. Counting Bits - Detailed Explanation
Count the set bits of every number from 0 to n in linear time, by reusing the count of a number you have already solved rather than counting bits again.
What to expect in a 1 hour coding interview?
875. Koko Eating Bananas - Detailed Explanation
How to answer Koko Eating Bananas by binary searching the eating speed itself instead of scanning every candidate rate.
Related Courses
New
Grokking Agentic Design Patterns course cover
Grokking Agentic Design Patterns
Design agents that act safely, recover from failure, and justify their cost.
4.9
(561 learners)
Discounted price for Your Region

$98

New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$98

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.