Core Operations
Curriculum
The foundational mechanics layer of all DSA patterns. Patterns fail because operations are weak.
What This Section Is Really About
The physical act of moving through data. Forward, backward, sliding, and nested loops. Mastering the mechanical flow before adding logic.
Precision at the edges. Inclusive vs. exclusive ranges. Handling empty inputs, single elements, and the last index correctly every time.
Managing variables that change over time. Knowing exactly when to read, when to write, and when to reset counters, flags, and accumulators.
The logical guarantee that holds true before, during, and after each loop iteration. The bedrock of correctness for any algorithm.
Mental Model
Algorithms are state machines.
"Do not view a loop as just 'repeating code'. View it as a discrete set of state transitions. At index i, the state must perfectly summarize everything from 0 to i-1."
Core Invariant
"I know exactly what is true about my data at line N."
Core Operations You Must Master
Boundary Arithmetic
Understanding [0, n), [0, n-1], and length vs index. Calculating midpoints without overflow. Handling inclusive/exclusive ranges.
Indices always point to valid data or explicit sentinels.
Conditional Ordering
Check-then-update vs Update-then-check. Determining the correct sequence of operations to avoid using stale state or overwriting data prematurely.
State is consistent before any mutation.
In-Place Mutation
Swapping, overwriting, and shifting data without auxiliary space. Managing read/write pointers to avoid data corruption.
No data is lost unless explicitly discarded.
State Resetting
Re-initializing counters, flags, and temporary variables at the start of inner loops or new passes. Preventing state leakage.
Start fresh context with clean state.
Sentinel Usage
Using -1, Infinity, or dummy nodes to handle edge cases uniformly. Removing the need for complex 'if' checks for boundaries.
Sentinels simplify logic, never corrupt result.
Coordinate Mapping
Mapping 2D coordinates to 1D indices and vice versa. Rotating matrices. Translating problem domains.
Bijective mapping between representations.
Complexity Thinking at the Operations Layer
speedHidden Costs
- !String Concatenation:
s += cin a loop is O(N²) in Java/C#/Go because strings are immutable. Use StringBuilders. - !Array Shifting:
insert(0, val)orremove(0)is O(N). Doing this in a loop makes your algorithm O(N²). - !Slicing: Creating a subarray slice
arr[i:j]often creates a copy (O(N) space/time). Pass references or indices instead.
Canonical Drill List
auto_awesome_motionFoundation — Mechanics
auto_awesome_motionIntermediate — State & Boundaries
Common Failure Patterns
Off-By-One Errors
Confusing length vs last index, or inclusive vs exclusive ranges.
Always use [start, end) (inclusive-exclusive) for ranges. Loop condition i < length.
Checking After Updating
Mutating state before verifying it, leading to self-matching or skipping checks.
Check conditions on the current state FIRST, then perform updates/mutations.
Stale State
Forgetting to reset counters or flags in nested loops.
Explicitly initialize state variables immediately before the loop that uses them.
Index Out of Bounds
Accessing i+1 or i-1 without boundary checks.
Guard clauses: if (i > 0) or if (i < n - 1). Use sentinels to remove checks.
Interview Guidance
What they test
- →Code correctness (bugs)
- →Edge case handling
- →Clean implementation
- →Operational speed
Strategy
Don't rush to patterns. First, verify the mechanics. "I'll iterate from 0 to N-1." "I need to handle the empty case." "I will perform the check before the update."
The Verbal Template
"My loop runs from 0 to N, exclusive. This covers all indices."
"At the start of each iteration, 'count' represents valid items found so far."
"If the input is empty, my loop won't execute, and I'll return the default value."
"I'll update the pointer only after confirming the condition to avoid overwriting needed data."
When You’ve Mastered Core Operations
Write loops without off-by-one errors
Handle empty inputs instinctively
Manage indices without confusion
Spot O(N²) string ops immediately
Use exclusive ranges consistently
Separate checks from updates cleanly
What This Unlocks Next
Sliding Window
Relies entirely on precise boundary movements and state tracking.
Two Pointers
Requires perfect index manipulation and conditional logic.
Binary Search
The ultimate test of boundary handling and invariants.
Matrix Traversal
Complex state management and coordinate mapping.