Practice: Median of Two Sorted Arrays

Problem: https://leetcode.com/problems/median-of-two-sorted-arrays/

Recognition reminder: two already-sorted arrays and a required O(log(m + n)). The merge-then-pick-the-middle idea is O(m + n) and misses the point. The binary-search idea is to find where to cut the two arrays into a left half and a right half of the correct sizes, without merging. This is the hardest problem of the week; budget real time for it.

Before you start (the five-beat rhythm)

  1. Your Pattern Card for this week is already written.
  2. Name the pattern aloud and write your approach as a plain-English comment before any code.
  3. Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
  4. If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
  5. Debrief in your commit message before moving to the next problem.

Your target

Fill these in yourself before you look at anyone else’s solution:

Target time complexity:  ____
Target space complexity: ____

The target is O(log(min(m, n))), which beats the required O(log(m + n)); binary search the smaller array so the search range is as short as possible. The idea to find is that you are searching for a place to cut, not for a value: a cut in the smaller array fixes a cut in the larger one by arithmetic, and your job is to define what makes a pair of cuts “correct” and search for it. As a Python tool, float('inf') and float('-inf') are the natural values to stand in just past the ends of an array so your boundary comparisons work without special-casing the edges. Handle the even-total and odd-total cases separately in your head before you code them.

Where your code goes

Write your solution in your own work repo (see getting-started.md), not in this folder. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.

Debrief (paste into your commit message)

1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?