Iterator & Composite Pattern - Traversal Abstractions - Watch the Algorithm Execute, Step by Step
Watching each iterator creation and traversal step reveals how the composite and iterator patterns work together to abstract tree traversal without type checks.
Step 1/18
·Active fill★Answer cell
Leaf class encapsulates simple components with a single-element iterator.
Leaf
+name: string
+create_iterator()
Composite class manages children and provides recursive iterators.
Composite
+name: string
−children: List<Component>
+add()
+create_iterator()
Composite aggregates children components, demonstrating composition.
They always improve performance by reducing method calls is false; method calls add overhead. They eliminate the need for access modifiers entirely is wrong; access modifiers remain essential. They guarantee thread safety without additional synchronization is incorrect; thread safety requires explicit handling.
Final Answer:
Option A -> Option A
Quick Check:
Use getters/setters judiciously to balance encapsulation and code clarity.
Hint: More getters/setters ≠ better encapsulation or performance [OK]
Common Mistakes:
Assuming getters/setters improve performance
Believing they replace access modifiers
Thinking they ensure thread safety automatically
2. What is the time complexity of performing a deep copy on an object with nested collections of total size n, assuming the deep copy recursively duplicates all nested objects?
medium
A. O(1) because only references are copied, no nested duplication
B. O(n log n) because copying nested objects requires sorting references
C. O(n) because each nested object must be duplicated once
D. O(n^2) because each nested object is copied multiple times due to recursion
Solution
Step 1: Identify what deep copy does
Deep copy recursively duplicates each nested object exactly once, traversing the entire object graph.
Step 2: Analyze complexity
Since each of the n nested objects is copied once, the time complexity is linear in n. The recursion does not cause repeated copying of the same object due to memoization.
Final Answer:
Option C -> Option C
Quick Check:
Deep copy visits each nested object once -> O(n) time [OK]
Hint: Deep copy time scales linearly with total nested objects [OK]
Common Mistakes:
Thinking recursion causes quadratic copying
3. Which of the following statements about the Single Responsibility Principle is INCORRECT?
medium
A. SRP means a class should only have one method to ensure simplicity.
B. Applying SRP improves cohesion and reduces coupling.
C. A class should have only one reason to change, which means it should have only one responsibility.
D. Violating SRP can lead to fragile code that breaks when unrelated changes occur.
Solution
Step 1: Analyze each statement
A class should have only one reason to change, which means it should have only one responsibility. correctly states the core SRP definition.
Step 2: Evaluate SRP means a class should only have one method to ensure simplicity.
SRP is about reasons to change, not the number of methods; a class can have many methods if they serve one responsibility.
Step 3: Confirm options A, B, and D
Options A, B, and D are true: SRP improves cohesion, reduces coupling, and prevents fragile code.
Final Answer:
Option A -> Option A
Quick Check:
SRP ≠ one method per class; it's about one reason to change.
Hint: SRP is about reasons to change, not method count.
Common Mistakes:
Confusing responsibility with method count.
Assuming fewer methods always means better design.
Ignoring cohesion and coupling effects.
4. If you want to reuse parts already built in a complex object (e.g., reuse walls and roof when building multiple houses), which modification to the Builder pattern is most appropriate?
hard
A. Modify the Builder to reset the product after each get_result call to avoid reuse.
B. Use Abstract Factory instead, as it naturally supports part reuse.
C. Cache built parts inside the Builder and reuse them when building new objects.
D. Switch to Factory pattern for simpler object creation and reuse.
Solution
Step 1: Understand reuse requirement
Reusing parts means the Builder should keep built parts cached and reuse them instead of rebuilding.
Step 2: Evaluate options
Cache built parts inside the Builder and reuse them when building new objects. caches parts inside Builder, enabling reuse. Modify the Builder to reset the product after each get_result call to avoid reuse. resets product, preventing reuse. Abstract Factory and Factory do not handle stepwise reuse well.
Final Answer:
Option C -> Option C
Quick Check:
Caching parts in Builder enables reuse efficiently [OK]
Hint: Cache parts in Builder to enable reuse [OK]
Common Mistakes:
Resetting product disables reuse
Confusing Abstract Factory with Builder reuse
5. If a class overloads a method and also overrides it in a subclass, which method call resolution scenario can cause unexpected behavior, and why?
hard
A. Calling an overloaded method with parameters matching the superclass signature may invoke the superclass version even if the subclass overrides another variant
B. Overloaded methods are resolved at runtime, so the most specific subclass method is always called
C. Calling the overloaded method via a superclass reference always invokes the subclass's overridden method, ignoring overloading
D. Overriding disables overloading in subclasses, so only one method version is callable
Solution
Step 1: Recall overloading vs overriding resolution
Overloading is resolved at compile time based on reference type and parameter list; overriding is resolved at runtime based on object type.
Step 2: Analyze call with superclass reference
If the method call matches an overloaded method signature declared in the superclass, that method is chosen at compile time.
Step 3: Consider overridden methods
If the subclass overrides a different method variant, it won't affect calls resolved to the superclass's overloaded method signature.
Step 4: Identify unexpected behavior
This can cause the superclass method to be called unexpectedly, even if the subclass has an overridden variant with a different signature.
Final Answer:
Option A -> Option A
Quick Check:
Overloading resolution depends on reference type and parameters, potentially bypassing subclass overrides.
Hint: Overloading resolved by reference type; overriding by object type
Common Mistakes:
Assuming overloaded methods are dynamically dispatched
Believing overriding disables overloading
Thinking subclass overrides affect all overloaded variants