Problem Β· Hash Table

Max Book Copies 🫐

Learn this problem
● MediumAmazon logoAmazonFULLTIMEOA
See Amazon hiring insights

Problem statement

Amazon invests in the success of entrepreneurs, artisans, and small business selling in the Amazon Store. Some of these small business are book stores.

Amazon maintains a protal, where the booksellers can update their inventories. An update received from the portal is represented by the array portalUpdate, whose valuess indicate the following:

  • If portalUpdate[i] is a positive integer (for example 7), then a copy of the book with boook ID portalUpdate[i] is added to the inventory.
  • If portalUpdate[i] is a negative integer (for example -11), then a copy of the book with book ID abs(portalUpdate[i]) (i.e., book ID 11) is removed from the inventory. It is gauranteed that each such update will only be requested if the inventory currently has at least one copy of that book ID.
  • portalUpdate[i] is gauranteed to be non-zero.
  • Given the list of portal updates, the task is to return the maximum copies of any book in the inventory after each update.

    Function

    maximumBookCopies(portalUpdate: int[]) β†’ int[]

    Complete the function maximumBookCopies in the editor.

    maximumBookCopies has the following parameter:

  • int portalUpate[n]: the updates to the inventory
  • Returns

    int[n]: an array of integers representing the maximum copies of any book after each update

    Examples

    Example 1

    portalUpdate = [6, 6, 2, -6, -2, -6]return = [1, 2, 2, 1, 1, 0]
    Example 1 illustration

    The inventory changes as follows:

    • After 6: book 6 has 1 copy, maximum is 1.
    • After 6: book 6 has 2 copies, maximum is 2.
    • After 2: book 6 has 2 copies and book 2 has 1 copy, maximum is 2.
    • After -6: book 6 and book 2 each have 1 copy, maximum is 1.
    • After -2: book 6 has 1 copy, maximum is 1.
    • After -6: the inventory is empty, maximum is 0.

    Example 2

    portalUpdate = [1, 2, -1, 2]return = [1, 1, 1, 2]

    After update 1, the maximum count is 1. After update 2, books 1 and 2 each have 1 copy, so the maximum remains 1. After update -1, only book 2 remains with 1 copy. After the final update 2, book 2 has 2 copies, so the maximum is 2.

    Constraints

    • 1 <= n <= 106
    • -109 <= portalUpdate[i] <= 109
    • portalUpdate[i] != 0
    • Every negative update removes a copy that is currently present in the inventory.

    More Amazon problems

    drafts saved locally
    public int[] maximumBookCopies(int[] portalUpdate) {
        // write your code here
    }
    
    portalUpdate[6, 6, 2, -6, -2, -6]
    expected[1, 2, 2, 1, 1, 0]
    checking account