Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Median-Level Array Manipulation

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Median-Level Array Manipulation. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

WePay's reporting tools receive transaction amounts from two already sorted sources. Given two sorted integer arrays, return the median of all values while avoiding unnecessary storage for a merged array.

Use the standard median definition: if the combined length is odd, return the middle value; if it is even, return the average of the two middle values.

Formal Specification

Implement median_two_sorted(nums1, nums2).

  • Input: Two nondecreasing arrays of integers, nums1 and nums2.
  • Output: A number representing the median. Return an integer when the median is integral, otherwise return a floating-point value.
  • The input arrays may have different lengths, and either array may be empty.
  • At least one array contains an element.

Constraints

  • 0 <= len(nums1), len(nums2) <= 10^5
  • 1 <= len(nums1) + len(nums2) <= 2 * 10^5
  • -10^9 <= nums1[i], nums2[i] <= 10^9
  • Both arrays are sorted in nondecreasing order

Function Signature

def median_two_sorted(nums1, nums2):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output