Neural Mastery
← Practice
State Update Merge Reducermedium

State Update Merge Reducer

medium Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Interactive
Graph & Tree Traversal, Step by Step
Structure
Algorithm
ABCDE
visiting A -> visited order so far: [A]
queue now: [B, C]
Step 1 / 5
Both modes run the real algorithm from the linked practice problem, one step at a time -- edit the graph or pick different tree nodes and every step recomputes live.

Implement a state-merge reducer, the real mechanism graph-based agent frameworks use to combine a node's partial output into the shared workflow state (some fields append, some overwrite).

Task

Implement merge_state_update(current_state, update, list_keys). For keys in list_keys, the merged value is current_state.get(key, []) + update[key] (append/extend, list-valued fields like message history). For all other keys in update, simply overwrite current_state's value. Return the new merged dict (don't mutate current_state).

Function Signature

merge_state_update(current_state: dict, update: dict, list_keys: list[str]) -> dict

Examples

Example 1: list field appends
Input: current_state = {'messages': ['hi']} update = {'messages': ['there']} list_keys = ['messages']
Output: {'messages': ['hi', 'there']}
Example 2: scalar field overwrites
Input: current_state = {'step': 1} update = {'step': 2} list_keys = ['messages']
Output: {'step': 2}
Example 3: new list field starts empty then appends
Input: current_state = {} update = {'messages': ['first']} list_keys = ['messages']
Output: {'messages': ['first']}

Constraints

  • update may contain keys not in current_state
Python3Saved ✓
def merge_state_update(current_state, update, list_keys):
# Your implementation here
pass

Case 1: list field appends
Input: current_state = {'messages': ['hi']} update = {'messages': ['there']} list_keys = ['messages']
Expected: {'messages': ['hi', 'there']}
Case 2: scalar field overwrites
Input: current_state = {'step': 1} update = {'step': 2} list_keys = ['messages']
Expected: {'step': 2}
Case 3: new list field starts empty then appends
Input: current_state = {} update = {'messages': ['first']} list_keys = ['messages']
Expected: {'messages': ['first']}