Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode Logger_Rate_Limiter.java #563

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create Logger_Rate_Limiter.java
  • Loading branch information
ANTRIKSH-GANJOO authored Oct 11, 2021
commit 7297e3b894a3b272675307e781dc860ef3110da8
60 changes: 60 additions & 0 deletions Java/Interview Questions/Java/Logger_Rate_Limiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main.cp.leetcode.august;

/*
Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.
Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.
It is possible that several messages arrive roughly at the same time.
*/

import java.util.HashMap;
import java.util.Map;

class Logger {
Map<String, Integer> map;

/**
* Initialize your data structure here.
*/
public Logger() {
map = new HashMap<>();
}

/**
* Returns true if the message should be printed in the given timestamp, otherwise returns false.
* If this method returns false, the message will not be printed.
* The timestamp is in seconds granularity.
*/
public boolean shouldPrintMessage(int timestamp, String message) {
if (!map.containsKey(message)) {
map.put(message, timestamp);
return true;
}
int lastTime = map.get(message);
if (timestamp - lastTime >= 10) {
map.put(message, timestamp);
return true;
}
return false;
}
}


public class Day_01_Logger_Rate_Limiter {

public static void main(String[] args) {

Logger logger = new Logger();
System.out.println(logger.shouldPrintMessage(1, "foo"));
System.out.println(logger.shouldPrintMessage(2, "bar"));
System.out.println(logger.shouldPrintMessage(3, "foo"));
System.out.println(logger.shouldPrintMessage(8, "bar"));
System.out.println(logger.shouldPrintMessage(10, "foo"));
System.out.println(logger.shouldPrintMessage(11, "foo"));
}

/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
*/
}