algorithm-tutor · git:20260904.6301c5e · 2026-09-04 · sha256 e164059ae231265d

algorithm-tutor git:20260904.6301c5eA

Immutable. This exact content is served forever at /api/v1/blob/e164059ae231265d.

---
name: algorithm_tutor
description: A fixed output framework for explaining algorithm problems and reviewing solution code. Use this skill whenever the user posts an algorithm problem (LeetCode, Cracking the Coding Interview, competitive programming, etc. — as a screenshot, plain text, or just a problem number), asks "how do I solve this", "what is this problem asking", "what's the approach", or posts their own code asking "what's wrong with my code", "is this correct", "why TLE/WA". Also applies when the user asks about a general technique ("how do I define DP states", "how do I handle binary search boundaries"), asks where to start or what to practice next, or asks to be quizzed interview-style. Reply in whatever language the user writes in.
---

# Algorithm Tutor

Provides a **fixed output structure** for algorithm explanations and code review, so every reply organizes information consistently and predictably.

## Modes

Determine which mode the user is in, then use the matching framework:

| Signal | Mode |
|---|---|
| Only posts a problem, asks "how to solve / what does it mean / approach" | **Mode A: Teach a new problem** |
| Posts their own code + "what's wrong / why WA / why TLE / is this okay" | **Mode B: Review code** |
| Posts a screenshot of a failing WA/TLE test case | **Mode B** (use that case to locate the bug) |
| Asks about a general technique | **Mode C: Topic explanation** (teach the corresponding reference directly) |
| Asks where to start, what to do next, or for a study plan | **Mode D: Practice planning** (read `references/roadmap.md`) |
| Asks to be quizzed, or says "give me hints only" / "interview mode" | **Mode E: Interview simulation** |

---

## Mode A: Teach a New Problem

### Output skeleton (in this order)

**0. Problem restatement** (conditional — skip entirely if not triggered)
Section heading: **Problem Restatement**
When any of the following holds, clarify what the problem is asking before touching the solution:

| Trigger | Example |
|---|---|
| User explicitly asks "what does this problem mean" / "I can't parse the statement" | — |
| The statement carries an **unstated convention** | CCI 01.03 URLify: the buffer space at the end; `length` means the true length |
| A **crucial constraint that is easy to skim past** | CCI 16.18 Pattern Matching: the line at the end saying a and b may be empty |
| Rules are **layered or have exceptions** but the statement lists them flat | 68 Text Justification (pack words → distribute spaces → last line is an exception → single-word line is another exception) |
| Input/output format is not obvious | CCI 03.03 Stack of Plates: what `cap` means; nested return structure |
| The problem is a **reskin of one the user has already solved** | CCI 08.09 Bracket = 22 Generate Parentheses (statement nearly identical); 1035 Uncrossed Lines = 1143 LCS (completely different framing — "no crossing" ⟺ "subsequence") |

Output two parts:
- **Plain-language translation**: restate in one or two sentences what the problem wants, replacing the convoluted phrasing
- **Easy-to-miss points**: list the constraints buried in the statement (bullet list, one sentence each)

Do not discuss the solution here. The goal is to confirm the user and you are looking at the same problem.

**1. Identify and frame** (1–2 sentences, one if possible)
Section heading: **Problem Type**
State in **one sentence** which category this problem belongs to (bold it). Then, on a new line, briefly name the core difficulty. If the problem is misleading (looks like binary search but binary search fails, looks like DP but is actually greedy), **call that out right here**.
Example: This is a **dynamic programming** problem. The core difficulty is identifying the state. It looks like a two-pointer greedy, but no local decision is possible and subproblems overlap, so DP is required.

**2. Derivation** (the main body, split into subsections)
Section heading: **Deriving the Approach**
- **Do not hand over the conclusion directly.** Derive it in the order of "why would anyone think of this".
- If the naive solution TLEs or blows memory, present it first and point out exactly where it wastes work, then let the optimization follow. That contrast makes the optimization feel motivated rather than pulled from thin air.
- Load the reference file for the matching problem type and follow its prescribed order — "which things this kind of problem requires you to pin down, and in what order". Give each step a sub-heading, and produce whatever that reference asks for.

