Problem · Array

Order and Batch Open-Close Lifecycle

Learn this problem
MediumHudson River Trading logoHudson River TradingFULLTIMEPHONE SCREEN

Problem statement

Open and close events are stored separately. Every event has a positive timestamp, an order ID, and a batch ID.

Given the parallel arrays for both event files plus queryType and queryId, return [start, end] for the requested lifecycle:

  • For an ORDER query, start is that order's open timestamp. end is its close timestamp, or 0 if it has no close event.
  • For a BATCH query, start is the earliest open timestamp among its orders. If any order in that batch has no close event, end is 0; otherwise, end is the latest close timestamp among those orders.

The input is well formed: every order opens exactly once, closes at most once, and keeps the same batch ID in both files.

Function

getLifecycleWindow(openTimestamps: long[], openOrderIds: String[], openBatchIds: String[], closeTimestamps: long[], closeOrderIds: String[], closeBatchIds: String[], queryType: String, queryId: String) → long[]

Examples

Example 1

openTimestamps = [10,12,15]openOrderIds = ["o1","o2","o3"]openBatchIds = ["b1","b1","b2"]closeTimestamps = [20,25]closeOrderIds = ["o1","o3"]closeBatchIds = ["b1","b2"]queryType = "BATCH"queryId = "b1"return = [10,0]

Batch b1 starts when o1 opens at 10. Order o2 remains open, so the batch end is 0.

Example 2

openTimestamps = [10,12,15]openOrderIds = ["o1","o2","o3"]openBatchIds = ["b1","b1","b2"]closeTimestamps = [20,25]closeOrderIds = ["o1","o3"]closeBatchIds = ["b1","b2"]queryType = "ORDER"queryId = "o1"return = [10,20]

Order o1 opens at 10 and closes at 20.

Example 3

openTimestamps = [5,7,9]openOrderIds = ["a","b","c"]openBatchIds = ["x","x","y"]closeTimestamps = [11,20,15]closeOrderIds = ["a","b","c"]closeBatchIds = ["x","x","y"]queryType = "BATCH"queryId = "x"return = [5,20]

Batch x starts at the earlier open time 5. Both orders close, and the later close is 20.

Constraints

  • The three open arrays have equal positive length; the three close arrays have equal length.
  • All timestamps are positive 64-bit integers, and each close timestamp is at least its order's open timestamp.
  • Every order ID appears exactly once in the open arrays and at most once in the close arrays.
  • A close event uses the same batch ID as its order's open event.
  • queryType is ORDER or BATCH, and queryId identifies an existing order or batch of that type.

More Hudson River Trading problems

drafts saved locally
public long[] getLifecycleWindow(long[] openTimestamps, String[] openOrderIds, String[] openBatchIds, long[] closeTimestamps, String[] closeOrderIds, String[] closeBatchIds, String queryType, String queryId) {
  // write your code here
}
openTimestamps[10,12,15]
openOrderIds["o1","o2","o3"]
openBatchIds["b1","b1","b2"]
closeTimestamps[20,25]
closeOrderIds["o1","o3"]
closeBatchIds["b1","b2"]
queryType"BATCH"
queryId"b1"
expected[10,0]
checking account