Bird
Raised Fist0

What is the time complexity of constructing a complex object using the Builder pattern when the object has k parts to build?

medium🪤 Complexity Trap Q13 of Q15
OOP & Design Patterns - Factory vs Abstract Factory vs Builder - When to Use Each
What is the time complexity of constructing a complex object using the Builder pattern when the object has k parts to build?
AO(k), because each of the k parts is built sequentially
BO(1), since each build method is called once
CO(k^2), due to nested calls between builder methods
DO(log k), as parts are built using divide-and-conquer
Step-by-Step Solution
  1. Step 1: Identify number of build steps

    The Builder pattern calls a build method for each part, so k parts mean k method calls.
  2. Step 2: Analyze time per build call

    Each build method adds a part in O(1) time, so total time is O(k).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Linear time proportional to number of parts built [OK]
Quick Trick: Each part built once -> O(k) time [OK]
Common Mistakes:
MISTAKES
  • Confusing constant time with O(k)
  • Assuming nested calls cause O(k^2)
Trap Explanation:
PITFALL
  • Option A looks plausible since each method is called once, but total calls scale with k, not constant.
Interviewer Note:
CONTEXT
  • Tests understanding of Builder pattern's time complexity and common off-by-one traps.
Master "Factory vs Abstract Factory vs Builder - When to Use Each" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes