Bird
Raised Fist0
LLDsystem_design~20 mins

Room type hierarchy in LLD - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Room Type Hierarchy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding inheritance in room type hierarchy

Consider a room type hierarchy where Room is the base class, and Bedroom and ConferenceRoom inherit from it. Which statement best describes the relationship?

ARoom inherits properties from Bedroom and ConferenceRoom.
BBedroom and ConferenceRoom are unrelated to Room and do not share any properties.
CBedroom and ConferenceRoom share common properties from Room and add their own specific features.
DRoom is a subtype of Bedroom and ConferenceRoom.
Attempts:
2 left
💡 Hint

Think about how inheritance works in object-oriented design.

Architecture
intermediate
2:00remaining
Designing a scalable room type hierarchy

You need to design a system to manage different room types in a hotel. Which design choice best supports adding new room types without changing existing code?

AUse an abstract Room class with subclasses for each room type implementing specific behaviors.
BCreate a single Room class with many conditional statements to handle all room types.
CDuplicate code for each room type in separate classes without inheritance.
DUse global variables to store room type information and behaviors.
Attempts:
2 left
💡 Hint

Consider the Open/Closed Principle in software design.

scaling
advanced
2:30remaining
Handling multiple room features in hierarchy

In a hotel system, rooms can have multiple features like 'hasProjector', 'hasBalcony', or 'isSoundproof'. What is the best way to design the room type hierarchy to handle these features efficiently?

ACreate a subclass for every combination of features (e.g., BalconyConferenceRoom).
BAdd all possible features as boolean fields in the base Room class.
CIgnore features and handle them only in the booking system.
DUse composition by creating feature classes and assign them to rooms instead of deep inheritance.
Attempts:
2 left
💡 Hint

Think about avoiding class explosion and promoting flexibility.

tradeoff
advanced
2:00remaining
Tradeoffs between inheritance and composition in room types

Which statement best describes a tradeoff when choosing inheritance over composition for room type features?

AInheritance always uses less memory than composition.
BInheritance is simpler but less flexible; composition is more flexible but can be more complex to implement.
CComposition cannot represent shared behavior, inheritance can only represent shared data.
DInheritance allows dynamic feature changes at runtime, composition does not.
Attempts:
2 left
💡 Hint

Consider flexibility and complexity in design patterns.

estimation
expert
3:00remaining
Estimating storage needs for room type hierarchy

A hotel has 10,000 rooms with 5 main room types and 20 optional features. Each room stores its type and features. If each feature is stored as a boolean and the room type as an integer, approximately how much storage is needed to store all room type and feature data for all rooms?

AAbout 250 KB
BAbout 2.5 MB
CAbout 25 MB
DAbout 250 MB
Attempts:
2 left
💡 Hint

Calculate bits per room and convert to bytes, then multiply by number of rooms.

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

  1. Step 1: Understand the concept of hierarchy

    A hierarchy groups items by common traits, making management simpler.
  2. Step 2: Apply to room types

    Using a base class for shared features and subclasses for specifics avoids duplication and eases updates.
  3. Final Answer:

    To organize rooms by shared and unique features for easier maintenance -> Option B
  4. 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

  1. Step 1: Identify correct syntax for inheritance

    In many modern languages, extends is used to inherit from a base class.
  2. Step 2: Check each option

    class Room {}; class ConferenceRoom extends Room {} uses correct syntax: class ConferenceRoom extends Room {}. Others use incorrect or incomplete syntax.
  3. Final Answer:

    class Room {}; class ConferenceRoom extends Room {} -> Option A
  4. 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

  1. Step 1: Trace object creation

    Creating Bedroom('Master', 'King') calls Bedroom's constructor, which calls Room's constructor with 'Master'.
  2. Step 2: Check printed attributes

    room.name is 'Master' from Room; room.bed_size is 'King' from Bedroom.
  3. Final Answer:

    Master King -> Option D
  4. 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

  1. Step 1: Check constructor chaining

    MeetingRoom's constructor sets capacity but does not call super().__init__(name), so name is not set.
  2. Step 2: Understand effect on attributes

    Without base constructor call, room.name is missing, causing error or undefined behavior.
  3. Final Answer:

    Missing call to base class constructor causes room.name to be undefined -> Option C
  4. 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

  1. Step 1: Analyze relationships

    Suite is a special Room that contains multiple Bedrooms and a living area, so it should inherit from Room.
  2. Step 2: Model composition

    Suite should have a list of Bedroom objects (composition) to represent multiple bedrooms, plus its own living area attributes.
  3. Final Answer:

    Make Suite inherit from Room and include a list of Bedroom objects plus living area -> Option A
  4. 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
  • Ignoring composition for complex room types
  • Making unrelated classes inherit incorrectly