FastPrepTrace Logs at a Two-Second Cadence

Trace Logs at a Two-Second Cadence

Klaviyo logoKlaviyo● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

For this exercise, start a virtual periodic logger at time 0 milliseconds and return its output events through durationMs.

  • The first event occurs at 2000 ms, then one event occurs every 2000 ms.
  • Include an event exactly at the supplied horizon, but none after it. There is no time-zero event.
  • Every event carries the supplied message unchanged.

Return events in increasing time order as [timestamp, message] rows. The timestamp is base-10 millisecond text with no leading zeros. Use virtual time and return the events immediately.

The source reports the two-second cadence, but no message, start/stop controls, horizon or interface. Those are the stated practice conventions; this function models output events rather than implementing a real browser timer.

Function

tracePeriodicLogs(durationMs: int, message: String) → String[][]

Examples

Example 1

durationMs = 5500message = "tick"return = [["2000","tick"],["4000","tick"]]

Events occur at 2000 and 4000 ms; the next event at 6000 ms is beyond the horizon.

Example 2

durationMs = 1999message = "ready"return = []

The first event is at 2000 ms, so this horizon contains no event.

Constraints

  • 0 <= durationMs <= 1000000.
  • 1 <= message.length <= 100.
  • Every message character is printable ASCII, with a code from 32 through 126.

More Klaviyo problems

See Klaviyo hiring insights
public String[][] tracePeriodicLogs(int durationMs, String message) {
    // Write your code here
}
durationMs5500
message"tick"
expected[["2000", "tick"], ["4000", "tick"]]
Checking account…