Transaction Event Store
Problem statement
A card team keeps a log of transaction events. Each event carries four values: an eventId, the accountId it belongs to, the timestamp when it happened, and an amount. Events reach the log in no particular time order, and the log starts with no events.
The rows of operations are calls made to the log, in order. Handle each call like this:
["RECORD", eventId, accountId, timestamp, amount]adds the event to the log and answerstrue. Event ids are unique across the whole log, not just within one account. When thiseventIdis already in the log, nothing is added, the logged event keeps its original values, and the answer isfalse.["EVENTS", accountId, startTime, endTime]lists the ids of that account's logged events withstartTime <= timestamp <= endTime. Earlier timestamps come first. For equal timestamps, the id that is smaller as a string comes first (see the rule below). With no matching events the list is empty.["TOTAL", accountId]answers the sum of the amounts of that account's logged events. An account with no logged events has a total of0.
Ids are compared as plain strings. Find the first position where the two ids differ: the id with the smaller character there comes first, and the digits 0 to 9 are smaller than the letters a to z. If one id is the beginning of the other, the shorter id comes first. So "b10" comes before "b7", and "e10" comes before "e100".
Write the answers into a list with one entry per call, in call order. Each entry is a list of strings: for EVENTS it is the id list itself, and for RECORD and TOTAL it holds the single answer written as a string, like ["false"] or ["0"].
Function
processTransactionEvents(operations: String[][]) → String[][]Examples
Example 1
operations = [["RECORD","t1","acct1","300","500"],["RECORD","t2","acct2","100","200"],["RECORD","t3","acct1","100","1500"],["RECORD","t1","acct2","50","999"],["EVENTS","acct1","0","1000"],["TOTAL","acct1"],["TOTAL","acct2"],["EVENTS","acct2","150","400"]]return = [["true"],["true"],["true"],["false"],["t3","t1"],["2000"],["200"],[]]- The first three events are new, so each
RECORDreturnstrue. Eventt3arrives aftert1but happened earlier, at time 100. - The fourth call reuses the id
t1. It is ignored and returnsfalse, even though its account and amount are different. acct1hast3at time 100 andt1at time 300, so the list is["t3","t1"].- The totals are
1500 + 500 = 2000foracct1and200foracct2. The ignored call added nothing. acct2has one event, at time 100, which is outside[150, 400], so the last result is empty.
Example 2
operations = [["RECORD","b7","acc","60","10"],["RECORD","b10","acc","60","20"],["RECORD","a5","acc","120","30"],["EVENTS","acc","60","120"],["EVENTS","acc","61","119"],["TOTAL","zzz"],["TOTAL","acc"]]return = [["true"],["true"],["true"],["b10","b7","a5"],[],["0"],["60"]]b7andb10both happened at time 60. Compared character by character,"b10"comes before"b7"because'1'comes before'7'. Then comesa5at time 120.- Both ends of the range count, so
[60, 120]includes all three events.[61, 119]includes none of them. zzzhas no events, so its total is0. The total foraccis10 + 20 + 30 = 60.
Constraints
1 <= operations.length <= 1000- Every operation row uses one of the three call forms shown above.
eventIdandaccountIdhave 1 to 10 characters, and each character is a lowercase English letter or a digit.timestamp,startTimeandendTimeare whole numbers from0to10^9, andstartTime <= endTime.amountis a whole number from1to10^9.- Every number is written in base 10 without leading zeros.