Description
Solutions
For All Intents And Purposes Part 4 - Timing Matters โฐ
๐Ÿค˜ INTERN๐Ÿ‘ฉโ€๐ŸŽ“ NEW GRAD

๐Ÿ Hi there! The description you are currently reading is just 4th part of the problem set. It is highly recommended to read ALL THE 4 PARTS before coding as parts may build on top of each other ๐Ÿณ

  • For All Intents And Purposes Part 1 - Initilizing the System ๐Ÿ—๏ธ
  • For All Intents And Purposes Part 2 - Change Is Good! ๐Ÿ‘
  • For All Intents And Purposes Part 3 - Accepting Failure ๐Ÿ˜Œ
  • For All Intents And Purposes Part 4 - Timing Matters โฐ
  • Last month, we received the new Payment Intent system you built ๐Ÿ‘, and merchants have been LOVING how simple it makes accepting payments. Congratulations ๐ŸŽ‰!

    However, numerous merchants have requested that we augment our refund functionality by letting merchants specify a refund timeout policy: the amount of time after a payment succeeds that refunds are permitted. For example, one merchant, LlmaCorp, only wants to accept refunds within 15 days of a payment succeeding. Being able to return a llama afte 15 days would be absurd after all.

    Let's add this functionality to our system, which means we will need to change the format of our commands to handle timing ๐Ÿ’ช.

    Timestamps

  • All commands will now have an integer timestamp in front. For example, intead of SUCCEED , you will now receiveSUCCEED
  • You can assume that the timestamps will always be integers (representing a unit of time) and that the timestamps of the list of commands will be strictly increasing
  • Merchant Refund Timeout Limit

  • The INIT command that initializes a merchant will now have an optional third argument specifying the merchant's refund timeout limit. The new specification is -
  • ๐Ÿ‘‰ <timestamp> INIT <merchant_id> <starting_balance> <refund_time_out_limit>

  • If a merchant has a refund_timeout_limit of n, and succeeds a Payment Intent (with the SUCCEED command) at timestamp t, then the timestamp of a REFUND command must be no greater than t + n, otherwise the refund should no succeed.
  • iF A refund_timeout_limit is not specified during merchant initialization, there is no refund time limit, a a refund should succeed regardless how much time has passed since Payment Intent confirmation. A reund_timeout_limit of 0 means no refunds should be accepted ever, and a negative refund_timeout_limit should be ignore (meaning all refunds should be accepted)
  • ๐ŸŒทเผŠยทหšCredit to Rachelแฅซแญก.

    Example 1:

    Input:  commands = ["1 INIT m1 0 5", "2 CREATE p1 m1 100", "3 CREATE p2 m1 50", "4 ATTEMPT p1", "5 ATTEMPT p2", "8 SUCCEED p1", "10 SUCCEED p2", "11 REFUND p1", "16 REFUND p2"]
    Output: ["m1 50"]
    Explanation:
    In the above example, the reund of Payment Intent p1 succeeded because p1 was succeeded at time 8 and refunded at time 11, which is within merchant m1's refund timeout limit of 5. However, the refund of Payment Intent p2 failed because p2 was succeeded at time 10 and refunded at time 16, and 16 - 10 = 6 > 5, meaning it occured after m1's timeout limit. Because p1 was successfully refunded and p2 was not, the final balance of merchant m1 should be 50 :P
    Constraints:
      ๐Ÿซถ
    Thumbnail 0
    Testcase

    Result
    Case 1

    input:

    output: