Bird
Raised Fist0
DynamoDBquery~10 mins

Adjacency list pattern in DynamoDB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Adjacency list pattern
Start with a node
↓
Store node with unique ID
↓
Store references (IDs) of connected nodes
↓
To find neighbors, query by stored references
↓
Repeat for each node
↓
Traverse graph by following references
Each node stores IDs of its connected nodes. To find neighbors, we look up these IDs. This builds a graph using references.
Execution Sample
DynamoDB
Node1 = {"id": "1", "name": "A", "neighbors": ["2", "3"]}
Node2 = {"id": "2", "name": "B", "neighbors": ["1"]}
Node3 = {"id": "3", "name": "C", "neighbors": ["1"]}
Three nodes with IDs and neighbor lists showing connections.
Execution Table
StepActionNode IDNeighbors StoredQuery ResultNotes
1Create node1["2", "3"]NoneNode A created with neighbors 2 and 3
2Create node2["1"]NoneNode B created with neighbor 1
3Create node3["1"]NoneNode C created with neighbor 1
4Query neighbors of node 11["2", "3"][Node 2, Node 3]Lookup neighbors by IDs
5Query neighbors of node 22["1"][Node 1]Lookup neighbor by ID
6Query neighbors of node 33["1"][Node 1]Lookup neighbor by ID
7Traversal example1["2", "3"][Node 2, Node 3]Traverse from node 1 to neighbors
8Traversal example2["1"][Node 1]Traverse from node 2 back to node 1
9Traversal example3["1"][Node 1]Traverse from node 3 back to node 1
10EndAll nodes created and neighbors queried
💡 All nodes created and neighbor queries completed, traversal demonstrated
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Node1None{"id": "1", "name": "A", "neighbors": ["2", "3"]}{"id": "1", "name": "A", "neighbors": ["2", "3"]}{"id": "1", "name": "A", "neighbors": ["2", "3"]}{"id": "1", "name": "A", "neighbors": ["2", "3"]}
Node2NoneNone{"id": "2", "name": "B", "neighbors": ["1"]}{"id": "2", "name": "B", "neighbors": ["1"]}{"id": "2", "name": "B", "neighbors": ["1"]}
Node3NoneNoneNone{"id": "3", "name": "C", "neighbors": ["1"]}{"id": "3", "name": "C", "neighbors": ["1"]}
QueryResultNoneNoneNoneNone[Node 2, Node 3] after querying neighbors of Node 1
Key Moments - 3 Insights
Why do we store neighbor IDs instead of full neighbor data inside each node?
Storing only neighbor IDs avoids data duplication and keeps nodes small. We fetch full neighbor data by querying these IDs as shown in execution_table rows 4-6.
How do we find neighbors of a node?
We look at the 'neighbors' list of IDs in the node, then query the database for those IDs. This is shown in execution_table rows 4-6.
What happens if a node has no neighbors?
Its 'neighbors' list is empty. Querying neighbors returns an empty list, meaning no connected nodes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What nodes are neighbors of node 1?
ANodes 2 and 3
BNode 1 only
CNo neighbors
DNodes 1, 2, and 3
💡 Hint
Check the 'Query Result' column at step 4 in execution_table
At which step do we create node 3 with its neighbors?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Node ID' columns in execution_table
If node 2 had no neighbors, how would the 'Neighbors Stored' column change at step 2?
A["1"]
B[]
C["2"]
Dnull
💡 Hint
Neighbors list is empty if no neighbors, see variable_tracker for neighbors format
Concept Snapshot
Adjacency list pattern stores each node with a unique ID and a list of neighbor IDs.
To find neighbors, query nodes by these IDs.
This avoids data duplication and models graph connections clearly.
Common in DynamoDB for graph-like data.
Traversal follows neighbor references step-by-step.
Full Transcript
The adjacency list pattern stores graph nodes with unique IDs and lists of neighbor IDs. Each node references connected nodes by their IDs, not by embedding full data. This keeps data small and avoids duplication. To find neighbors, we query the database for nodes matching the stored neighbor IDs. The execution table shows creating three nodes and querying their neighbors. Traversal follows these references to move through the graph. Key moments clarify why IDs are stored, how neighbors are found, and what happens if no neighbors exist. The visual quiz tests understanding of neighbor queries and node creation steps. This pattern is common in DynamoDB for representing graphs efficiently.

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