Asked at 12 companies
11 recent
- Rippling
- Goldman Sachs
- Bloomberg
- Amazon
- Microsoft
- Meta
- tcs
- Zoho
- eBay
+2 moreShow fewer
- Infosys
- Zenefits
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 160 pointsSolved (Hard) 0/70Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Two pointers from opposite ends · When sorting first unlocks the pattern · Make the input smaller every call
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · git bisect: To find the commit that broke a build, git halves the range of commits on every test, so even 1,000 commits take about 10 checks.
ProblemProblem statementGiven two sorted arrays nums1 and nums2, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).ReadHide
Problem Statement
Given two sorted arrays nums1 and nums2, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Added 2026-11-01 from Srujan's LeetCode Recent AC list (photo) — not previously in the repo.
Input / Output
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Input: nums1 = [0,0], nums2 = [0,0]
Output: 0.00000
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Binary Search Partition — the median is the point where the merged array splits in half. Cut both arrays so the left half holds exactly half the elements, with every left element <= every right element. Then the median is just the border values. You binary-search the cut in the shorter array, which is why the time is O(log(min(m, n))).
Approach
Brute Force — Merge and Sort, O((m+n) log(m+n))
merged = sorted(nums1 + nums2)
n = len(merged)
return merged[n // 2] if n is odd else (merged[n // 2 - 1] + merged[n // 2]) / 2
Optimized — Binary Search Partition, O(log(min(m, n)))
## make nums1 the shorter array
## binary search a cut i in nums1, j = half - i in nums2
## correct cut: nums1[i-1] <= nums2[j] and nums2[j-1] <= nums1[i]
## odd total: median = min(nums1[i], nums2[j])
## even total: median = (max(left) + min(right)) / 2
The cut is correct when the biggest element on the left of either array is still smaller than the smallest element on the right of the other. If nums1[i-1] > nums2[j], the cut in nums1 is too far right — move it left. Otherwise move it right.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Merge and sort | O((m+n) log(m+n)) | O(m+n) | Simple, ignores the sorted input |
| Binary partition | O(log(min(m, n))) | O(1) | The interview answer |
Solution code
from typing import List
class SolutionBruteForce:
"""O((m+n) log(m+n)) time | O(m+n) space — merge, sort, pick the middle."""
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
merged = sorted(nums1 + nums2)
n = len(merged)
if n % 2 == 1:
return float(merged[n // 2])
return (merged[n // 2 - 1] + merged[n // 2]) / 2
class Solution:
"""O(log(min(m, n))) time | O(1) space — binary search the partition cut."""
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
# Always binary search the shorter array.
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
m, n = len(nums1), len(nums2)
half = (m + n + 1) // 2
lo, hi = 0, m
while lo <= hi:
i = (lo + hi) // 2 # cut in nums1: i elements on the left
j = half - i # cut in nums2: j elements on the left
left1 = nums1[i - 1] if i > 0 else float("-inf")
right1 = nums1[i] if i < m else float("inf")
left2 = nums2[j - 1] if j > 0 else float("-inf")
right2 = nums2[j] if j < n else float("inf")
if left1 <= right2 and left2 <= right1:
if (m + n) % 2 == 1:
return float(max(left1, left2))
return (max(left1, left2) + min(right1, right2)) / 2
elif left1 > right2:
hi = i - 1 # cut in nums1 is too far right
else:
lo = i + 1 # cut in nums1 is too far left
return 0.0Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language