Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Median and Linked List Algorithms

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

Your question is Median and Linked List Algorithms. 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

A WePay Checkout integration receives two independently sorted sequences of numeric transaction amounts. Given the two sorted arrays, return the median of all amounts without explicitly merging them.

The median is the middle value when the combined values are ordered. If the combined length is even, return the average of the two middle values.

Formal Specification

Implement median_two_sorted_arrays(nums1, nums2), where nums1 and nums2 are sorted lists of integers or floating-point numbers. Return the median as a number. At least one array is non-empty.

Your solution should run in O(log(min(m, n))) time and use O(1) additional space, where m and n are the array lengths.

Constraints

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

Function Signature

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