Design a Bookstore Word and Character Counter

Quick Overview

Design a class that stores bookstore texts by id and returns the total count of a word or a letter across all books, replacing a book's text when its id is added again. It tests hash map indexing, keeping running totals correct on updates, and complexity analysis for a read-heavy API.

Design a Bookstore Word and Character Counter

Company: Amazon

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a class `BookstoreWordCounter` for a bookstore. It stores books with their text content and searches for a specific word or character across all stored books, returning the total number of occurrences. Each book is identified by a unique `bookId`. If a book with the same `bookId` is added again, its text content is replaced and the search indexes are updated accordingly. Words in book text are separated by one or more spaces. Searches are case-sensitive, and all text consists of lowercase English letters and spaces. Implement the following methods: - `addBook(bookId: int, text: str)`: Add a book with the given `bookId` and text content. If a book with the same `bookId` already exists, replace its text and update the indexes accordingly. - `countWord(word: str) -> int`: Return the total number of occurrences of the given word across all books. Words are delimited by spaces, so a word counts only when it appears as a whole space-delimited word. - `countChar(char: str) -> int`: Return the total number of occurrences of the given single character across all book texts (spaces are not counted). - `getBookCount() -> int`: Return the number of books currently stored in the bookstore. Example 1: two books, a word that appears in both, and a search string that only appears inside other words. ```text Input: ["BookstoreWordCounter", "addBook", "addBook", "countWord", "countChar", "countWord", "getBookCount"] [[], [1, "the cat sat"], [2, "the dog"], ["the"], ["t"], ["at"], []] Output: [null, null, null, 2, 4, 0, 2] ``` The word `the` appears once in each book, so `countWord("the")` returns 2, and the double space in book 2 does not create an extra word. The letter `t` appears three times in book 1 and once in book 2, giving 4. The string `at` occurs inside `cat` and `sat` but never as a whole word, so its count is 0. Example 2: replacing a book. ```text Input: ["BookstoreWordCounter", "addBook", "countWord", "addBook", "countWord", "getBookCount"] [[], [1, "hello world"], ["hello"], [1, "goodbye world"], ["hello"], []] Output: [null, null, 1, null, 0, 1] bookstore = BookstoreWordCounter() # empty bookstore bookstore.addBook(1, "hello world") # adds book 1 bookstore.countWord("hello") # 1, appears once in book 1 bookstore.addBook(1, "goodbye world") # replaces book 1's text, indexes updated bookstore.countWord("hello") # 0, "hello" is no longer in any book bookstore.getBookCount() # 1, still one book because it was replaced ``` ```hint Where to pay the cost A query should not rescan every stored book. Think about which totals you could keep up to date at the moment a book is added. ``` ```hint Replacing a book When a `bookId` is added again, its old text still contributes to any totals you keep. Decide what you need to remember about each book so you can undo that contribution. ``` ### Constraints & Assumptions - `1 <= bookId <= 10^5` - `1 <= len(text) <= 10^4` - Book text consists of lowercase English letters and spaces, and words are separated by one or more spaces. - Search words consist of lowercase English letters only. - Search characters are single lowercase English letters. - If a book with the same `bookId` is added again, its text is replaced. - At most `10^4` calls will be made in total. ### Clarifying Questions to Ask - Does `countWord` count only whole space-delimited words, or also substrings such as `at` inside `cat`? - Is there a delete operation, or is a book only ever added or replaced? - Are searches much more frequent than additions, so that it pays to do extra work when a book is added? - Can a text start or end with spaces? ### What a Strong Answer Covers - A store of books keyed by `bookId` that makes a replacement detectable and keeps whatever is needed to undo the old text. - Word and character totals across all books that stay current on every add, so the query methods are constant-time lookups. - Correct replacement: the old text's contribution is removed before the new text is counted, and the book count does not change. - Tokenizing that handles runs of spaces and leaves spaces out of character counts. - Time and space complexity per method, including the cost of replacing a long book. ### Follow-up Questions - How would you add `removeBook(bookId)`, and what does it change in the other methods? - How would you count all words that start with a given prefix? - How would you return a word's count within one book, or the list of books that contain it? - What has to change if several threads call `addBook` and `countWord` at the same time?

