0
0
DynamoDBquery~20 mins

Lambda function with DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DynamoDB Lambda Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this Lambda function when querying DynamoDB?

Consider a Lambda function written in Node.js that queries a DynamoDB table named Users with a partition key userId. The function queries for userId = '123'. What will the function return if the item exists?

DynamoDB
import AWS from 'aws-sdk';
const dynamoDb = new AWS.DynamoDB.DocumentClient();

export const handler = async (event) => {
  const params = {
    TableName: 'Users',
    Key: { userId: '123' }
  };
  try {
    const data = await dynamoDb.get(params).promise();
    return data.Item || null;
  } catch (error) {
    return { error: error.message };
  }
};
A{"userId":"123","name":"Alice","age":30}
Bnull
C{"error":"ResourceNotFoundException"}
D{"userId":"123"}
Attempts:
2 left
💡 Hint

If the item exists, the get method returns the full item under data.Item.

📝 Syntax
intermediate
2:00remaining
Which option correctly updates an item in DynamoDB using Lambda?

You want to update the age attribute of a user with userId = '456' in DynamoDB using a Lambda function. Which code snippet is syntactically correct?

DynamoDB
import AWS from 'aws-sdk';
const dynamoDb = new AWS.DynamoDB.DocumentClient();

export const handler = async () => {
  const params = {
    TableName: 'Users',
    Key: { userId: '456' },
    UpdateExpression: 'set age = :a',
    ExpressionAttributeValues: { ':a': 35 },
    ReturnValues: 'UPDATED_NEW'
  };
  return await dynamoDb.update(params).promise();
};
AUpdateExpression: 'set age = :a', ExpressionAttributeValues: { ':a': 35 }
BUpdateExpression: 'set age = a', ExpressionAttributeValues: { 'a': 35 }
CUpdateExpression: 'set age = :a', ExpressionAttributeValues: { a: 35 }
DUpdateExpression: 'set age = :a', ExpressionAttributeValues: { ':a' => 35 }
Attempts:
2 left
💡 Hint

Expression attribute values must use placeholders starting with a colon : and be keys in an object.

optimization
advanced
2:00remaining
How to optimize a Lambda function to batch write multiple items to DynamoDB?

You want to insert 25 user records into a DynamoDB table in a single Lambda invocation. Which approach is the most efficient and correct?

ACall <code>dynamoDb.put</code> 25 times in parallel without waiting for promises.
BCall <code>dynamoDb.put</code> 25 times sequentially inside a loop.
CUse <code>dynamoDb.update</code> with 25 update expressions in one call.
DUse <code>dynamoDb.batchWrite</code> with up to 25 <code>PutRequest</code> items in one call.
Attempts:
2 left
💡 Hint

DynamoDB supports batch writes with a limit of 25 items per batch.

🔧 Debug
advanced
2:00remaining
Why does this Lambda function fail with a ProvisionedThroughputExceededException?

This Lambda function writes items to DynamoDB but sometimes fails with ProvisionedThroughputExceededException. What is the likely cause?

DynamoDB
import AWS from 'aws-sdk';
const dynamoDb = new AWS.DynamoDB.DocumentClient();

export const handler = async (event) => {
  const params = {
    TableName: 'Users',
    Item: event.item
  };
  try {
    await dynamoDb.put(params).promise();
    return { status: 'success' };
  } catch (error) {
    return { error: error.code };
  }
};
AThe AWS SDK is not configured with correct credentials.
BThe Item parameter is missing a required attribute.
CThe function is writing too many items too quickly, exceeding the table's write capacity.
DThe table name 'Users' is misspelled.
Attempts:
2 left
💡 Hint

ProvisionedThroughputExceededException means the request rate is too high for the provisioned capacity.

🧠 Conceptual
expert
2:00remaining
What is the best practice for handling DynamoDB pagination in a Lambda function?

You query a DynamoDB table with a large dataset that returns paginated results. How should your Lambda function handle pagination to retrieve all items efficiently?

ASet <code>Limit</code> to a very high number to get all items in one query call.
BUse the <code>LastEvaluatedKey</code> from the query response to iteratively request the next page until no more keys remain.
CIgnore pagination and process only the first page of results.
DUse <code>Scan</code> instead of <code>Query</code> to avoid pagination.
Attempts:
2 left
💡 Hint

DynamoDB paginates query results and provides LastEvaluatedKey to continue fetching.