No result illustrationNo result illustration

DevLoom

search

Lets Connect

chevron right
Menu
Home

Home

Community

Community

Blog Posts

Blog Posts

Programming

Programming

Tech Topics

Tech Topics

Explore

Explore

Find Jobs

Find Jobs

Tags

Tags

DevLoomPerfect Place for devs
    profile

    Jubair

    upvoteupvote

    1

    downvote

    0

    star

    Message Rate Limitter

    clock icon

    Asked 9 months ago

    message

    0Comments

    eye

    0 Views

    This code implements a message rate limiter. It processes a series of logs, each containing a timestamp and a message. The goal is to determine whether each message should be printed based on the following rules:

      • If it's the first occurrence of the message, print it.
      • If the message has been seen before, only print it if at least 10 seconds have passed since its last printing
    #include <iostream>
    #include <unordered_map>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    // Function to check if a message should be printed
    bool shouldPrintMessage(int timestamp, string message, unordered_map<string, int>& lastPrintTime) {
        // If the message hasn't been seen before
        if (lastPrintTime.find(message) == lastPrintTime.end()) {
            lastPrintTime[message] = timestamp;
            return true;
        }
        
        // If the message has been seen before
        int oldTimestamp = lastPrintTime[message];
        if (timestamp - oldTimestamp >= 10) {
            lastPrintTime[message] = timestamp;
            return true;
        }
        
        return false;
    }
    
    int main() {
        // Define the logs as a vector of pairs
        vector<pair<int, string>> logs = {
            {1, "cat"},
            {2, "dog"},
            {3, "cat"},
            {8, "rat"},
            {11, "dog"}
        };
    
        unordered_map<string, int> lastPrintTime;
    
        for (const auto& log : logs) {
            int timestamp = log.first;
            string message = log.second;
    
            bool canPrint = shouldPrintMessage(timestamp, message, lastPrintTime);
            cout << (canPrint ? "true" : "false") << " ";
        }
    
        return 0;
    }
    • Data Structure:
      • We use a vector of pairs to store the logs: vector<pair<int, string>> logs
      • Each pair contains a timestamp (int) and a message (string)
      • We use an unordered_map lastPrintTime to keep track of when each message was last printed
    • Main Function:
      • We iterate through each log in the logs vector
      • For each log, we call the shouldPrintMessage function
      • We print "true" if the message should be printed, and "false" otherwise
    • shouldPrintMessage Function: This function encapsulates the logic for determining whether a message should be printed:
      • If the message is new (not in the map), we add it to the map and return true
      • If the message exists in the map, we check if at least 10 seconds have passed since its last printing
      • If 10 seconds have passed, we update the timestamp and return true
      • Otherwise, we return false
      •  
    • Output: The code will output "true" or "false" for each log, indicating whether the message should be printed.
    • Time Complexity:
      • The time complexity is O(n), where n is the number of logs
      • Each log is processed once, and unordered_map operations are O(1) on average
    • Space Complexity:
      • The space complexity is O(m), where m is the number of unique messages
      • In the worst case, if all messages are unique, this could be O(n)
    Atlassian
    Hashmap
    easy

    Share

    Write your comment here !

    0 Responses