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.
Input: ([],)
Expected Output: 0
Explanation: No books available, so the maximum total is 0.
Input: ([('x', 8), ('y', 6)],)
Expected Output: 14
Explanation: Only two distinct categories exist; take both = 14 (fewer than 3 is allowed).
Input: ([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)],)
Expected Output: 12
Explanation: Five categories but at most 3 books: take the top three values 5+4+3 = 12.
Input: ([('a', -5), ('b', -3), ('c', -1)],)
Expected Output: 0
Explanation: All books have negative points; choosing none yields the maximum total of 0.
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.
Input: ({'a': ['x'], 'b': ['y']}, ['a', 'b'])
Expected Output: {}
Explanation: Both locations are closed, leaving an empty dictionary.
Input: ({}, ['a'])
Expected Output: {}
Explanation: There are no locations to begin with, so the result is empty.
Input: ({'main': [], 'annex': ['p1', 'p2']}, ['ghost'])
Expected Output: {'main': [], 'annex': ['p1', 'p2']}
Explanation: The closed name 'ghost' does not exist, so all real locations are kept (including 'main' with an empty people list).
Input: ({'lib1': ['a'], 'lib2': ['b'], 'lib3': ['c']}, ['lib2', 'lib2'])
Expected Output: {'lib1': ['a'], 'lib3': ['c']}
Explanation: Duplicate closed entries are harmless; 'lib2' is removed once and the rest remain.
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.
Input: ([(1, 'return')],)
Expected Output: False
Explanation: Book 1 is returned but was never checked out.
Input: ([],)
Expected Output: True
Explanation: An empty log is trivially valid.
Input: ([(1, 'checkout'), (2, 'checkout'), (1, 'return'), (2, 'return')],)
Expected Output: True
Explanation: Two different books are tracked independently; all four actions are valid.
Input: ([(1, 'checkout'), (1, 'return'), (1, 'return')],)
Expected Output: False
Explanation: After the first return, book 1 is no longer checked out, so the second return is invalid.
Input: ([(5, 'checkout')],)
Expected Output: True
Explanation: A single checkout with no return is still a valid (open) sequence.
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.