0
0
DynamoDBquery~20 mins

Hierarchical data modeling in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hierarchical Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Query to retrieve all child items of a parent

You have a DynamoDB table storing categories and subcategories. Each item has a PK as the parent category ID and a SK as the child category ID.

Which query will return all subcategories of the parent category with PK = 'CAT#100'?

DynamoDB
TableName: Categories
PartitionKey: PK
SortKey: SK
Example item: { PK: 'CAT#100', SK: 'SUB#200', Name: 'Subcategory A' }
AQuery with KeyConditionExpression: "PK = :pkval" and ExpressionAttributeValues: {":pkval": "CAT#100"}
BScan with FilterExpression: "PK = :pkval" and ExpressionAttributeValues: {":pkval": "CAT#100"}
CScan with FilterExpression: "SK = :skval" and ExpressionAttributeValues: {":skval": "SUB#200"}
DQuery with KeyConditionExpression: "SK = :skval" and ExpressionAttributeValues: {":skval": "SUB#200"}
Attempts:
2 left
💡 Hint

Remember, Query operations use the partition key to efficiently find items.

🧠 Conceptual
intermediate
2:00remaining
Best way to model parent-child relationships in DynamoDB

Which DynamoDB design pattern is best suited to model hierarchical data with multiple levels of parent-child relationships?

AUse a single table with only a partition key and no sort key
BUse multiple tables, one per hierarchy level, linked by foreign keys
CUse a single table with composite keys where <code>PK</code> is the root ancestor and <code>SK</code> encodes the path to the child
DUse a global secondary index on a timestamp attribute
Attempts:
2 left
💡 Hint

Think about how DynamoDB's single-table design supports hierarchical queries.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this DynamoDB query expression

What is wrong with this DynamoDB query KeyConditionExpression?

KeyConditionExpression: "PK = :pkval AND begins_with(SK, :skprefix)"
ExpressionAttributeValues: {":pkval": "CAT#100", ":skprefix": "SUB#"}
APK must be compared with SK in KeyConditionExpression
Bbegins_with function requires two attribute names, not attribute value
CExpressionAttributeValues keys must not start with a colon
DThe AND operator cannot be used in KeyConditionExpression
Attempts:
2 left
💡 Hint

Check the rules for KeyConditionExpression syntax in DynamoDB.

optimization
advanced
2:00remaining
Optimize hierarchical queries with large datasets

You have a DynamoDB table with millions of hierarchical items. Queries for all descendants of a parent are slow. Which optimization will improve query performance?

AUse Scan operation with FilterExpression to find descendants
BUse a composite sort key encoding the full path and query with begins_with on SK
CAdd a global secondary index on the child item name attribute
DStore all descendants in a single item as a JSON array
Attempts:
2 left
💡 Hint

Think about how sort keys and begins_with can limit query scope.

🔧 Debug
expert
2:00remaining
Why does this hierarchical query return no results?

Given this DynamoDB query:

KeyConditionExpression: "PK = :pkval and SK > :skval"
ExpressionAttributeValues: {":pkval": "CAT#100", ":skval": "SUB#100"}

But no items are returned, even though items with SK values greater than "SUB#100" exist. What is the most likely cause?

AThe SK values are strings but the comparison is lexicographical and does not match expected order
BSK attribute is stored as a number, but compared as a string
CThe comparison operator > is not supported in KeyConditionExpression
DThe partition key value "CAT#100" does not exist in the table
Attempts:
2 left
💡 Hint

Consider how string comparisons work in DynamoDB sort keys.