0
0
AI for Everyoneknowledge~5 mins

Iterating and refining prompts in AI for Everyone - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Iterating and refining prompts
O(n)
Understanding Time Complexity

When working with AI prompts, it is important to understand how the time to get a good answer grows as you try more versions.

We want to know how the effort changes when you keep improving your prompt step by step.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


prompt = initial_prompt
for attempt in range(n):
    response = AI.generate(prompt)
    prompt = refine(prompt, response)

This code sends a prompt to an AI, gets a response, then improves the prompt based on that response, repeating this process n times.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Sending a prompt to the AI and refining it.
  • How many times: Exactly n times, once per loop iteration.
How Execution Grows With Input

Each new attempt adds one more prompt sent and one refinement step.

Input Size (n)Approx. Operations
1010 prompt sends and refinements
100100 prompt sends and refinements
10001000 prompt sends and refinements

Pattern observation: The work grows directly in proportion to the number of attempts.

Final Time Complexity

Time Complexity: O(n)

This means the time grows in a straight line as you try more prompt versions.

Common Mistake

[X] Wrong: "Refining the prompt multiple times will take the same time as just one prompt."

[OK] Correct: Each new prompt requires a new AI call and refinement, so the total time adds up with each attempt.

Interview Connect

Understanding how repeated attempts affect time helps you plan and explain your approach when working with AI tools or iterative processes.

Self-Check

"What if each refinement step itself involved a loop over m items? How would the time complexity change?"