settings

Core Operations
Curriculum

The foundational mechanics layer of all DSA patterns. Patterns fail because operations are weak.

What This Section Is Really About

sync_altIteration Mechanics

The physical act of moving through data. Forward, backward, sliding, and nested loops. Mastering the mechanical flow before adding logic.

crop_freeBoundary Handling

Precision at the edges. Inclusive vs. exclusive ranges. Handling empty inputs, single elements, and the last index correctly every time.

history_eduState Tracking

Managing variables that change over time. Knowing exactly when to read, when to write, and when to reset counters, flags, and accumulators.

gavelInvariant Maintenance

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

The Operational Contract
Every operation maintains a precise contract. Input assumptions, output guarantees, and state transitions are explicit and verified.

"I know exactly what is true about my data at line N."

Core Operations You Must Master

straighten

Boundary Arithmetic

Understanding [0, n), [0, n-1], and length vs index. Calculating midpoints without overflow. Handling inclusive/exclusive ranges.

// Invariant
Indices always point to valid data or explicit sentinels.
compare_arrows

Conditional Ordering

Check-then-update vs Update-then-check. Determining the correct sequence of operations to avoid using stale state or overwriting data prematurely.

// Invariant
State is consistent before any mutation.
swap_horiz

In-Place Mutation

Swapping, overwriting, and shifting data without auxiliary space. Managing read/write pointers to avoid data corruption.

// Invariant
No data is lost unless explicitly discarded.
restart_alt

State Resetting

Re-initializing counters, flags, and temporary variables at the start of inner loops or new passes. Preventing state leakage.

// Invariant
Start fresh context with clean state.
low_priority

Sentinel Usage

Using -1, Infinity, or dummy nodes to handle edge cases uniformly. Removing the need for complex 'if' checks for boundaries.

// Invariant
Sentinels simplify logic, never corrupt result.
open_with

Coordinate Mapping

Mapping 2D coordinates to 1D indices and vice versa. Rotating matrices. Translating problem domains.

// Invariant
Bijective mapping between representations.

Complexity Thinking at the Operations Layer

speedHidden Costs

  • !String Concatenation: s += c in a loop is O(N²) in Java/C#/Go because strings are immutable. Use StringBuilders.
  • !Array Shifting: insert(0, val) or remove(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

Common Failure Patterns

error_outline

Off-By-One Errors

Failure Mode

Confusing length vs last index, or inclusive vs exclusive ranges.

Mastery Fix

Always use [start, end) (inclusive-exclusive) for ranges. Loop condition i < length.

error_outline

Checking After Updating

Failure Mode

Mutating state before verifying it, leading to self-matching or skipping checks.

Mastery Fix

Check conditions on the current state FIRST, then perform updates/mutations.

error_outline

Stale State

Failure Mode

Forgetting to reset counters or flags in nested loops.

Mastery Fix

Explicitly initialize state variables immediately before the loop that uses them.

error_outline

Index Out of Bounds

Failure Mode

Accessing i+1 or i-1 without boundary checks.

Mastery Fix

Guard clauses: if (i > 0) or if (i < n - 1). Use sentinels to remove checks.

Interview Guidance

Interview Protocol

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

Bounds

"My loop runs from 0 to N, exclusive. This covers all indices."

Invariant

"At the start of each iteration, 'count' represents valid items found so far."

Edge Case

"If the input is empty, my loop won't execute, and I'll return the default value."

Update

"I'll update the pointer only after confirming the condition to avoid overwriting needed data."

When You’ve Mastered Core Operations

done_all

Write loops without off-by-one errors

done_all

Handle empty inputs instinctively

done_all

Manage indices without confusion

done_all

Spot O(N²) string ops immediately

done_all

Use exclusive ranges consistently

done_all

Separate checks from updates cleanly

What This Unlocks Next

swipe

Sliding Window

Relies entirely on precise boundary movements and state tracking.

compare_arrows

Two Pointers

Requires perfect index manipulation and conditional logic.

search

Binary Search

The ultimate test of boundary handling and invariants.

grid_on

Matrix Traversal

Complex state management and coordinate mapping.