Skip to main content

Code Katas

These are short, deliberate drills. Each takes 15-30 minutes. Repeat until the shape is automatic. Use your strongest language; Python, Java, and TypeScript are fine.

Every kata should end with:

  • a passing test
  • no commented-out debug code
  • one sentence in the commit message naming the pattern and the one axis of variation

Kata 1: Strategy for a Sort Comparator

Skill targeted: Strategy where a function reference is actually the right shape.

Instructions:

  • Build a sortPeople(people, strategy) function.
  • Provide three strategies: by name, by most recent login, by priority-then-name.
  • One strategy must capture configuration (locale or secondary key).
  • Show that a client switches strategies at runtime.

Extension: Convert one strategy to a class and justify in a comment why it deserved to be an object.

Time limit: 20 minutes.

Kata 2: Observer for a Stock Ticker

Skill targeted: Subject/observer wiring, snapshot iteration, unsubscribe tokens.

Instructions:

  • A Ticker publishes (symbol, price) updates.
  • Three observers: Printer, PriceAlert(threshold), MovingAverage(window).
  • subscribe returns an unsubscribe function.
  • Handle one observer throwing without breaking the others.
  • Test: add/remove observers during publication without corrupting iteration.

Time limit: 25 minutes.

Kata 3: Command with an Undo Stack

Skill targeted: Command + history, inverse vs snapshot.

Instructions:

  • A Counter with increment, decrement, set(value).
  • Wrap each as a Command. increment/decrement use inverses; set uses a snapshot.
  • History with bounded size 20 and a redo stack.
  • Test: 30 mixed operations followed by 30 undos returns to the initial counter.

Time limit: 25 minutes.

Kata 4: State for an Order Lifecycle

Skill targeted: State pattern and elimination of switch.

Instructions:

  • Order states: Placed, Paid, Shipped, Delivered, Canceled.
  • Operations: pay, ship, deliver, cancel.
  • Every state is its own class. Context holds currentState.
  • Test every (state, operation) cell.
  • Order contains no switch or if state ==.

Time limit: 30 minutes.

Kata 5: Iterator for a Tree

Skill targeted: Traversal decoupling, multiple orders, lazy iteration.

Instructions:

  • Binary tree with at least 15 nodes.
  • Provide inOrder, preOrder, postOrder iterators (generators are fine).
  • One traversal must be lazy: only fetch the next node when requested.
  • Test: consuming only the first 5 elements does not walk the whole tree.

Time limit: 25 minutes.

Kata 6: Chain of Responsibility for a Logger

Skill targeted: Handler chains, stop conditions, explicit ordering.

Instructions:

  • Handlers: LevelFilter(min), ConsoleSink, FileSink (simulate), MetricsCounter.
  • Build the chain externally; handlers do not hardcode each other.
  • LevelFilter stops the chain for messages below min; sinks forward.
  • Test: reordering sinks after LevelFilter does not change behavior; removing LevelFilter makes everything log.

Time limit: 25 minutes.

Kata 7: Template Method Refactor

Skill targeted: Pulling shared skeleton up, leaving hooks.

Instructions:

  • Start from two classes that both load, parse, and save data, with duplicated load/save.
  • Introduce a base class with a final run() template.
  • Extract shared steps; mark variable steps abstract; add one hook.
  • Verify that deleting one subclass's unused hook override does not change behavior.

Time limit: 20 minutes.

Kata 8: Command Queue Replay

Skill targeted: Command serialization, idempotency, replay.

Instructions:

  • Commands: Credit, Debit, Transfer. Pure data payloads.
  • In-process queue and a single worker that appends every accepted command to a JSON log.
  • Write a replay(log) that rebuilds balances from scratch.
  • Test: a crash simulated mid-batch; after restart, replay reaches the expected final state exactly.

Time limit: 30 minutes.

Completion Checklist

  • 6 or more katas finished with passing tests
  • Mistake log updated with at least 4 real errors caught
  • One kata repeated on a different day for retention
  • One kata rewritten in a second language to feel the pattern independent of syntax

Case Katas: Pattern Pressure

Meeting Scheduler

Given recurring meetings, one-off meetings, room-capacity checks, and notification channels:

  • choose one behavioral pattern only after naming the variation pressure
  • write the smallest implementation that proves the pattern helps
  • add a "when not to use it" note

Restaurant Order Flow

Given order states Draft, Placed, Preparing, Ready, Served, and Cancelled:

  • decide whether State, Command, or a plain transition table is the simplest correct model
  • write tests for invalid transitions
  • include a short critique of the pattern you did not choose