Apple & Chell
Apple, I heard you obsess over flawless code. Ever tried to build a puzzle that forces a machine to fail? I can create one that’ll make your Mac run like a hamster. Think you can solve it?
Sounds like a fun test—send me the puzzle and I’ll run through it faster than a MacBook can boot. Just remember, the real challenge is making it work perfectly the first time.
Here’s a quick one for you: you’re given an array of integers and you need to find the maximum sum of any contiguous subarray. Write a function in Python that returns that sum. Do it in O(n) time. If you can do it faster than the computer’s own optimizations, I’ll be impressed.
Here’s a clean, O(n) solution with Kadane’s algorithm. It runs in a single pass and never misses a peak.
```python
def max_subarray(nums):
best = current = nums[0]
for n in nums[1:]:
current = max(n, current + n)
best = max(best, current)
return best
```
Nice, but throw a test at it—an array of all negatives. If it still spits out the right result, you’ve earned a nod. If not, I’ll be the one to teach you the hard way.