The adjacency list pattern helps you store and find relationships between items, like family trees or company teams, in a simple way.
Adjacency list pattern in DynamoDB
Table: Items
Attributes:
- id (Primary Key)
- parent_id (can be null or id of another item)
- other attributes...
To find children of an item:
Query where parent_id = :id_valueThe id uniquely identifies each item.
The parent_id points to the item's parent, or is null if it has none.
id: 1, parent_id: null, name: 'Root Node' id: 2, parent_id: 1, name: 'Child A' id: 3, parent_id: 1, name: 'Child B'
id: 4, parent_id: 2, name: 'Grandchild of Root' id: 5, parent_id: null, name: 'Another Root'
Query: SELECT * FROM Items WHERE parent_id = 1Query: SELECT * FROM Items WHERE parent_id IS NULL
This example creates a table, adds a root item and two children, then queries the children of the root.
/* DynamoDB example using AWS CLI syntax for clarity */ # Create table aws dynamodb create-table \ --table-name Items \ --attribute-definitions AttributeName=id,AttributeType=S \ --key-schema AttributeName=id,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 # Insert root item aws dynamodb put-item --table-name Items --item '{"id": {"S": "1"}, "parent_id": {"NULL": true}, "name": {"S": "Root Node"}}' # Insert child items aws dynamodb put-item --table-name Items --item '{"id": {"S": "2"}, "parent_id": {"S": "1"}, "name": {"S": "Child A"}}' aws dynamodb put-item --table-name Items --item '{"id": {"S": "3"}, "parent_id": {"S": "1"}, "name": {"S": "Child B"}}' # Query children of root aws dynamodb scan --table-name Items --filter-expression "parent_id = :pid" --expression-attribute-values '{":pid": {"S": "1"}}'
The adjacency list pattern is simple but can be slow for deep trees because you query one level at a time.
Time complexity for finding direct children is fast (depends on number of children), but finding all descendants requires multiple queries.
Use this pattern when your relationships are mostly one-level or you don't need to query deep hierarchies often.
The adjacency list pattern stores each item's parent to represent relationships.
It is easy to understand and works well for simple hierarchies.
Querying deep or complex trees may need extra logic or different patterns.
Practice
Solution
Step 1: Understand adjacency list pattern basics
The adjacency list pattern stores a reference to the parent item for each node to represent hierarchy.Step 2: Identify what is stored per item
Each item contains the parent's ID, not the children or full path or depth.Final Answer:
The parent item's identifier -> Option AQuick Check:
Adjacency list = parent ID stored [OK]
- Thinking it stores all children in each item
- Confusing with storing full path or depth
- Assuming it stores unrelated metadata
Solution
Step 1: Identify adjacency list item structure
Adjacency list stores the parent ID inside each item to link it to its parent.Step 2: Match correct JSON structure
{ "ItemId": "123", "ParentId": "456" }shows an item with its own ID and a ParentId field, which is correct.Final Answer:
{ "ItemId": "123", "ParentId": "456" } -> Option BQuick Check:
ParentId field present = adjacency list format [OK]
- Using Children array instead of ParentId
- Including full path or depth instead of parent reference
- Confusing item ID with parent ID
{ "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?Solution
Step 1: Understand query condition
The query filters items where ParentId equals '1'.Step 2: Identify matching items
Items with ParentId '1' are ItemId '2' and '3'. Item '4' has ParentId '2', so it does not match.Final Answer:
[{ "ItemId": "2", "ParentId": "1" }, { "ItemId": "3", "ParentId": "1" }] -> Option AQuick Check:
Filter ParentId='1' returns items 2 and 3 [OK]
- Including items with different ParentId values
- Confusing root item with children
- Assuming query returns grandchildren
FilterExpression: "ParentId = :pid", ExpressionAttributeValues: { ":pid": "5" }But it returns no results, even though you know children exist. What is the likely error?
Solution
Step 1: Check attribute naming
If ParentId attribute is missing or named differently, filter finds no matches.Step 2: Verify attribute presence in items
Ensure all child items have ParentId attribute exactly named 'ParentId'. If not, query returns empty.Final Answer:
ParentId attribute is missing or named differently in items -> Option DQuick Check:
Attribute name mismatch causes no results [OK]
- Confusing FilterExpression with KeyConditionExpression
- Incorrect syntax in ExpressionAttributeValues
- Querying by ItemId instead of ParentId
Solution
Step 1: Understand adjacency list limitations
Adjacency list stores only parent references, so full path is not stored.Step 2: Retrieve full path by iterative queries
To get full path, query each item's parent repeatedly until root (ParentId null) is reached.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.Final Answer:
Iteratively query each item's ParentId until reaching the root -> Option CQuick Check:
Adjacency list needs iterative parent queries for full path [OK]
- Expecting DynamoDB to support joins
- Assuming full path is stored in adjacency list
- Trying to get full path with single query filtering children
Ready to prove it?
Pass the test and this pattern moves unknown
