KatInterview

Shelf Command Simulator with Versioned Edits and Durable Bookmarks

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designdesignhash-mapsimulation

Last updated

Simulate a shelf of books with positional ordering, version-checked edits and ID-based bookmarks that survive reordering and are cleaned up on deletion.

Shelf Command Simulator

You are building the engine behind a small in-memory library shelf. The shelf holds books in a user-controlled order, and readers can leave bookmarks inside books. A book's identity (its book_id) never changes, but its place on the shelf can. Bookmarks must therefore follow the book's identity, not its slot.

Write a function that receives a list of command names and a parallel list of argument lists. It runs the commands in order against one fresh, initially empty shelf and returns the result of each command, in order.

Commands

params[i] holds the positional arguments of commands[i].

CommandArgumentsBehaviorResult
add_bookbook_id, title, content or book_id, title, content, positionCreates a book with version 1. If position is omitted or None, the book goes at the end of the shelf. Otherwise it is inserted so that it ends up at index position.True on success. False if book_id already exists or position is outside [0, N], where N is the current number of books. Failure changes nothing.
get_bookbook_idLooks up a book.A dict {"id", "title", "content", "version"}, or None if the book does not exist.
update_bookbook_id, new_title, new_content, expected_versionVersion-checked edit. If the book exists and its current version equals expected_version, replace the title with new_title if it is not None, replace the content with new_content if it is not None, then increase the version by 1.True on success. False if the book is missing or the version differs.
remove_bookbook_idRemoves the book from the shelf and deletes every bookmark that points to it.True if removed, False if the book does not exist.
move_bookbook_id, new_positionTakes the book off the shelf and puts it back so that its final index is new_position. The other books keep their relative order.True on success. False if the book is missing or new_position is outside [0, N-1].
get_ordernoneReads the current shelf layout.A list of book_id values from index 0 onward.
set_bookmarkbookmark_id, book_id, char_offsetCreates a bookmark, or overwrites the one with the same bookmark_id.True on success. False if the book does not exist or char_offset is not in [0, len(content)]. Failure leaves any existing bookmark with that ID unchanged.
get_bookmarkbookmark_idReads a bookmark.{"bookmark_id", "book_id", "char_offset"}, or None if it does not exist or was deleted along with its book.

Rules to get exactly right

  • Bookmarks refer to a book_id, never to a shelf index, so reordering books never changes what a bookmark points to.
  • Removing a book deletes all of its bookmarks. If the same book_id is added again later, it is a brand-new book with version 1, and the old bookmarks do not come back.
  • An update_book call with a matching version and both new_title and new_content equal to None still succeeds and still increases the version.
  • Updating content does not revalidate or change existing bookmarks, even if an offset is now beyond the new content length.
  • Overwriting a bookmark with a different book_id re-points it to the new book. Removing the old book afterwards must not delete it.
  • A bookmark offset equal to len(content) is valid.
  • The dicts and lists you return must be copies, so later commands cannot alter earlier results.

Output

Return a list with exactly one entry per command, in the same order as commands.

Constraints

  • len(commands) == len(params), and every command name is one of the eight listed above.
  • Titles and contents are non-empty strings.
  • book_id and bookmark_id are alphanumeric strings.
  • Positions, offsets and versions are integers.
  • Inputs are small enough that an O(N) shelf operation per command is fine.

Examples

Example 1

Input: run_shelf_commands(['add_book', 'add_book', 'get_order', 'set_bookmark', 'move_book', 'get_bookmark', 'update_book', 'update_book', 'remove_book', 'get_bookmark'], [['b1', 'Title 1', 'Hello World'], ['b2', 'Title 2', 'Sample Content'], [], ['mark1', 'b1', 6], ['b2', 0], ['mark1'], ['b1', None, 'Hello Universe', 1], ['b1', None, 'Conflict', 1], ['b1'], ['mark1']])

Output: [True, True, ['b1', 'b2'], True, True, {'bookmark_id': 'mark1', 'book_id': 'b1', 'char_offset': 6}, True, False, True, None]

Hints

Hint 1

Keep three separate pieces of state: a map from book_id to its record, a list holding only book_ids for the layout, and a map from bookmark_id to a record that stores a book_id (never an index).

Hint 2

Validate positions yourself: Python's list.insert silently clamps out-of-range indexes. add_book allows [0, N], while move_book allows [0, N-1] and means the final index after removing then reinserting.

Hint 3

On remove_book, delete the book, take it out of the order list, and drop every bookmark whose book_id matches. Check the rules for failed set_bookmark calls and for update_book with both fields None.