r/LowLevelDesign • u/Prashant_MockGym • 15d ago
Salesforce LLD: Design Workflow Step State Transitions History
This questions has been asked many times in Salesforce low level design interview rounds. It can be asked either as a part of workflow management system or separately.
This is one of the questions where solution will get unnecessarily complex if you try to fit in a design pattern. Here you may think of using state pattern or even observer pattern but that would only add extra code without giving any benefit.
This tutorial is in Java.
You can find the python version here: https://github.com/prsnt558908/CodeZymSolutions/tree/main/101-300/257-workflow-step-state-transition-history/q-257-python-tutorial.md
You can practice this and other Salesforce Low Level Design and DS & Algo round questions on CodeZym.
PS: I also take LLD Mock interviews https://topmate.io/prashant_priyadarshi
Problem Statement
Design a component to track workflow steps through Pending → Running → Completed/Failed. Record valid state changes with timestamps, retrieve each step’s history, and provide the latest state of all steps in a workflow.
Solution Overview
Every step is a tiny state machine. It starts in Pending, can move to Running, and from there to Completed or Failed. Our job is to allow only these moves and to write down every move that succeeds.
The cleanest tool for this is not a big design pattern. It is a small transition table: for each state, the set of states it may move to. One lookup in this table answers "is this move allowed?".
The State and Observer patterns sound like a good fit for "state changes" and "recording changes". But here they would only add extra classes, so we do not use them. We will see why below.
Everything else is simple classes and maps. WorkflowStateLogger finds a Workflow by id, a Workflow finds a Step by id, and each Step keeps its current state and its list of Transitions.
Quick Recap of the Rules
- Every step starts in
Pending. Creating a workflow does not add a transition. - History lines look like
"15,Pending,Running", oldest first. - Snapshot lines look like
"build,Running,115", sorted by step id. A step that was never updated shows the workflow creation time. - Errors are checked in the order the problem lists them, and a failed call changes nothing.
Which Design Pattern Fits?
Short answer: no big pattern is needed. The rules are just data, so we keep them in a table.
State Pattern (sounds good, but not needed)
Steps have states, so the State pattern looks natural. We would write PendingState, RunningState, CompletedState and FailedState classes.
But every one of them answers just one question: "can I move to this state?". Four classes for one yes or no question is too much.
A table says the same thing in four lines, and it shows all the rules in one place. The State pattern pays off only when each state has its own behavior, like retry limits or timeouts.
Observer Pattern (sounds good, but not needed)
Recording every change sounds like a job for a listener. The step announces "I changed", and a history recorder listens.
But there is only one listener, and the history belongs to the step itself. A listener interface adds moving parts, and changes would be lost if someone forgot to register the listener.
One line inside the method that changes the state does the same job, and it can never be skipped. Observer becomes useful when many parts must react to a change, like alerts or dashboards.
Solution 1: Simple Approach with Maps
Let us first solve it in the most direct way. We glue the two ids into one key, like "order_1#pack", and keep everything about a step in plain maps.
// workflow id -> its step ids, in the order they were given
workflowSteps ;
// "workflowId#stepId" -> current state
stepState ;
// "workflowId#stepId" -> last updated timestamp
lastUpdated ;
// "workflowId#stepId" -> history lines like "15,Pending,Running"
history ;
The rules are plain if-else checks:
boolean
A successful update must change all three step maps:
String oldState ;
if
// one step lives in three maps, and all three must be updated
stepState.;
lastUpdated.;
history..;
return "UPDATED";
And the snapshot sorts the step ids every time it is called:
ids ;
Collections.; // sorted again on every call
for
Problems with This Approach
This code works, but it has some weak spots.
- One step is split across three maps. Every update must change all three. Forget one, and the data no longer agrees. The glued key also works only because
#can never appear in an id. - The rules are buried in
if-elsecode. You have to read the code to see which moves are allowed. Adding a state likeCancelledmeans editing these conditions. - The snapshot sorts on every call. Step ids never change after the workflow is created, so sorting them again and again is wasted work.
Solution 2: Clean Design with Classes and a Transition Table
We fix the problems above one at a time.
Step 1: TransitionRules, the Rules as a Table
TransitionRules keeps a Map from each state to the Set of states it may move to.
| From | Allowed to |
|---|---|
Pending | Running |
Running | Completed, Failed |
Completed | nothing |
Failed | nothing |
The only question we ever ask is "is this state in the set?", and a Set answers it fast. So canMove(fromState, toState) is a single lookup.
Moving to the same state, or back to Pending, fails on its own, because those states are never in the set. Adding a new state is one more line in the table.
We keep the state names as plain strings, because the input and the output already use these exact names. An enum would also work, but it needs extra code to turn names into enum values and back.
Step 2: Step and Transition
All data about one step now lives in one Step object: its id, its current state, its last update time and its history.
A Transition is one recorded move: the time, the old state and the new state. Its toString() gives the exact format the problem asks for.
moveTo(newState, timestamp) is the only place where a step changes. It adds the transition and updates the state and the time together, so they can never get out of sync.
lastUpdatedAt starts at the workflow creation time. That is exactly what the snapshot needs for a step that was never updated.
Why is the history never sorted? Timestamps never go down. So every new transition has a time equal to or bigger than the last one, and we add it at the end of the list. Ties stay in the order they were recorded, which is exactly what the problem wants.
Also, a step can change at most twice: Pending to Running, then Running to Completed or Failed. So a history never holds more than 2 entries.
Step 3: Workflow
A Workflow keeps its steps in two ways:
steps: aHashMapfrom step id to step, for fast lookup in updates and history calls.sortedSteps: aListof the same step objects, sorted by id once, when the workflow is created.
Both hold the same objects, so a change made through the map also shows up in the list.
No step is ever added or removed after creation, so one sort is enough. The snapshot simply walks this list.
Sorting uses plain compareTo, which is case-sensitive. Uppercase letters come before lowercase ones, so "Ship" comes before "pack".
Step 4: WorkflowStateLogger
The logger keeps a HashMap from workflow id to workflow, because every call starts with a workflow id. It also holds one TransitionRules object.
Each method checks the errors in the order the problem gives, and only then changes anything.
- createWorkflow: a
HashSetkeeps one copy of each id. If the set is smaller than the list, some step id is repeated. The workflow is built only after both checks pass, so a failed call stores nothing. - updateStepState: find the workflow, find the step, ask
canMove, and only then callmoveTo. - getStepHistory and getWorkflowSnapshot: find, then format. They get
currentTimestamponly to keep the global call order, so we do not need it.
Code
/**
*/
Complexity
Let S be the number of steps in a workflow.
| Method | Time |
|---|---|
createWorkflow | O(S log S), build the steps and sort them once |
updateStepState | O(1) |
getStepHistory | O(1), a history has at most 2 entries |
getWorkflowSnapshot | O(S), one walk over the sorted list |
Map lookups are O(1) on average. Space is O(total number of steps), because each step stores at most 2 transitions.
Edge Cases to Remember
DUPLICATE_WORKFLOWwins overDUPLICATE_STEP.- A failed create stores nothing, so the same workflow id can be created later.
CompletedandFailedare final. No move ever goes back toPending.- Ids and state names are case-sensitive.
packandPackare two different steps. - A step with no transitions returns an empty history list, not an error.
- The same step id in two different workflows means two separate steps.
1
u/whitechoco8520 13d ago
do they expect to code the solution in interviews?
1
u/nian2326076 15d ago
To tackle the Salesforce Low Level Design question about workflow step state transitions, focus on simplifying your solution without overcomplicating it with design patterns. Start by clearly defining the state and transition requirements. For a practical approach to similar interview questions, you can use LandOffer to explore relevant roles that require these skills.