Design a Building Interface with Hospital and School Subclasses

Quick Overview

Design a small class hierarchy where Hospital and School buildings share one Building interface, each keeps its own floor count, and a controller manages them by id. It tests polymorphism, per-instance state, constant-time lookup and exact output formatting.

Design a Building Interface with Hospital and School Subclasses

Company: KPMG

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a `Building` abstraction that supports adding floors, counting floors, and reporting status. The system manages two concrete building types: `Hospital` and `School`. Each building instance maintains its own internal floor count that persists across method calls. Multiple buildings of either type may be created and operated on independently through a single controller class `BuildingSystem`. Building ids are assigned by the caller. When `printStatus` is invoked, it returns a string of the form `"<Type>: <count> floors"`, where `<Type>` is either `Hospital` or `School` and `<count>` is the current number of floors. Implement the following methods: - `createHospital(buildingId: int)`: Create a new Hospital with the given id and an initial floor count of 0. - `createSchool(buildingId: int)`: Create a new School with the given id and an initial floor count of 0. - `buildFloor(buildingId: int)`: Add one floor to the building with the given id. - `countFloors(buildingId: int) -> int`: Return the current number of floors of the building with the given id. - `printStatus(buildingId: int) -> str`: Return a status string of the form `"<Type>: <count> floors"`, where `<Type>` is `Hospital` or `School`. For example, this call sequence creates one hospital and builds three floors on it: ```text Input: ["BuildingSystem", "createHospital", "buildFloor", "buildFloor", "buildFloor", "countFloors", "printStatus"] [[], [1], [1], [1], [1], [1], [1]] Output: [null, null, null, null, null, 3, "Hospital: 3 floors"] Explanation: sys = BuildingSystem() # empty controller sys.createHospital(1) # hospital id=1, floors=0 sys.buildFloor(1) # floors: 1 sys.buildFloor(1) # floors: 2 sys.buildFloor(1) # floors: 3 sys.countFloors(1) # -> 3 sys.printStatus(1) # -> "Hospital: 3 floors" ``` ```hint What differs between the types Compare what a Hospital and a School actually do differently. Only one small piece of behavior changes between them, so everything else can live in one shared place. ``` ### Constraints & Assumptions - `1 <= buildingId <= 10^4` - At most `10^4` operations will be performed. - Every `buildFloor`, `countFloors`, and `printStatus` call references an id that was previously created. - A building id is created at most once. ### Clarifying Questions to Ask - If `createHospital` or `createSchool` is called with an id that already exists, should it overwrite the old building, raise an error, or be ignored? - Is the status text fixed as `"<Type>: <count> floors"` even when the count is 1? - Should `printStatus` return the string, print it to standard output, or both? - Should the design make it easy to add more building types later? ### What a Strong Answer Covers - A shared base type (abstract class or interface) that holds the floor count and the common operations, with each concrete type supplying only what differs. - Per-instance state, so operations on one building id never change another building. - A controller that maps ids to building objects and delegates each call, with constant-time lookups. - The exact status format, including the literal `floors` suffix for every count. - A stated policy for duplicate or unknown ids, even though the constraints rule them out. ### Follow-up Questions - How would you add a third type such as `Office`, and which existing classes would have to change? - Suppose hospitals may not exceed a maximum number of floors. Where does that rule belong in your design? - What goes wrong if several threads call `buildFloor` on the same building at once, and how would you prevent it?

Overview: Design a small class hierarchy where Hospital and School buildings share one Building interface, each keeps its own floor count, and a controller manages them by id. It tests polymorphism, per-instance state, constant-time lookup and exact output formatting.

KPMG logo
KPMG
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a Building abstraction that supports adding floors, counting floors, and reporting status. The system manages two concrete building types: Hospital and School.

Each building instance maintains its own internal floor count that persists across method calls. Multiple buildings of either type may be created and operated on independently through a single controller class BuildingSystem.

Building ids are assigned by the caller. When printStatus is invoked, it returns a string of the form "<Type>: <count> floors", where <Type> is either Hospital or School and <count> is the current number of floors.

Implement the following methods:

  • createHospital(buildingId: int) : Create a new Hospital with the given id and an initial floor count of 0.
  • createSchool(buildingId: int) : Create a new School with the given id and an initial floor count of 0.
  • buildFloor(buildingId: int) : Add one floor to the building with the given id.
  • countFloors(buildingId: int) -> int : Return the current number of floors of the building with the given id.
  • printStatus(buildingId: int) -> str : Return a status string of the form "<Type>: <count> floors" , where <Type> is Hospital or School .

For example, this call sequence creates one hospital and builds three floors on it:

Input:
["BuildingSystem", "createHospital", "buildFloor", "buildFloor", "buildFloor", "countFloors", "printStatus"]
[[],               [1],              [1],          [1],          [1],          [1],           [1]]

Output: [null, null, null, null, null, 3, "Hospital: 3 floors"]

Explanation:
sys = BuildingSystem()           # empty controller
sys.createHospital(1)            # hospital id=1, floors=0
sys.buildFloor(1)                # floors: 1
sys.buildFloor(1)                # floors: 2
sys.buildFloor(1)                # floors: 3
sys.countFloors(1)               # -> 3
sys.printStatus(1)               # -> "Hospital: 3 floors"

Constraints & Assumptions

  • 1 <= buildingId <= 10^4
  • At most 10^4 operations will be performed.
  • Every buildFloor , countFloors , and printStatus call references an id that was previously created.
  • A building id is created at most once.

Clarifying Questions to Ask Guidance

  • If createHospital or createSchool is called with an id that already exists, should it overwrite the old building, raise an error, or be ignored?
  • Is the status text fixed as "<Type>: <count> floors" even when the count is 1?
  • Should printStatus return the string, print it to standard output, or both?
  • Should the design make it easy to add more building types later?

What a Strong Answer Covers Guidance

  • A shared base type (abstract class or interface) that holds the floor count and the common operations, with each concrete type supplying only what differs.
  • Per-instance state, so operations on one building id never change another building.
  • A controller that maps ids to building objects and delegates each call, with constant-time lookups.
  • The exact status format, including the literal floors suffix for every count.
  • A stated policy for duplicate or unknown ids, even though the constraints rule them out.

Follow-up Questions Guidance

  • How would you add a third type such as Office , and which existing classes would have to change?
  • Suppose hospitals may not exceed a maximum number of floors. Where does that rule belong in your design?
  • What goes wrong if several threads call buildFloor on the same building at once, and how would you prevent it?