Coding questions · Meta
Compute maximum score using up to 3 categories
This question evaluates skills in grouping and aggregation of items and constrained selection to maximize a numeric objective, reflecting competency in data manipulation and combinatorial optimization.
Problem
A library runs a summer reading program. Each book a student reads earns a certain number of points, and each book belongs to a category.
A student may score points from at most 3 books, with the restriction that the selected books must be from distinct categories (i.e., no two selected books can share the same category).
Given a list of books the student read, compute the maximum total score the student can achieve.
Input
books: a list of tuples(category: str, points: int)- The list may be empty.
- Assume
pointsare integers (typically non-negative).
Output
- Return an integer: the maximum achievable total points.
Function signature (Python)
def get_max_score(books: list[tuple[str, int]]) -> int:
...
Examples / Tests
test1 = [
("Adventure", 5),
("Adventure", 2),
("History", 3),
]
assert get_max_score(test1) == 8 # 5 (Adventure) + 3 (History)
test2 = [
("Adventure", 4),
("History", 3),
("Reference", 1),
("Fiction", 2),
]
assert get_max_score(test2) == 9 # 4 + 3 + 2
test3 = [
("Biography", 2),
("Biography", 4),
("Science", 3),
("Science", 1),
]
assert get_max_score(test3) == 7 # 4 + 3
test4 = []
assert get_max_score(test4) == 0
Clarifications
- You may pick 0, 1, 2, or 3 books (whichever maximizes the score).
- You cannot pick more than one book from the same category.
Max Score Up To Three Categories
Pick at most one book per category and at most three books total to maximize score.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.