How to Spot AI-Generated Code in a Programming Assignment

When Submissions Look Too Perfect

Picture a data structures course grading its first major programming assignment: implement a BinarySearchTree class with insertion, deletion, and traversal. Most submissions look the way student code usually looks, uneven and a little rough. But a growing share look different.

The first sign is often submissions that are flawless. Not just functionally correct, but stylistically pristine: consistent, verbose error handling, comments that read like technical documentation, and a structural polish you rarely see in second-year code. Several of them implement the tricky remove() function, with its node promotion and subtree reattachment, in almost the same way.

Run the batch through MOSS and it may come back clean, with low pairwise similarity across the board. That is not a failure of MOSS. It was built to find copied code blocks, and AI-generated code is not copied from a classmate. Each student got a fresh answer from a model, so there is nothing for a peer comparison to match.

The Hallmarks of Synthetic Code

Put ten suspicious submissions side by side and the differences in variable names and formatting start to look like a mask over deeper commonalities.

First, the commentary. Students tend to comment sparingly, and often poorly. Generated submissions often carry lavish, generic docstrings:

/**
 * Recursively removes a node with the specified value from the binary search tree.
 * Utilizes a helper function to handle the three distinct cases of node deletion:
 * leaf node, node with one child, and node with two children.
 * @param value The integer value to be removed from the tree.
 * @throws std::runtime_error if the tree is empty or the value is not found.
 */

Few students write "utilizes" in a comment or throw a std::runtime_error for a missing value in a classroom assignment. They return false, or print "Not found" and move on.

Second, error handling that is robust yet contextually naive, such as checks for memory allocation failure in an environment where new never returns nullptr.

Third, and often most telling, the algorithmic structure. Deleting a node with two children requires finding a successor. Students usually write a simple iterative or recursive search. Generated code is more likely to reach for an elaborate private helper such as findInorderSuccessor(), complete with redundant parent pointer updates, a pattern that mirrors canonical online explanations and the training data behind them. When many submissions share that same unusual decomposition without sharing any text, that is worth noticing.

A Three-Layer Audit

A suspicion is not evidence. A structured audit turns one into the other, or clears it.

Layer 1: Stylometric analysis. Measure what similarity checkers ignore:

  • Comment-to-code ratio, and how evenly comments are spread through the file.
  • Average identifier length and descriptiveness (for example, findInorderSuccessor versus getNext).
  • Consistency of bracket placement and whitespace across the entire file.

Human submissions tend to be inconsistent; generated ones tend to be uniform. Those metrics will not sort a class cleanly into two groups, but they will show you where to look.

Layer 2: Logical redundancy. Look for "correct but taught" solutions: code that solves the problem the way tutorials explain it rather than the way a student working from lecture would reason it out. An elaborate successor helper is a typical marker.

Layer 3: The walkthrough. For the submissions flagged by the first two layers, schedule brief code walkthroughs and ask the student to explain the remove() function line by line. A student who wrote it can explain why each step is there. A student who pasted it tends to describe what the code does rather than why it is shaped that way.

The walkthrough is the layer that settles cases. The first two only decide who gets one.

Responding at Scale

A standard academic integrity process is built for a handful of copying cases, not for widespread AI use in one assignment. If an audit points to a large share of a class, a blanket punishment is neither fair nor practical. A more workable response has three parts.

First, offer a window for students to come forward under the course policy in exchange for a reduced penalty. Many will, and it shrinks the number of contested cases.

Second, verify the learning outcome directly. An in-class exercise or short written exam on the same material tells you what students can actually do; the grade on the original assignment becomes secondary.

Third, update the tooling. Peer similarity is still needed for ordinary copying, but it has to be paired with checks that look at provenance, not just similarity. AI code detectors, such as the one in Codequiry, look for the statistical fingerprints that models like GPT-4 and GitHub Copilot leave in token prediction and structure, patterns that syntactic similarity engines cannot see.

A workable pipeline sends every submission through a similarity checker and an AI-origin check, and has TAs review flags before any conversation with a student. No score should reach a student as an accusation on its own.

Changing the Assignments Themselves

Detection is half the answer. The other half is assignment design.

Course policies should define "unauthorized AI assistance" explicitly, and it is worth spending some lecture time explaining how it is detected. Students who understand the review process are less likely to gamble on it.

Assignments can also include contextual anchors: course-specific requirements that are not easily found in online tutorials or a model's training data. Instead of "implement a BST," ask for "a BST that stores the custom Widget class from the lecture notes and logs every rotation to a global EventLogger object." Anchors like that break the copy chain, human and synthetic, and make generated answers that ignore them easy to spot.

The Takeaway

Cheating has shifted from duplication to generation. Detection has to shift with it, from finding sameness to identifying synthetic origin, and review has to rest on a conversation with the student rather than a number. Course teams that combine similarity checks, AI detection, walkthroughs, and anchored assignments are in a much better position to see when intellectual self-reliance is missing, and to teach it.