0
0
DynamoDBquery~20 mins

Scan pagination in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scan Pagination 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 DynamoDB scan pagination code?

Consider a DynamoDB table with 25 items. You run a scan with a limit of 10 items per page. The code below fetches the first page and then the second page using the LastEvaluatedKey.

response1 = table.scan(Limit=10)
items_page1 = response1['Items']
last_key = response1.get('LastEvaluatedKey')
response2 = table.scan(Limit=10, ExclusiveStartKey=last_key)
items_page2 = response2['Items']
total_items = len(items_page1) + len(items_page2)

What is the value of total_items after running this code?

DynamoDB
response1 = table.scan(Limit=10)
items_page1 = response1['Items']
last_key = response1.get('LastEvaluatedKey')
response2 = table.scan(Limit=10, ExclusiveStartKey=last_key)
items_page2 = response2['Items']
total_items = len(items_page1) + len(items_page2)
A20
B25
C10
D15
Attempts:
2 left
💡 Hint

Each scan call returns up to the limit number of items. The second scan starts after the last key from the first scan.

🧠 Conceptual
intermediate
1:30remaining
Why is LastEvaluatedKey important in DynamoDB scan pagination?

When paginating scan results in DynamoDB, what role does LastEvaluatedKey play?

AIt sorts the scan results in ascending order
BIt marks the last item returned so the next scan can continue from there
CIt limits the number of items returned in a scan
DIt filters items based on a condition
Attempts:
2 left
💡 Hint

Think about how to continue scanning from where you left off.

📝 Syntax
advanced
2:30remaining
Which option correctly uses scan pagination to fetch all items?

You want to scan all items in a DynamoDB table using pagination. Which code snippet correctly implements this?

A
items = []
last_key = None
while True:
    response = table.scan(Limit=5, ExclusiveStartKey=last_key) if last_key else table.scan(Limit=5)
    items.extend(response['Items'])
    last_key = response.get('LastEvaluatedKey')
    if not last_key:
        break
B
items = []
last_key = None
while last_key:
    response = table.scan(Limit=5, ExclusiveStartKey=last_key)
    items.extend(response['Items'])
    last_key = response.get('LastEvaluatedKey')
C
items = []
last_key = None
while True:
    response = table.scan(Limit=5)
    items.extend(response['Items'])
    last_key = response.get('LastEvaluatedKey')
    if last_key is None:
        break
D
items = []
last_key = None
while True:
    response = table.scan(Limit=5, ExclusiveStartKey=last_key)
    items.extend(response['Items'])
    if 'LastEvaluatedKey' not in response:
        break
Attempts:
2 left
💡 Hint

Remember to handle the first scan call without ExclusiveStartKey.

optimization
advanced
1:30remaining
How to optimize scan pagination to reduce read capacity usage?

You want to paginate scan results but reduce the read capacity units consumed. Which approach helps achieve this?

AUse a smaller Limit value and filter results client-side
BUse ExclusiveStartKey to skip items
CUse ProjectionExpression to return only needed attributes
DIncrease the Limit value to fetch more items per scan
Attempts:
2 left
💡 Hint

Think about reducing the amount of data read per item.

🔧 Debug
expert
2:30remaining
Why does this scan pagination code cause an infinite loop?

Review this code snippet that paginates a DynamoDB scan. It causes an infinite loop. What is the cause?

items = []
last_key = None
while True:
    response = table.scan(Limit=10, ExclusiveStartKey=last_key)
    items.extend(response['Items'])
    if 'LastEvaluatedKey' not in response:
        break
    # Missing update of last_key here
AItems are not extended properly causing an error
BLimit is too high causing the scan to never finish
CThe break condition is incorrect and never triggers
Dlast_key is never updated, so ExclusiveStartKey is always None causing repeated scans
Attempts:
2 left
💡 Hint

Check if the pagination key is updated inside the loop.