All Data Structures
Array
Core Implementation
Python offers several ways to implement arrays:
Built-in Lists (Dynamic Arrays)
NumPy Arrays (Static Arrays)
Array Module (Homogeneous Arrays)
Implementation Variations
1. Circular Array
2. Dynamic Array (Manual Implementation)
Time Complexities
Operation | Dynamic Array (List) | Static Array (NumPy) | Circular Array |
---|---|---|---|
Access (index) | O(1) | O(1) | O(1) |
Append | O(1)* | N/A | O(1) |
Insert (beginning) | O(n) | N/A | N/A |
Insert (middle) | O(n) | N/A | N/A |
Delete (beginning) | O(n) | N/A | O(1)** |
Delete (middle) | O(n) | N/A | N/A |
Search (unsorted) | O(n) | O(n) | O(n) |
Search (sorted) | O(log n)*** | O(log n)*** | O(n) |
* Amortized time complexity ** For circular array queue implementation *** Using binary search if array is sorted
Space Complexities
Implementation | Space Complexity | Additional Notes |
---|---|---|
Dynamic Array (List) | O(n) | Typically allocates 2x needed space |
Static Array (NumPy) | O(n) | Fixed size, more memory efficient |
Circular Array | O(n) | Fixed size with constant extra space |
Multi-dimensional | O(n*m) | For 2D array with n rows and m columns |
Common Data Structure Patterns
-
Two Pointer Technique
- Fast and slow pointers
- Start and end pointers
- Sliding window
-
Sliding Window
- Fixed size window
- Variable size window
- Multiple windows
-
Prefix Sum
- Cumulative sum array
- Difference array
-
Kadane’s Algorithm Pattern
- Maximum subarray
- Dynamic programming with arrays
-
Dutch National Flag
- Three-way partitioning
- Quick sort partitioning
-
Cyclic Sort
- In-place sorting
- Missing/duplicate numbers
Edge Cases to Consider
- Empty Array
- Single Element
- Array Size Limits
- Integer Overflow
- Duplicate Elements
Optimization Techniques
- Pre-allocation
- List Comprehension
- Using NumPy for Numerical Operations
Memory Management
- Garbage Collection
- Memory Views
- Array Slicing
Performance Considerations
- Array Copy vs Reference
- Batch Processing
Common Pitfalls
- List Reference Pitfall
- Index Out of Range
- Memory Leak in Circular References