Bird
Raised Fist0
DynamoDBquery~20 mins

Adjacency list pattern in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Adjacency List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Retrieve all direct children of a parent node

Given a DynamoDB table using the adjacency list pattern where each item has a NodeId and a ParentId, which query will return all direct children of the node with NodeId = 'A1'?

DynamoDB
Table: Nodes
Attributes: NodeId (PK), ParentId
Sample data:
{NodeId: 'A1', ParentId: null}
{NodeId: 'B1', ParentId: 'A1'}
{NodeId: 'B2', ParentId: 'A1'}
{NodeId: 'C1', ParentId: 'B1'}
AQuery with KeyConditionExpression: ParentId = 'A1' using ParentId as partition key
BQuery with KeyConditionExpression: NodeId = 'A1'
CScan with FilterExpression: ParentId = 'A1'
DScan with FilterExpression: NodeId = 'A1'
Attempts:
2 left
💡 Hint

Remember that in DynamoDB, Query works only on the partition key. If ParentId is not the partition key, you cannot query by it directly.

🧠 Conceptual
intermediate
1:30remaining
Understanding adjacency list pattern in DynamoDB

In the adjacency list pattern, how is the parent-child relationship typically represented in a DynamoDB table?

AEach item stores its own <code>NodeId</code> and the <code>ParentId</code> of its parent node
BEach item stores a list of all its children <code>NodeIds</code> in an attribute
CEach item stores only its <code>NodeId</code> without any reference to parent or children
DEach item stores the entire path from root to itself as a string
Attempts:
2 left
💡 Hint

Think about how you can find a node's parent or children by looking at attributes.

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

Which option contains a syntax error in the DynamoDB query expression to find children nodes?

