Step 01 Beginner Problems — Concept Basics
Basic Arrays
The array is the first data structure and still the most important: a contiguous block of memory where every element is reachable in O(1) by index. This lesson covers what that memory picture implies, the traversal patterns that solve most beginner array problems (sum, count, maximum, sortedness), the in-place reversal trick, and the costs that will later motivate fancier structures.
The memory picture and why indexing is O(1)
Elements of an array sit side by side in memory, each the same size. The address of element i is base_address + i × element_size — pure arithmetic, no searching — which is why a[i] costs O(1) regardless of the array's size, and why indices start at 0 (the first element is 0 steps from the base).
The same picture explains the weaknesses: inserting or deleting in the middle forces every later element to shift (O(n)), and a fixed-size array cannot grow. Dynamic arrays (Python list, C++ vector, Java ArrayList) hide growth by occasionally copying into a bigger block — amortised O(1) append, the same shifting cost in the middle.
Traversal patterns: one pass, one accumulator
Most beginner array problems are a single left-to-right pass with a small accumulator:
Sum — total += x. Count matching — count += 1 when a condition holds (odd numbers, values above a threshold). Maximum / minimum — best = max(best, x), seeded with the FIRST element, not 0 (seeding max with 0 fails on all-negative arrays — a classic). Search — return the index on first match, or a boolean for membership.
Recognising 'one pass, one accumulator' as a shape lets you write these in seconds and save your thinking for problems that genuinely need more.
Checking sortedness and in-place reversal
An array is sorted (non-decreasing) when no element is smaller than its predecessor: scan adjacent pairs and fail on the first a[i] < a[i-1]. Equal neighbours are allowed — non-decreasing vs strictly increasing is a problem-statement detail worth re-reading.
Reversal is the first two-pointer pattern: swap the outermost pair, step both pointers inward, stop when they meet. It runs in O(n) time and O(1) extra space — 'in place' — versus the easy but wasteful build-a-new-array method. This swap-from-both-ends skeleton returns in string reversal, rotation problems and partitioning later.
def reverse_in_place(a):
l, r = 0, len(a) - 1
while l < r:
a[l], a[r] = a[r], a[l]
l += 1
r -= 1
return a
def is_sorted(a):
return all(a[i] >= a[i - 1] for i in range(1, len(a)))
print(reverse_in_place([1, 2, 3, 4])) # [4, 3, 2, 1]
print(is_sorted([1, 2, 2, 5])) # TrueCommon mistakes
Index out of range — looping to len(a) inclusive, or touching a[i+1] on the last iteration; adjacent-pair loops must stop one early. Seeding a maximum with 0 instead of a[0] (breaks on all-negative input) and a sum with anything but 0. Modifying a list while iterating over it — removals shift indices under your feet; iterate over a copy or build a new list. Assuming the array is non-empty: a[0] on an empty array is a crash, so decide what empty should return before coding.
Key points
- Arrays are contiguous memory: address = base + i × size, so indexing is O(1) and indices start at 0.
- Middle insert/delete costs O(n) shifting — that cost is why other structures exist.
- Most beginner problems are one pass with one accumulator: sum, count-if, max/min, first-match search.
- Seed maximum with the first element, never 0 — all-negative arrays are the trap.
- In-place reversal: two pointers swapping inward, O(n) time, O(1) space — a skeleton reused everywhere.
- Adjacent-pair loops stop one index early; guard the empty array before touching a[0].