Arrays and Strings

πŸ“– Introduction

Arrays and strings are the workhorses of programming. In my projects, I constantly work with lists of dataβ€”user records, log entries, API responsesβ€”and string manipulation for parsing, validation, and formatting. Understanding how Python implements these structures, and mastering the algorithmic techniques for processing them efficiently, has been fundamental to writing performant code.

This article covers Python's list and string internals, essential techniques like two-pointer and sliding window, and the most common array/string problems you'll encounter.


🎯 Python List Internals

Python lists are dynamic arraysβ€”contiguous memory blocks that resize automatically.

Memory Layout

import sys

# Lists are over-allocated for efficiency
lst = []
print(f"Empty list: {sys.getsizeof(lst)} bytes")

for i in range(20):
    lst.append(i)
    print(f"Length {len(lst):2d}: {sys.getsizeof(lst)} bytes")

# Output shows capacity jumps:
# Length  1: 88 bytes
# Length  5: 120 bytes
# Length  9: 184 bytes
# ...

Time Complexity of List Operations

Operation
Average
Worst
Notes

lst[i]

O(1)

O(1)

Direct index access

lst.append(x)

O(1)*

O(n)

Amortized O(1)

lst.pop()

O(1)

O(1)

Remove from end

lst.pop(0)

O(n)

O(n)

Shift all elements

lst.insert(i, x)

O(n)

O(n)

Shift elements after i

x in lst

O(n)

O(n)

Linear search

lst[i:j]

O(j-i)

O(j-i)

Create new list

lst.sort()

O(n log n)

O(n log n)

Timsort


πŸ“ Python String Internals

Strings in Python are immutable sequences of Unicode characters.

Immutability Implications

String Interning


πŸ‘† Two-Pointer Technique

Two pointers is a pattern where you use two indices to traverse an array, often from opposite ends or at different speeds.

Pattern 1: Opposite Direction

spinner

Pattern 2: Same Direction (Fast/Slow)

Pattern 3: Three Pointers


πŸͺŸ Sliding Window Technique

Sliding window maintains a "window" over contiguous elements, sliding it to process subarrays/substrings efficiently.

Fixed-Size Window

Variable-Size Window

spinner

πŸ“Š Prefix Sum Technique

Prefix sums enable O(1) range sum queries after O(n) preprocessing.


πŸ”„ Common Array Problems

Kadane's Algorithm (Maximum Subarray)

Merge Intervals

Rotate Array


πŸ”€ Common String Problems

Anagram Check

Valid Parentheses (Stack-based)

Longest Common Prefix


πŸ“ Practice Exercises

Exercise 1: Container With Most Water

chevron-rightSolutionhashtag

Exercise 2: Minimum Window Substring

chevron-rightSolutionhashtag

Exercise 3: Product of Array Except Self

chevron-rightSolutionhashtag

πŸ”‘ Key Takeaways

  1. Python lists are dynamic arrays with O(1) amortized append

  2. Strings are immutableβ€”use join() for efficient concatenation

  3. Two pointers solve many problems in O(n) time

  4. Sliding window efficiently processes contiguous subarrays

  5. Prefix sums enable O(1) range queries

  6. Know your techniquesβ€”most problems combine these patterns


πŸš€ What's Next?

In the next article, we'll explore linked lists:

  • Singly and doubly linked list implementation

  • Pointer manipulation techniques

  • Fast/slow pointer (Floyd's algorithm)

  • Common linked list problems

Next: Article 5 - Linked Lists


πŸ“š References

  • Python Wiki: TimeComplexity

  • LeetCode Array/String problems

  • "Cracking the Coding Interview" - Arrays and Strings chapter

Last updated