Coding questions · Meta
Solve library coding tasks in Python
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.
Implement the following Python tasks:
- Given a list of (category, points) for books, choose up to 3 books with all different categories to maximize total points; return the maximum achievable points.
- Given a dictionary mapping library_location -> list_of_people and a collection of closed locations, return a new dictionary containing only open locations and their associated people.
- Given an ordered list of log entries (book_id, action) where action is 'checkout' or 'return', validate the sequence: return False if a book is checked out twice in a row without a return, or returned when it is not currently checked out; otherwise return True.
What a Strong Answer Covers
- A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
- A correctness argument that explains why the algorithm covers all required cases.
- Time and space complexity, plus at least one alternative approach when relevant.
- Focused tests for normal cases, edge cases, and failure modes.
Follow-up Questions
- How would the approach change if the input were streaming or too large for memory?
- What invariants would you assert in production code?
- Which tests would catch off-by-one, duplicate, or tie-breaking bugs?
Maximize Points from Up to 3 Different-Category Books
You are given a list of (category, points) tuples describing books. Choose up to 3 books such that all chosen books belong to different categories, and return the maximum total points achievable.
You may choose fewer than 3 books (including zero). Since each chosen book must come from a distinct category, at most one book per category counts toward the total, and you should pick the highest-points book within any category you use.
Assumptions
- If two books share a category, only the higher-points one is ever useful.
- Because you may choose fewer than 3 books, never include a book that would lower the total (i.e. negative-points books are skipped). With all non-negative points this reduces to summing the top 3 per-category maxima.
Example
books = [('fiction', 5), ('science', 3), ('history', 4), ('fiction', 2)]
-> 12 # fiction(5) + history(4) + science(3)
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
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
You are given a dictionary locations mapping each library location to the list of people associated with it, and a collection closed of locations that are now closed. Return a new dictionary that contains only the open locations (those not in closed) and their associated people.
The original dictionary should not be required as output — return a fresh dictionary. The closed collection may contain duplicate entries or names of locations that do not exist; both should be handled gracefully.
Example
locations = {'downtown': ['alice', 'bob'], 'uptown': ['carol'], 'eastside': ['dan']}
closed = ['uptown']
-> {'downtown': ['alice', 'bob'], 'eastside': ['dan']}
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
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
You are given an ordered list of log entries, where each entry is a tuple (book_id, action) and action is either 'checkout' or 'return'. Validate the sequence and return a boolean:
- Return
Falseif a book is checked out while it is already checked out (checked out twice without an intervening return). - Return
Falseif a book is returned while it is not currently checked out. - Otherwise return
True.
Different books are tracked independently — a checkout of book A does not affect book B.
Example
logs = [(1, 'checkout'), (2, 'checkout'), (1, 'return'), (2, 'return')]
-> True
logs = [(1, 'checkout'), (1, 'checkout')]
-> False # book 1 checked out twice without a return
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
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.
Solution
Solution Alignment
The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.
Problem Restatement
Implement the following Python tasks: 1) Given a list of (category, points) for books, choose up to 3 books with all different categories to maximize total points; return the maximum achievable points. 2) Given a dictionary mapping library_location -> list_of_people and a collection of closed locations, return a new dictionary containing only open locations and their associated people. 3) Given an ordered list of log entries (book_id, action) where action is 'checkout' or 'return', validate the sequence: return False if a book is checked out twice in a row without a return, or returned when it is not currently checked out; otherwise return True.
Recommended Approach
Start with a brute-force baseline to confirm correctness, then identify the repeated work or ordering property that enables a better data structure such as a hash map, heap, stack, queue, two pointers, prefix sums, BFS/DFS, or dynamic programming. Write the implementation around a small invariant and test that invariant directly.
Correctness
The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.
Complexity
State the baseline complexity and the optimized complexity. For most interview constraints, justify why the optimized approach meets the expected input size.
Edge Cases and Tests
Empty and singleton inputs, duplicates, ties, invalid inputs, boundary values, and tests that exercise the main invariant.