|
| 1 | +# Logger Rate Limiter |
| 2 | + |
| 3 | +For the given stream of message requests and their timestamps as input, you must implement a logger rate limiter system |
| 4 | +that decides whether the current message request is displayed. The decision depends on whether the same message has |
| 5 | +already been displayed in the last S seconds. If yes, then the decision is FALSE, as this message is considered a |
| 6 | +duplicate. Otherwise, the decision is TRUE. |
| 7 | + |
| 8 | +> Note: Several message requests, though received at different timestamps, may carry identical messages. |
| 9 | +
|
| 10 | +## Constraints |
| 11 | + |
| 12 | +- 1 <= `request.length` <= 10^2 |
| 13 | +- 0 <= `timestamp` <= 10^3 |
| 14 | +- Timestamps are in ascending order. |
| 15 | +- Messages can be written in lowercase or uppercase English alphabets. |
| 16 | + |
| 17 | +## Examples |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +## Solution |
| 24 | + |
| 25 | +We need to know if a message already exists and keep track of its time limit. When thinking about such problems where |
| 26 | +two associated values need to be checked, we can use a hash map. |
| 27 | + |
| 28 | +We can use all incoming messages as keys and their respective time limits as values. This will help us eliminate |
| 29 | +duplicates and respect the time limit of S seconds as well. |
| 30 | + |
| 31 | +Here is how we’ll implement our algorithm using hash maps: |
| 32 | + |
| 33 | +1. Initialize a hash map. |
| 34 | +2. When a request arrives, check if it’s a new request (the message is not among the keys stored in the hash map) or a |
| 35 | + repeated request (an entry for this message already exists in the hash map). If it’s a new request, accept it and add |
| 36 | + it to the hash map. |
| 37 | +3. If it’s a repeated request, compare the difference between the timestamp of the incoming request and the timestamp of |
| 38 | + the previous request with the same message. If this difference is greater than the time limit, S, accept it and |
| 39 | + update the timestamp for that specific message in the hash map. Otherwise, reject it. |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +### Solution Summary |
| 48 | + |
| 49 | +Let’s summarize our optimized algorithm: |
| 50 | + |
| 51 | +1. After initializing a hash map, whenever a request arrives, we check whether it’s a new request or a repeated request |
| 52 | + after the assigned time limit |
| 53 | + |
| 54 | +2. If the request meets either of the conditions mentioned in the above step, we accept and update the entry associated |
| 55 | + with that specific request in the hash map. Otherwise, reject the request and return the final decision. |
| 56 | + |
| 57 | +### Time Complexity |
| 58 | + |
| 59 | +The decision function checks whether a message has already been encountered, and if so, how long ago it was encountered. |
| 60 | +Thanks to the use of hash maps, both operations are completed in constant time—therefore, the time complexity of the |
| 61 | +decision function is O(1). |
| 62 | + |
| 63 | +### Space Complexity |
| 64 | + |
| 65 | +The space complexity of the algorithm is O(n), where n is the number of incoming requests that we store. |
| 66 | + |
| 67 | + |
0 commit comments