Why Should Students Explain an Algorithm Before Coding?

☰ Start NorthStar

AI Learning & Future Skills

Why Should Students Explain an Algorithm Before Coding?

Explain your model and test your reasoning before asking AI to write code.

AI 学习与未来技能

为什么学生应该先讲清算法,再开始写代码?

先讲清模型、检验推理,再请 AI 协助写代码。

Share this article

Copy the link, open your phone share sheet, scan the QR code for WeChat, or send by email.

Email

Code can look convincing while hiding a wrong model. A short algorithm explanation exposes the model before syntax makes it harder to inspect. This is especially important when AI can produce polished code from an incomplete request.

Explanation Is a Test

“I will use BFS” is a label, not an explanation. A useful explanation answers five questions:

  1. Model: What are the objects and relationships?
  2. State: What information must be stored?
  3. Transition: How does one step change the state?
  4. Invariant: What remains true after every step?
  5. Complexity: How much work and memory are required?

If any answer is vague, coding is likely to turn that vagueness into a bug.

Worked Example: Choose your own path

Consider CCC 2018 J5. A weak plan says:

Use BFS and print the answers.

This does not explain what the graph is, what distance means, or why the search must continue after finding an ending.

A stronger explanation is:

Each page is a vertex and each choice is a directed edge.
Run BFS from page 1 and store distance as the number of pages read,
so the source starts at 1. The first ending dequeued has minimum distance
because BFS processes distance layers in order. Record that value, but finish
the traversal so the visited count can determine whether every page is reachable.
The running time is O(N + E).

Now the code is mostly a translation of decisions already made.

Where AI Helps

AI is most useful after the student provides a concrete explanation. Ask it to challenge the reasoning, generate counterexamples, compare alternatives, or review an implementation against the stated invariant.

Good requests include:

Here is my model and algorithm. Ask me three questions that could expose a gap.
Do not write code yet.
Find the smallest counterexample to my stopping condition.
Explain which assumption fails.
Review this code only against the invariant and complexity I stated.
Point to mismatches before suggesting edits.

Where AI Should Wait

A prompt such as “solve this problem in Python” skips the student's model-building step. The result may run, but the learner cannot tell whether a directed edge was treated as undirected, whether distance counts pages or choices, or whether the stopping condition is valid.

The issue is not that code generation is always wrong. The issue is that an unexplained answer is difficult for the learner to verify or transfer to a new problem.

A Predict–Run–Explain Loop

Use this short loop during practice:

  1. Predict: Write the model, expected state changes, and output for a small case.
  2. Run: Execute the algorithm or code on that case.
  3. Explain: Reconcile every difference between prediction and result.

Do not repair a mismatch by changing code at random. First decide whether the model, transition, expected result, or implementation was wrong.

The 60-Second Readiness Check

Before opening the editor, answer these aloud or in writing:

  • What is one state?
  • What information makes two states different?
  • How do I move to the next state?
  • Why will the process find the required answer?
  • When can it stop?
  • What is the worst-case complexity?

If the answers fit together, implementation becomes faster to review and easier to debug.

Common Failure Modes

  • Naming an algorithm without explaining why it applies.
  • Describing code lines instead of the underlying state transition.
  • Letting AI choose a model without checking the problem statement.
  • Giving only normal examples and never testing an edge case.
  • Accepting complexity claims that do not match the data structure operations.
  • Asking for a final solution before making a prediction.

A Better Collaboration Contract

Student responsibilities:

  • State the model and assumptions.
  • Predict at least one small trace.
  • Decide what evidence would change the current plan.

AI responsibilities:

  • Ask for missing definitions.
  • Challenge the stopping condition and edge cases.
  • Separate reasoning feedback from implementation feedback.
  • Explain why a proposed correction works.

Review

  1. Why is an algorithm name not yet an explanation?
  2. Which of the five parts would reveal an off-by-one distance definition?
  3. What should AI do before generating code if the model is incomplete?

Sources and Companion Reading

  • CEMC 2018 Junior problem set: https://cemc.uwaterloo.ca/sites/default/files/documents/2018/2018CCCJrProblemSet.html
  • Python collections.deque: https://docs.python.org/3/library/collections.html#collections.deque
  • Companion lesson: Why Does BFS Find Shortest Paths?

