Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Memory for conversation history in Prompt Engineering / GenAI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Conversation Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does short-term memory affect conversation flow in AI chatbots?

Imagine an AI chatbot that can only remember the last 2 messages in a conversation. What is the most likely effect on the chatbot's responses?

AThe chatbot will provide very detailed answers about the entire conversation history.
BThe chatbot might lose context from earlier messages, causing less coherent replies.
CThe chatbot will remember everything perfectly and never forget any detail.
DThe chatbot will ignore the last messages and only respond based on its training data.
Attempts:
2 left
💡 Hint

Think about how limited memory affects understanding of ongoing topics.

Predict Output
intermediate
2:00remaining
What is the output of this conversation memory update code?

Given this Python code simulating conversation memory updates, what is the final memory content?

Prompt Engineering / GenAI
memory = []

# User says hello
memory.append('User: Hello')

# Bot replies
memory.append('Bot: Hi! How can I help?')

# User asks a question
memory.append('User: What is AI?')

# Limit memory to last 2 messages
memory = memory[-2:]

print(memory)
A['Bot: Hi! How can I help?', 'User: What is AI?']
B['User: Hello']
C['User: Hello', 'Bot: Hi! How can I help?', 'User: What is AI?']
D['User: Hello', 'User: What is AI?']
Attempts:
2 left
💡 Hint

Remember the memory is limited to the last 2 messages.

Model Choice
advanced
2:00remaining
Which model architecture is best for long conversation memory?

You want to build a chatbot that remembers long conversations (hundreds of messages). Which model architecture is best suited for this?

AA recurrent neural network (RNN) with gated units like LSTM or GRU.
BA linear regression model.
CA convolutional neural network (CNN) designed for image data.
DA simple feedforward neural network without memory components.
Attempts:
2 left
💡 Hint

Think about models designed to handle sequences and remember past inputs.

Hyperparameter
advanced
2:00remaining
Which hyperparameter controls how much past conversation is remembered in a Transformer model?

In Transformer-based chatbots, which hyperparameter mainly controls the length of conversation history the model can attend to?

ANumber of attention heads
BLearning rate
CMaximum sequence length (context window size)
DBatch size
Attempts:
2 left
💡 Hint

Consider what limits the input size the model can process at once.

Metrics
expert
2:00remaining
Which metric best measures how well a chatbot remembers conversation history?

You want to evaluate if a chatbot correctly uses past conversation context in its replies. Which metric is most appropriate?

ATraining loss on next word prediction without conversation context
BPerplexity on isolated sentences without conversation history
CBLEU score comparing generated replies to reference replies ignoring context
DContextual coherence score measuring relevance of replies to previous messages
Attempts:
2 left
💡 Hint

Think about a metric that checks if replies fit the conversation flow.

Practice

(1/5)
1. What is the main purpose of memory in a conversation AI system?
easy
A. To store past user and AI messages for context
B. To speed up the internet connection
C. To generate random responses without context
D. To delete all previous messages after each reply

Solution

  1. Step 1: Understand the role of memory in AI conversations

    Memory keeps track of previous messages so the AI can understand the flow of the conversation.
  2. Step 2: Identify the correct purpose

    Storing past messages helps the AI respond with context, making conversations meaningful.
  3. Final Answer:

    To store past user and AI messages for context -> Option A
  4. Quick Check:

    Memory = store past messages [OK]
Hint: Memory keeps conversation context, not random or deleted [OK]
Common Mistakes:
  • Thinking memory speeds up internet
  • Believing memory deletes all messages
  • Assuming memory generates random replies
2. Which of the following is the correct way to add a new message to conversation memory in Python?
easy
A. memory.append(new_message)
B. memory.add(new_message)
C. memory.insert(new_message)
D. memory.push(new_message)

Solution

  1. Step 1: Recall Python list methods for adding items

    Python lists use append() to add an item at the end.
  2. Step 2: Match method to memory update

    Since conversation memory is often a list, append() is the correct method to add a new message.
  3. Final Answer:

    memory.append(new_message) -> Option A
  4. Quick Check:

    Python list add = append() [OK]
Hint: Use append() to add items to a Python list [OK]
Common Mistakes:
  • Using add() which is for sets
  • Using insert() without index
  • Using push() which is not a Python list method
3. Given this Python code snippet managing conversation memory:
memory = ['Hi', 'How are you?']
new_message = 'I am fine'
memory.append(new_message)
print(len(memory))

What will be the output?
medium
A. 2
B. 3
C. 1
D. Error

Solution

  1. Step 1: Check initial memory length

    Memory starts with 2 messages: 'Hi' and 'How are you?'.
  2. Step 2: Append new message and count

    Appending 'I am fine' adds one more message, so total becomes 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    2 + 1 = 3 messages [OK]
Hint: Appending adds one item, so length increases by 1 [OK]
Common Mistakes:
  • Forgetting append adds item
  • Thinking length stays same
  • Assuming code causes error
4. You have this code to keep conversation memory but it causes an error:
memory = []
new_message = 'Hello'
memory.add(new_message)

What is the error and how to fix it?
medium
A. No error; code runs fine
B. Error: new_message undefined; fix by defining new_message
C. Error: list has no add(); fix by using memory.append(new_message)
D. Error: memory is not a list; fix by initializing memory as a dict

Solution

  1. Step 1: Identify the error cause

    Python lists do not have an add() method; this causes an AttributeError.
  2. Step 2: Correct method to add item to list

    Use append() to add an item to a list, so replace add() with append().
  3. Final Answer:

    Error: list has no add(); fix by using memory.append(new_message) -> Option C
  4. Quick Check:

    List add() wrong, use append() [OK]
Hint: Lists use append(), sets use add() [OK]
Common Mistakes:
  • Using add() on list
  • Thinking new_message is undefined
  • Confusing list with dict
5. You want to keep only the last 3 messages in conversation memory to save space. Which code correctly updates memory after adding a new message?
hard
A. memory.insert(0, new_message) memory = memory[:3]
B. memory = memory[:3] memory.append(new_message)
C. memory.pop() memory.append(new_message)
D. memory.append(new_message) memory = memory[-3:]

Solution

  1. Step 1: Add new message to memory

    Use append() to add the new message at the end.
  2. Step 2: Keep only last 3 messages

    Slicing with memory[-3:] keeps the last 3 items, removing older ones.
  3. Final Answer:

    memory.append(new_message) memory = memory[-3:] -> Option D
  4. Quick Check:

    Append then slice last 3 [OK]
Hint: Append first, then slice last 3 messages [OK]
Common Mistakes:
  • Slicing before append loses new message
  • Using insert at start changes order
  • Popping removes wrong message