Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Inheritance Code Example
00:00
5 left

Inheritance Code Example

MediumPython

Problem

UST HealthProof receives alerts from several monitoring surfaces. Design an inheritance hierarchy that calculates each alert's priority through polymorphism, then return the highest-priority alert IDs.

Create a base Alert class with shared fields: alert_id, severity, age_minutes, and created_at. Implement these subclasses:

  1. PatientDataAlert: priority is the base priority plus affected_users // 100.
  2. PlatformAlert: priority is the base priority plus affected_users // 50.
  3. ComplianceAlert: priority is the base priority plus 15.

The base priority is severity * 10 + min(age_minutes, 120) // 30. Each subclass must override a priority() method while reusing inherited fields. The function must construct the correct subclass from each input record, sort alerts by descending priority, then ascending created_at, and finally ascending alert_id. Return at most limit IDs.

Formal Specification

rank_alerts(alerts, limit) receives a list of dictionaries and an integer. Each dictionary contains id, type, severity, age_minutes, created_at, and affected_users. The type is one of patient_data, platform, or compliance. Return a list of alert ID strings.

Constraints

  • 1 <= len(alerts) <= 10^5
  • 1 <= limit <= len(alerts)
  • 1 <= severity <= 5
  • 0 <= age_minutes, affected_users, created_at <= 10^9
  • All alert IDs are unique
  • Every type is patient_data, platform, or compliance

Function Signature

def rank_alerts(alerts, limit):
Interviewer

Your question is Inheritance Code Example. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.