Arrays and strings are foundational data structures in programming, representing ordered collections and text sequences respectivelyβarrays store elements in contiguous memory locations enabling constant-time indexed access, while strings are typically immutable sequences of characters. Mastering arrays and strings is essential because nearly every program manipulates collections or processes text, and understanding their memory layout and time complexity (O(1) for access, O(n) for insertion/deletion in middle positions) directly impacts performance. One key insight: strings are often implemented as character arrays but with special behaviors like immutability in many languages (Java, Python), copy-on-write semantics, and encoding concerns (ASCII vs UTF-8 vs UTF-16)βrecognizing when operations create new objects versus modifying in-place prevents subtle bugs and performance pitfalls.
What This Cheat Sheet Covers
This topic spans 26 focused tables and 199 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Array Fundamentals
| Concept | Example | Description |
|---|---|---|
arr[0]arr[arr.length - 1] | Retrieves element at zero-based index in O(1) time via contiguous memory address calculation. | |
First: arr[0]Last: arr[n-1] | Most languages start indexing at 0 rather than 1, simplifying pointer arithmetic and offset calculations. | |
arr[-1]arr[-3] | Counts backward from end in Python, Ruby, and via at() in JS β -1 refers to the last element. | |
arr.lengthlen(arr)arr.size() | β’ Returns total number of elements currently stored β’ property access is typically O(1) as length is cached. | |
[a][b][c][d] sequential | All elements stored adjacently in RAM, enabling cache-friendly access and address calculation via base + (index Γ element_size). |