KatInterview

Replayable Library Lending Operations

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationstring-matching

Last updated

Simulate a book catalog with case-insensitive title/author search and atomic borrow/return state transitions replayed from an operation log.

You are building the core of a small lending library. Every book has a string id, a title, an author, and an availability flag that starts out as available for every book in the catalog.

You must implement a function that replays a recorded log of user actions against the catalog and reports what each action returned.

The function receives:

  • books: a list of dicts, each with keys "id", "title", and "author". All ids are distinct.
  • operations: a list of operation names, each one of "search_books", "borrow_book", or "return_book".
  • arguments: a list of argument lists, parallel to operations.

For each operation, apply it to the catalog and append exactly one value to the output list, in the same order as operations:

  1. "search_books" with arguments [query] — return a list of book snapshots, in original catalog order, for every book whose title or author contains query as a case-insensitive substring. Each snapshot is a dict with keys "id", "title", "author" (original casing preserved) and "is_available" reflecting the catalog's current state at the moment of the search. If query is empty, every book matches.
  2. "borrow_book" with arguments [user_id, book_id] — if book_id exists and the book is currently available, mark it unavailable and return True. If the book exists but is already checked out, return False. If book_id does not exist in the catalog, return False. user_id is not stored or validated.
  3. "return_book" with arguments [book_id] — if book_id exists, mark the book available (even if it was already available) and return None. If book_id does not exist, return None.

Operations whose name matches none of the three above append nothing to the output list.

The books passed in must not be mutated: the catalog keeps its own internal state, and search results are independent copies.

Constraints:

  • 0 ≤ number of books ≤ 10000
  • 1 ≤ length of operations = length of arguments ≤ 10000
  • Book ids are unique, non-empty, and short strings.
  • title and author are strings (possibly empty); substring matching is case-insensitive.
  • The same book may be searched, borrowed and returned repeatedly across the log.

Examples

Example 1

Input: replay_library_actions([{'id': '1', 'title': 'The Hobbit', 'author': 'J.R.R. Tolkien'}], ['borrow_book', 'borrow_book', 'return_book', 'borrow_book'], [['user_A', '1'], ['user_B', '1'], ['1'], ['user_B', '1']])

Output: [True, False, None, True]

Example 2

Input: replay_library_actions([{'id': '1', 'title': 'The Hobbit', 'author': 'J.R.R. Tolkien'}], ['search_books', 'borrow_book', 'search_books'], [['tolkien'], ['user_A', '1'], ['HOBBIT']])

Output: [[{'id': '1', 'title': 'The Hobbit', 'author': 'J.R.R. Tolkien', 'is_available': True}], True, [{'id': '1', 'title': 'The Hobbit', 'author': 'J.R.R. Tolkien', 'is_available': False}]]

Hints

Hint 1

Store the catalog in a dict keyed by `id` so borrow and return are constant-time lookups; insertion order preserves catalog order for searches.

Hint 2

For search, lowercase both the query and each field and use Python's substring `in` operator — that is exactly a case-insensitive partial match.

Hint 3

Return fresh dicts from search rather than the internal records, and read `is_available` at search time so results reflect the current lending state.