Bird
Raised Fist0
DynamoDBquery~20 mins

Partition key distribution in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Partition Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why is a good partition key important in DynamoDB?

Imagine you have a large table in DynamoDB. Why does choosing a good partition key matter for performance?

AIt automatically backs up the table data to another region.
BIt makes the table smaller by compressing data automatically.
CIt encrypts the data stored in the table for security.
DIt evenly spreads data and requests across partitions to avoid hot spots.
Attempts:
2 left
💡 Hint

Think about how DynamoDB stores data internally and handles requests.

❓ query_result
intermediate
1:30remaining
What is the output of this DynamoDB query?

Given a DynamoDB table with partition key 'UserID' and sort key 'Timestamp', what will this query return?

Query: KeyConditionExpression = UserID = 'user123' AND Timestamp BETWEEN 100 AND 200
AAll items for 'user123' with Timestamp between 100 and 200 inclusive.
BAll items for all users with Timestamp between 100 and 200.
CAll items for 'user123' with Timestamp exactly 100 or 200 only.
DAn error because BETWEEN cannot be used with sort keys.
Attempts:
2 left
💡 Hint

Remember how KeyConditionExpression works with partition and sort keys.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this DynamoDB query expression

Which option contains a syntax error in the KeyConditionExpression for DynamoDB?

KeyConditionExpression: "UserID = :uid AND Timestamp > :start AND Timestamp < :end"
AUserID = :uid AND Timestamp >= :start AND Timestamp <= :end
BUserID = :uid OR Timestamp BETWEEN :start AND :end
CUserID = :uid AND Timestamp BETWEEN :start AND :end
DUserID = :uid AND Timestamp > :start AND Timestamp < :end
Attempts:
2 left
💡 Hint

Check if all logical operators are allowed in KeyConditionExpression.

❓ optimization
advanced
2:00remaining
How to optimize partition key design for a high-traffic table?

You have a DynamoDB table receiving many requests for a few partition key values, causing throttling. What is the best way to optimize partition key design?

ARemove the partition key and use only a sort key.
BUse a single static partition key for all items to simplify queries.
CAdd a random suffix to the partition key to spread traffic across more partitions.
DIncrease the provisioned throughput without changing the key design.
Attempts:
2 left
💡 Hint

Think about how to avoid hot partitions by spreading requests.

🔧 Debug
expert
2:30remaining
Why does this DynamoDB query cause a 'ValidationException' error?

Consider this query code snippet:

KeyConditionExpression = "UserID = :uid AND OrderDate = :date"
ExpressionAttributeValues = {":uid": "user1", ":date": "2023-01-01"}

Why does this cause a ValidationException?

ABecause the table does not have a sort key named 'OrderDate'.
BBecause the sort key condition uses '=' instead of a range operator like BETWEEN or >.
CBecause the sort key 'OrderDate' must use a range operator, '=' is not allowed.
DBecause the partition key 'UserID' is missing from ExpressionAttributeValues.
Attempts:
2 left
💡 Hint

Check the table schema and key names carefully.

Practice

(1/5)
1. What is the main purpose of a partition key in DynamoDB?
easy
A. To create backups of the database
B. To store large files in the database
C. To encrypt data automatically
D. To divide data into parts for faster access

Solution

  1. Step 1: Understand the role of partition key

    The partition key is used to split data into different sections called partitions.
  2. Step 2: Recognize the benefit of partitioning

    This splitting helps DynamoDB access data faster by looking in smaller parts instead of the whole table.
  3. Final Answer:

    To divide data into parts for faster access -> Option D
  4. Quick Check:

    Partition key divides data = faster access [OK]
Hint: Partition key splits data to speed up access [OK]
Common Mistakes:
  • Thinking partition key stores files
  • Confusing partition key with encryption
  • Assuming partition key creates backups
2. Which of the following is the correct way to define a partition key in a DynamoDB table using AWS CLI?
easy
A. aws dynamodb create-table --table-name MyTable --key-schema AttributeName=UserId,KeyType=HASH --attribute-definitions AttributeName=UserId,AttributeType=S --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
B. aws dynamodb create-table --table-name MyTable --partition-key UserId --attribute-type String
C. aws dynamodb create-table --table-name MyTable --key UserId --type HASH
D. aws dynamodb create-table --table-name MyTable --primary-key UserId STRING

