Bird
Raised Fist0
DynamoDBquery~20 mins

PutItem (creating items) in DynamoDB - 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
🎖️
PutItem Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What happens when you put an item with an existing primary key?

You have a DynamoDB table with a primary key id. You run a PutItem operation with an item that has the same id as an existing item but different attribute values. What will be the result?

DynamoDB
PutItem({TableName: 'Users', Item: {id: {S: '123'}, name: {S: 'Alice'}}})
PutItem({TableName: 'Users', Item: {id: {S: '123'}, name: {S: 'Bob'}}})
AThe item with id '123' is replaced with the new item having name 'Bob'.
BThe operation fails with a ConditionalCheckFailedException.
CThe new item is added alongside the old item, so two items with id '123' exist.
DThe operation is ignored and the old item remains unchanged.
Attempts:
2 left
💡 Hint

Think about how PutItem works with existing keys in DynamoDB.

📝 Syntax
intermediate
1:30remaining
Which PutItem request is syntactically correct?

Choose the correct DynamoDB PutItem request syntax to add an item with id as a string and age as a number.

A{TableName: 'People', Item: {id: '001', age: 30}}
B{TableName: 'People', Item: {id: S:'001', age: N:'30'}}
C{TableName: 'People', Item: {id: {S: '001'}, age: {N: '30'}}}
D{TableName: 'People', Item: {id: {String: '001'}, age: {Number: 30}}}
Attempts:
2 left
💡 Hint

Remember DynamoDB expects attribute values to be typed with S for string and N for number, both as strings.

optimization
advanced
2:30remaining
How to avoid overwriting existing items when using PutItem?

You want to add a new item only if an item with the same primary key does not exist. Which option correctly achieves this using PutItem?

AUse <code>ConditionExpression: 'attribute_not_exists(id)'</code> in the PutItem request.
BUse <code>ReturnValues: 'ALL_OLD'</code> in the PutItem request.
CUse <code>UpdateItem</code> instead of <code>PutItem</code>.
DUse <code>PutItem</code> without any condition; it will not overwrite existing items.
Attempts:
2 left
💡 Hint

Think about how to tell DynamoDB to only put if the item does not exist.

🔧 Debug
advanced
2:00remaining
Why does this PutItem request fail with ValidationException?

Consider this PutItem request:

{
  TableName: 'Orders',
  Item: {
    orderId: {S: 'A123'},
    amount: {N: 100}
  }
}

Why does it fail with a ValidationException?

AThe table name 'Orders' does not exist.
BThe primary key attribute name is incorrect; it should be <code>id</code> not <code>orderId</code>.
CThe attribute <code>amount</code> cannot be a number.
DThe number value must be a string, so <code>{N: '100'}</code> is required instead of <code>{N: 100}</code>.
Attempts:
2 left
💡 Hint

Check the data types required by DynamoDB for number attributes.

🧠 Conceptual
expert
3:00remaining
What is the effect of using ReturnValues='ALL_OLD' in PutItem?

You perform a PutItem operation with ReturnValues set to 'ALL_OLD'. What will the response contain if the item already existed?

AThe response contains the new item that was just inserted.
BThe response contains the entire old item that was replaced by the new item.
CThe response contains only the primary key of the old item.
DThe response is empty regardless of whether the item existed.
Attempts:
2 left
💡 Hint

Think about what ReturnValues='ALL_OLD' means in DynamoDB PutItem.

Practice

(1/5)
1. What does the PutItem operation do in DynamoDB?
easy
A. Reads an item from a table
B. Deletes an item from a table
C. Updates only specific attributes of an item
D. Adds a new item or replaces an existing item in a table

Solution

  1. Step 1: Understand the purpose of PutItem

    PutItem is used to add a new item or replace an existing item in a DynamoDB table.
  2. Step 2: Compare with other operations

    Delete removes items, Get reads items, and Update modifies specific attributes, so they differ from PutItem.
  3. Final Answer:

    Adds a new item or replaces an existing item in a table -> Option D
  4. Quick Check:

    PutItem = Add or replace item [OK]
Hint: PutItem adds or replaces whole items, not partial updates [OK]
Common Mistakes:
  • Confusing PutItem with UpdateItem
  • Thinking PutItem only adds without replacing
  • Mixing PutItem with Delete or Get operations
2. Which of the following is the correct syntax snippet to add an item with PutItem in DynamoDB using AWS SDK?
easy
A. dynamodb.putItem({ TableName: 'Users', Item: { 'UserId': { S: '123' }, 'Name': { S: 'Alice' } } })
B. dynamodb.put({ Table: 'Users', Item: { UserId: '123', Name: 'Alice' } })
C. dynamodb.insertItem({ TableName: 'Users', Item: { 'UserId': '123', 'Name': 'Alice' } })
D. dynamodb.addItem({ TableName: 'Users', Item: { 'UserId': { N: 123 }, 'Name': { S: 'Alice' } } })

