Back to writing

Logger Rate Limiter


Design a logger system that receives a stream of messages along with their timestamps. Each unique message should only be printed at most every 10 seconds (i.e. a message printed at timestamp t will prevent other identical messages from being printed until timestamp t + 10).

All messages will come in chronological order. Several messages may arrive at the same timestamp.

Implement the Logger class:

  • Logger() Initializes the logger object.
  • bool shouldPrintMessage(int timestamp, string message) Returns true if the message should be printed in the given timestamp, otherwise returns false.

 

Example 1:

Input
["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]
[[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10, "foo"], [11, "foo"]]
Output
[null, true, true, false, false, false, true]

Explanation
Logger logger = new Logger();
logger.shouldPrintMessage(1, "foo");  // return true, next allowed timestamp for "foo" is 1 + 10 = 11
logger.shouldPrintMessage(2, "bar");  // return true, next allowed timestamp for "bar" is 2 + 10 = 12
logger.shouldPrintMessage(3, "foo");  // 3 < 11, return false
logger.shouldPrintMessage(8, "bar");  // 8 < 12, return false
logger.shouldPrintMessage(10, "foo"); // 10 < 11, return false
logger.shouldPrintMessage(11, "foo"); // 11 >= 11, return true, next allowed timestamp for "foo" is 11 + 10 = 21

 

Constraints:

  • 0 <= timestamp <= 109
  • Every timestamp will be passed in non-decreasing order (chronological order).
  • 1 <= message.length <= 30
  • At most 104 calls will be made to shouldPrintMessage.

 class Logger:

    def __init__(self):
        self.hashmap = {}

    def shouldPrintMessage(self, timestamp: int, message: str) -> bool:

        if message in self.hashmap:
            print_time = self.hashmap[message]
            if timestamp >= print_time:
                self.hashmap[message] = 10 + timestamp
                return True
            return False
        self.hashmap[message] = timestamp+10
        return True



# Your Logger object will be instantiated and called as such:
# obj = Logger()
# param_1 = obj.shouldPrintMessage(timestamp,message)

𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 𝗞𝗲𝘆 𝗖𝗼𝗻𝗰𝗲𝗽𝘁𝘀:

  1. Scalability: https://lnkd.in/gpge_z76
  2. Latency vs Throughput: https://lnkd.in/g_amhAtN
  3. CAP Theorem: https://lnkd.in/g3hmVamx
  4. ACID Transactions: https://lnkd.in/gMe2JqaF
  5. Rate Limiting: https://lnkd.in/gWsTDR3m
  6. API Design: https://lnkd.in/ghYzrr8q
  7. Strong vs Eventual Consistency: https://lnkd.in/gJ-uXQXZ
  8. Distributed Tracing: https://lnkd.in/d6r5RdXG
  9. Sync vs Async Communication: https://lnkd.in/gC3F2nvr
  10. Batch vs Stream Processing: https://lnkd.in/g4_MzM4s
  11. Fault Tolerance: https://lnkd.in/dVJ6n3wA

𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 𝗕𝘂𝗶𝗹𝗱𝗶𝗻𝗴 𝗕𝗹𝗼𝗰𝗸𝘀:

  1. Database: https://lnkd.in/gti8gjpz
  2. Horizontal vs Vertical Scaling: https://lnkd.in/gAH2e9du
  3. Caching: https://lnkd.in/gC9piQbJ
  4. Distributed Caching: https://lnkd.in/g7WKydNg
  5. Load Balancing: https://lnkd.in/gQaa8sXK
  6. SQL vs NoSQL: https://lnkd.in/g3WC_yxn
  7. Database Scaling: https://lnkd.in/gAXpSyWQ
  8. Data Replication: https://lnkd.in/gVAJxTpS
  9. Data Redundancy: https://lnkd.in/gNN7TF7n
  10. Database Sharding: https://lnkd.in/gMqqc6x9
  11. Database Indexes: https://lnkd.in/gCeshYVt
  12. Proxy Server: https://lnkd.in/gi8KnKS6
  13. WebSocket: https://lnkd.in/g76Gv2KQ
  14. API Gateway: https://lnkd.in/gnsJGJaM
  15. Message Queues: https://lnkd.in/gTzY6uk8

𝗔𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀:

  1. Event-Driven Architecture: https://lnkd.in/dp8CPvey
  2. Client-Server Architecture: https://lnkd.in/dAARQYzq
  3. Serverless Architecture: https://lnkd.in/gQNAXKkb
  4. Microservices Architecture: https://lnkd.in/gFXUrz_T

𝗟𝗼𝘄-𝗟𝗲𝘃𝗲𝗹 𝗗𝗲𝘀𝗶𝗴𝗻 𝗣𝗿𝗼𝗯𝗹𝗲𝗺𝘀:

  1. Design Parking Lot: https://lnkd.in/dQaAuFd2
  2. Design Splitwise: https://lnkd.in/dF5fBnex
  3. Design Chess Validator: https://lnkd.in/dfAQHvN4
  4. Design Distributed Queue | Kafka: https://lnkd.in/dQ6_B4_M

Share with someone who might need this.

FAANGTips