0
0
Prompt Engineering / GenAIml~10 mins

Load balancing for AI services in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple round-robin load balancer that selects the next AI service endpoint.

Prompt Engineering / GenAI
def get_next_endpoint(endpoints, current_index):
    return endpoints[[1]]  # Select next endpoint
Drag options to blanks, or click blank then click option'
Acurrent_index + 1 % len(endpoints)
Bcurrent_index - 1 % len(endpoints)
C(current_index + 1) % len(endpoints)
Dcurrent_index % len(endpoints)
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses causing wrong order of operations.
Not using modulo leading to index out of range.
2fill in blank
medium

Complete the code to check if an AI service endpoint is healthy before sending a request.

Prompt Engineering / GenAI
def is_healthy(endpoint):
    response = send_health_check_request(endpoint)
    return response.status_code == [1]
Drag options to blanks, or click blank then click option'
A200
B404
C500
D302
Attempts:
3 left
💡 Hint
Common Mistakes
Using error codes like 404 or 500 instead of 200.
Confusing redirect codes like 302 with success.
3fill in blank
hard

Fix the error in the code that distributes requests evenly across AI service endpoints using modulo.

Prompt Engineering / GenAI
def distribute_request(request_id, endpoints):
    index = request_id [1] len(endpoints)
    return endpoints[index]
Drag options to blanks, or click blank then click option'
A%
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulo (%).
Using multiplication (*) or subtraction (-) which do not wrap indices.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each AI service endpoint to its current load if the load is below 70%.

Prompt Engineering / GenAI
loads = {endpoint: [1] for endpoint, load in endpoint_loads.items() if load [2] 70}
Drag options to blanks, or click blank then click option'
Aload
B>
C<
Dendpoint
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causing wrong filtering.
Mapping to 'endpoint' instead of 'load'.
5fill in blank
hard

Fill all three blanks to create a function that selects the AI service endpoint with the lowest load from a dictionary.

Prompt Engineering / GenAI
def select_least_loaded(endpoint_loads):
    return min(endpoint_loads, key=lambda [1]: endpoint_loads[[2]]) if endpoint_loads else [3]
Drag options to blanks, or click blank then click option'
Aendpoint
CNone
Dload
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in lambda.
Not handling empty dictionary case.