Compute Max Score From Up to 3 Categories
MetaData Engineer · Technical ScreenLast updated
This Coding & Algorithms question in the Data Engineer domain evaluates grouping and top‑k aggregation concepts, testing the ability to combine per-category aggregation with selection under a uniqueness constraint.
You are implementing a scoring function for a library summer reading program.
Each book a student read is represented as a tuple (category: str, points: int).
Rules:
- The student may count points from at most 3 books.
- Any counted books must be from distinct categories (i.e., you can pick at most one book per category).
- The student may pick fewer than 3 books if fewer categories exist.
Task: Implement the function:
from typing import List, Tuple
def get_max_score(books: List[Tuple[str, int]]) -> int:
"""Return the maximum total points achievable under the rules."""
Examples:
test1 = [
("Adventure", 5),
("Adventure", 2),
("History", 3),
]
# Pick Adventure(5) + History(3) = 8
assert get_max_score(test1) == 8
test2 = [
("Adventure", 4),
("History", 3),
("Reference", 1),
("Fiction", 2),
]
# Pick top 3 categories: 4 + 3 + 2 = 9
assert get_max_score(test2) == 9
test3 = [
("Biography", 2),
("Biography", 4),
("Science", 3),
("Science", 1),
]
# Pick Biography(4) + Science(3) = 7
assert get_max_score(test3) == 7
test4 = []
assert get_max_score(test4) == 0
Constraints: assume len(books) can be large, so your solution should be efficient (better than trying all combinations).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.