Batched Key Fetch Callbacks
Updated
Problem statement
Simulate calls to startFetch(key, callback). The arrays callTimes, keys, and callbackIds have the same length, and the same index in each describes one call. Calls are listed from earliest to latest time, and several calls may share a time. Each row [key, value] in keyValues gives the value the server returns for that key.
When a call arrives and no timer is waiting, set a timer to flush the batch (send it) exactly 100 milliseconds later. Every call whose time is strictly earlier than that deadline joins the waiting batch. The timer fires before any call stamped exactly at the deadline, so that call starts a new window. Each window includes its start time but not its deadline.
At each flush, send exactly one request in the form GET:/get?keys=key1,key2,.... List each requested key once, in the order of its first call in that batch. Treat the response as arriving right away, at the moment of the flush.
After the request, run every callback in the batch in the order the calls appear in the input. Each callback gets the value for its key, even when several callbacks asked for the same key. Record each callback run as CALL:callbackId:value.
Return one row of strings for each request. The row starts with the request string, followed by that batch's callback strings. If there are no calls, return an empty array.
Function
batchFetchCallbacks(callTimes: int[], keys: String[], callbackIds: String[], keyValues: String[][]) → String[][]Examples
Example 1
callTimes = [0,30,70,120]keys = ["foo","bar","foo","foo"]callbackIds = ["cb1","cb2","cb3","cb4"]keyValues = [["foo","123"],["bar","abs"]]return = [["GET:/get?keys=foo,bar","CALL:cb1:123","CALL:cb2:abs","CALL:cb3:123"],["GET:/get?keys=foo","CALL:cb4:123"]]The call at time 0 sets a flush for time 100. The calls at times 30 and 70 join that batch. The request lists foo once, and both of its callbacks get 123. The call at time 120 comes after that flush, so it starts a second batch with its own request.
Example 2
callTimes = [5,104,105,105]keys = ["a","a","b","a"]callbackIds = ["first","second","third","fourth"]keyValues = [["a","A"],["b","B"]]return = [["GET:/get?keys=a","CALL:first:A","CALL:second:A"],["GET:/get?keys=b,a","CALL:third:B","CALL:fourth:A"]]The first window is [5,105). It includes its start time but not its deadline. The timer fires before the calls stamped 105, so those two calls start a new window and keep their input order.
Example 3
callTimes = [10,10,50,250,349]keys = ["z","x","z","x","y"]callbackIds = ["c0","c1","c2","c3","c4"]keyValues = [["x","vx"],["y","vy"],["z","vz"]]return = [["GET:/get?keys=z,x","CALL:c0:vz","CALL:c1:vx","CALL:c2:vz"],["GET:/get?keys=x,y","CALL:c3:vx","CALL:c4:vy"]]The two calls with the same time keep their input order. After the first flush, the call at time 250 starts a new window that ends at 350, so the call at 349 joins it.
Constraints
0 <= callTimes.length = keys.length = callbackIds.length <= 200000.0 <= callTimes[i] <= 10^9, andcallTimesis nondecreasing.keyValuescontains exactly one row for every distinct key inkeys.- Keys contain 1 to 20 lowercase ASCII letters, digits, or underscores and do not contain commas.
- Callback IDs contain 1 to 20 ASCII letters, digits, or underscores and do not contain colons.
- Response values contain 1 to 20 ASCII letters, digits, underscores, or hyphens and do not contain colons.
Similar questions at other companies
- Logger Rate Limiter with Every-Arrival HistoryGoogle · PHONE SCREENGoogle interview questions
- Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Order and Batch Open-Close LifecycleHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Resource Reconciler: Plan and Apply CreatesPerplexity AI · PHONE SCREENPerplexity AI interview questions
- Realized Gains from a Real-Estate Transaction StreamKikoff · PHONE SCREENKikoff interview questions
- Process On-Call Routing OperationsOpenAI · PHONE SCREENOpenAI interview questions
- Earliest Car for Each PassengerWhatNot · PHONE SCREENWhatNot interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Count Active Employee Access GrantsIBM · PHONE SCREENIBM interview questions