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

    Mock Interview: Creational

    Mock interview: creational simulates a 45-minute senior loop on object-creation patterns.

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

    Introduction

    Mock interview: creational simulates a 45-minute senior loop on object-creation patterns. Each question includes junior, mid, and senior answers so you see the upgrade path explicitly.

    The story

    Candidate asked "implement Singleton" — wrote double-checked locking from memory. Follow-up: "How test code depending on it?" Froze. Singleton answer correct; test answer revealed gap. Mock practice fixes this.

    The business problem

    Creational questions trap candidates who memorize implementation but miss judgment:

    • Singleton testability: can't mock global instance.
    • Factory overuse: Abstract Factory for one product.
    • Builder vs constructor: when telescoping actually matters.
    • DI vs Singleton: modern alternative not mentioned.

    The problem teams faced

    Creational mock covers:

    • Singleton — when, testability, DI alternative.
    • Factory Method vs Abstract Factory — force distinction.
    • Builder — complex object with optional fields.
    • Prototype — clone vs reconstruct; when copy expensive.

    Understanding the topic

    Intent: Practice creational Q&A at three seniority levels.

    • Junior — definition + naive implementation.
    • Mid — force + basic trade-off.
    • Senior — alternatives, testing, production story, when-not.

    Internal architecture

    Mock Q1: Singleton — three answers:

    text
    JUNIOR: "One instance." + writes double-checked locking.
    MID: "Global state; hurts testing. Use DI scoped singleton."
    SENIOR: "Creational — one instance per process. Force: expensive shared
    resource. Trade-off: hidden dependency, test difficulty. Alternative: DI
    container singleton scope. Story: metrics registry — legacy boundary required
    hand-roll. Test: inject MetricsCollector interface; prod uses singleton impl,
    tests use fake. Never for request-scoped data."

    Visual explanation

    Three diagrams cover answer levels, Factory vs Abstract Factory, Singleton test seam:

    Answer level ladder
    Q: Singleton
    Common
    Junior: DCL code
    Memorized
    Mid: + trade-offs
    Test pain
    Senior: DI + story
    Judgment
    Follow-up 'how test?' separates mid from senior.
    Factory Method vs Abstract Factory
    One product hierarchy?
    Factory Method
    Families of products?
    Abstract Factory
    WinButton + WinDialog
    Family
    One impl forever?
    Neither
    Force: single product vs coordinated product family.
    Singleton test seam
    Global getInstance()
    Untestable
    Inject interface
    Seam
    DI container
    Scoped singleton
    Test with fake
    Green
    Senior answer always addresses testability.

    Informative example

    Mock Q2: Builder — senior answer sketch:

    ts
    // Force: Order with 12 optional fields — constructor telescoping
    // Senior: "Builder when >4 optional params or validation between steps.
    // Alternative: factory function with defaults object in TS.
    // Story: checkout OrderBuilder — validate address before payment step.
    // Not Builder for 3-field DTO — YAGNI."
    class OrderBuilder {
    private order: Partial<Order> = {}
    withItems(items: LineItem[]) { this.order.items = items; return this }
    withShipping(addr: Address) { /* validate */ this.order.shipTo = addr; return this }
    build(): Order { /* final validation */ return Order.create(this.order) }
    }

    Execution workflow

    1Mock creational practice
    1 / 4

    Timer 45 min

    4-5 questions.

    Realistic pace.

    Real-world use

    Senior loops at Google/Meta often start creational then pivot to system design; Stripe interviews Factory/Adapter for payment provider integration.

    Production case study

    Singleton freeze on test question:

    • Implementation: correct double-checked locking.
    • Follow-up: "How test dependents?" — no answer.
    • Lesson: always pair creational with test seam.

    Trade-offs

    • Pro: explicit junior/mid/senior ladder accelerates learning.
    • Con: mock ≠ real interviewer unpredictability.

    Decision framework

    • Every creational answer: force, test, when-not.
    • Factory Method: one product type varies. Abstract Factory: product families.
    • Builder: many optional fields + step validation.

    Best practices

    • Never write Singleton code without mentioning test alternative.
    • Prototype: mention structuredClone vs custom clone when fields are complex.
    • Object Pool: only when allocation measurably expensive.

    Anti-patterns to avoid

    • Abstract Factory with one family — deletion candidate.
    • Singleton for database connection in serverless — wrong scope.

    Common mistakes

    • Confusing Factory Method (method) with Simple Factory (function).
    • Builder for 2-field object — overengineering.

    Debugging tips

    • If stuck on Factory vs Abstract Factory — ask "one product or family?"

    Optimization strategies

    • Prepare 4 creational stories: Singleton removal, Factory for payments, Builder for orders, Prototype for templates.

    Common misconceptions

    • Simple Factory is not GoF Factory Method — know the distinction.
    • DI replaces most Singleton hand-rolls — say it in interview.

    Advanced interview questions

    Interview Prep

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

    3 questions
    1IntermediateQuestionFactory Method vs Abstract Factory?+

    Answer

    Factory Method: subclass decides which product to create — one hierarchy. Abstract Factory: creates families of related products — WinButton+WinDialog vs MacButton+MacDialog.

    Follow-up

    When neither?
    2AdvancedQuestionTest Singleton dependents?+

    Answer

    Don't — inject interface; DI container provides singleton in prod, fake in test. Or pass instance through constructor.

    Follow-up

    Hand-roll case?
    3IntermediateQuestionBuilder vs factory function?+

    Answer

    Builder when step validation order matters or fluent API helps. TS factory function with defaults often suffices for simple cases.

    Follow-up

    OrderBuilder?

    Summary

    You can upgrade creational answers from junior to senior and survive the Singleton test follow-up. Complete this mock aloud — you own Creational Interview prep.

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