Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a room type hierarchy in system design?
A room type hierarchy organizes different room categories in a structured way, showing parent-child relationships. It helps manage shared and specific properties efficiently.
Click to reveal answer
beginner
Why use inheritance in a room type hierarchy?
Inheritance allows child room types to reuse common features from parent types, reducing duplication and making the system easier to maintain.
Click to reveal answer
intermediate
How does polymorphism help in a room type hierarchy?
Polymorphism lets the system treat different room types uniformly while allowing each type to behave differently when needed.
Click to reveal answer
intermediate
What is the benefit of using composition over inheritance in room type design?
Composition allows building room types by combining smaller parts or features, offering more flexibility and easier changes than rigid inheritance chains.
Click to reveal answer
advanced
Name a common challenge when designing a room type hierarchy.
One challenge is balancing between too many specific types (which complicate the system) and too few (which reduce flexibility).
Click to reveal answer
What does a room type hierarchy primarily represent?
AA structure showing relationships between room categories
BA list of all rooms in a building
CA database of room prices
DA schedule for room cleaning
✗ Incorrect
A room type hierarchy organizes room categories by their relationships, not by lists or schedules.
Which principle helps avoid repeating common room features in multiple types?
AEncapsulation
BInheritance
CAbstraction
DPolymorphism
✗ Incorrect
Inheritance allows sharing common features from parent to child room types.
What is a downside of deep inheritance in room type hierarchies?
AIt reduces code reuse
BIt improves flexibility
CIt makes the system rigid and hard to change
DIt simplifies debugging
✗ Incorrect
Deep inheritance can make the system rigid and difficult to modify.
Which design approach allows combining features to build room types?
APolymorphism
BInheritance
CEncapsulation
DComposition
✗ Incorrect
Composition builds complex types by combining smaller parts or features.
Polymorphism in room types allows:
ADifferent room types to be treated the same way with specific behaviors
BRooms to change their size automatically
CRooms to share the same price
DRooms to be cleaned faster
✗ Incorrect
Polymorphism lets different room types respond differently while using the same interface.
Explain how you would design a room type hierarchy for a hotel system.
Think about shared features like size or amenities and specific ones like suite or deluxe.
You got /4 concepts.
What are the advantages and disadvantages of using inheritance in room type hierarchies?
Consider how inheritance helps and when it might cause problems.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a Room type hierarchy in system design?
easy
A. To randomly assign room types without any structure
B. To organize rooms by shared and unique features for easier maintenance
C. To store all room data in a single flat list without categories
D. To duplicate room properties in every class separately
Solution
Step 1: Understand the concept of hierarchy
A hierarchy groups items by common traits, making management simpler.
Step 2: Apply to room types
Using a base class for shared features and subclasses for specifics avoids duplication and eases updates.
Final Answer:
To organize rooms by shared and unique features for easier maintenance -> Option B
Quick Check:
Hierarchy = Organize by features [OK]
Hint: Think: shared features go in base, unique in subclasses [OK]
Common Mistakes:
Confusing hierarchy with flat lists
Duplicating properties in every room class
Ignoring shared features in base class
2. Which of the following is the correct way to define a base class Room with a subclass ConferenceRoom in a typical object-oriented design?
easy
A. class Room {}; class ConferenceRoom extends Room {}
B. class Room; class ConferenceRoom inherits Room
C. class Room() {}; class ConferenceRoom() inherits Room()
D. class Room {}; class ConferenceRoom inherits Room {}
Solution
Step 1: Identify correct syntax for inheritance
In many modern languages, extends is used to inherit from a base class.
Step 2: Check each option
class Room {}; class ConferenceRoom extends Room {} uses correct syntax: class ConferenceRoom extends Room {}. Others use incorrect or incomplete syntax.
Final Answer:
class Room {}; class ConferenceRoom extends Room {} -> Option A
Quick Check:
Inheritance syntax = extends [OK]
Hint: Remember: subclass extends base class in OOP [OK]
Common Mistakes:
Using 'inherits' instead of 'extends'
Missing curly braces for class body
Incorrect parentheses in class declaration
3. Given this Python-like pseudocode for room types:
class Room:
def __init__(self, name):
self.name = name
class Bedroom(Room):
def __init__(self, name, bed_size):
super().__init__(name)
self.bed_size = bed_size
room = Bedroom('Master', 'King')
print(room.name, room.bed_size)
What will be the output?
medium
A. Error: missing argument
B. Bedroom King
C. Master None
D. Master King
Solution
Step 1: Trace object creation
Creating Bedroom('Master', 'King') calls Bedroom's constructor, which calls Room's constructor with 'Master'.
Step 2: Check printed attributes
room.name is 'Master' from Room; room.bed_size is 'King' from Bedroom.
Final Answer:
Master King -> Option D
Quick Check:
Subclass calls base, attributes set correctly [OK]
Hint: Remember: super() sets base class attributes [OK]
Common Mistakes:
Assuming subclass overwrites base attributes
Forgetting to call super().__init__
Confusing attribute names
4. Consider this code snippet for a room hierarchy:
class Room:
def __init__(self, name):
self.name = name
class MeetingRoom(Room):
def __init__(self, name, capacity):
self.capacity = capacity
room = MeetingRoom('Boardroom', 20)
print(room.name, room.capacity)
What is the issue here?
medium
A. capacity attribute is not set correctly
B. Syntax error in class definition
C. Missing call to base class constructor causes room.name to be undefined
D. No issue; code runs fine
Solution
Step 1: Check constructor chaining
MeetingRoom's constructor sets capacity but does not call super().__init__(name), so name is not set.
Step 2: Understand effect on attributes
Without base constructor call, room.name is missing, causing error or undefined behavior.
Final Answer:
Missing call to base class constructor causes room.name to be undefined -> Option C
Quick Check:
Always call base __init__ in subclass [OK]
Hint: Call super().__init__ to set base attributes [OK]
Common Mistakes:
Assuming base constructor runs automatically
Ignoring missing attributes in subclass
Confusing syntax errors with logic errors
5. You need to design a room type hierarchy for a hotel system that includes Room, Bedroom, ConferenceRoom, and Suite. Suites can have multiple bedrooms and a living area. Which design approach best models this?
hard
A. Make Suite inherit from Room and include a list of Bedroom objects plus living area
B. Make Suite inherit from Bedroom and add living area attributes
C. Make Suite a separate class unrelated to Room hierarchy
D. Make Bedroom inherit from Suite to reuse features
Solution
Step 1: Analyze relationships
Suite is a special Room that contains multiple Bedrooms and a living area, so it should inherit from Room.
Step 2: Model composition
Suite should have a list of Bedroom objects (composition) to represent multiple bedrooms, plus its own living area attributes.
Final Answer:
Make Suite inherit from Room and include a list of Bedroom objects plus living area -> Option A
Quick Check:
Use inheritance + composition for complex types [OK]
Hint: Use composition for multiple rooms inside Suite [OK]
Common Mistakes:
Using inheritance to model 'has-many' relationships