Design Patterns Tutorial 0/70 lessons ~6 min read Lesson 64

    Mock Interview: Behavioral

    Mock interview: behavioral focuses on collaboration patterns.

    Course progress0%
    Focus
    21 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Mock interview: behavioral focuses on collaboration patterns. Questions are open-ended ("How build undo?") and reward production stories over textbook recall. Command, Observer, State, Strategy composition wins senior loops.

    The story

    Asked "design undo system?" Junior: "Stack of states." Mid: "Command pattern; each command undoes itself." Senior: Command + Memento composition, snapshot vs action trade-off, operational-transform for collaborative undo, named production systems for each. Composed patterns, didn't memorize.

    The business problem

    Behavioral questions test composition and real-world judgment:

    • Single-pattern answers: undo needs Command + maybe Memento.
    • No trade-off: snapshot vs action undo — senior discusses both.
    • No scale: collaborative editing needs OT/CRDT, not Command alone.
    • Textbook only: no Figma/Google Docs style production reference.

    The problem teams faced

    Behavioral mock covers:

    • Undo/redo — Command + Memento composition.
    • Event systems — Observer vs pub/sub vs event bus.
    • Workflow — State vs Strategy vs chain of responsibility.
    • Extensibility — Open-closed via Strategy/Decorator.

    Understanding the topic

    Intent: Practice behavioral Q&A with composition thinking.

    • Command — encapsulate action + undo.
    • Memento — snapshot state for undo.
    • Observer — publish domain events.
    • State — object behavior varies by internal state.

    Internal architecture

    Undo — senior answer structure:

    ts
    1. Single-user editor: Command with execute()/undo() + two stacks (undo/redo)
    2. Complex state: Memento snapshots on save points — memory cost trade-off
    3. Collaborative (Figma/Docs): Command insufficient alone — OT or CRDT layer
    4. Production refs: Photoshop action history (Command), Git (Memento-ish snapshots)
    interface Command { execute(): void; undo(): void }
    class Invoker {
    private undoStack: Command[] = []
    run(cmd: Command) { cmd.execute(); this.undoStack.push(cmd) }
    undo() { this.undoStack.pop()?.undo() }
    }

    Visual explanation

    Three diagrams cover undo composition, event bus, state machine:

    Undo system composition
    Command
    execute + undo
    Memento?
    Snapshot undo
    Action undo
    Inverse command
    Collaborative?
    OT/CRDT layer
    Senior answer composes — doesn't pick one pattern.
    Observer vs Event Bus
    In-process
    Observer
    Cross-service
    Outbox + Kafka
    Sync coupling?
    Observer risk
    Async decouple
    Events
    'Build notification system' — Observer in-process, bus distributed.
    Order state machine
    Pending
    State
    Paid
    Transition
    Shipped
    State
    Invalid transition?
    Guard in State
    State pattern when transitions are complex and guarded.

    Informative example

    Event system — three levels:

    text
    JUNIOR: "Observer — subject notifies observers."
    MID: "Observer for in-process. Domain events as Observer on aggregate.
    Cross-service: Outbox + message broker — not in-process Observer."
    SENIOR: "In-process: Observer or event emitter on OrderPlaced. Distributed:
    Outbox ensures atomic publish with DB write. Notification service
    subscribes — Strategy per channel. Avoid sync Observer across HTTP —
    coupling and failure cascade. Story: 47 if-branches → Strategy rebuild."

    Execution workflow

    1Behavioral mock practice
    1 / 4

    Open-ended Q

    Undo, events, workflow.

    No pattern name in question.

    Real-world use

    Figma undo stack; Git reflog; Redux (Command-ish actions); Kafka event-driven microservices; workflow engines (State + Chain of Responsibility).

    Production case study

    Undo system three-level answers:

    • Junior: stack — incomplete.
    • Mid: Command — correct core.
    • Senior: Command + Memento + OT mention + production refs — offer signal.

    Trade-offs

    • Pro: composition thinking matches real system design.
    • Con: risk over-composing in simple scenarios — scope first.

    Decision framework

    • Undo single-user → Command stacks.
    • Undo with rich state → Memento snapshots at checkpoints.
    • Multi-user realtime → OT/CRDT above Command.
    • In-process events → Observer; distributed → Outbox + bus.

    Best practices

    • Ask: single user or collaborative? sync or async? before naming pattern.
    • Name one production system using the composition.
    • Discuss memory cost of Memento snapshots.

    Anti-patterns to avoid

    • Memento every keystroke in large document — memory explosion.
    • Observer across microservices via HTTP callbacks.

    Common mistakes

    • Strategy vs State confusion — State transitions are internal; Strategy chosen externally.
    • Chain of Responsibility without guaranteed handler.

    Debugging tips

    • If interviewer narrows scope ("single user only") — simplify composition.

    Optimization strategies

    • Prepare 3 compositions: undo, events, order workflow.

    Common misconceptions

    • Command ≠ CQRS command — same word, different layer. Clarify in interview.
    • Observer ≠ Kafka — pattern vs infrastructure.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1AdvancedQuestionDesign undo/redo?+

    Answer

    Command with execute/undo, undo/redo stacks. Complex docs: Memento at save points. Collaborative: OT/CRDT. Trade snapshot memory vs action inverse complexity.

    Follow-up

    Figma approach?
    2IntermediateQuestionObserver vs pub/sub?+

    Answer

    Observer: direct subject-observer list, sync, in-process. Pub/sub: broker decouples publishers/subscribers, async, often distributed. Enterprise: Outbox + Kafka.

    Follow-up

    When not Observer?
    3IntermediateQuestionState vs Strategy?+

    Answer

    State: object changes behavior as internal state changes; transitions often guarded. Strategy: algorithm chosen externally, typically stable during operation.

    Follow-up

    Order workflow?

    Summary

    You can answer behavioral questions at senior level with composition and trade-offs. Run the undo mock aloud — you own Behavioral Interview prep.

    Ready to mark this lesson complete?Track your journey across the entire course.