Programming Fundamentals · Algorithms Behind AI

Run a Complexity Checkup on AI-Written Code

Three tiers: have AI self-report complexity, demand one-tier optimization and spell out the cost, then benchmark on large data to verify it isn't bluffing

THE QUESTION THIS PAGE ANSWERS

ANSWER FIRST

What is the key idea behind “Run a Complexity Checkup on AI-Written Code”?

Three tiers: have AI self-report complexity, demand one-tier optimization and spell out the cost, then benchmark on large data to verify it isn't bluffing

DECISION RULE

Follow the handoffs, not the demo. A system becomes dependable at the boundaries between model, tools, state, permissions, and people. Read each handoff as a place where you can observe, test, and recover.

TRY NEXT

Name the input, owner, approval, and recovery action for one automated step.

WATCH FOR

A successful run that cannot explain what happened or be safely repeated.

Pick a tier · start today
🥉

Task 1 · Account for itself

Have AI label the complexity of its own code

30 min

Take a function AI just wrote (if you don’t have one, ask it to write “find the common elements of two lists”—nine times out of ten it hands you an O(n²) version first). Drop the prompt below on it and make it confess how fast or slow it is.

For the function you just wrote, do a complexity self-check. Answer in plain language: 1. Line by line (or loop block by loop block), label the time complexity; for each spot, explain why in one sentence; 2. Clearly point out the bottleneck line: which line / loop is the slowest in the whole function? Why that one? 3. Summarize: what are the overall time complexity and space complexity? 4. Translate into human terms: if the data grows from 1,000 to 1,000,000, how many times slower does this function get? Will users feel “nothing,” “a brief hitch,” or “spin until you question your life choices”? I’m a beginner—don’t pile on jargon; use analogies when you can.
Done when: you can point at the code and say “the bottleneck is this line, because it’s a nested loop / a full scan”—once you can name the bottleneck line, this tier is done.
🥈

Task 2 · Optimize one tier

Demand a speedup—and demand it spell out the cost

Half a day

Follow Task 1: if AI self-reported O(n²), ask it to optimize to O(n log n) or O(n)—but the point isn’t just going faster; it’s making it spell out the cost. After finishing ds-6, you should be able to point to which line in its plan is “trading space for time.”

Please optimize the O(n²) function above to O(n log n) or O(n). Requirements: 1. Keep both old and new versions; add comments on the critical optimization lines; 2. Explain clearly what technique you used to speed it up (sort then binary search? switch to a hash table?), and which family of algorithm ideas it belongs to; 3. Honestly state the cost: did readability get worse? How much more memory? Are there scenarios where the old version is actually better (e.g. very small data)? 4. If your plan “trades space for time,” clearly point out which line, how much space buys how much time. Finally, give me a quiz: ask me to point out where “trade space for time” happens in the new version, then reveal the answer.
Done when: you correctly answer its quiz—being able to point with your own hand to which line “trades space for time” means ds-6 and this chapter actually connected.
🥇

Task 3 · Measure until it can’t bluff

Verify with real data that it isn’t bluffing

One week

Complexity is paper reasoning; the timing curve is hard evidence. Have AI write a benchmark script, generate three scales of random data, time each run, and see with your own eyes how far the before/after curves diverge—and check whether the complexity it self-reported was bluffing.

Please write a benchmark script for both the old and new versions of the function. Requirements: 1. Generate three tiers of random test data: 1,000 / 100,000 / 10,000,000 rows (if 10M won’t run, you’re allowed to drop to 1M and explain why); 2. For each tier, run old and new versions 3 times each, take the average time, and output a comparison table: data size | old time | new time | speedup; 3. The script must run as-is—tell me which command to use and roughly how long to wait; 4. After the runs, help me judge: does the measured growth trend match the complexity you self-reported earlier? If not (e.g. you claimed O(n) but it grows like a square), analyze honestly why. I’ll paste the measured results back; you interpret the curves.
Done when: you have a measured timing table across three data sizes, and you can answer “does the measured curve match the self-reported complexity?”—match or call the bluff, either way the checkup is complete.
🩺 Checkup report generator · fill three fields, get a summary you can share in the group chat
Fill all three fields so the report is complete~
Why insist on measuring?AI’s self-reported complexity is right most of the time—but “most” ≠ “every time.” Sorting hidden inside library calls, string concat quietly inside a loop—any of these can make the real curve disagree with the paper math. Anyone who’s run a benchmark once keeps a professional suspicion toward “it said O(n), so it is O(n).” That’s exactly the posture a reviewer should have.

