PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates algorithmic problem-solving skills related to sorted arrays and median computation, testing competency in handling numeric order properties and complexity analysis within the Coding & Algorithms domain.

  • medium
  • Rippling
  • Coding & Algorithms
  • Software Engineer

Find 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` in non-decreasing order. Let the lengths be `m = nums1.length` and `n = nums2.length`. Either array may be empty, but not both at the same time. If you were to conceptually merge them into a single sorted array `merged` of length `m + n`, the **median** is defined as: - If `m + n` is odd: the element at index `(m + n) / 2` (0-based) in `merged`. - If `m + n` is even: the average of the two middle elements at indices `(m + n) / 2 - 1` and `(m + n) / 2` in `merged`. ### Task Implement a function that returns the median of the two sorted arrays **without actually merging them**. - **Function signature (conceptual):** - Input: `nums1: integer[]`, `nums2: integer[]` - Output: `median: float` (or double) ### Constraints (you may assume) - `0 ≤ m, n ≤ 10^5` - `m + n ≥ 1` - Each array is individually sorted in non-decreasing order. - Integer values fit in a 32-bit signed integer range. - Target time complexity: **O(log(m + n))**. - Extra space complexity: **O(1)** beyond input storage.

Quick Answer: This question evaluates algorithmic problem-solving skills related to sorted arrays and median computation, testing competency in handling numeric order properties and complexity analysis within the Coding & Algorithms domain.

You are given two sorted arrays of integers, nums1 and nums2, in non-decreasing order. Either array may be empty, but not both. If the two arrays were conceptually merged into one sorted array, the median is: - the middle element when the total length is odd - the average of the two middle elements when the total length is even Return the median without actually merging the arrays. Your solution should run in logarithmic time relative to the smaller array.

Constraints

  • 0 <= len(nums1), len(nums2) <= 10^5
  • len(nums1) + len(nums2) >= 1
  • Each array is sorted in non-decreasing order
  • Integer values fit in a 32-bit signed integer range
  • Target time complexity: O(log(min(m, n)))
  • Extra space complexity: O(1)

Examples

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

Expected Output: 2.0

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

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

Expected Output: 2.5

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

Input: ([], [1])

Expected Output: 1.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 values are both 0.

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

Expected Output: -2.5

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

Input: ([1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11])

Expected Output: 6.0

Explanation: The combined sorted array has 11 elements, so the median is the element at index 5, which is 6.

Solution

def solution(nums1, nums2):
    if not nums1 and not nums2:
        raise ValueError('At least one array must be non-empty')

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

    m, n = len(nums1), len(nums2)
    total = m + n
    half = (total + 1) // 2

    left, right = 0, m

    while left <= right:
        i = (left + right) // 2
        j = half - i

        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 total % 2 == 1:
                return float(max(left1, left2))
            return (max(left1, left2) + min(right1, right2)) / 2.0
        elif left1 > right2:
            right = i - 1
        else:
            left = i + 1

    raise ValueError('Input arrays must be sorted in non-decreasing order')

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

Hints

  1. Try binary searching on the smaller array instead of thinking about merging both arrays.
  2. A valid partition is one where every element on the left side is less than or equal to every element on the right side.
Last updated: Jun 8, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,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

  • Compare Complete and Partial Poker Hands - Rippling (medium)
  • Implement Courier Delivery Cost Tracking - Rippling (medium)
  • Implement Article Vote Tracking - Rippling (medium)
  • Implement logger and card ranking - Rippling (medium)
  • Design a Driver Payroll System - Rippling (medium)