Bird
Raised Fist0
DynamoDBquery~20 mins

Scan pagination 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
🎖️
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.

Practice

(1/5)
1. What is the main purpose of using Scan pagination in DynamoDB?
easy
A. To speed up writes to the database
B. To delete items in batches
C. To create indexes automatically
D. To break a large scan into smaller parts for easier processing

Solution

  1. Step 1: Understand Scan Pagination Concept

    Scan pagination is used to divide a large scan operation into smaller chunks to avoid timeouts and manage resource use.
  2. Step 2: Identify the Purpose

    The main goal is to process big scans in parts, not to speed writes or create indexes.
  3. Final Answer:

    To break a large scan into smaller parts for easier processing -> Option D
  4. Quick Check:

    Scan pagination = break large scan into parts [OK]
Hint: Scan pagination splits big scans into smaller chunks [OK]
Common Mistakes:
  • Thinking pagination speeds up writes
  • Confusing scan with index creation
  • Assuming pagination deletes items
2. Which of the following is the correct way to continue a paginated scan in DynamoDB?
easy
A. Use LastEvaluatedKey as the ExclusiveStartKey in the next scan
B. Use Limit as the ExclusiveStartKey
C. Use ScanIndexForward to continue scanning
D. Use StartKey to begin the scan

Solution

  1. Step 1: Identify Pagination Parameters

    In DynamoDB, LastEvaluatedKey from the previous scan is used as ExclusiveStartKey to continue scanning.
  2. Step 2: Eliminate Incorrect Options

    Limit sets chunk size, not start key. ScanIndexForward is for queries, not scans. StartKey is not a valid parameter.
  3. Final Answer:

    Use LastEvaluatedKey as the ExclusiveStartKey in the next scan -> Option A
  4. Quick Check:

    Continue scan = ExclusiveStartKey = LastEvaluatedKey [OK]
Hint: Use LastEvaluatedKey as ExclusiveStartKey to continue scan [OK]
Common Mistakes:
  • Using Limit as ExclusiveStartKey
  • Confusing scan with query parameters
  • Using invalid parameter names
3. Given the following scan code snippet, what will be the output if the table has 15 items and Limit is set to 5?
response = table.scan(Limit=5)
items = response['Items']
last_key = response.get('LastEvaluatedKey')
print(len(items), last_key is not None)
medium
A. 5 False
B. 15 False
C. 5 True
D. 15 True

Solution

  1. Step 1: Understand Limit Effect on Scan

    Setting Limit=5 returns up to 5 items in one scan call, not all 15.
  2. Step 2: Check LastEvaluatedKey Presence

    Since there are more items after 5, LastEvaluatedKey will be present (not None), indicating more data.
  3. Final Answer:

    5 True -> Option C
  4. Quick Check:

    Limit=5 returns 5 items and LastEvaluatedKey exists [OK]
Hint: Limit controls items returned; LastEvaluatedKey shows more data [OK]
Common Mistakes:
  • Assuming all items return ignoring Limit
  • Expecting LastEvaluatedKey to be None always
  • Confusing item count with total table size
4. You wrote this code to paginate a scan but it returns only the first page repeatedly:
response = table.scan(Limit=3)
items = response['Items']
while 'LastEvaluatedKey' in response:
    response = table.scan(Limit=3)
    items.extend(response['Items'])
print(len(items))

What is the error?
medium
A. Missing ExclusiveStartKey in the subsequent scan calls
B. Limit should be increased to get all items
C. Items list should be reset inside the loop
D. LastEvaluatedKey should be deleted after each scan

Solution

  1. Step 1: Analyze Pagination Loop

    The loop calls scan repeatedly but does not pass ExclusiveStartKey, so it always scans from start.
  2. Step 2: Identify Missing Parameter

    To continue scanning, ExclusiveStartKey must be set to previous LastEvaluatedKey in each call.
  3. Final Answer:

    Missing ExclusiveStartKey in the subsequent scan calls -> Option A
  4. Quick Check:

    Pagination needs ExclusiveStartKey to continue [OK]
Hint: Pass ExclusiveStartKey to continue scan pages [OK]
Common Mistakes:
  • Not passing ExclusiveStartKey in loop
  • Increasing Limit instead of fixing pagination
  • Resetting items list inside loop
5. You want to scan a DynamoDB table with 1000 items but only want to process 100 items at a time to avoid timeouts. Which approach correctly implements scan pagination to achieve this?
hard
A. Set ExclusiveStartKey to null and scan with Limit=100 once
B. Use a loop with Limit=100 and pass ExclusiveStartKey from LastEvaluatedKey until no more keys
C. Use ScanIndexForward=true with Limit=100 to paginate
D. Scan once with Limit=1000 and split results in code

Solution

  1. Step 1: Understand Pagination Requirements

    To process 100 items at a time, scan must be done in chunks with Limit=100 and continue using ExclusiveStartKey.
  2. Step 2: Evaluate Options

    Use a loop with Limit=100 and pass ExclusiveStartKey from LastEvaluatedKey until no more keys correctly loops with Limit=100 and uses ExclusiveStartKey from LastEvaluatedKey. Scan once with Limit=1000 and split results in code fetches all at once, risking timeout. Use ScanIndexForward=true with Limit=100 to paginate uses wrong parameter for scan. Set ExclusiveStartKey to null and scan with Limit=100 once scans only once without continuation.
  3. Final Answer:

    Use a loop with Limit=100 and pass ExclusiveStartKey from LastEvaluatedKey until no more keys -> Option B
  4. Quick Check:

    Paginate with Limit and ExclusiveStartKey loop [OK]
Hint: Loop with Limit and ExclusiveStartKey until no LastEvaluatedKey [OK]
Common Mistakes:
  • Fetching all items at once risking timeout
  • Using query parameters for scan
  • Not looping to continue scan