The algorithmic cost curve in “Pick a tier · start today”

“Have AI label the complexity of its own code” is not asking you to memorize steps. It trains you to spot repeated work: as the input grows, how many comparisons, moves, or recursive calls does the program perform?

Find repeated work before declaring something fast

Break “Take a function AI just wrote (if you don’t have one, ask it to write “find the common elements of two lists” —nine times out of ten it hands you an O(n²) version first).” into three questions: how input size changes, what each round does, and whether the next round can shrink its search space. Big-O describes growth, not an exact time on every machine; constants, memory, and data distribution still matter.

  • Checkup in three steps : self-report complexity → optimize and state the cost → measure to verify—each step harder than the last
  • The bottleneck line is your handle : once you can say “this line is the slowest,” review has somewhere to grip
  • Optimization must talk cost : any speedup that skips readability and memory deserves one more question

Theoretical optimum is not always practical optimum

When AI writes an algorithm, trace a small input by hand and benchmark progressively larger inputs. That turns “Complexity is paper reasoning;” from a slogan into a performance claim you can check.

Take the example one step further

The lesson starts with “Have AI label the complexity of its own code” and then moves to “Take a function AI just wrote (if you don’t have one, ask it to write “find the common elements of two lists” —nine times out of ten it hands you an O(n²) version first). Drop the prompt below on it and make it…”. Reading those two pieces together makes the distinction clearer: which points are facts in the lesson, and which judgments depend on their conditions.

Carry the judgment into the next situation

For a real task, find the repeated work first, ask how input size changes, and use a small benchmark to verify the theoretical judgment. Complexity should not become a label detached from the situation.

  • “Pick a tier · start today”: Have AI label the complexity of its own code
  • “Take it further”: Take a function AI just wrote (if you don’t have one, ask it to write “find the common elements of two lists” —nine times out of ten it hands you an O(n²) version first). Drop the prompt below on it and make it…
  • “The closing point”: The measured curve is hard evidence : paper complexity can bluff; a timing table across three data sizes won’t

The final “The closing point” brings the discussion to “The measured curve is hard evidence : paper complexity can bluff; a timing table across three data sizes won’t”. The useful thing to carry forward is knowing which judgments must be revisited when input, scale, or risk changes.

✅ What this lesson wants to share

  • Checkup in three steps: self-report complexity → optimize and state the cost → measure to verify—each step harder than the last
  • The bottleneck line is your handle: once you can say “this line is the slowest,” review has somewhere to grip
  • Optimization must talk cost: any speedup that skips readability and memory deserves one more question
  • The measured curve is hard evidence: paper complexity can bluff; a timing table across three data sizes won’t
Mark as learned Your reading progress updates automatically
← PreviousNext →

Keep reading

The next useful article in the thread.

ARTICLE DISCUSSION

Leave one useful thought here.

Keep the idea that clicked, the question that stayed open, or a small note for the next learner.

Discussing Run a Complexity Checkup on AI-Written Code Algorithms Behind AI
3discussionsArticle discussion · synced with the Circle
View in the learning circle
AM
Asha MorganContent editor
INSIGHTField note

I turned one judgment from this article into a small experiment I could run today. Knowing what to observe next is more useful than simply remembering the conclusion.

ARTICLE DISCUSSION7 helpful
LH
Lin HarperIndie developer
INSIGHTInsight

After reading this, I first looked for the conditions behind the idea instead of copying the method into a project. That order made the later trade-offs much clearer.

ARTICLE DISCUSSION5 helpful
KM
Kiki MooreProduct operations
QUESTIONQuestion

When this judgment reaches real work, which constraint should be added first? I am curious which step matters most between reading and the first practical attempt.

ARTICLE DISCUSSION4 helpful