Problem · Design

Customer Checkout Duration

Learn this problem
MediumOptiver logoOptiverINTERNOA

Problem statement

A supermarket has several checkout lines. Each customer joins one line and cannot switch lines before leaving. Process the instruction stream and return customer IDs in the order in which they leave the supermarket.

Priority rules

  • A customer leaves as soon as all of their items have been processed.
  • If a customer increases their total item count, move them to the back of the same line.
  • If a customer decreases their total item count, keep their position. If the new total is no greater than the number of items already processed, they leave immediately.
  • When one operation makes customers from several lines leave simultaneously, customers from smaller line IDs leave first.

Instructions

  • CustomerEnter customerId lineNumber numItems: add a new customer to the back of the specified line.
  • BasketChange customerId newNumItems: replace the customer's total item count with newNumItems. Previously processed items remain processed.
  • LineService lineNumber numProcessedItems: process that many items from the front of one line. Any unused processing continues with the next customer in that line.
  • LinesService: simultaneously process one item from the front customer of every non-empty line.

Complete customerCheckout(String[] instructions) and return an array containing every departing customer ID in departure order.

Function

customerCheckout(instructions: String[]) → int[]

Examples

Example 1

instructions = ["CustomerEnter 123 1 5", "CustomerEnter 3 1 2", "LineService 1 4", "BasketChange 123 6", "LineService 1 5"]return = [3, 123]

Upon first LineService 4 out of 5 items of customer 123 are processed. However, customer then increases the number of items in their basket (namely adds 1 extra item), this puts them at the back of the line.

During the next LineService call customer 3 is checked out first, and customer 123 is checked out next (as they only had 2 items left to process).

More Optiver problems

drafts saved locally
public int[] customerCheckout(String[] instructions) {
  // write your code here
}
instructions["CustomerEnter 123 1 5", "CustomerEnter 3 1 2", "LineService 1 4", "BasketChange 123 6", "LineService 1 5"]
expected[3, 123]
checking account