Stop 12 · Data Structures & AlgorithmsTopics · 2/11

Arrays & Strings

Part of the Python Roadmap

In short

Tokenising text, cleaning a CSV column, reversing words: half of all coding rounds start with a plain array or string. Get fast here and every other topic feels lighter.

Python lists act as dynamic arrays and strings are immutable sequences. Many coding questions are about scanning, counting, reversing or rearranging them efficiently.

The purple blob walks past a long row of train berths, checking one with a clipboard.

Think of it like this

Berths in a sleeper coach

Berths in a coach sit in one straight line. The TT doesn't search the whole coach; he walks straight to berth 37. That's array indexing. But if a new passenger must squeeze in at berth 5, everyone after has to shift one place. That's why inserting in the middle is slow.

Watch it happen

Two pointers check "racecar"

r
0
L
a
1
c
2
e
3
c
4
a
5
r
6
R

1/4Put one pointer at each end. Compare s[0] and s[6]: both 'r'. Match.

The big ideas

What you need to know

Index access is instant

arr[i] jumps straight to slot i in O(1) time, because items sit side by side in memory.

Middle inserts are slow

Inserting or deleting near the front shifts every later item, so it costs O(n). Appending at the end is cheap.

Python strings can't change

Strings are immutable. Building one with += in a loop copies it again and again; collect parts in a list and "".join them.

Learn the patterns

Two pointers, prefix sums and counting with a dict solve a surprising number of array and string questions.

See it in code

Walk through the code

python
1def is_palindrome(s: str) -> bool:2    left, right = 0, len(s) - 13    while left < right:4        if not s[left].isalnum():5            left += 16        elif not s[right].isalnum():7            right -= 18        elif s[left].lower() != s[right].lower():9            return False10        else:11            left += 112            right -= 113    return True14 15print(is_palindrome("Race car"))  # True

1/4Start one pointer at each end of the string.

Classic two pointers: O(n) time and O(1) extra space. It skips spaces and punctuation and ignores case, which interviewers love to ask about.

Save yourself the pain

Common mistakes

Oops

Building a long string with result += ch inside a loop.

Do this instead

Append pieces to a list and call "".join(parts) once at the end. It avoids copying the string every time.

Oops

Jumping into code and getting the last index wrong.

Do this instead

Say the boundaries out loud first: is right len(s) - 1 or len(s)? Test with an empty string and a single character.

Your mission

Four warm-up problems

~40 min

Quick check

You call list.insert(0, x) on a Python list with a million items. Roughly how much work is that?

Go deeper

  1. 1Solve reverse a string, find duplicates and move zeros to the end.
  2. 2Build strings with ''.join(parts) instead of += inside loops.
  3. 3Practise problems that need prefix sums.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge