0
0
LLDsystem_design~10 mins

Composite pattern in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the base component interface in the Composite pattern.

LLD
interface Component {
    void [1]();
}
Drag options to blanks, or click blank then click option'
AaddChild
Boperation
CremoveChild
DgetChild
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing methods specific to composite like addChild instead of the common operation method.
2fill in blank
medium

Complete the code to add a child component in the Composite class.

LLD
class Composite implements Component {
    private List<Component> children = new ArrayList<>();

    public void [1](Component component) {
        children.add(component);
    }

    public void operation() {
        for (Component child : children) {
            child.operation();
        }
    }
}
Drag options to blanks, or click blank then click option'
Aoperation
BremoveChild
CgetChild
DaddChild
Attempts:
3 left
💡 Hint
Common Mistakes
Using removeChild or getChild instead of addChild for adding components.
3fill in blank
hard

Fix the error in the Leaf class operation method to correctly implement the Composite pattern.

LLD
class Leaf implements Component {
    public void [1]() {
        System.out.println("Leaf operation");
    }
}
Drag options to blanks, or click blank then click option'
AremoveChild
BaddChild
Coperation
DgetChild
Attempts:
3 left
💡 Hint
Common Mistakes
Using composite-specific methods like addChild in Leaf.
4fill in blank
hard

Fill both blanks to define the Composite class constructor and initialize the children list.

LLD
class Composite implements Component {
    private List<Component> children;

    public Composite() {
        children = new [1]<>();
    }

    public void addChild(Component component) {
        children.[2](component);
    }
}
Drag options to blanks, or click blank then click option'
AArrayList
BLinkedList
Cadd
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove instead of add to add children.
Using LinkedList instead of ArrayList (both work but ArrayList is more common).
5fill in blank
hard

Fill all three blanks to implement a recursive operation method in Composite that calls operation on all children.

LLD
class Composite implements Component {
    private List<Component> children = new ArrayList<>();

    public void operation() {
        for ([1] : children) {
            [2].[3]();
        }
    }
}
Drag options to blanks, or click blank then click option'
AComponent child
Bchild
Coperation
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect loop variable type or name.
Forgetting to call the operation method on the child.