Amazon questions

Package Locker Station

AmazonPhone screenMedium

A package-locker station has a fixed set of numbered compartments, each with a size label such as "S", "M" or "L". A delivery driver deposits a package into a free compartment of a matching size and receives a one-time pickup code; the customer later enters the code to open the compartment. Time is a whole number of days now, and now never decreases from one call to the next within a test.

Part 1: Locker station

You are given a locker station with compartments numbered 0 to len(sizes) - 1, where compartment i has size sizes[i]. Implement the LockerStation class with four methods.

  • deposit(size, now) puts a package into the lowest-numbered free compartment whose size equals size and returns a new pickup code. Codes are positive integers handed out as 1, 2, 3, ... in order of successful deposits. If no free compartment has that size, return None and do not use a code.
  • pickup(code, now) opens the compartment of code, returns its number and frees it. A code works once, on days d through d + 6, where d is the day the package was deposited. Return "invalid" for a code that was never issued, was already used or was removed by staff. Return "expired" for a code that is still waiting but was deposited 7 or more days before now. An expired package stays in its compartment, which remains occupied, and its code stays unusable.
  • open_expired(now) returns the numbers, in ascending order, of every compartment whose package is expired on day now (deposited 7 or more days earlier and not picked up), or an empty list if there are none. It changes nothing: the packages stay inside until the removal is confirmed.
  • confirm_removal(compartments, now) confirms that the packages in compartments were physically taken out. If every listed compartment holds a package that is expired on day now, remove their codes, free the compartments and return True. Otherwise change nothing and return False. A compartment listed twice counts once, and an empty list returns True.
  1. Example 1
    init
    [["S","M","M","L"]]
    operations
    [["deposit",["M",0]],["deposit",["M",1]],["deposit",["M",2]],["pickup",[1,3]],["deposit",["M",3]],["pickup",[1,4]],["pickup",[99,4]],["deposit",["S",4]],["deposit",["S",5]],["deposit",["XL",5]],["pickup",[2,7]],["pickup",[2,8]],["pickup",[3,9]],["open_expired",[9]],["open_expired",[11]]]
    Output
    [1,2,null,1,3,"invalid","invalid",4,null,null,2,"invalid",1,[],[0]]

    Why: Compartments 1 and 2 are the medium ones, so the third medium deposit finds none. Code 1 opens compartment 1 on day 3 and frees it, so the next medium deposit reuses it (code 3); code 1 is then already used and 99 never existed. Only compartment 0 is small, so the second small deposit fails, and no compartment has size XL. Code 2 (deposited day 1) still works on day 7 but is used on day 8. Code 3 (day 3) opens compartment 1 on day 9. On day 9 no package is 7 days old; by day 11 the small package from day 4 is, so staff open compartment 0.

Constraints

  • 1 <= len(sizes) <= 10^4
  • 1 <= len(size) <= 5
  • 0 <= now <= 10^9
  • 1 <= len(operations) <= 10^4
  • now never decreases from one call to the next within a test
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question