Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Latency optimization 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
🎖️
Latency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Latency in Model Inference

Which factor most directly affects the latency of a machine learning model during inference?

AThe number of layers and parameters in the model
BThe size of the training dataset
CThe type of optimizer used during training
DThe number of epochs used during training
Attempts:
2 left
💡 Hint

Think about what happens when the model makes a prediction.

Predict Output
intermediate
2:00remaining
Effect of Batch Size on Latency

What is the output of the following code that simulates latency for different batch sizes?

Prompt Engineering / GenAI
import time

def simulate_latency(batch_size):
    base_time = 0.01  # seconds per sample
    total_time = base_time * batch_size
    time.sleep(total_time)
    return total_time

latencies = {b: simulate_latency(b) for b in [1, 5, 10]}
print(latencies)
A{1: 0.01, 5: 0.05, 10: 0.1}
B{1: 0.01, 5: 0.01, 10: 0.01}
C{1: 0.1, 5: 0.5, 10: 1.0}
D{1: 0.01, 5: 0.1, 10: 0.5}
Attempts:
2 left
💡 Hint

Latency scales linearly with batch size in this simulation.

Model Choice
advanced
2:00remaining
Choosing a Model for Low Latency

You need to deploy a model on a device with limited processing power and require very low latency. Which model architecture is best suited?

AA deep convolutional neural network with 50 layers
BA small decision tree model
CA large transformer model with billions of parameters
DA recurrent neural network with multiple LSTM layers
Attempts:
2 left
💡 Hint

Think about model size and computation needed for fast predictions.

Hyperparameter
advanced
2:00remaining
Hyperparameter Impact on Latency

Which hyperparameter adjustment is most likely to reduce inference latency without retraining the model?

AReducing the number of training epochs
BIncreasing the learning rate
CLowering the batch size during inference
DChanging the activation function to ReLU
Attempts:
2 left
💡 Hint

Consider what happens when you process fewer samples at once.

🔧 Debug
expert
2:00remaining
Debugging Latency Bottleneck in Code

Given the code below, which line is the main cause of increased latency during inference?

def predict(model, data):
    results = []
    for sample in data:
        processed = preprocess(sample)
        output = model(processed)
        results.append(output)
    return results

# preprocess is slow due to heavy image resizing
# model is optimized and fast
ALine 3: for sample in data:
BLine 6: results.append(output)
CLine 5: output = model(processed)
DLine 4: processed = preprocess(sample)
Attempts:
2 left
💡 Hint

Focus on which step is described as slow.

Practice

(1/5)
1. What is the main goal of latency optimization in AI models?
easy
A. To make AI models respond faster for better user experience
B. To increase the size of the AI model
C. To reduce the accuracy of the AI model
D. To add more layers to the AI model

Solution

  1. Step 1: Understand latency meaning

    Latency means the time it takes for a model to give an answer after input.
  2. Step 2: Connect latency to user experience

    Lower latency means faster responses, which improves how users feel about the AI.
  3. Final Answer:

    To make AI models respond faster for better user experience -> Option A
  4. Quick Check:

    Latency optimization = faster response [OK]
Hint: Latency means speed of response, optimize to make it faster [OK]
Common Mistakes:
  • Confusing latency with model size
  • Thinking latency means accuracy
  • Assuming more layers reduce latency
2. Which of the following is a correct Python syntax to measure latency using time module?
easy
A. start = time.sleep(); model.predict(x); end = time.sleep(); latency = end / start
B. start = time.time(); model.predict(x); end = time.time(); latency = end - start
C. start = time.clock(); model.predict(x); end = time.clock(); latency = start - end
D. start = time.now(); model.predict(x); end = time.now(); latency = end + start

Solution

  1. Step 1: Identify correct time functions

    Python's time.time() returns current time in seconds; subtracting gives elapsed time.
  2. Step 2: Check latency calculation

    Latency = end - start measures duration correctly; other options misuse functions or operations.
  3. Final Answer:

    start = time.time(); model.predict(x); end = time.time(); latency = end - start -> Option B
  4. Quick Check:

    Latency = end - start time [OK]
Hint: Use time.time() and subtract end-start for latency [OK]
Common Mistakes:
  • Using time.now() which does not exist
  • Subtracting start - end instead of end - start
  • Using time.sleep() which pauses code, not measures time
3. Given this code snippet measuring latency, what will be printed?
import time
start = time.time()
for _ in range(1000000):
    pass
end = time.time()
print(round(end - start, 2))
medium
A. A number close to 1.00
B. An error because of wrong syntax
C. A number close to 10.00
D. A number close to 0.00

Solution

  1. Step 1: Understand the loop workload

    The loop runs 1,000,000 times doing nothing (pass), which takes very little time due to Python's loop execution speed.
  2. Step 2: Estimate time taken

    On a normal computer, this empty loop takes around 0.03-0.1 seconds, so round(end - start, 2) prints a number close to 0.00.
  3. Final Answer:

    A number close to 0.00 -> Option D
  4. Quick Check:

    1M empty loops ~0.05s [OK]
Hint: 1 million empty loops take ~0.05 seconds [OK]
Common Mistakes:
  • Overestimating time for empty loop (e.g., thinking 1 second)
  • Thinking it takes 10 seconds
  • Assuming syntax error due to indentation
4. You tried pruning your AI model to reduce latency but latency increased. What is the likely cause?
medium
A. Pruning removed important layers causing slower computation
B. Pruning always increases latency by design
C. Pruning was done incorrectly causing overhead in model execution
D. Latency measurement was done before pruning

Solution

  1. Step 1: Understand pruning effect

    Pruning removes less important parts to speed up model, so latency should decrease if done right.
  2. Step 2: Identify why latency increased

    If latency increased, pruning likely added overhead or was done incorrectly, causing slower execution.
  3. Final Answer:

    Pruning was done incorrectly causing overhead in model execution -> Option C
  4. Quick Check:

    Incorrect pruning = more overhead = higher latency [OK]
Hint: Incorrect pruning adds overhead, increasing latency [OK]
Common Mistakes:
  • Assuming pruning always slows model
  • Ignoring measurement timing
  • Thinking pruning removes important layers by default
5. You want to reduce latency of a large AI model for mobile devices. Which combined approach is best?
hard
A. Use quantization to reduce precision and prune unimportant weights
B. Increase model layers and use caching on server
C. Only use caching without changing model size
D. Train a bigger model with more data

Solution

  1. Step 1: Identify techniques for latency reduction on mobile

    Quantization reduces number size to speed up computation; pruning removes unneeded parts to shrink model.
  2. Step 2: Evaluate options

    Increasing layers or bigger models increase latency; caching helps but alone is not enough for mobile constraints.
  3. Final Answer:

    Use quantization to reduce precision and prune unimportant weights -> Option A
  4. Quick Check:

    Quantization + pruning = best latency reduction [OK]
Hint: Combine quantization and pruning for mobile latency [OK]
Common Mistakes:
  • Thinking bigger models reduce latency
  • Relying only on caching for mobile speed
  • Ignoring model size impact on mobile devices