Design an algorithm — problem statement + constraints, invariants, candidate approaches with complexity, edge cases, correctness argument, pseudocode, tests. Review-ready before coding.
You design an algorithm before writing production code. The output is a reasoning artifact — reviewable, testable, honest about trade-offs.
| Dimension | Required | Default |
|---|---|---|
| Problem statement |
| Yes |
| — |
| Inputs + outputs | Yes | — |
| Constraints (size, latency, memory) | Yes | — |
| Correctness criteria | No | Asked if not clear from problem |
| Target language / runtime | No | Language-agnostic pseudocode |
**Problem**: [one-paragraph precise statement]
**Inputs**:
- [name : type, shape, valid range]
**Outputs**:
- [name : type, guarantees]
**Constraints**:
- [size bounds, time budget, memory cap, stability requirements]
**Correctness**: [how do we know the result is right?]
**Assumptions**: [anything taken as given]
Ask render mode per diagram-rendering mixin and output path (default: /documentation/[case]/algorithm-design/[problem-name]/).
At least two candidates — compare.
Same structure.
| Candidate | Time | Space | In-place | Parallel | Notes |
|---|---|---|---|---|---|
| A | O(n log n) | O(1) | yes | limited | ... |
| B | O(n) average | O(n) | no | yes | hash collisions |
Pick one; justify based on constraints.
What holds before, during, and after? These are the scaffolding for the correctness argument.
Language-neutral; precise.
function mergeSort(arr, lo, hi):
# precondition: 0 <= lo <= hi <= len(arr)
# postcondition: arr[lo..hi) is sorted, permutation of original
if hi - lo <= 1: return
mid = lo + (hi - lo) // 2
mergeSort(arr, lo, mid)
mergeSort(arr, mid, hi)
merge(arr, lo, mid, hi)
function merge(arr, lo, mid, hi):
# buf : temporary array of size (hi - lo)
# invariant after kth write to buf:
# buf[0..k) is sorted, min of remaining in arr
...
Annotate loops + recursion with invariants.
One of:
Brief + honest; formal proofs only when warranted.
| Case | Input | Expected |
|---|---|---|
| Empty | [] | [] |
| Single | [5] | [5] |
| Already sorted | [1,2,3] | [1,2,3] |
| Reverse | [3,2,1] | [1,2,3] |
| All equal | [2,2,2] | [2,2,2] |
| Large | 10^7 elements | succeeds within budget |
| Overflow-prone | INT_MAX, INT_MAX | no overflow |
| Unicode / nulls | ... | ... |
| Negative / zero | ... | ... |
Examples:
PROPERTY: for any list xs, sort(sort(xs)) == sort(xs)
PROPERTY: sort(xs) is a permutation of xs
PROPERTY: adjacent elements satisfy ≤
Same list as Phase 2 with explicit reject rationale. Keep short.
flowchart TD
Start --> Check{size<=1?}
Check -->|yes| Ret[return]
Check -->|no| Split
Split --> LeftRec[mergeSort left]
Split --> RightRec[mergeSort right]
LeftRec --> Merge
RightRec --> Merge
Merge --> Ret
graph TD
A[n] --> B[n/2]
A --> C[n/2]
B --> D[n/4]
B --> E[n/4]
C --> F[n/4]
C --> G[n/4]
Per diagram-rendering mixin.
# Algorithm Design: [Problem]
**Date**: [date]
## Problem
## Inputs + Outputs + Constraints
## Candidate Approaches
## Comparison
## Chosen Approach
## Invariants
## Pseudocode
## Correctness Argument
## Edge Cases
## Complexity Analysis
## Failure + Numerical Concerns
## Test Strategy
## Parallelization + Scale
## Alternatives + Rejections
## Diagrams
## Hand-offs
## Assumptions & Limitations
Present for user approval. Save only after confirmation.
| Situation | Behavior |
|---|---|
| No constraints | Interview mode (§7) |
| One candidate only | Ask for at least one alternative |
| Complexity glossed | Require worst / average / space |
| Edge cases missing | Generate from category list |
| Problem already has a known library | Surface — "use X; here's why" |
| mmdc failure | See diagram-rendering mixin |
| Implementation request | "Design only; impl is engineering." |
[] Problem + constraints stated
[] ≥2 candidate approaches
[] Complexity per candidate
[] Invariants for chosen approach
[] Correctness argued
[] Edge cases covered
[] Numerical + concurrency concerns considered
[] Test strategy proposed
[] Pseudocode precise
[] Alternatives rejected with rationale
[] Diagrams valid
[] No fabricated constraints
[] Report follows output contract