PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates algorithmic problem-solving with sorted arrays, focusing on concepts like binary search, partitioning strategies, complexity analysis, and careful handling of edge cases when computing order statistics.

  • medium
  • Rippling
  • Coding & Algorithms
  • Software Engineer

Compute median of two sorted arrays

Company: Rippling

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given two sorted arrays of integers `nums1` and `nums2`. - `nums1` has length `m`, `nums2` has length `n`. - Both arrays are sorted in non-decreasing order. - Either array can be empty, but **not both** at the same time. Your task is to find the **median** of the combined multiset formed by all elements of `nums1` and `nums2`. - The overall run time complexity must be **O(log (m + n))**. - You may not merge the two arrays into a third array of size `m + n` in a way that makes the complexity worse than \(O(m + n)\). **Definition of median:** - If `m + n` is odd, the median is the middle element after the two arrays' elements are conceptually merged and sorted. - If `m + n` is even, the median is the average (as a floating point number) of the two middle elements after the two arrays' elements are conceptually merged and sorted. **Function signature (conceptual):** - Input: integer arrays `nums1`, `nums2`. - Output: a floating point number representing the median. **Examples:** 1. `nums1 = [1, 3]`, `nums2 = [2]` → merged is `[1, 2, 3]`, median = `2.0`. 2. `nums1 = [1, 2]`, `nums2 = [3, 4]` → merged is `[1, 2, 3, 4]`, median = `(2 + 3) / 2 = 2.5`. Explain the algorithm you would use to achieve \(O(\log(m + n))\) time and its correctness, then implement it in code.

Quick Answer: This question evaluates algorithmic problem-solving with sorted arrays, focusing on concepts like binary search, partitioning strategies, complexity analysis, and careful handling of edge cases when computing order statistics.

You are given two sorted arrays of integers, nums1 and nums2, with lengths m and n. Both arrays are sorted in non-decreasing order, and at least one of them is non-empty. Return the median of the combined multiset formed by all elements of nums1 and nums2. The solution must run in O(log(m + n)) time, so simply merging the arrays and then taking the middle is not sufficient for full credit. Median definition: - If m + n is odd, the median is the middle element of the combined sorted order. - If m + n is even, the median is the average of the two middle elements, returned as a floating point number.

Constraints

  • 0 <= len(nums1), len(nums2) <= 10^5
  • 1 <= len(nums1) + len(nums2) <= 2 * 10^5
  • -10^6 <= nums1[i], nums2[i] <= 10^6
  • nums1 and nums2 are each sorted in non-decreasing order

Examples

Input: ([1, 3], [2])

Expected Output: 2.0

Explanation: The combined sorted array is [1, 2, 3], so the middle element is 2.

Input: ([1, 2], [3, 4])

Expected Output: 2.5

Explanation: The combined sorted array is [1, 2, 3, 4], so the median is (2 + 3) / 2 = 2.5.

Input: ([], [7])

Expected Output: 7.0

Explanation: Only one element exists in total, so that element is the median.

Input: ([0, 0], [0, 0])

Expected Output: 0.0

Explanation: The combined sorted array is [0, 0, 0, 0], and the two middle elements are both 0.

Input: ([-5, -3, -1], [2, 4, 6, 8])

Expected Output: 2.0

Explanation: The combined sorted array is [-5, -3, -1, 2, 4, 6, 8], so the middle element is 2.

Input: ([1, 2, 2], [2, 2, 3])

Expected Output: 2.0

Explanation: The combined sorted array is [1, 2, 2, 2, 2, 3], and the two middle elements are both 2.

Solution

def solution(nums1, nums2):
    # Binary search on the smaller array.
    # We choose partitions i and j such that:
    # - i elements are taken from nums1 into the left half
    # - j elements are taken from nums2 into the left half
    # - i + j == (m + n + 1) // 2
    # A valid partition satisfies:
    #   left1 <= right2 and left2 <= right1
    # Once found, the median comes from the boundary values.

    if len(nums1) > len(nums2):
        nums1, nums2 = nums2, nums1

    m, n = len(nums1), len(nums2)
    total_left = (m + n + 1) // 2
    lo, hi = 0, m

    while lo <= hi:
        i = (lo + hi) // 2
        j = total_left - i

        left1 = float('-inf') if i == 0 else nums1[i - 1]
        right1 = float('inf') if i == m else nums1[i]
        left2 = float('-inf') if j == 0 else nums2[j - 1]
        right2 = float('inf') if j == n else nums2[j]

        if left1 <= right2 and left2 <= right1:
            if (m + n) % 2 == 1:
                return float(max(left1, left2))
            return (max(left1, left2) + min(right1, right2)) / 2.0
        elif left1 > right2:
            hi = i - 1
        else:
            lo = i + 1

    raise ValueError('Input arrays are not sorted or constraints are violated.')

Time complexity: O(log(min(m, n))). Space complexity: O(1).

Hints

  1. Try binary searching on the smaller array instead of thinking about positions in the merged array directly.
  2. Think of splitting both arrays into a left half and a right half such that every value on the left is less than or equal to every value on the right.
Last updated: May 14, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Design a Driver Payroll System - Rippling (medium)
  • Compare Complete or Partial Hands - Rippling (medium)
  • Implement Food Delivery Billing - Rippling (medium)
  • Implement a balance tracker and interval merger - Rippling (medium)
  • Compute total wages and partial-hour payments - Rippling (medium)