learningto/pass

Arrays & Strings

BeginnerData Structures

The foundation of all interview problems. Master two-pointer techniques, sliding window patterns, and in-place manipulation to solve the majority of Google array questions.

AVG TIME
O(n)
SPACE
O(1)
BEST
O(1)
WORST
O(n)

Step-by-Step Walkthrough

step-by-step walkthrough

Two Sum - Hash Map

1 / 7
algorithm.py
1def two_sum(nums, target):
2 seen = {}
3 for i, num in enumerate(nums):
4 complement = target - num
5 if complement in seen:
6 return [seen[complement], i]
7 seen[num] = i
8 return []
visualization
nums
2
0
7
1
11
2
15
3
variable inspectorStarting...
nums=[2, 7, 11, 15]
target=9
seen={}
i=None
num=None
complement=None
The Setup
We have an array of numbers and a target. We need to find two numbers that add up to the target and return their positions (indices). Our example: find two numbers in [2, 7, 11, 15] that add up to 9.
speedmed
← →space

In Python

Concept to Code- 2 structures for this topic
declare
nums = [1, 2, 3]
common ops
nums.append(x)
O(1)
nums.pop()
O(1)
nums[i]
O(1)
nums.insert(i, x)
O(n)
x in nums
O(n)
nums.sort()
O(n log n)
len(nums)
O(1)
say in interview
"I'll use an array to store the elements"
Python lists ARE dynamic arrays (like ArrayList in Java), NOT linked lists. Access by index is O(1).

Math You Need For This

Imagine scanning a hallway of n lockers. O(n) = open each locker once. O(1) space = you just use your two hands as pointers, no backpack.

Required concepts
Key math ideas
1 / 4

Linear growth - O(n)

If you have 10 items to check, you do 10 steps. 1,000 items? 1,000 steps. The work grows at exactly the same rate as the input.

Visual: A straight diagonal line on a graph. Double the input, double the time. No surprises.

Think of it this way: Reading every page of a book. If the book is twice as long, it takes twice as long to read.

For algorithms: O(n) is usually the target for one-pass algorithms like simple array traversals.

Interactive 3D Visualization

Array
Access: O(1)Search: O(n)Space: O(n)
Drag to orbit - Scroll to zoom

Brute Force vs Optimized

Interview Strategy - Two Sum

Start brute force, explain it, THEN optimize. "I can see a straightforward approach. Let me start there, then we can optimize."

Brute Force
Brute Force - Nested Loops
TimeO(n^2)
SpaceO(1)

"Check every pair of numbers. Two nested loops."

Pros
  • +Easy to understand
  • +No extra memory needed
  • +Good starting point in interview
Cons
  • -Too slow for large arrays (n=10,000 means 100 million checks)
  • -Interviewer will ask you to optimize
Optimized
Hash Map - Single Pass
TimeO(n)
SpaceO(n)

"Store each number in a hash map. Check if complement exists in O(1)."

Pros
  • +Fast - single pass through array
  • +Each lookup is O(1)
  • +This is what Google wants to see
Cons
  • -Uses O(n) extra memory for the hash map
The Tradeoff

Trading O(n) extra space to reduce time from O(n^2) to O(n). For n=10,000 that is 100,000,000 operations down to 10,000.

For n=1,000
Brute Force1,000,000 ops
Optimized1,000 ops
Say This

Say: "The brute force checks every pair in O(n^2). We can do better by using a hash map to remember what we have seen, giving us O(n) time with O(n) space."

Watch It Run

Two Pointers - Container With Most Water
Brute force would check every pair in O(n^2). Two pointers eliminate that by scanning from both ends - same answer in O(n) with O(1) space.
SPEED
1 / 11
Your data
algorithm.py
1>def max_water(height):
2  left, right = 0, len(height) - 1
3  best = 0
4  while left < right:
5  width = right - left
6  h = min(height[left], height[right])
7  area = width * h
8  best = max(best, area)
9  if height[left] < height[right]:
10  left += 1
11  else:
12  right -= 1
13  return best
array visualization
1[0]
8[1]
6[2]
2[3]
5[4]
4[5]
8[6]
3[7]
7[8]
0
1
2
3
4
5
6
7
8
i

Starting max_water on height=[1, 8, 6, 2, 5, 4, 8, 3, 7]. We want the two bars that hold the most water. Water volume = min(left height, right height) * width between them.

VariablesStep 1 of 11 (setup)
height=[186254837]
Spaceplay/pause← →step+ -speed

Interactive Playground

Experiment hands-on before writing a single line in the practice editor. Try different inputs and watch the structure behave.

interactive sandbox

Data Structure Playground

4
[0]
7
[1]
2
[2]
9
[3]
1
[4]
Click any element to delete it

Python Implementation

example.py
Loading...
Now try it yourself
3 challenges with test cases and AI feedback
Practice Now

Complexity Analysis

1481216nlog n1n (input size)
COMMON OPERATIONS
Array
Access:O(1)
Search:O(n)
Insert end:O(1)*
Insert mid:O(n)
Hash Map
Get/Set:O(1)*
Delete:O(1)*
Search value:O(n)
Iterate:O(n)
Linked List
Access:O(n)
Search:O(n)
Insert head:O(1)
Delete known:O(1)
Binary Search
Search:O(log n)
Insert:O(log n)
Balanced BST
Search:O(log n)
Insert:O(log n)
Delete:O(log n)
Heap
Peek min/max:O(1)
Push:O(log n)
Pop:O(log n)
Heapify:O(n)
Graph (BFS/DFS)
Traversal:O(V+E)
Dijkstra (heap):O((V+E) log V)
Merge / Quick Sort
Sort:O(n log n)
Space (merge):O(n)
Space (quick):O(log n)

* amortized average case

READY TO TEST YOURSELF?

Put Arrays & Strings into practice

The best way to lock in what you've learned is to write code. Solve real interview-style problems right now.

Next: Linked Lists
Arrays & Strings
Next: Linked Lists