Solution

  1. Step 1: Review AWS CLI syntax for creating table with partition key

    The correct syntax uses --key-schema with AttributeName and KeyType=HASH for partition key.
  2. Step 2: Check attribute definitions and throughput

    Attribute definitions must specify AttributeName and AttributeType, and provisioned throughput is required.
  3. Final Answer:

    aws dynamodb create-table --table-name MyTable --key-schema AttributeName=UserId,KeyType=HASH --attribute-definitions AttributeName=UserId,AttributeType=S --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 -> Option A
  4. Quick Check:

    Correct AWS CLI syntax = aws dynamodb create-table --table-name MyTable --key-schema AttributeName=UserId,KeyType=HASH --attribute-definitions AttributeName=UserId,AttributeType=S --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 [OK]
Hint: Use --key-schema with KeyType=HASH for partition key [OK]
Common Mistakes:
  • Using --partition-key instead of --key-schema
  • Missing attribute definitions
  • Incorrect key type syntax
3. Given a DynamoDB table with partition key 'UserId' having only two values: 'A' and 'B', what is the likely impact on partition key distribution?
medium
A. Good distribution because two keys are enough
B. Poor distribution causing hot partitions
C. No impact because DynamoDB balances automatically
D. Table will reject writes due to low key variety

Solution

  1. Step 1: Understand partition key uniqueness importance

    Partition keys with few unique values cause data to cluster in few partitions.
  2. Step 2: Recognize effect of low unique keys

    This clustering leads to hot partitions, slowing down performance and limiting scalability.
  3. Final Answer:

    Poor distribution causing hot partitions -> Option B
  4. Quick Check:

    Few unique keys = hot partitions [OK]
Hint: Few keys cause hot partitions, hurting performance [OK]
Common Mistakes:
  • Assuming two keys are enough for good distribution
  • Believing DynamoDB auto-balances all cases
  • Thinking table rejects writes for low key variety
4. You notice your DynamoDB table has uneven partition key distribution causing slow queries. Which change will fix this?
medium
A. Change partition key to a field with many unique values
B. Increase read capacity units only
C. Add a sort key with the same values as partition key
D. Delete and recreate the table without changes

Solution

  1. Step 1: Identify cause of uneven distribution

    Uneven distribution happens when partition key has few unique values.
  2. Step 2: Choose a partition key with high uniqueness

    Changing to a key with many unique values spreads data evenly across partitions, improving performance.
  3. Final Answer:

    Change partition key to a field with many unique values -> Option A
  4. Quick Check:

    High uniqueness partition key = even distribution [OK]
Hint: Pick partition key with many unique values to fix distribution [OK]
Common Mistakes:
  • Only increasing capacity without fixing key
  • Adding sort key with same values doesn't help
  • Recreating table without changing key
5. You have a DynamoDB table storing orders with partition key 'CustomerId'. Some customers place thousands of orders daily, others only a few. How can you improve partition key distribution to avoid hot partitions?
hard
A. Use 'OrderId' alone as partition key
B. Keep 'CustomerId' as partition key and add 'OrderId' as sort key
C. Use a composite key combining 'CustomerId' and 'OrderDate' as partition key
D. Use a random number as partition key for all orders

Solution

  1. Step 1: Identify problem with current partition key

    Using only 'CustomerId' causes hot partitions for customers with many orders.
  2. Step 2: Improve distribution by combining keys

    Combining 'CustomerId' and 'OrderDate' creates more unique partition keys, spreading load better.
  3. Step 3: Understand why other options fail

    Keep 'CustomerId' as partition key and add 'OrderId' as sort key keeps same partition key, so hot partitions remain; Use 'OrderId' alone as partition key loses customer grouping; Use a random number as partition key for all orders breaks data organization.
  4. Final Answer:

    Use a composite key combining 'CustomerId' and 'OrderDate' as partition key -> Option C
  5. Quick Check:

    Composite key increases uniqueness, reduces hot partitions [OK]
Hint: Combine keys to increase uniqueness and spread load [OK]
Common Mistakes:
  • Adding sort key but not changing partition key
  • Using only order ID loses customer grouping
  • Random keys break data access patterns
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard