Problem · Design
Record Log
Suppose you are responsible for building a log server that will receive a large number of logs. Each log will have a log ID and timestamp. We want to keep track of these logs, but due to storage limitations, we can only return up to m logs from the last hour when requested.
Complete the following functions:
-
recordLog(logId, timestamp): Records a new log entry.- Each log is represented as an object with a logId and timestamp. The logId is an identifier for each log and the timestamp is an integer in seconds representing when the log was sent.
- Logs may be received out of order.
- The log ID is not guaranteed to be unique - the same log ID can be used for different logs.
-
getLogs(): Returns a comma separated string of the latestmlogs from the last hour ascending by timestamp. In the event of a timestamp tie, order from earliest received to latest.- Return a string of the form "logId1,logId2,logId3,logId4" where logId4 is the latest timestamp log and logId1 is the earliest timestamp log received < 1 hour before logId4's timestamp.
-
getLogCount(): Returns the total number of logs received < 1 hour from the most recently stored log timestamp. In the event more thanmlogs have been received, still return the full count of logs.
Examples
01 · Example 1
m = 100 q = 10 operations = ["RECORD 1 0", "RECORD 2 300", "GET_LOGS", "COUNT", "RECORD 3 1200", "RECORD 1 1800", "GET_LOGS", "COUNT", "RECORD 4 3900", "GET_LOGS"] return = "1,2\n2\n1,2,3,1\n4\n3,1,4"
- After the first two RECORD operations, the logs within the last hour are 1 and 2. GET_LOGS returns "1,2".
- COUNT returns 2 since there are two logs within the last hour.
- After two more RECORD operations, the logs within the last hour are 1, 2, 3, and 1. GET_LOGS returns "1,2,3,1".
- COUNT returns 4 since there are four logs within the last hour.
- After the last RECORD operation, the logs within the last hour are 3, 1, and 4. GET_LOGS returns "3,1,4".
Constraints
- 1 ≤
m≤ 1000 - 1 ≤
q≤ 10^6 logIdis an integertimestampis an integer representing a timestamp in seconds
More Optiver problems
public void recordLog(int logId, int timestamp) {
// write your code here
}
public String getLogs() {
// write your code here
}
public int getLogCount() {
// write your code here
}
m100
q10
operations["RECORD 1 0", "RECORD 2 300", "GET_LOGS", "COUNT", "RECORD 3 1200", "RECORD 1 1800", "GET_LOGS", "COUNT", "RECORD 4 3900", "GET_LOGS"]
expected"1,2\n2\n1,2,3,1\n4\n3,1,4"
sign in to submit