Overview: Design a class that stores bookstore texts by id and returns the total count of a word or a letter across all books, replacing a book's text when its id is added again. It tests hash map indexing, keeping running totals correct on updates, and complexity analysis for a read-heavy API.

Amazon logo
Amazon
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a class BookstoreWordCounter for a bookstore. It stores books with their text content and searches for a specific word or character across all stored books, returning the total number of occurrences.

Each book is identified by a unique bookId. If a book with the same bookId is added again, its text content is replaced and the search indexes are updated accordingly.

Words in book text are separated by one or more spaces. Searches are case-sensitive, and all text consists of lowercase English letters and spaces.

Implement the following methods:

  • addBook(bookId: int, text: str) : Add a book with the given bookId and text content. If a book with the same bookId already exists, replace its text and update the indexes accordingly.
  • countWord(word: str) -> int : Return the total number of occurrences of the given word across all books. Words are delimited by spaces, so a word counts only when it appears as a whole space-delimited word.
  • countChar(char: str) -> int : Return the total number of occurrences of the given single character across all book texts (spaces are not counted).
  • getBookCount() -> int : Return the number of books currently stored in the bookstore.

Example 1: two books, a word that appears in both, and a search string that only appears inside other words.

Input:
["BookstoreWordCounter", "addBook",          "addBook",       "countWord", "countChar", "countWord", "getBookCount"]
[[],                     [1, "the cat sat"], [2, "the  dog"], ["the"],     ["t"],       ["at"],      []]
Output: [null, null, null, 2, 4, 0, 2]

The word the appears once in each book, so countWord("the") returns 2, and the double space in book 2 does not create an extra word. The letter t appears three times in book 1 and once in book 2, giving 4. The string at occurs inside cat and sat but never as a whole word, so its count is 0.

Example 2: replacing a book.

Input:
["BookstoreWordCounter", "addBook",          "countWord", "addBook",            "countWord", "getBookCount"]
[[],                     [1, "hello world"], ["hello"],   [1, "goodbye world"], ["hello"],   []]
Output: [null, null, 1, null, 0, 1]

bookstore = BookstoreWordCounter()      # empty bookstore
bookstore.addBook(1, "hello world")     # adds book 1
bookstore.countWord("hello")            # 1, appears once in book 1
bookstore.addBook(1, "goodbye world")   # replaces book 1's text, indexes updated
bookstore.countWord("hello")            # 0, "hello" is no longer in any book
bookstore.getBookCount()                # 1, still one book because it was replaced

Constraints & Assumptions

  • 1 <= bookId <= 10^5
  • 1 <= len(text) <= 10^4
  • Book text consists of lowercase English letters and spaces, and words are separated by one or more spaces.
  • Search words consist of lowercase English letters only.
  • Search characters are single lowercase English letters.
  • If a book with the same bookId is added again, its text is replaced.
  • At most 10^4 calls will be made in total.

Clarifying Questions to Ask Guidance

  • Does countWord count only whole space-delimited words, or also substrings such as at inside cat ?
  • Is there a delete operation, or is a book only ever added or replaced?
  • Are searches much more frequent than additions, so that it pays to do extra work when a book is added?
  • Can a text start or end with spaces?

What a Strong Answer Covers Guidance

  • A store of books keyed by bookId that makes a replacement detectable and keeps whatever is needed to undo the old text.
  • Word and character totals across all books that stay current on every add, so the query methods are constant-time lookups.
  • Correct replacement: the old text's contribution is removed before the new text is counted, and the book count does not change.
  • Tokenizing that handles runs of spaces and leaves spaces out of character counts.
  • Time and space complexity per method, including the cost of replacing a long book.

Follow-up Questions Guidance

  • How would you add removeBook(bookId) , and what does it change in the other methods?
  • How would you count all words that start with a given prefix?
  • How would you return a word's count within one book, or the list of books that contain it?
  • What has to change if several threads call addBook and countWord at the same time?