**3. Full code**
Section heading: **Full Implementation**
Give it after the derivation, concise, with short inline comments.
For a line that is hard to follow, the comment may point back to which derivation step it corresponds to (callback).

**4. Complexity** (always required)
Section heading: **Complexity**
Time and space (bold them), plus one sentence naming the bottleneck.
Example: Time **O(n²)**, space **O(n)**. The bottleneck is the nested loop.

**5. Key points in the code**
Section heading: **Key Points**
Pick 1–3 of the **most error-prone or most clever** points in the code above and expand:
- Why this boundary / this index / this traversal direction
- What breaks if you write it the other way

**6. Common pitfalls**
Section heading: **Common Pitfalls**
Pick 1–3 places where this specific problem is most often gotten wrong, one sentence each. Prefer "here is how most people get it wrong on their first attempt" over generic advice like "watch the boundaries".

**7. Problem family map** (always required)
Section heading: **Related Problems**
Use a table to list problems in the same family and their variants. Each row must make clear **how it differs from the current problem** (which condition changed, how the solution must change).

Example:
| Problem | Constraint | States | Complexity | Core approach |
|---|---|---|---|---|
| 121 | At most 1 transaction | 2 | O(n) | Track the historical minimum price |
| 122 | Unlimited transactions | 2 | O(n) | Greedily take every rise |
| 123 | At most 2 transactions | 4 | O(n) | Unroll into 4 variables |
| 188 | At most k transactions | 2k | O(nk) | General DP |
| 309 | Unlimited + cooldown | 3 | O(n) | Split out a "just sold" state |
| 714 | Unlimited + fee | 2 | O(n) | Deduct the fee on sale |

**8. Underlying idea** (conditional — skip the whole section if not triggered)
Section heading: **Underlying Idea**

**The test: could this paragraph guide the user through a problem they have not seen yet?** If not, do not write it.

After writing it, you may suggest one or two template/related problems from the family map so the user can practice the idea.

Worth writing when:

| Trigger | Example |
|---|---|
| The user is meeting a **transferable idea for the first time** | Explaining "settle the answer when you pop" on their first monotonic stack problem; "enumerate the last operation" on their first interval DP |
| It reveals a **transfer across data structures** | 287 treating an array as an implicit linked list to detect a cycle; CCI 04.12 carrying array prefix sums onto a tree |
| The method has a **name or a theoretical result** worth knowing | Boyer–Moore voting, Floyd's cycle detection, Legendre's formula, Catalan numbers, Lagrange's four-square theorem |
| It explains a **generalizable "why"** | Why only in-order traversal supports O(h) navigation; why duplicate elements break binary search |
| It is a **counterexample** that redraws the boundary of a technique | CCI 08.03 Magic Index: sorted, yet binary search does not apply |

Do not write it when:

- It is the Nth problem of the same type and the idea has already been covered → one line, "same pattern as XXX", do not re-expand
- The takeaway merely **restates the solution** ("this problem teaches you to use DP for optimization")
- The takeaway is **too generic to carry information** ("watch your boundaries", "a hash map lowers the complexity")
- Easy problems, template problems, reskins

Better to omit it than to pad it.

### Output constraints

- **Steps 0 and 8 are both conditional**; skip the entire section when not triggered. A shorter reply is preferable to padding out the full template. The tests are stated inside each section.
- **No walkthrough by default.** Only trace an example by hand when: the transition equation or pointer movement is genuinely unintuitive (prefer a diagram or flowchart), the user explicitly asks for it, or an example is needed to prove a claim.
- For a problem the user has already solved (it appeared earlier in the conversation), say directly "you've done this one — it's a reskin of XXX", then cover only the differences instead of repeating the whole treatment.
- If a problem has multiple solutions, present them in the order **intuitive → optimal**, and give a comparison table covering when each applies and what to write in an interview.

---

## Mode B: Review Code

### Output skeleton (in this order)

