0
0
LLDsystem_design~10 mins

Relationships (association, aggregation, composition) 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 define an association relationship between two classes.

LLD
class Teacher:
    def __init__(self, name):
        self.name = name

class Student:
    def __init__(self, name, teacher):
        self.name = name
        self.teacher = [1]
Drag options to blanks, or click blank then click option'
Ateacher
Bname
CStudent
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'teacher' causes the student to store a string, not the teacher object.
Using 'self' or 'Student' is incorrect as they do not refer to the teacher instance.
2fill in blank
medium

Complete the code to show aggregation where a Library has many Books.

LLD
class Book:
    def __init__(self, title):
        self.title = title

class Library:
    def __init__(self):
        self.books = []

    def add_book(self, book):
        self.books.[1](book)
Drag options to blanks, or click blank then click option'
Aremove
Bappend
Cclear
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'remove' or 'pop' deletes items instead of adding.
Using 'clear' empties the list, which is not intended.
3fill in blank
hard

Fix the error in the composition example where a Car has an Engine.

LLD
class Engine:
    def __init__(self, power):
        self.power = power

class Car:
    def __init__(self, power):
        self.engine = [1](power)
Drag options to blanks, or click blank then click option'
Apower
Bengine
CEngine
DCar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'engine' or 'power' as a function causes errors.
Using 'Car' tries to create a Car inside Car, which is incorrect.
4fill in blank
hard

Fill both blanks to complete the aggregation example where a Team has multiple Players.

LLD
class Player:
    def __init__(self, name):
        self.name = name

class Team:
    def __init__(self):
        self.players = []

    def add_player(self, player):
        self.players.[1](player)

    def remove_player(self, player):
        self.players.[2](player)
Drag options to blanks, or click blank then click option'
Aappend
Bpop
Cremove
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop to remove by value causes errors.
Using clear removes all players, not just one.
5fill in blank
hard

Fill all three blanks to complete the composition example where a House has Rooms with names and sizes.

LLD
class Room:
    def __init__(self, name, size):
        self.name = [1]
        self.size = [2]

class House:
    def __init__(self):
        self.rooms = []

    def add_room(self, name, size):
        room = [3](name, size)
        self.rooms.append(room)
Drag options to blanks, or click blank then click option'
Aname
Bsize
CRoom
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up parameter names or using 'self' incorrectly.
Not calling the Room class to create a new object.