Solve library coding tasks in Python
Company: Meta
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: Solve library coding tasks in Python evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Maximize Points from Up to 3 Different-Category Books
Constraints
- 0 <= number of books <= 10^5
- category is a non-empty string
- points is an integer (may be negative)
- At most one book per distinct category contributes to the total
Examples
Input: ([('fiction', 5), ('science', 3), ('history', 4), ('fiction', 2)],)
Expected Output: 12
Explanation: fiction max is 5, plus history 4 and science 3 = 12. The fiction(2) duplicate is ignored.
Input: ([('a', 10), ('a', 7), ('a', 4)],)
Expected Output: 10
Explanation: All same category, so only the single best book (10) can be used.
Hints
- Within a single category only the highest-points book is ever useful — collapse each category to its max first.
- After collapsing categories, you just need the sum of the largest (up to) 3 values.
- Because you may choose fewer than 3 books, never add a value that decreases the total.
Filter Out Closed Library Locations
Constraints
- 0 <= number of locations <= 10^5
- 0 <= number of closed entries <= 10^5
- Location keys are strings; people are lists of strings
- closed may contain duplicates or names not present in locations
Examples
Input: ({'downtown': ['alice', 'bob'], 'uptown': ['carol'], 'eastside': ['dan']}, ['uptown'])
Expected Output: {'downtown': ['alice', 'bob'], 'eastside': ['dan']}
Explanation: Only 'uptown' is closed, so it is dropped and the other two locations remain.
Input: ({'a': ['x'], 'b': ['y']}, [])
Expected Output: {'a': ['x'], 'b': ['y']}
Explanation: Nothing is closed, so the result equals the original mapping.
Hints
- Convert the closed collection to a set so membership checks are O(1).
- Build and return a brand-new dictionary rather than mutating the input.
- Locations whose name is not in the closed set are kept exactly as-is.
Validate Library Checkout / Return Log
Constraints
- 0 <= number of log entries <= 10^5
- action is exactly 'checkout' or 'return'
- book_id is hashable (e.g. an integer)
- Different book_ids are tracked independently
Examples
Input: ([(1, 'checkout'), (1, 'return'), (1, 'checkout')],)
Expected Output: True
Explanation: Book 1 is checked out, returned, then checked out again — every action is valid.
Input: ([(1, 'checkout'), (1, 'checkout')],)
Expected Output: False
Explanation: Book 1 is checked out a second time without an intervening return.
Hints
- Maintain a set of book_ids that are currently checked out.
- On 'checkout', the book must NOT already be in the set; on 'return' it MUST be in the set.
- Update the set as you go and return False the moment an invariant is violated.