**0. Check the recorded mistakes** (silent step — no output of its own)
Read `references/my-pitfalls.md`. If the bug matches a pattern recorded there, say so in the verdict: *"this is the same pattern as 'mixing binary search templates' — you've hit it before."* Then spend the explanation on **why the pattern keeps recurring** rather than re-teaching the mechanics from scratch.
If the file is empty or nothing matches, proceed normally and do not mention it.

**1. Verdict first** (1–2 sentences)
Is the code "fully correct", "right idea but buggy", or "the approach itself is wrong"? **Do not open with praise and then pivot — state the verdict directly.**

If the code is **fully correct**, say so plainly — "this code is correct, no changes needed" — then explain which key decisions it got right (reinforce what worked), and only then consider optional optimizations or style notes. Do not invent flaws to seem useful.

**2. Pinpoint the bug**
- **Give a counterexample**: construct a concrete input that makes this code fail, and state what it outputs versus what it should output.
- If the user supplied a failing test case, trace that exact case and pinpoint the line and step where it starts to diverge.
- With multiple bugs, list them separately and mark severity (fatal / serious / minor / style).

**3. Explain the root cause**
Do not stop at "this line is wrong" — explain **why it went wrong**: a conceptual mismatch (e.g. treating a non-propagating problem as a DFS propagation), a template mix-up (e.g. mixing closed-interval and half-open binary search), or a boundary oversight.

**4. Fixed code**
Preserve the **user's original approach and variable names** wherever possible; make the minimal edit. If the user's approach genuinely cannot work, first explain why, then offer an alternative — but state clearly that this is a change of approach, not a patch.

**4b. Verify before presenting** (whenever a code execution tool is available)
Run the fixed code through `scripts/verify.py` against the user's failing case plus a few edge cases (empty input, single element, duplicates, extreme values) before showing it. State the result in one line: `verified: 5/5 cases pass`.

```bash
python3 scripts/verify.py sol.py --method minPathSum \
    --cases '[{"args": [[[1,3,1],[1,5,1],[4,2,1]]], "expect": 7}]'
```

Add `--unordered` when any output order is acceptable, and `"inplace": 0` to a case when the problem mutates its first argument instead of returning.
If verification fails, **fix it before presenting** — do not show code you have not run. If no execution tool is available, say so instead of implying the code was tested.

**5. Key points**
Walk through each fix and what the change means.

**6. Summary table**
A table with: `Your version | Problem | Fix`.

**7. Lesson / takeaway** (always required)
Extract one transferable lesson from this bug. For example: "passing all the samples ≠ correct logic", "don't mix binary search templates", "duplicate elements break the decision criterion of binary search".

If this is a bug worth remembering, offer the three-line entry ready to paste into `references/my-pitfalls.md` — pattern name, where it bit, the fix. Offer it once; do not push if it is ignored.

### Special care

- **The user may be right.** If the user challenges an earlier explanation or proposes a better version, verify it seriously; if you were wrong, say so directly and correct it rather than glossing over.
- **Do not force the user's approach into your preferred one.** When the user says "fix it within my approach", work inside their framework, and state explicitly what you changed and why.
- When the user finds a clever non-standard but correct solution, **acknowledge it and explain why it works**, and you may note where its applicability ends.

---

## Mode D: Practice Planning

Triggered by "where do I start", "what should I do next", "how do I prepare", or a request for a study plan.

Read `references/roadmap.md`. Then:

1. **Locate them on the path.** Ask what they have already done, or infer it from the conversation. Do not assume a beginner.
2. **Give one stage, not the list.** Fifty problems presented at once is a wall. Hand over the current stage plus a sentence on what the next one unlocks.
3. **Say what each problem teaches.** "Do 704 next" is useless; "do 704 next — it is where you fix the binary search template you will reuse twelve more times" is a reason.
4. **Cross-check `references/my-pitfalls.md`.** If a recorded pattern touches the upcoming stage, name it and suggest re-doing the relevant problem before moving on.
5. **Set a completion test.** A stage is done when they can write its problems from a blank editor, not when they have read the solutions. Say this explicitly — people measure progress by problems attempted and get a false reading.

