Implement Director awards with Python OOP
Company: HBO
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
You are given a simple hierarchy where Director extends Cast. Implement a robust, testable solution that avoids common Python pitfalls and demonstrates correct use of @property, @classmethod, and @staticmethod.
Starting point:
class Cast:
def __init__(self, actors: list[str]):
self.actors = list(actors)
class Director(Cast):
def __init__(self, name: str, actors: list[str], movies: list[str]):
super().__init__(actors)
self.name = name
self.movies = list(movies)
self._awards: list[tuple[str,int]] = [] # (award_name, year)
Tasks:
1) Implement add_award(award_name: str, year: int) that appends a new (award_name, year) pair only if it does not already exist for this director (idempotent). Ensure no mutable-default arguments and that self._awards cannot be mutated from outside the class.
2) Add a property awards that returns an immutable, chronologically sorted view of awards; add a property latest_award that returns Optional[tuple[str,int]].
3) Add a @staticmethod validate_award(award_name: str, year: int) that raises ValueError for empty names or years outside [1900, current_year]. Use it inside add_award.
4) Add a @classmethod from_filmography(cls, name: str, filmography: dict) that constructs a Director from {'actors': [...], 'movies': [...]}. Show how subclassing would work if you later create an AwardWinningDirector that adds fields like guild_membership.
5) Implement __eq__ and __hash__ so that two Director instances with the same name compare equal. Explain the trade-offs of this choice.
6) Provide a short doctest-style example demonstrating creation, adding duplicate awards, and the latest_award property.
Quick Answer: This question evaluates proficiency in Python object-oriented programming, specifically correct use of @property, @classmethod, and @staticmethod, safe handling of mutable defaults and encapsulation of internal state, and design decisions around equality and hashing.