Bird
Raised Fist0
DynamoDBquery~20 mins

Key-value and document store model 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
🎖️
Key-Value DynamoDB Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Query Result: Retrieving an Item by Primary Key

You have a DynamoDB table named Users with UserID as the primary key. Which query will return the item with UserID equal to 123?

AGetItem with Key = {"UserID": {"N": "123"}}
BQuery with KeyConditionExpression = "UserID = :id" and ExpressionAttributeValues = {":id": {"N": "123"}}
CScan with FilterExpression = "UserID = :id" and ExpressionAttributeValues = {":id": {"S": "123"}}
DUpdateItem with Key = {"UserID": {"N": "123"}}
Attempts:
2 left
💡 Hint

Use the simplest method to get an item by its primary key without scanning the whole table.

🧠 Conceptual
intermediate
1:30remaining
Understanding Partition Keys in DynamoDB

Which statement best describes the role of a partition key in a DynamoDB table?

AIt uniquely identifies each item and determines the partition where the item is stored.
BIt is used only for sorting items within a partition.
CIt is optional and only used for indexing secondary attributes.
DIt stores the actual data values of the item.
Attempts:
2 left
💡 Hint

Think about how DynamoDB organizes data physically.

📝 Syntax
advanced
2:30remaining
Correct Syntax for Adding a New Attribute to an Item

You want to add a new attribute Age with value 30 to an existing item with UserID = 101 in DynamoDB. Which UpdateItem syntax is correct?

AUpdateItem with Key={"UserID": {"N": "101"}}, UpdateExpression="SET Age = 30"
BUpdateItem with Key={"UserID": {"N": "101"}}, UpdateExpression="ADD Age :val", ExpressionAttributeValues={":val": {"N": "30"}}
CUpdateItem with Key={"UserID": {"N": "101"}}, UpdateExpression="SET Age = :val", ExpressionAttributeValues={":val": {"N": "30"}}
DUpdateItem with Key={"UserID": {"N": "101"}}, UpdateExpression="SET Age :val", ExpressionAttributeValues={":val": {"N": "30"}}
Attempts:
2 left
💡 Hint

Remember the syntax for UpdateExpression requires an equals sign and placeholders for values.

optimization
advanced
2:30remaining
Optimizing Queries with Secondary Indexes

You have a DynamoDB table with UserID as the partition key and OrderDate as a sort key. You want to efficiently query all orders placed on a specific OrderDate regardless of UserID. What is the best approach?

AQuery the table using <code>UserID</code> and filter by <code>OrderDate</code>.
BUse Scan operation with a filter on <code>OrderDate</code>.
CCreate a Local Secondary Index (LSI) with <code>OrderDate</code> as the sort key and query it.
DCreate a Global Secondary Index (GSI) with <code>OrderDate</code> as the partition key and query it.
Attempts:
2 left
💡 Hint

Think about how to query by an attribute that is not the primary key.

🔧 Debug
expert
3:00remaining
Debugging a Conditional Write Failure

You try to update an item in DynamoDB with a ConditionExpression to only update if Status is "Pending". The update fails with a ConditionalCheckFailedException. What is the most likely cause?

AThe <code>Status</code> attribute is misspelled in the condition expression.
BThe item does not exist, so the condition cannot be evaluated.
CThe <code>ConditionExpression</code> syntax is invalid and causes the failure.
DThe update operation does not support conditional expressions.
Attempts:
2 left
💡 Hint

Think about what happens if the item you want to update is missing.

Practice

(1/5)
1. What is the main feature of a key-value store in DynamoDB?
easy
A. It uses a unique key to quickly find data.
B. It requires complex joins between tables.
C. It stores data only in fixed columns.
D. It only supports relational data models.

Solution

  1. Step 1: Understand key-value store basics

    A key-value store uses a unique key to store and retrieve data quickly without complex relations.
  2. Step 2: Compare options with key-value features

    Options A, C, and D describe relational or fixed schema models, not key-value stores.
  3. Final Answer:

    It uses a unique key to quickly find data. -> Option A
  4. Quick Check:

    Key-value store = unique key fast access [OK]
Hint: Key-value means unique key for fast lookup [OK]
Common Mistakes:
  • Confusing key-value with relational databases
  • Thinking key-value needs fixed columns
  • Assuming key-value supports joins
