Category: dsa Date: 2026-04-02
Median of Two Sorted Arrays System Design Discussion
Problem Statement: Given two sorted arrays, find the median of the combined array.
Requirements (Functional + Non-functional):
High-Level Architecture:
Database Design (Not applicable for this problem, as it’s a standalone algorithm)
Scaling Strategy:
Bottlenecks:
Trade-offs:
Solution using the First Principle of System Design (Separation of Concerns):
Median of Two Sorted Arrays Solution:
def findMedianSortedArrays(nums1, nums2):
# Ensure nums1 is the smaller array to simplify logic
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
total_len = len(nums1) + len(nums2)
half_len = total_len // 2
left = 0
right = len(nums1) - 1
while True:
# Find the partition for nums1
i = (left + right) // 2
# Find the corresponding partition for nums2
j = half_len - i - 2
nums1_left = nums1[i] if i >= 0 else float('-infinity')
nums1_right = nums1[i + 1] if (i + 1) < len(nums1) else float('infinity')
nums2_left = nums2[j] if j >= 0 else float('-infinity')
nums2_right = nums2[j + 1] if (j + 1) < len(nums2) else float('infinity')
# Check if the partitions are correct
if nums1_left <= nums2_right and nums2_left <= nums1_right:
# Check if the total length is odd or even
if total_len % 2 == 0:
# Return the average of the two middle elements
return (max(nums1_left, nums2_left) + min(nums1_right, nums2_right)) / 2
else:
# Return the middle element
return min(nums1_right, nums2_right)
# Adjust the partition for nums1
elif nums1_left > nums2_right:
right = i - 1
else:
left = i + 1
This solution follows the first principle of system design by separating input processing and median calculation, using a binary search approach for median calculation, and decoupling result generation from median calculation.