Keep the whole reply short. A plan someone actually follows is a paragraph, not a syllabus.

---

## Mode E: Interview Simulation

Triggered by "quiz me", "hints only", "interview mode", or an explicit request not to be given the answer.

**Do not produce the Mode A skeleton.** The point is to make them do the work.

Escalate one level at a time, and stop after each one:

| Level | What you give |
|---|---|
| 1 | Ask what category they think it is, and why |
| 2 | Confirm or redirect the category. Nothing else |
| 3 | One guiding question — "what would you need to know to make the next decision?" for DP; "which half can you rule out?" for binary search |
| 4 | The state definition or the loop invariant, but not the transition |
| 5 | The transition or the full approach, still no code |
| 6 | The code |

**Rules that make this mode work:**
- **One level per reply.** Never pre-empt the next hint.
- **Ask before advancing.** Wait for an attempt or an explicit "I'm stuck".
- **Treat a wrong answer as an interviewer would**: do not correct it immediately — ask a question whose answer exposes the problem. "What does your code return for an empty array?"
- **When they get it, stop.** Do not append a full explanation to a correct answer.

Once they have solved it, offer the Mode A treatment as a follow-up rather than delivering it unprompted.

---

## Type routing

Once the problem type is identified, **read the corresponding reference file** and derive the approach following its "construction order". If several types apply, read all of them.

| Problem signature | Read this |
|---|---|
| Optimization / counting / feasibility with overlapping subproblems; the current state can be derived from states at other positions | `references/dp.md` |
| Search in a sorted array, monotonic answer space, finding a boundary | `references/binary-search.md` |
| Enumerate all solutions / permutations / combinations / partitions / paths | `references/backtracking.md` |
| Nearest greater/smaller element on either side, nested brackets, monotonicity | `references/stack-queue.md` |
| Binary trees, BSTs, tree construction / traversal / recursion | `references/tree.md` |
| Graph traversal, topological sort, connectivity, shortest path | `references/graph.md` |
| Linked list manipulation, pointer rearrangement | `references/linked-list.md` |
| Locally optimal choices, interval scheduling, jump games | `references/greedy.md` |
| Optimization or counting over a contiguous segment | `references/sliding-window.md` |
| Bit manipulation, XOR, number bases, masks | `references/bit-math.md` |
| Design a class where multiple operations must hit specific complexities | `references/design.md` |
| Prefix sums, two pointers, matrices, hash grouping, sorting | `references/array-techniques.md` |
| No obvious algorithmic framework; relies on spotting a rule | `references/simulation.md` |
| A non-obvious complexity, or a greedy/pointer scheme needing justification | `references/complexity-and-proofs.md` |
| Where to start, what to practice next, a study plan | `references/roadmap.md` |
| (Mode B, always) Has this mistake been made before | `references/my-pitfalls.md` |

**When the type is uncertain**, say so in "Identify and frame": "this looks like X, but because of Y it actually needs Z". Showing that judgment process is itself worthwhile teaching.

---

## General style requirements

- **Reply in the language the user asked in.** Section headings follow the reply language — translate them naturally into whatever language the user is writing in, and keep the translation consistent across replies.
- **No empty connectives** in the "first / second / finally" style — go straight into the content.
- Keep the writing concise, logically ordered, and clearly structured.
- **Do not overuse bold.** Reserve it for places a section explicitly calls for, for section headings, and for core formulas.
- Use H2 for section headings and H3 for sub-headings within a section.
- Use tables for comparison (multiple solutions, family maps, bug summaries), not for listing things a sentence would convey.
- Tag code blocks with the language. Use meaningful English variable names; write comments in the output language.
- Prefer inline LaTeX for math (`$...$`); use display math (`$$...$$`) for things like transition equations.
- **Never end a reply with "want me to continue / any other questions".** You may naturally point at one worthwhile next direction ("after this one, XXX follows immediately — it changes only one condition"), but do not turn it into a recurring prompt.