FastPrepTruck Position Subscriber

Truck Position Subscriber

Optiver logoOptiver● MediumFULLTIMEOA
Learn

Problem statement

A central server tracks truck positions. Local clients subscribe through one subscriber service so they receive only the truck updates they need.

Truck i starts at initialPositions[i] = [x, y]. Process these operations in order:

  • U truckId deltaX deltaY moves that truck. The server position always changes, even when no client has subscribed yet.
  • S clientId truckId subscribes the client and immediately returns the truck's current position. Later duplicate subscriptions are idempotent but still return the current position.
  • R clientId returns every update to any truck that client subscribed to after that subscription and since the client's previous request. Preserve the original global update order, then clear those pending updates for that client.

Return one string for every subscription result and every delivered update. Use S clientId truckId x y for a subscription and U clientId truckId deltaX deltaY for a delivered update.

Function

truckPositionUpdates(initialPositions: double[][], operations: String[]) → String[]

Examples

Example 1

initialPositions = [[2,3]]operations = ["U 0 1.5 2.5","S 0 0","U 0 1 2","U 0 -0.5 -0.5","S 1 0","R 0","U 0 1 1","R 1"]return = ["S 0 0 3.5 5.5","S 1 0 4 7","U 0 0 1 2","U 0 0 -0.5 -0.5","U 1 0 1 1"]

The first update changes the server position before any subscription. Client 0 later receives two deltas, while client 1 receives only the delta that occurred after its own subscription.

Constraints

  • 1 <= initialPositions.length, operations.length <= 10^5
  • Every truck ID is a valid zero-based row index.
  • Client IDs are nonnegative integers.
  • Coordinates and deltas are finite decimal values.
  • A request names a client that has subscribed at least once.

More Optiver problems

See Optiver hiring insights
public String[] truckPositionUpdates(double[][] initialPositions, String[] operations) {
  // write your code here
}
initialPositions[[2,3]]
operations["U 0 1.5 2.5","S 0 0","U 0 1 2","U 0 -0.5 -0.5","S 1 0","R 0","U 0 1 1","R 1"]
expected["S 0 0 3.5 5.5", "S 1 0 4 7", "U 0 0 1 2", "U 0 0 -0.5 -0.5", "U 1 0 1 1"]
Checking account…