Reported February 2022
Airbnbbinary search

Median of Two Sorted Arrays

Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Airbnb OA. Under 2s to a working solution.
Founder's read

The Airbnb OA reported in February 2022 asks for the median of two sorted arrays, and the O(log(min(n, m))) requirement is the whole point. Merging both arrays works on paper, but it's linear and it's not what they want. If you've got an invite and 48 hours, this is a binary search problem dressed up as an array problem. Learn the partition idea once and it clicks. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop and can hand you the partition logic while the proctor sees nothing.

The problem

Given two sorted integer arrays nums1 and nums2, return the median of the two arrays as if they were merged.
Your algorithm must run in O(log(min(nums1.length, nums2.length))) time.

Function
findMedianSortedArrays(nums1: int[], nums2: int[]) → double

Examples
Example 1
nums1 = [1,3]
nums2 = [2]
return = 2.0
The conceptual merged order is [1,2,3], whose middle value is 2.
Example 2
nums1 = [1,2]
nums2 = [3,4]
return = 2.5
The middle values are 2 and 3, so the median is their average.

Constraints
0 <= nums1.length, nums2.length <= 10^5.
1 <= nums1.length + nums2.length <= 2 * 10^5.
-10^9 <= nums1[i], nums2[i] <= 10^9.
Both arrays are sorted in nondecreasing order.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to binary search on the partition of the smaller array, not on values. Pick a cut i in the smaller array and derive the cut j in the larger one so the left half holds (n+m+1)/2 elements total. A cut is valid when maxLeft1 <= minRight2 and maxLeft2 <= minRight1. If maxLeft1 is too big, move i left. Otherwise move it right. With an odd total, the answer is the max of the left sides. With an even total, average that max with the min of the right sides. The classic pitfalls are empty arrays and cuts at the edges, so use negative and positive infinity sentinels for missing elements. Always search the shorter array or j can go negative. Integer overflow matters little here, but return a double. If the edge cases tangle you up live, StealthCoder is the hedge that gives you a clean sentinel-based solution fast.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Median of Two Sorted Arrays cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as median of two sorted arrays. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Airbnb's OA.

Airbnb reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Median of Two Sorted Arrays FAQ

How hard is Median of Two Sorted Arrays really?+

It's a hard-tagged problem, but the difficulty is almost all in the edge cases. The idea is one binary search on a partition. If you can explain why the left half needs (n+m+1)/2 elements, you've got 80 percent of it.

Can I just merge the arrays and pick the middle?+

That's O(n+m) and it breaks the stated O(log(min)) requirement. With up to 2 * 10^5 total elements it would likely pass a basic test, but the problem explicitly demands log time, so expect it to be judged on that. Write the binary search.

What's the trick for the partition search?+

Binary search the cut index in the shorter array. Compute the other cut from the half-size target. Compare the elements on each side of both cuts. Shift left or right until left maxes are at most right mins. Then compute the median from those four values.

How do I handle empty arrays and edge cuts?+

Use negative infinity when a cut leaves nothing on the left, and positive infinity when nothing remains on the right. Always binary search the shorter array. That handles an empty nums1 or nums2 without special-case branches, since the constraints allow length 0.

How do I prepare for this in 48 hours?+

Write the partition solution from scratch twice, then test it on both examples, an empty array, and arrays with all duplicates. Trace the cut movement by hand once. Don't memorize code. Memorize the two validity conditions and the odd versus even return.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Airbnb.

OA at Airbnb?
Invisible during screen share
Get it