FastPrepMessages in an Inclusive Timestamp Range

Messages in an Inclusive Timestamp Range

Attentive logoAttentive● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

In-memory logs are stored in nondecreasing timestamp order. Parallel arrays timestamps and messages describe each log entry.

Given inclusive bounds start and end, return the messages for every entry whose timestamp is in [start, end]. Preserve input order and retain entries with duplicate timestamps.

Function

messagesInRange(timestamps: long[], messages: String[], start: long, end: long) → String[]

Examples

Example 1

timestamps = [1,2,2,5,8]messages = ["a","b","c","d","e"]start = 2end = 5return = ["b","c","d"]

Both entries at timestamp 2 and the entry at timestamp 5 lie inside the inclusive range.

Example 2

timestamps = [3,7,9]messages = ["x","y","z"]start = 10end = 12return = []

No timestamp lies in the requested range.

Constraints

  • 0 <= timestamps.length == messages.length <= 200000.
  • timestamps is sorted in nondecreasing order.
  • -10^18 <= timestamps[i], start, end <= 10^18.
  • start <= end.
  • Each message has length at most 200.

More Attentive problems

See Attentive hiring insights
public String[] messagesInRange(long[] timestamps, String[] messages, long start, long end) {
    // Write your code here.
}
timestamps[1,2,2,5,8]
messages["a","b","c","d","e"]
start2
end5
expected["b", "c", "d"]
Checking account…