Interview Deep Dive: Common Sequence Diagram Questions
Interviewers often ask you to draw sequence diagrams for critical workflows to verify your design handles edge cases and validates object interactions.
What they're testing: Can you identify all participating objects, proper ordering of operations, and validation steps?
Key points to mention: Validate member eligibility before modifying state. Check book availability before creating transaction. Update both Book and Member state atomically. Return transaction object for record-keeping.
canBorrow() check to include that logic.What they're testing: Do you understand synchronization and race conditions in shared state?
Two drivers request spots simultaneously:
Discussion: Without synchronization, both threads might see the same spot as available. Solution: Make findAndReserve() atomic using locks, or use optimistic locking where markOccupied() fails if spot already taken, triggering retry. In interviews, acknowledge the issue exists but note that "full implementation would need thread-safety, possibly using synchronized blocks or database transactions."
What they're testing: Can you model asynchronous operations and callbacks?
Key pattern: Asynchronous request-response with callback. addStop() is async (open arrowhead), control returns immediately. Later, elevator notifies controller via arrivedAt() callback. This shows understanding of event-driven design.
- "Add payment processing to parking lot:" Extend exit sequence to include
:PaymentProcessor,:PricingStrategy, and handling declined payments with retry loop - "Support reservation system in parking:" Show reservation creation, validation at entry (
hasReservation()), and reserved spot allocation - "Handle elevator emergency stop:" Show alternative path where
:SafetySysteminterrupts normal flow, sendsemergencyStop(), triggers:AlarmSystem
- Showing getter/setter calls explicitly (
getPrice(),setPrice()) - assume property access - Having God Object that does everything -
:Systemshouldn't orchestrate all operations - Missing validation steps - always check preconditions before state changes
- Inconsistent state - if you
markOccupied(), must latermarkAvailable() - Forgetting return values - show what data flows back, especially for queries
After drawing sequence diagram, interviewers probe deeper:
- "What if this operation fails?" - Show alternative error path or explain rollback strategy
- "Is this thread-safe?" - Discuss synchronization needs for shared state
- "What's the time complexity?" - If sequence includes search, mention algorithm used
- "How would you test this?" - Talk about mocking objects and verifying message order