Arrays & Strings Warmup
Read a little, play a little. No scary maths, and no rush.
Why warm up here
Arrays and strings are where interviews start — and where nervous candidates fumble basics like indices and off-by-ones. Five fluent warmups buy the confidence (and the muscle memory) that harder patterns assume. Think of this as scales before the concerto.
The five must-dos
1. Reverse in place. Two pointers walk inward, swapping. No extra array, O(1) space:
def reverse(a):
l, r = 0, len(a) - 1
while l < r:
a[l], a[r] = a[r], a[l]
l += 1
r -= 1
2. Max (and second max). One pass tracking the champion — and optionally the runner-up. Trivial, but off-by-one initializations (max = 0 vs max = a[0] on negatives!) end interviews early.
3. Palindrome check. Clean the string (lowercase, alphanumeric only), then compare ends inward — or compare against its reverse for clarity when O(n) space is fine:
def is_pal(s):
s = ''.join(c.lower() for c in s if c.isalnum())
return s == s[::-1]
4. Move zeroes. Stable partition preserving order: sweep non-zeroes forward, backfill zeroes. Introduces the read/write pointer idea that sliding windows generalize.
5. Two Sum, brute then map. Write the O(n²) double loop first (shows honesty), then the hash-map version (shows growth). Compare the section on hash tables for the full story.
Off-by-one errors, demystified
Most index bugs come from mixing up whether an end is included. Python (like most languages) uses half-open ranges: the start is included, the end is not.
range(0, n)gives 0 through n − 1. That is n numbers, and n − 1 is the last valid index.a[i:j]takes elements i up to but not including j, so its length is j − i.- In a two-pointer loop,
while l < rstops when the pointers meet, which is right for swapping.while l <= ralso processes the middle element, which is right for binary search but would swap the middle element with itself here.
The quickest check is to run the loop in your head on arrays of length 0, 1, and 2. If all three behave, the boundaries are almost certainly right.
Strings are immutable
In Python and Java, a string cannot be changed in place; every "change" builds a new string. Adding characters one at a time in a loop (s += c) can therefore cost far more than it looks. The habit that avoids it: collect pieces in a list and join once at the end with "".join(parts). For counting characters, use a dictionary, collections.Counter, or for lowercase letters only, an array of 26 counts.
One more warmup: valid anagram
Are "listen" and "silent" made of the same letters? Two clean approaches:
- Sort both and compare. Simple and correct, O(n log n) time.
- Count letters. Count each letter in the first word, subtract for the second, and check every count ends at zero. O(n) time.
Either way, check the lengths first: if they differ, the answer is false immediately, without counting anything.
The edge-case habit
For each: empty input? Single element? All identical? Negatives? Saying "let me check empty and single" before coding is a small sentence that signals a tested engineer.
Remember this
- Reverse, max, palindrome, move-zeroes, Two Sum — fluent fingers, calm brain.
- In-place means O(1) extra space; name your space complexity every time.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Related chapters
Spotted a mistake or want a topic covered? Report an issue