🔍 Deep Dive into Arrays: 10 Python List Techniques That Are Interview Gold

Working with arrays (or lists in Python) is fundamental in any programming language. Whether you're preparing for coding interviews or enhancing your development skills, mastering basic array operations is essential.

Here are 11 common array tasks in Python—with simple and beginner-friendly code examples.


 ✅ 1. Find the Largest Element in an Array

arr = [1, 2, 3, 4, 5] largest = max(arr) print("Largest element in the array is:", largest)

🔍 Explanation:

  • max() is a built-in Python function that returns the maximum value from a list.

  • It internally compares each element and keeps track of the largest.


✅ 2. Find the Smallest Element in an Array

smallest = min(arr) print("Smallest element in the array is:", smallest)

🔍 Explanation:

  • Similar to max(), min() returns the smallest value.

  • It works in O(n) time—meaning it goes through all elements once.


✅ 3. Reverse an Array (After Sorting Descending)

arr.sort(reverse=True) print("Reversed array is:", arr)

🔍 Explanation:

  • arr.sort(reverse=True) sorts the array in descending order.

  • It's not a true reversal of the original order.

  • If you want to reverse without sorting, use:

    arr.reverse()

✅ 4. Check if an Array is Sorted

if arr == sorted(arr): print("Array is sorted") else: print("Array is not sorted")

🔍 Explanation:

  • sorted(arr) creates a sorted copy of the array in ascending order.

  • If the original array is already sorted, both will match.


✅ 5. Remove Duplicates from an Array

arr1 = [1, 2, 2, 3, 4, 4, 5] arr1 = list(set(arr1)) print("Array after removing duplicates is:", arr1)

🔍 Explanation:

  • set() is an unordered collection of unique elements.

  • Converting a list to a set removes duplicates, then list(set(...)) brings it back to list form.

  • ⚠️ Order is not guaranteed. To preserve order:

    arr1 = list(dict.fromkeys(arr1))

✅ 6. Rotate an Array by K Elements

arr2 = [1, 2, 3, 4, 5] k = 2 arr2 = arr2[k:] + arr2[:k] print("Array after rotating by", k, "elements is:", arr2)

🔍 Explanation:

  • Array rotation means shifting elements.

  • The above logic:

    • arr2[k:] → elements from index k to end

    • arr2[:k] → first k elements

    • Then we concatenate the two to simulate rotation


✅ 7. Find the Intersection of Two Arrays


arr3 = [1, 2, 3, 4, 5] arr4 = [4, 5, 6, 7, 8] intersection = list(set(arr3) & set(arr4)) print("Intersection of the two arrays is:", intersection)

🔍 Explanation:

  • & (set intersection) finds elements common to both sets.

  • This is efficient and reduces complexity compared to looping.


✅ 8. Find the Union of Two Arrays


union = list(set(arr5) | set(arr6))

🔍 Explanation:

  • | (set union) combines all unique elements from both arrays.

  • Avoids duplicates automatically.


✅ 9. Find the Second Largest Element


arr7.sort(reverse=True) print(arr7[1], "is the second largest")

🔍 Explanation:

  • Sorts the array in descending order.

  • arr7[0] is largest, arr7[1] is second largest.

  • 🔁 For robustness (if duplicates exist), use:


    arr7 = list(set(arr7)) arr7.sort(reverse=True) print(arr7[1])

✅ 10. Merge Two Sorted Arrays


merged_array = sorted(arr8 + arr9)

🔍 Explanation:

  • arr8 + arr9 merges the two arrays into one.

  • sorted() then sorts the merged array.

  • If both are already sorted, you could also merge manually using two pointers (for optimization in interviews).


✅ 11. Find the Kth Largest Element


arr10.sort(reverse=True) kth_largest = arr10[k-1] if k <= len(arr10) else None

🔍 Explanation:

  • After descending sort, the k-1th index gives the kth largest value.

  • Defensive check ensures k is within bounds.


✨ Wrapping Up

These examples demonstrate key array operations useful in day-to-day programming and interviews. Whether it’s finding max/min values, sorting, or merging—mastering these will make your code cleaner and more efficient.


💬 Have questions or want more examples like these? Drop a comment or share this with fellow Python learners!

Comments