Solution

  1. Step 1: Check the correct method and parameters

    The AWS SDK method is putItem with parameters TableName and Item where each attribute has a type like S for string.
  2. Step 2: Validate the attribute format

    dynamodb.putItem({ TableName: 'Users', Item: { 'UserId': { S: '123' }, 'Name': { S: 'Alice' } } }) uses the correct method and attribute typing. Other options use wrong method names or omit types.
  3. Final Answer:

    dynamodb.putItem({ TableName: 'Users', Item: { 'UserId': { S: '123' }, 'Name': { S: 'Alice' } } }) -> Option A
  4. Quick Check:

    Correct method and typed attributes = dynamodb.putItem({ TableName: 'Users', Item: { 'UserId': { S: '123' }, 'Name': { S: 'Alice' } } }) [OK]
Hint: Use putItem with typed attributes like { S: 'value' } [OK]
Common Mistakes:
  • Using wrong method names like put or insertItem
  • Not specifying attribute types (S, N, etc.)
  • Using wrong parameter names like Table instead of TableName
3. Given the following PutItem request, what will be the result in the DynamoDB table?
{
  TableName: 'Products',
  Item: {
    'ProductId': { S: 'p100' },
    'Name': { S: 'Pen' },
    'Price': { N: '5' }
  }
}
medium
A. An error occurs because Price is a number but given as a string
B. Only the ProductId attribute is saved, others are ignored
C. A new item with ProductId 'p100', Name 'Pen', and Price 5 is added or replaced
D. The item is added but Price is stored as a string, not number

Solution

  1. Step 1: Understand attribute types in PutItem

    In DynamoDB, number attributes are passed as strings inside the N type, so '5' is valid for number.
  2. Step 2: Result of PutItem operation

    The item with all specified attributes is added or replaces existing item with same ProductId.
  3. Final Answer:

    A new item with ProductId 'p100', Name 'Pen', and Price 5 is added or replaced -> Option C
  4. Quick Check:

    PutItem stores typed attributes correctly = A new item with ProductId 'p100', Name 'Pen', and Price 5 is added or replaced [OK]
Hint: Number values are strings inside N type in PutItem [OK]
Common Mistakes:
  • Thinking number values must be numeric type, not string
  • Assuming partial attributes are saved
  • Confusing attribute types and values
4. You try to run this PutItem request but get an error:
{
  TableName: 'Orders',
  Item: {
    'OrderId': 'o123',
    'Amount': { N: '100' }
  }
}

What is the likely cause of the error?
medium
A. The table name 'Orders' is invalid
B. The attribute 'OrderId' is missing its type wrapper like { S: 'o123' }
C. The number value '100' should be a number, not a string
D. The Item object must be an array, not an object

Solution

  1. Step 1: Check attribute format in Item

    Each attribute must specify its type, e.g., { S: 'value' } for strings. Here, 'OrderId' lacks the type wrapper.
  2. Step 2: Validate other parts

    TableName is valid, number values are strings inside N, and Item is an object, so those are correct.
  3. Final Answer:

    The attribute 'OrderId' is missing its type wrapper like { S: 'o123' } -> Option B
  4. Quick Check:

    All attributes need type wrappers = The attribute 'OrderId' is missing its type wrapper like { S: 'o123' } [OK]
Hint: Always wrap attributes with type like { S: 'text' } or { N: '123' } [OK]
Common Mistakes:
  • Omitting type wrappers for string attributes
  • Confusing number values as numeric instead of string
  • Assuming Item can be an array
5. You want to add a new user item with UserId as the primary key and optional Age attribute only if it is provided (not null). Which PutItem approach correctly handles this conditional attribute?
hard
A. Include Age in the Item only if it is not null, otherwise omit it
B. Always include Age with value { N: '0' } if null
C. Set Age to an empty string { S: '' } when null
D. Use PutItem with a condition expression to skip Age attribute

Solution

  1. Step 1: Understand optional attribute handling

    In DynamoDB PutItem, you include only attributes you want saved. Omitting optional attributes if null is correct.
  2. Step 2: Evaluate other options

    Setting Age to zero or empty string changes data meaning. Condition expressions control item existence, not attribute presence.
  3. Final Answer:

    Include Age in the Item only if it is not null, otherwise omit it -> Option A
  4. Quick Check:

    Omit null attributes to avoid wrong data = Include Age in the Item only if it is not null, otherwise omit it [OK]
Hint: Only add attributes if they have real values, omit nulls [OK]
Common Mistakes:
  • Adding attributes with zero or empty string instead of omitting
  • Misusing condition expressions for attribute presence
  • Assuming PutItem auto-skips null attributes