Why AI generates text word by word in AI for Everyone - Performance Analysis
When AI generates text word by word, it processes each word step by step. We want to understand how the time it takes grows as the text gets longer.
How does the AI's work increase when it writes more words?
Analyze the time complexity of the following code snippet.
for i in range(n):
word = generate_next_word(context)
context.append(word)
output.append(word)
This code shows AI generating text one word at a time, updating context each step.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating the next word based on current context.
- How many times: Once for each word in the output, so n times.
Each new word requires the AI to look at all previous words to decide the next one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 55 (sum of 1 to 10) |
| 100 | About 5,050 |
| 1000 | About 500,500 |
Pattern observation: The work grows much faster than the number of words; it grows roughly like the square of n.
Time Complexity: O(n²)
This means if you double the number of words, the work more than doubles, growing roughly with the square of the length.
[X] Wrong: "Generating each word takes the same fixed time, so total time is just n times."
[OK] Correct: Each word depends on all previous words, so the time to generate grows as the text gets longer, not stays fixed.
Understanding how AI text generation time grows helps you explain performance in real projects. It shows you how tasks that build on previous results can become slower as they get bigger.
"What if the AI only looked at a fixed number of previous words instead of all of them? How would the time complexity change?"
Practice
Solution
Step 1: Understand AI text generation process
AI predicts each word based on the words that came before it to keep sentences logical.Step 2: Reason why word-by-word helps
This method helps AI create clear and meaningful sentences by building step-by-step.Final Answer:
To make sure each word fits well with the previous words -> Option BQuick Check:
Word-by-word prediction = fits well [OK]
- Thinking AI writes all words at once
- Believing AI forgets previous words
- Assuming speed is the main reason
Solution
Step 1: Identify AI's prediction method
AI looks at the words already written to decide the next word logically.Step 2: Eliminate incorrect options
Random guessing, writing whole sentence first, or copying dictionary order are not how AI works.Final Answer:
It uses previous words to choose the next word -> Option AQuick Check:
Next word depends on previous words = C [OK]
- Thinking AI guesses words randomly
- Believing AI writes full sentences before output
- Assuming AI uses dictionary order
Input: "The cat"
AI predicts next word options: "sat", "runs", "blue", "quickly".
Which word is AI most likely to choose next and why?Solution
Step 1: Analyze context of words "The cat"
"The cat" is a subject phrase, so a verb like "sat" fits naturally next.Step 2: Compare options for logical fit
"Sat" is a past tense verb that fits well; "blue" is an adjective unrelated here; "runs" is possible but less common; "quickly" is an adverb needing a verb first.Final Answer:
"sat", because it fits logically after 'The cat' -> Option CQuick Check:
Logical next word = "sat" [OK]
- Picking adjectives without verbs
- Ignoring sentence flow
- Choosing words just by type, not context
Solution
Step 1: Understand why AI might output unrelated words
If AI ignores previous words, it loses context and may pick words that don't fit.Step 2: Identify correct cause among options
Using previous words helps context; generating all at once or copying fixed list doesn't explain unrelated words well.Final Answer:
The AI is ignoring previous words when choosing next words -> Option DQuick Check:
Ignoring context causes wrong words = D [OK]
- Thinking generating all at once causes errors
- Believing copying fixed list causes unrelated words
- Assuming using previous words causes errors
Solution
Step 1: Understand story coherence needs
A story must flow logically, so each new word should fit the words before it.Step 2: Explain why word-by-word helps story flow
Generating word by word lets AI consider the story so far, ensuring each word fits and keeps meaning.Final Answer:
Because it lets AI check each word fits the story so far -> Option AQuick Check:
Word-by-word ensures story fits = A [OK]
- Thinking speed is main reason
- Believing AI forgets all but one word
- Assuming AI uses fixed word lists
Ready to prove it?
Pass the test and this pattern moves unknown
