Creational PatternsPrototype PatternHard⏱️ ~3 min

Prototype Pattern: Interview Deep Dive

Common Interview Questions:

Interviewers test whether you understand cloning mechanics, handle edge cases, and make appropriate design decisions. They often ask about deep vs shallow copying, performance implications, and alternatives.

Question 1: Deep vs Shallow Copy

"When would you use shallow copy vs deep copy in Prototype pattern?"

Answer: Use shallow copy when nested objects are immutable or when shared state is intentional. Use deep copy when nested objects are mutable and clones should be independent. For example, if a Document has a metadata object containing author and creation date (immutable), shallow copy is fine. But if it has a paragraphs list that can be edited, deep copy prevents changes in one document affecting another. Always ask the interviewer about mutability assumptions upfront.
Question 2: Handling Circular References

"How do you handle circular references when cloning?"

Answer: Maintain a cloning cache (map from original to clone). Before cloning an object, check if it's already in the cache. If yes, return the cached clone. If no, create a new clone, add it to cache, then recursively clone its fields. This prevents infinite loops and preserves reference structure. For example, in a document with footnotes that reference back to sections, the cache ensures the cloned footnote points to the cloned section, not the original.
function deepClone(obj, cache = {}):
  if obj in cache:
    return cache[obj]
  clone = new obj.constructor()
  cache[obj] = clone
  for each field in obj:
    clone[field] = deepClone(obj[field], cache)
  return clone
Question 3: Prototype Registry Implementation

"Design a prototype registry for a parking lot with different vehicle types."

Answer: Create a VehicleRegistry that stores prototype instances mapped to vehicle types. When creating a vehicle, look up the prototype and clone it. This avoids rebuilding complex vehicle configurations each time.
class VehicleRegistry:
  prototypes: Map<String, Vehicle>

  function register(type: String, prototype: Vehicle):
    prototypes[type] = prototype

  function createVehicle(type: String): Vehicle
    if type not in prototypes:
      throw InvalidVehicleType
    return prototypes[type].clone()

// Usage
registry.register("MOTORCYCLE", new Motorcycle(wheels=2))
registry.register("CAR", new Car(wheels=4, doors=4))
bike = registry.createVehicle("MOTORCYCLE")
Machine Coding Considerations:

In a timed coding round, focus on correctness first. Start with shallow copy, then add deep copy only for fields that need it. Document assumptions about immutability. If the problem involves complex nested structures, discuss with the interviewer whether full deep cloning is required or if selective field-by-field copying is acceptable. Always handle null checks when cloning references.

Common Pitfall: Forgetting to clone mutable collection fields. If a class has List<Item> items, ensure you create a new list and clone each item if items are mutable. Simply copying the reference creates shared state bugs.
Variations to Discuss:

First, Copy Constructor: Instead of a clone() method, use a constructor that takes an existing instance. This is clearer in languages without native clone support. Second, Serialization-based Cloning: Serialize the object to bytes and deserialize to create a copy. This handles deep cloning automatically but is slower. Third, Clone Builder: Combine with Builder pattern to allow customization after cloning (clone then modify specific fields).

Interview Tip: If asked to implement Prototype in a language without clone support (JavaScript, Python), mention copy constructors or copying libraries. Show you adapt patterns to language idioms, not just memorize UML.
💡 Key Takeaways
Deep copy for mutable nested objects, shallow for immutable references
Use cloning cache to handle circular references and preserve structure
Prototype Registry combines pattern with type-based lookup
In machine coding, start with shallow copy and add deep copy incrementally
Consider copy constructors as an alternative to clone methods in some languages
📌 Examples
1Document cloning with circular footnote references
2Vehicle registry for parking lot with pre-configured prototypes
3Game entity spawning with complex AI and inventory state
← Back to Prototype Pattern Overview
Prototype Pattern: Interview Deep Dive | Prototype Pattern - System Overflow