Data Structures
Arrays & Strings
Difficulty: Beginner
Reading arr[1000000] takes the same amount of time as reading arr[0], because an array's contiguous memory layout lets the runtime compute the address of any element with a single multiplication. That single property is what makes Arrays...
Arrays & Strings
Reading arr[1000000] takes the same amount of time as reading arr[0], because an array's contiguous memory layout lets the runtime compute the address of any element with a single multiplication. That single property is what makes Arrays & Strings the workhorse data structure of nearly every program you will ever write.
This lesson walks through array declaration and indexing in JavaScript and Python, the cost of inserts and deletes that force shifting, slicing and subarray patterns, and the basic string operations every interview problem assumes you know. You will also see why strings behave as immutable character arrays in both languages and what that means for performance when you build strings inside a loop.
In How to Read Code (JS & Python), you practiced tracing programs line by line; that habit is exactly how you will reason about loop indices and slice boundaries here. Big-O Notation (Upper Bound) gave you the language to express scaling behavior, and arrays are where you will first see O(1) access sit next to O(n) insertion in the same data structure.
Once array fundamentals are second nature, Matrix/Grid Fundamentals extends the same indexing intuition into two dimensions, where row and column traversal patterns power BFS, DFS, and dynamic programming on grids.
Topics:
What You'll Learn
By the end of this lesson, you will be able to:
Declare and initialize arrays in JavaScript and Python, and access elements by index.
Explain why array access is O(1) and insertion/deletion is O(n).
Perform common operations: iterate, slice, insert, delete, and search.
Describe string immutability and its performance implications in JavaScript and Python.
Trace through basic array and string manipulation code step by step.
Identify when to use arrays vs other data structures.
Why This Matters
01
Arrays are the building block for almost every other data structure — stacks, queues, heaps, and hash tables all use arrays internally.
02
Strings appear in nearly every real-world application: user input, file processing, network data, and database queries.
03
Understanding array time complexities (O(1) access, O(n) insert/delete) is the foundation for choosing the right data structure for any problem.
04
The majority of coding interview questions begin with an array or string as input.
Key Terms
7 terms you'll encounter in this lesson
Array
A contiguous block of memory storing elements of the same type, accessible by integer index in O(1) time.
Index
A 0-based integer position used to access an element in an array. The first element is at index 0.
Contiguous memory
Elements stored in adjacent memory locations, enabling direct address calculation from the index.
Slice
A sub-portion of an array or string, extracted by specifying start and end positions.
Immutability
The property of an object that cannot be changed after creation. Strings in JavaScript and Python are immutable — modifying them creates a new string.
Dynamic array
An array that automatically resizes when it runs out of capacity, offering amortized O(1) appends. JavaScript arrays and Python lists are dynamic arrays.
Subarray
A contiguous portion of an array, defined by start and end indices.
Related Problems
Coding problems that put this lesson's concepts into practice
Heads Up
Common misconceptions to watch out for
"Inserting at the end of an array is always O(1)"
Appending to a dynamic array is amortized O(1), but inserting at a specific position (e.g., the beginning or middle) requires shifting all subsequent elements, making it O(n).
"Strings and arrays behave the same way"
Strings are immutable in both JavaScript and Python. Concatenating strings in a loop creates a new string each time, which can be O(n) per concatenation — resulting in O(n^2) total. Use array-based approaches (join) for efficient string building.
"Array access by index is O(n) because it has to search"
Array access is O(1) because elements are stored in contiguous memory. The address of element i is computed directly as: base_address + i * element_size. No searching is needed.
"Deleting an element from an array just removes it"
Deleting from the middle requires shifting all subsequent elements left to fill the gap, which is O(n). Only deleting from the end is O(1).
