PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Meta
  • Coding & Algorithms
  • Data Engineer

Solve library coding tasks in Python

Company: Meta

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

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.

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

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

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

  1. Within a single category only the highest-points book is ever useful — collapse each category to its max first.
  2. After collapsing categories, you just need the sum of the largest (up to) 3 values.
  3. 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

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

  1. Convert the closed collection to a set so membership checks are O(1).
  2. Build and return a brand-new dictionary rather than mutating the input.
  3. 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 `False` if a book is **checked out while it is already checked out** (checked out twice without an intervening return). - Return `False` if 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

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

  1. Maintain a set of book_ids that are currently checked out.
  2. On 'checkout', the book must NOT already be in the set; on 'return' it MUST be in the set.
  3. Update the set as you go and return False the moment an invariant is violated.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)