FastPrepFastPrep
Problem Brief

Max Book Copies 🫐

FULLTIMEOA
See Amazon online assessment and hiring insights

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 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 Description

    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

    1Example 1

    Input
    portalUpdate = [6, 6, 2, -6, -2, - 6]
    Output
    [1, 2, 2, 1, 1, 0]
    Explanation
    Example 1 illustration
    The inventory will be updated as follows:
  • After the first update, the inventory contains one copy of book ID 6. Maximum copies = 1, of book ID 6.
  • After the second update, the inventory contains two copies of book ID 6. Maximum copies = 2, of book ID 6.
  • After third update, the inventory contains two copies of book ID 6 and one copy of book ID 2. Maximum copies = 2, off book ID 6.
  • After the fourth update, the inventory contains one copy of book ID 6 and one copy of book ID 2. Maximum copies = 1, of book ID 6 or book ID 2. E: After the fifth update, the inventory contains one copy of book ID 6. Maxmum copies = 1, of book ID 6.
  • After the last upate, the inventory is empty. Maximum copies = 0, no books are present.
  • The answer is [1, 2, 2, 1, 1, 0].

    2Example 2

    Input
    portalUpdate = [1, 2, -1, 2]
    Output
    [1, 1, 1, 2]
    Explanation
    Unknown so far πŸ₯Ή If you know about it, feel free to let me know.. But you can still use this example to test your code πŸ’ͺ

    Constraints

    Limits and guarantees your solution can rely on.

  • 1 <= n <= 106
  • -109 <= portalUpdate[i] <= 109, for 0 <= i < n
  • portalUpdate[i] != 0
  • public int[] maximumBookCopies(int[] portalUpdate) {
        // write your code here
    }
    
    Input

    portalUpdate

    [6, 6, 2, -6, -2, - 6]

    Output

    [1, 2, 2, 1, 1, 0]

    Sign in to submit your solution.