Read Next

代码可能看起来很完整,却建立在错误模型上。简短的算法解释能在语法遮住问题之前暴露模型错误。当 AI 可以根据不完整要求生成漂亮代码时,这一步尤其重要。

解释本身就是测试

“我会用 BFS”只是算法名称,不是解释。有效解释应回答五个问题:

  1. 模型: 题目中的对象和关系是什么?
  2. 状态: 必须保存哪些信息?
  3. 转移: 每一步如何改变状态?
  4. 不变量: 每一步之后什么性质仍然成立?
  5. 复杂度: 需要多少时间与空间?

如果其中任何一项含糊,代码很可能只是把含糊变成 bug。

示例:Choose your own path

Use BFS and print the answers.

这句话没有解释图是什么、距离代表什么,也没有说明为什么找到结局后仍要继续。

Each page is a vertex and each choice is a directed edge.
Run BFS from page 1 and store distance as the number of pages read,
so the source starts at 1. The first ending dequeued has minimum distance
because BFS processes distance layers in order. Record that value, but finish
the traversal so the visited count can determine whether every page is reachable.
The running time is O(N + E).

更完整的中文解释是:

每个页面是节点,每个选择是有向边。从第 1 页开始 BFS。
distance 表示读过的页面数,因此起点为 1。BFS 按距离层处理,
所以第一个出队的结局最短。记录最短值后继续搜索,最后用访问数
判断是否所有页面可达。时间复杂度为 O(N + E)。

此时,代码基本只是把已经做出的决定翻译成语法。

AI 适合做什么

当学生已经给出具体解释后,AI 最有价值。可以让它质疑推理、生成反例、比较替代方案,或根据已声明的不变量审查实现。

Here is my model and algorithm. Ask me three questions that could expose a gap.
Do not write code yet.
Find the smallest counterexample to my stopping condition.
Explain which assumption fails.
Review this code only against the invariant and complexity I stated.
Point to mismatches before suggesting edits.

有效的中文提示可以是:

这是我的模型和算法。请先提出三个可能暴露漏洞的问题,不要写代码。
请找出能推翻我停止条件的最小反例,并解释哪个假设失败。
请只根据我写出的不变量和复杂度审查代码,先指出不一致,再建议修改。

AI 应该暂缓什么

“用 Python 解这道题”会跳过学生的建模过程。生成的代码或许能运行,但学习者无法判断:有向边是否被误当成无向边、距离数的是页面还是选择、停止条件是否成立。

问题不是代码生成一定错误,而是没有解释的答案很难由学生验证,也很难迁移到新题。

预测、运行、解释

练习时可以使用这个循环:

  1. 预测: 写出模型、小样例中的状态变化和预期输出。
  2. 运行: 在该样例上执行算法或代码。
  3. 解释: 对预测与结果的每处差异给出原因。

不要通过随机改代码来消除差异。先判断错的是模型、转移、预期结果,还是实现。

60 秒开工检查

打开编辑器前,先口头或书面回答:状态是什么?状态如何区分?如何转移?为什么能找到答案?什么时候停止?最坏复杂度是多少?

如果这些答案能彼此衔接,实现会更容易审查,也更容易调试。

常见失败方式

更好的协作约定

学生负责提出模型、假设和预测;AI 负责追问缺失定义、挑战边界条件,并把推理反馈与代码反馈分开。这样的分工保留了学习者的判断权。

复习

  1. 为什么只说算法名称还不算解释?
  2. 五个部分中的哪一项最容易暴露距离定义的差一错误?
  3. 如果模型不完整,AI 在生成代码前应该先做什么?

来源及延伸阅读

  • CEMC 2018 Junior problem set: https://cemc.uwaterloo.ca/sites/default/files/documents/2018/2018CCCJrProblemSet.html
  • Python collections.deque: https://docs.python.org/3/library/collections.html#collections.deque
  • Companion lesson: Why Does BFS Find Shortest Paths?

延伸阅读

Questions or feedback?

Have a question about this article or want to suggest an improvement? Send us a private message.

Send Feedback

有问题或建议?

如果你对本文有疑问,或希望提出改进建议,请给我们发送私密留言。

发送反馈
Back to AI Learning & Future Skills
返回AI 学习与未来技能

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top