2. Which of the following is the correct way to define a composite primary key in a DynamoDB table?
easy
A. PrimaryKey: { PartitionKey: 'UserId', SortKey: 'Timestamp' }
B. PrimaryKey = UserId, SortKey = Timestamp
C. PrimaryKey: { PartitionKey: 'UserId' }
D. PrimaryKey: UserId, ForeignKey: Timestamp

Solution

  1. Step 1: Recall DynamoDB primary key syntax

    DynamoDB primary key can be simple (partition key) or composite (partition + sort key) defined as an object with keys PartitionKey and SortKey.
  2. Step 2: Check each option's syntax

    PrimaryKey: { PartitionKey: 'UserId', SortKey: 'Timestamp' } correctly shows both PartitionKey and SortKey in an object. PrimaryKey: { PartitionKey: 'UserId' } misses SortKey, B uses wrong assignment syntax, D uses ForeignKey which is invalid in DynamoDB.
  3. Final Answer:

    PrimaryKey: { PartitionKey: 'UserId', SortKey: 'Timestamp' } -> Option A
  4. Quick Check:

    Primary key = PartitionKey + SortKey object [OK]
Hint: Primary key uses PartitionKey and optional SortKey keys [OK]
Common Mistakes:
  • Using equal sign instead of colon in definitions
  • Confusing foreign key with sort key
  • Omitting sort key when needed
3. Given a DynamoDB table with items: {UserId: '123', Name: 'Alice'}, {UserId: '456', Name: 'Bob'}, what will the query return if you request the item with UserId '123'?
medium
A. [{UserId: '456', Name: 'Bob'}]
B. Error: Item not found
C. []
D. [{UserId: '123', Name: 'Alice'}]

Solution

  1. Step 1: Understand query by primary key

    Querying by UserId '123' returns the item with that key if it exists.
  2. Step 2: Match UserId '123' in given items

    The item {UserId: '123', Name: 'Alice'} matches the query.
  3. Final Answer:

    [{UserId: '123', Name: 'Alice'}] -> Option D
  4. Quick Check:

    Query by key returns matching item [OK]
Hint: Query by key returns exact matching item [OK]
Common Mistakes:
  • Expecting multiple items for a unique key
  • Confusing query result with scan result
  • Assuming error if item not found instead of empty
4. You wrote this DynamoDB query but it returns no results: Table.query(KeyConditionExpression='UserId = :uid', ExpressionAttributeValues={':uid': '789'}). What is the likely problem?
medium
A. KeyConditionExpression must use '==' instead of '='.
B. ExpressionAttributeValues should not use colons in keys.
C. The key name 'UserId' is incorrect or missing in the table.
D. Query requires a ScanIndexForward parameter.

Solution

  1. Step 1: Check key name correctness

    If the key name 'UserId' does not exist or is misspelled in the table schema, the query returns no results.
  2. Step 2: Validate syntax and parameters

    Using ':' in ExpressionAttributeValues keys is correct. '=' is valid in KeyConditionExpression. ScanIndexForward is optional for sorting, not required for results.
  3. Final Answer:

    The key name 'UserId' is incorrect or missing in the table. -> Option C
  4. Quick Check:

    Correct key name needed for query success [OK]
Hint: Check key names match table schema exactly [OK]
Common Mistakes:
  • Removing colons from ExpressionAttributeValues keys
  • Using '==' instead of '=' in expressions
  • Adding unnecessary parameters
5. You want to store user profiles with flexible nested data like addresses and preferences in DynamoDB. Which model best fits this need?
hard
A. Use a relational model with multiple tables and foreign keys.
B. Use a document store model to save JSON-like nested objects.
C. Use a key-value store with only simple string values.
D. Use a graph database to store nested user data.

Solution

  1. Step 1: Identify data flexibility needs

    Nested and flexible data like addresses and preferences require a document model that supports JSON-like structures.
  2. Step 2: Match model to DynamoDB capabilities

    DynamoDB supports document store model allowing nested objects. Relational and graph models are not native to DynamoDB. Simple key-value stores do not support nested data well.
  3. Final Answer:

    Use a document store model to save JSON-like nested objects. -> Option B
  4. Quick Check:

    Nested data = document store model [OK]
Hint: Nested JSON data fits document store model best [OK]
Common Mistakes:
  • Choosing relational model for flexible nested data
  • Assuming key-value stores handle nested objects well
  • Confusing graph databases with DynamoDB