Number of Recent Calls LeetCode Solution - The Coding Shala

Home >> LeetCode >> Number of Recent Calls

 In this post, we will learn how to solve LeetCode's Number of Recent Calls Problem and will implement its solution in Java.

Number of Recent Calls Problem

You have a RecentCounter class that counts the number of recent requests within a certain time frame.
Implement the RecentCounter class:
  • RecentCounter() Initializes the counter with zero recent requests.
  • int ping(int t) Adds a new request at time t, where t represents some time in milliseconds and returns the number of requests that have happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t].
  • It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.

Example 1:
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]]
[null, 1, 2, 3, 3]

Practice this problem on LeetCode.

LeetCode - Number of Recent Calls Java Solution

Approach 1

Using Queue.

Add new Ping into the queue and remove all the times that don't come in the range t-3000 to t. Return size of the queue.

Java Program: 

class RecentCounter {
    Queue<Integer> queue;

    public RecentCounter() {
        queue = new LinkedList<Integer>();
    public int ping(int t) {
        int time = t - queue.peek();
        while(time > 3000) {
            time = t - queue.peek();
        return queue.size();

 * Your RecentCounter object will be instantiated and called as such:
 * RecentCounter obj = new RecentCounter();
 * int param_1 =;

Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.


Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala