Amazon Interview Question
Software DevelopersCountry: United States
Let's call the period P and the threshold T
1. Let's suppose we have an event listener that listens for incoming events and we have a function to handle the call, we can use a queue or either just a plain int for counting the number of events within a timewindow P:
int numEvents = 0;
function eventHandler {
numEvents ++;
if (numEvents >= T) {
return true
}
return false
}
2. After that we build a scheduler that will run every P units of time (milliseconds, seconds etc...) the scheduler will run a task that resets the counter to 0
class ResetTask extends TimerTask {
public void run() {
numEvents = 0;
}
}
Timer timer = new Timer();
timer.schedule(new ResetTask(), 0, T); // make sure to conver to proper unit : milis, seconds, etc...
This way you can know for any given timestamp X if the threshold has been surpassed
Use a queue, when queue.size > 3, empty the queue for the result
- ian April 12, 2018