💡
Exercise 11

Track Min/Max — Your Memory Superpower 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

You've learned to search, count, and find pairs. Now let's unlock a technique that's deceptively simple but incredibly powerful — tracking a running minimum and maximum in a single pass. 🏃‍♂️

Imagine you're walking through a flea market with dozens of stalls, each selling the same vintage t-shirt at different prices. You want to know the cheapest and most expensive prices — but you can only walk through once (the market is closing!). What do you do? 🛒

  • At the first stall, note the price — that's your current cheapest AND most expensive (it's the only one you've seen!).
  • At every next stall, ask yourself two questions:
  • → Is this price lower than the cheapest I've seen? If yes, update!
  • → Is this price higher than the most expensive I've seen? If yes, update!
  • By the end, you know both extremes — all in one walk. 🎯

In code, this looks like:

# Array: [3, 7, 1, 9, 4, 2] # Start: min_val = 3, max_val = 3 # See 7: min_val = 3, max_val = 7 (7 > 3, new max!) # See 1: min_val = 1, max_val = 7 (1 < 3, new min!) # See 9: min_val = 1, max_val = 9 (9 > 7, new max!) # See 4: min_val = 1, max_val = 9 (no change) # See 2: min_val = 1, max_val = 9 (no change) # Result: (1, 9) ✅

⏱ Time Complexity: O(n) — One pass through the array, two comparisons per element. Can't do better than this!

💾 Space Complexity: O(1) — Just two variables: min_val and max_val. No extra arrays or dictionaries. Your brain (well, two variables) is all you need. 🧠

🔮 Why this matters: The next problem is Best Time to Buy and Sell Stock — one of the most famous interview questions ever. Its core trick? Tracking a running minimum as you scan through prices. If you nail min/max tracking here, that problem becomes a cakewalk. Let's practice!

📋 Instructions
Write a function `find_min_max(arr)` that returns a tuple `(minimum, maximum)` from the array. **Rules:** - Do it in a **single pass** through the array. - Do **NOT** use Python's built-in `min()` or `max()` functions. - Initialize with the first element, then scan the rest. Test it: ```python lo, hi = find_min_max([3, 7, 1, 9, 4, 2]) print(f"Min: {lo}, Max: {hi}") lo, hi = find_min_max([100, -5, 42, 0, 88]) print(f"Min: {lo}, Max: {hi}") lo, hi = find_min_max([42]) print(f"Min: {lo}, Max: {hi}") ```
Initialize both min_val and max_val to arr[0]. Then loop through arr[1:] — for each number, check: is it smaller than min_val? Update. Is it bigger than max_val? Update. Return both at the end!
⚠️ Try solving it yourself first — you'll learn more!
def find_min_max(arr):
    min_val = arr[0]
    max_val = arr[0]
    for num in arr[1:]:
        if num < min_val:
            min_val = num
        if num > max_val:
            max_val = num
    return min_val, max_val

lo, hi = find_min_max([3, 7, 1, 9, 4, 2])
print(f"Min: {lo}, Max: {hi}")
lo, hi = find_min_max([100, -5, 42, 0, 88])
print(f"Min: {lo}, Max: {hi}")
lo, hi = find_min_max([42])
print(f"Min: {lo}, Max: {hi}")
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.