Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DEV Community

Nitinn S Kulkarni
Nitinn S Kulkarni

Posted on

Part 2: Arrays, Lists, and Strings โ€“ Patterns, Tricks, and Pitfalls in Python

๐Ÿš€ Introduction

Arrays (or lists in Python) and strings are among the most common data structures youโ€™ll use. They form the backbone of many algorithms and interview questions.

In this post, weโ€™ll explore:

โœ… Pythonโ€™s built-in list and string capabilities

โœ… Essential algorithmic patterns: sliding window, two pointers, prefix sum

โœ… Real-world problems and how to solve them

โœ… Best practices and Pythonic shortcuts

๐Ÿ“ฆ 1๏ธโƒฃ Python Lists: More Than Just Arrays

Python lists are dynamic arrays that can grow or shrink in size.


arr = [1, 2, 3]
arr.append(4)       # [1, 2, 3, 4]
arr.pop()           # [1, 2, 3]
arr.insert(1, 10)   # [1, 10, 2, 3]
del arr[0]          # [10, 2, 3]

Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น List Slicing


nums = [1, 2, 3, 4, 5]
print(nums[1:4])     # [2, 3, 4]
print(nums[::-1])    # [5, 4, 3, 2, 1] โ€“ reversed

Enter fullscreen mode Exit fullscreen mode

โœ… Slicing is your best friend in array problems.

๐Ÿ”ค 2๏ธโƒฃ Strings in Python

Python strings are immutable and extremely versatile.


s = "hello"
print(s.upper())        # "HELLO"
print(s[1:4])           # "ell"
print("h" in s)         # True

Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น String Tricks


# Reversing a string
reversed_s = s[::-1]

# Check for palindrome
def is_palindrome(s):
    return s == s[::-1]

Enter fullscreen mode Exit fullscreen mode

๐Ÿšฆ 3๏ธโƒฃ Pattern: Two Pointers

Use two pointers to scan a list or string from both ends or at different speeds.

๐Ÿ”น Example: Is a list a palindrome?


def is_palindrome(lst):
    left, right = 0, len(lst) - 1
    while left < right:
        if lst[left] != lst[right]:
            return False
        left += 1
        right -= 1
    return True

Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Example: Remove Duplicates from Sorted Array


def remove_duplicates(nums):
    if not nums:
        return 0
    i = 0
    for j in range(1, len(nums)):
        if nums[j] != nums[i]:
            i += 1
            nums[i] = nums[j]
    return i + 1

Enter fullscreen mode Exit fullscreen mode

โœ… Use when you need to compare or update positions in-place.

๐Ÿ” 4๏ธโƒฃ Pattern: Sliding Window

Efficiently track values over a moving range.

๐Ÿ”น Example: Max sum of subarray of size k


def max_subarray_sum(nums, k):
    window_sum = sum(nums[:k])
    max_sum = window_sum
    for i in range(k, len(nums)):
        window_sum += nums[i] - nums[i - k]
        max_sum = max(max_sum, window_sum)
    return max_sum

Enter fullscreen mode Exit fullscreen mode

โœ… Use when dealing with contiguous sequences like subarrays, substrings, etc.

โž• 5๏ธโƒฃ Pattern: Prefix Sum

Prefix sum helps in range sum queries and cumulative processing.

๐Ÿ”น Example: Range sum query


def prefix_sum(nums):
    prefix = [0]
    for num in nums:
        prefix.append(prefix[-1] + num)
    return prefix

# Sum of elements from index i to j: prefix[j+1] - prefix[i]

Enter fullscreen mode Exit fullscreen mode

โœ… Reduces repeated computation in range-based problems.

๐Ÿงช 6๏ธโƒฃ Practice Problems

Problem Pattern Tip
Two Sum Hashing / Two Pointers Use a set for complements
Maximum Subarray Kadaneโ€™s Algorithm Track max ending at current index
Longest Substring Without Repeating Characters Sliding Window Use a set or dict for seen chars
Palindrome Check Two Pointers Compare left and right
Product of Array Except Self Prefix & Suffix Arrays Avoid division

โœ… Best Practices

โœ”๏ธ Know when to use in-place operations to save space
โœ”๏ธ Use enumerate() to loop with index
โœ”๏ธ Avoid += on strings in loops (use ''.join() instead)
โœ”๏ธ Leverage collections.Counter, set, and defaultdict for advanced use cases
โŒ Avoid nested loops unless necessary โ€” try sliding window or hashing

๐Ÿง  Summary

โœ”๏ธ Lists and strings are the foundation of most coding problems
โœ”๏ธ Learn and practice patterns like two pointers, sliding window, and prefix sums
โœ”๏ธ Pythonโ€™s expressive syntax helps solve these problems cleanly and concisely
โœ”๏ธ Build your DSA muscle memory with hands-on problems using these patterns

๐Ÿ”œ Coming Up Next:

๐Ÿ‘‰ Part 3: Stacks and Queues โ€“ Theory, Python Implementations, and Interview Classics

Weโ€™ll explore:

  1. Real-world uses of stacks & queues
  2. How to implement with Python lists and deque
  3. Problems like parentheses validation, undo/redo, and level-order traversal

๐Ÿ’ฌ Got a favorite array/string trick? Or stuck on a pattern? Drop a comment and letโ€™s chat! ๐Ÿงฉ๐Ÿ”ฅ

Top comments (0)