What is a UML Sequence Diagram?
UML sequence diagrams explained — lifelines, calls, returns, and combined fragments like alt and loop — with a Java example.
Expected Interview Answer
A UML sequence diagram models the time-ordered flow of messages exchanged between objects or components to carry out a specific interaction or use case, showing who calls whom, in what order, and with what response.
Each participant is drawn as a vertical lifeline, and horizontal arrows between lifelines represent method calls (solid arrow with a filled head) and their returns (dashed arrow). Activation bars on a lifeline show when that object is actively executing. Sequence diagrams also support combined fragments — alt for conditional branches, loop for repetition, opt for optional steps — letting a single diagram express branching flows, not just a single straight-line path. Unlike a class diagram, which is static and shows structure, a sequence diagram is dynamic and shows behavior over time for one specific scenario, such as 'user places an order.'
- Makes call order and timing explicit for complex interactions
- Surfaces which object initiates and which objects merely respond
- Reveals synchronous vs asynchronous calls and long-running operations
- Helps debug multi-service or multi-object flows before implementation
AI Mentor Explanation
A sequence diagram of a run-out appeal shows lifelines for Fielder, Umpire, and ThirdUmpire, with a solid arrow from Fielder to Umpire labeled appeal(), then a solid arrow from Umpire to ThirdUmpire labeled reviewFootage(), and a dashed return arrow back with the decision. The vertical order top-to-bottom is the actual time order the calls happened in real play. Reading the diagram top to bottom tells you exactly who asked whom and in what sequence, the same way code executes.
Step-by-Step Explanation
Step 1
List the participants
Identify each object or actor involved and draw a vertical lifeline for each.
Step 2
Order the messages
Draw solid arrows top-to-bottom in the exact chronological order calls are made.
Step 3
Add activation bars
Mark the span of time each lifeline is actively processing a call.
Step 4
Wrap branches and loops
Use alt, opt, and loop combined fragments for conditional or repeated flows.
What Interviewer Expects
- Understanding that sequence diagrams model behavior over time, not static structure
- Correct arrow semantics: solid for calls, dashed for returns
- Familiarity with combined fragments (alt, opt, loop)
- Ability to pick a realistic scenario and trace it correctly
Common Mistakes
- Confusing a sequence diagram with a class diagram
- Drawing calls out of chronological order
- Forgetting return messages entirely
- Not knowing how to represent conditional branches with alt/opt fragments
Best Answer (HR Friendly)
“A sequence diagram is like a timeline of a conversation between objects for one specific scenario. Each participant gets a vertical line, and arrows moving down the page show which object called which, in what order, and what came back. It is great for understanding complex multi-step interactions before writing any code.”
Code Example
class Kiosk {
private final BaggageSystem baggageSystem;
Kiosk(BaggageSystem baggageSystem) { this.baggageSystem = baggageSystem; }
// Corresponds to the checkIn() -> tagBag() -> return arrows
BoardingPass checkIn(Passenger passenger, boolean hasBag) {
if (hasBag) {
baggageSystem.tagBag(passenger); // maps to an "alt" fragment branch
}
return new BoardingPass(passenger);
}
}
class BaggageSystem {
void tagBag(Passenger passenger) {
System.out.println("Tagged bag for " + passenger.getName());
}
}Follow-up Questions
- How does a sequence diagram differ from an activity diagram?
- What does an activation bar on a lifeline represent?
- How would you show an asynchronous call in a sequence diagram?
- When would you prefer a sequence diagram over a class diagram in a design review?
MCQ Practice
1. A UML sequence diagram primarily models?
Sequence diagrams are behavioral, showing message order over time for a specific interaction.
2. What does a dashed arrow typically represent in a sequence diagram?
Dashed arrows represent return messages, as opposed to solid arrows for calls.
3. Which combined fragment represents a conditional branch?
The alt fragment expresses mutually exclusive conditional branches within a sequence diagram.
Flash Cards
What does a lifeline represent? — A single participant (object/actor) over the duration of the interaction.
Solid arrow vs dashed arrow? — Solid is a call/message; dashed is the return.
What is an activation bar? — A rectangle on a lifeline showing when that object is actively executing.
Class diagram vs sequence diagram? — Class diagrams show static structure; sequence diagrams show dynamic behavior over time.