0
0
LangChainframework~20 mins

Graph nodes and edges in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LangChain Graph Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this LangChain graph node connection?

Consider a LangChain graph where you create two nodes and connect them with an edge. What will be the output when you print the edges of the graph?

LangChain
from langchain.graphs import Graph
from langchain.graphs.nodes import Node

g = Graph()
node1 = Node(id="1", data="Start")
node2 = Node(id="2", data="End")
g.add_node(node1)
g.add_node(node2)
g.add_edge("1", "2")
print(g.edges)
A[('2', '1')]
B[]
C[('1', '2')]
DSyntaxError
Attempts:
2 left
💡 Hint

Edges are directional from source to target node.

📝 Syntax
intermediate
2:00remaining
Which option correctly creates a node with metadata in LangChain?

Given the LangChain Node class, which code snippet correctly creates a node with id '3' and metadata containing {'type': 'question'}?

ANode(id='3', data='Q1', metadata={'type': 'question'})
BNode('3', 'Q1', {'type': 'question'})
CNode(id=3, data='Q1', metadata={'type': 'question'})
DNode(id='3', data='Q1', meta={'type': 'question'})
Attempts:
2 left
💡 Hint

Check the parameter names and types carefully.

state_output
advanced
2:00remaining
What is the value of g.nodes after adding nodes and edges?

After running the following code, what is the value of g.nodes?

LangChain
from langchain.graphs import Graph
from langchain.graphs.nodes import Node

g = Graph()
node1 = Node(id="1", data="A")
node2 = Node(id="2", data="B")
node3 = Node(id="3", data="C")

g.add_node(node1)
g.add_node(node2)
g.add_node(node3)
g.add_edge("1", "2")
g.add_edge("2", "3")

print(g.nodes)
A['1', '2', '3']
B{'1': Node(id='1', data='A'), '2': Node(id='2', data='B'), '3': Node(id='3', data='C')}
C{Node(id='1', data='A'), Node(id='2', data='B'), Node(id='3', data='C')}
D['A', 'B', 'C']
Attempts:
2 left
💡 Hint

Remember that g.nodes stores nodes keyed by their id.

🔧 Debug
advanced
2:00remaining
Which option causes a runtime error when adding an edge?

Given a LangChain graph g with nodes having ids '1' and '2', which code snippet will cause a runtime error when adding an edge?

LangChain
from langchain.graphs import Graph
from langchain.graphs.nodes import Node

g = Graph()
g.add_node(Node(id='1', data='X'))
g.add_node(Node(id='2', data='Y'))
Ag.add_edge('1', '2')
Bg.add_edge('2', '1')
Cg.add_edge('1', '1')
Dg.add_edge('2', '3')
Attempts:
2 left
💡 Hint

Check if both nodes exist before adding an edge.

🧠 Conceptual
expert
2:00remaining
What is the effect of removing a node on edges in LangChain graph?

In LangChain's graph, if you remove a node with id '5' that has edges connected to it, what happens to those edges?

AAll edges connected to node '5' are automatically removed.
BEdges remain but become invalid references causing errors on access.
CEdges connected to node '5' are reassigned to a default node.
DEdges connected to node '5' are converted to undirected edges.
Attempts:
2 left
💡 Hint

Think about graph consistency when nodes are removed.