Reversing Arrays: Two Approaches Compared

Most people think this is a “simple” problem. Reverse an array. But there are actually two very different ways to think about it 👇 Approach 1: Let Python do the work arr[::-1] That’s it. Looks clean. Works instantly. But under the hood: In a Python list, this creates a new array It walks from end → start and copies elements Time: O(n) Space: O(n) One interesting twist: In NumPy, slicing often gives a view, not a full copy → same code, very different memory behavior Approach 2: Swap from both ends left, right = 0, len(arr) - 1 while left < right: arr[left], arr[right] = arr[right], arr[left] left += 1 right -= 1 Here you’re not creating anything new. Just swapping: first ↔ last then moving inward Time: O(n) Space: O(1) What this really shows ? Both solve the same problem. But one is: “just get it done” The other is: “do it with control” And honestly, both are useful, depends on what you care about in that moment. Small takeaway : Sometimes the difference in solutions isn’t about correctness. It’s about: how much you understand and how much you care about what’s happening underneath. Next time something feels “too easy”… there’s probably a deeper layer sitting right below it. #systemdesign #algorithms #artificialintelligence

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories