161. Design BookShelf Manager with Bookmark Functionality

Asked in

Design BookShelf Manager with Bookmark Functionality
Design a BookShelfManager class that manages books on a shelf.

The shelf contains books in a specific order.

Each book is represented by a string.

The class should support adding books, removing books, moving books, returning all books, setting a bookmark index, and getting the bookmark index.

Behavior

The shelf is initially empty.

Indexing is zero-based.

For all range operations, fromIndex is inclusive and toIndex is exclusive.

The bookmark stores an index in the current shelf.

If no bookmark is set, getBookmarkIndex() returns -1.

If books are inserted, removed, or moved, the bookmark index must be updated so that it continues to point to the same book whenever that book still exists.

If the bookmarked book is removed, the bookmark becomes unset and getBookmarkIndex() returns -1.

Method Signatures

BookShelfManager()
  • Initializes an empty bookshelf.
void addBooks(int toIndex, List<String> books)
  • Adds all books from books starting at index toIndex.
  • The relative order of the inserted books must remain the same.
  • 0 ≤ toIndex ≤ current number of books
  • books is never empty.
void removeBooks(int fromIndex, int toIndex)
  • Removes books from index fromIndex inclusive to toIndex exclusive.
  • 0 ≤ fromIndex < toIndex ≤ current number of books
void moveBooks(int fromIndex, int toIndex, int size)
  • Moves size consecutive books starting from fromIndex.
  • The moved books are inserted starting at index toIndex after removing them from their original position.
  • The relative order of the moved books must remain the same.
  • 0 ≤ fromIndex < current number of books
  • 1 ≤ size
  • fromIndex + size ≤ current number of books
  • 0 ≤ toIndex ≤ current number of books - size after the moved books are removed
List<String> getBooks()
  • Returns all books currently on the shelf in order.
void setBookmarkIndex(int index)
  • Sets the bookmark to the book currently present at index.
  • 0 ≤ index < current number of books
int getBookmarkIndex()
  • Returns the current index of the bookmarked book.
  • Returns -1 if no bookmark is set.

Constraints

  • 0 ≤ current number of books ≤ 10^5
  • 1 ≤ books.size() ≤ 10^4
  • 1 ≤ book.length() ≤ 100
  • All method parameters are valid according to the rules above.
  • No method parameter will be null.
  • Book names may repeat.
  • If duplicate book names exist, the bookmark must track the exact book occurrence that was bookmarked, not only the book name.

Example 1

BookShelfManager manager = new BookShelfManager()
Shelf becomes [].

manager.addBooks(toIndex = 0, books = List.of("A", "B", "C"))
Shelf becomes ["A", "B", "C"].

manager.getBooks()
Returns ["A", "B", "C"].

manager.setBookmarkIndex(index = 1)
Bookmark points to book "B".

manager.getBookmarkIndex()
Returns 1.

Example 2

BookShelfManager manager = new BookShelfManager()
Shelf becomes [].

manager.addBooks(toIndex = 0, books = List.of("A", "B", "C", "D"))
Shelf becomes ["A", "B", "C", "D"].

manager.setBookmarkIndex(index = 2)
Bookmark points to book "C".

manager.addBooks(toIndex = 1, books = List.of("X", "Y"))
Shelf becomes ["A", "X", "Y", "B", "C", "D"].

manager.getBookmarkIndex()
Returns 4, because the bookmarked book "C" moved from index 2 to index 4.

manager.getBooks()
Returns ["A", "X", "Y", "B", "C", "D"].

Example 3

BookShelfManager manager = new BookShelfManager()
Shelf becomes [].

manager.addBooks(toIndex = 0, books = List.of("A", "B", "C", "D", "E"))
Shelf becomes ["A", "B", "C", "D", "E"].

manager.setBookmarkIndex(index = 3)
Bookmark points to book "D".

manager.moveBooks(fromIndex = 1, toIndex = 3, size = 2)
Books ["B", "C"] are removed first, so the shelf becomes ["A", "D", "E"].
Then they are inserted at index 3, so the shelf becomes ["A", "D", "E", "B", "C"].

manager.getBookmarkIndex()
Returns 1, because the bookmarked book "D" is now at index 1.

manager.getBooks()
Returns ["A", "D", "E", "B", "C"].

Example 4

BookShelfManager manager = new BookShelfManager()
Shelf becomes [].

manager.addBooks(toIndex = 0, books = List.of("A", "B", "C", "D"))
Shelf becomes ["A", "B", "C", "D"].

manager.setBookmarkIndex(index = 1)
Bookmark points to book "B".

manager.removeBooks(fromIndex = 1, toIndex = 3)
Books ["B", "C"] are removed, so the shelf becomes ["A", "D"].

manager.getBookmarkIndex()
Returns -1, because the bookmarked book was removed.

manager.getBooks()
Returns ["A", "D"].




Please use Laptop/Desktop or any other large screen to add/edit code.