DynamoDB
Query parameters example:
{
  TableName: 'Nodes',
  KeyConditionExpression: 'ParentId = :pid',
  ExpressionAttributeValues: { ':pid': 'A1' }
}
AKeyConditionExpression: 'ParentId = :pid', ExpressionAttributeValues: { ':pid': 'A1' }
B} '1A' :'dip:' { :seulaVetubirttAnoisserpxE ,'dip: = dItneraP' :noisserpxEnoitidnoCyeK
CKeyConditionExpression: 'ParentId = :pid', ExpressionAttributeValues: { ':pid': { S: 'A1' } }
DKeyConditionExpression: 'ParentId == :pid', ExpressionAttributeValues: { ':pid': 'A1' }
Attempts:
2 left
💡 Hint

Check the operator used in the KeyConditionExpression.

❓ optimization
advanced
2:30remaining
Optimize adjacency list queries for children nodes

You want to efficiently query all children of a node by ParentId in DynamoDB. Which table design improves query performance?

AUse <code>ParentId</code> as the partition key and <code>NodeId</code> as the sort key
BUse <code>NodeId</code> as the partition key and <code>ParentId</code> as a regular attribute
CUse a composite key with <code>NodeId</code> and a timestamp
DUse a global secondary index with <code>NodeId</code> as partition key
Attempts:
2 left
💡 Hint

Think about which key allows you to query all children by parent efficiently.

🔧 Debug
expert
3:00remaining
Why does this recursive adjacency list traversal fail in DynamoDB?

A developer tries to recursively fetch all descendants of a node by repeatedly querying children using ParentId. The code runs but returns incomplete results. What is the most likely cause?

AThe developer used Scan instead of Query, causing inconsistent results
BDynamoDB Query only returns up to 1MB of data per request, so pagination is needed to get all children
CThe table does not have a <code>ParentId</code> attribute, so queries fail silently
DRecursive queries are not supported in DynamoDB and cause runtime errors
Attempts:
2 left
💡 Hint

Consider DynamoDB limits on query results and how to handle large datasets.

Practice

(1/5)
1. What does the adjacency list pattern store in each item to represent relationships in DynamoDB?
easy
A. The parent item's identifier
B. The list of all child items
C. The full path from root to the item
D. The depth level of the item in the hierarchy

Solution

  1. Step 1: Understand adjacency list pattern basics

    The adjacency list pattern stores a reference to the parent item for each node to represent hierarchy.
  2. Step 2: Identify what is stored per item

    Each item contains the parent's ID, not the children or full path or depth.
  3. Final Answer:

    The parent item's identifier -> Option A
  4. Quick Check:

    Adjacency list = parent ID stored [OK]
Hint: Remember: adjacency list stores parent ID only [OK]
Common Mistakes:
  • Thinking it stores all children in each item
  • Confusing with storing full path or depth
  • Assuming it stores unrelated metadata
2. Which of the following is the correct way to represent a parent-child relationship in a DynamoDB item using the adjacency list pattern?
easy
A. { "ItemId": "123", "Children": ["456", "789"] }
B. { "ItemId": "123", "ParentId": "456" }
C. { "ItemId": "123", "Path": "/root/456/123" }
D. { "ItemId": "123", "Depth": 2 }

Solution

  1. Step 1: Identify adjacency list item structure

    Adjacency list stores the parent ID inside each item to link it to its parent.
  2. Step 2: Match correct JSON structure

    { "ItemId": "123", "ParentId": "456" } shows an item with its own ID and a ParentId field, which is correct.
  3. Final Answer:

    { "ItemId": "123", "ParentId": "456" } -> Option B
  4. Quick Check:

    ParentId field present = adjacency list format [OK]
Hint: Look for ParentId key in item JSON [OK]
Common Mistakes:
  • Using Children array instead of ParentId
  • Including full path or depth instead of parent reference
  • Confusing item ID with parent ID
3. Given the following DynamoDB items using adjacency list pattern:
{ "ItemId": "1", "ParentId": null }
{ "ItemId": "2", "ParentId": "1" }
{ "ItemId": "3", "ParentId": "1" }
{ "ItemId": "4", "ParentId": "2" }

If you query for items with ParentId = '1', which items will you get?
medium
A. [{ "ItemId": "2", "ParentId": "1" }, { "ItemId": "3", "ParentId": "1" }]
B. [{ "ItemId": "1", "ParentId": null }]
C. [{ "ItemId": "4", "ParentId": "2" }]
D. [{ "ItemId": "2", "ParentId": "1" }, { "ItemId": "3", "ParentId": "1" }, { "ItemId": "4", "ParentId": "2" }]

Solution

  1. Step 1: Understand query condition

    The query filters items where ParentId equals '1'.
  2. Step 2: Identify matching items

    Items with ParentId '1' are ItemId '2' and '3'. Item '4' has ParentId '2', so it does not match.
  3. Final Answer:

    [{ "ItemId": "2", "ParentId": "1" }, { "ItemId": "3", "ParentId": "1" }] -> Option A
  4. Quick Check:

    Filter ParentId='1' returns items 2 and 3 [OK]
Hint: Filter by ParentId to get direct children only [OK]
Common Mistakes:
  • Including items with different ParentId values
  • Confusing root item with children
  • Assuming query returns grandchildren
4. You wrote this DynamoDB query to get children of item '5':
FilterExpression: "ParentId = :pid", ExpressionAttributeValues: { ":pid": "5" }

But it returns no results, even though you know children exist. What is the likely error?
medium
A. The ExpressionAttributeValues syntax is incorrect
B. You should use KeyConditionExpression instead of FilterExpression
C. You need to query with ItemId = '5' instead
D. ParentId attribute is missing or named differently in items

Solution

  1. Step 1: Check attribute naming

    If ParentId attribute is missing or named differently, filter finds no matches.
  2. Step 2: Verify attribute presence in items

    Ensure all child items have ParentId attribute exactly named 'ParentId'. If not, query returns empty.
  3. Final Answer:

    ParentId attribute is missing or named differently in items -> Option D
  4. Quick Check:

    Attribute name mismatch causes no results [OK]
Hint: Check attribute names match exactly in query and items [OK]
Common Mistakes:
  • Confusing FilterExpression with KeyConditionExpression
  • Incorrect syntax in ExpressionAttributeValues
  • Querying by ItemId instead of ParentId
5. You want to retrieve the full hierarchy path from a child item up to the root using the adjacency list pattern in DynamoDB. Which approach is best?
hard
A. Use a DynamoDB join operation to combine parent and child items
B. Store the full path string in each item and query it directly
C. Iteratively query each item's ParentId until reaching the root
D. Use a single query filtering all items with ParentId equal to the child's ID

Solution

  1. Step 1: Understand adjacency list limitations

    Adjacency list stores only parent references, so full path is not stored.
  2. Step 2: Retrieve full path by iterative queries

    To get full path, query each item's parent repeatedly until root (ParentId null) is reached.
  3. Step 3: Evaluate other options

    Store the full path string in each item and query it directly is a different pattern (materialized path). Use a single query filtering all items with ParentId equal to the child's ID gets children, not parents. Use a DynamoDB join operation to combine parent and child items is invalid as DynamoDB has no join.
  4. Final Answer:

    Iteratively query each item's ParentId until reaching the root -> Option C
  5. Quick Check:

    Adjacency list needs iterative parent queries for full path [OK]
Hint: Follow parent links step-by-step to build full path [OK]
Common Mistakes:
  • Expecting DynamoDB to support joins
  • Assuming full path is stored in adjacency list
  • Trying to get full path with single query filtering children
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