969. Number of Recent Calls
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to implement a RecentCounter class that counts recent requests. For each ping(t) call, we need to add the timestamp t and then count how many requests have occurred in the inclusive time window [t - 3000, t]. A crucial piece of information is that the timestamps t in consecutive calls to ping are always strictly increasing.
Solution
Number of Recent Calls in Python
class RecentCounter:
def __init__(self):
# Initialize a deque (double-ended queue) to store request timestamps.
# Deques provide O(1) time complexity for appends and pops from both ends.
self.requests = collections.deque()
def ping(self, t: int) -> int:
# Add the new request timestamp to the right side of the deque.
self.requests.append(t)
# The valid time window starts at t - 3000.
window_start = t - 3000
# Remove timestamps from the left side of the deque that are older
# than the start of the window.
# self.requests[0] is the leftmost element.
while self.requests and self.requests[0] < window_start:
self.requests.popleft()
# The length of the deque is the number of recent requests.
return len(self.requests)
# Your RecentCounter object will be instantiated and called as such:
# obj = RecentCounter()
# param_1 = obj.ping(t)
Number of Recent Calls in Java
class RecentCounter {
private Queue<Integer> requests;
public RecentCounter() {
// Initialize the queue. LinkedList is a common choice for a Queue implementation.
requests = new LinkedList<>();
}
public int ping(int t) {
// Add the new request timestamp to the end of the queue.
requests.add(t);
// The valid time window starts at t - 3000.
int windowStart = t - 3000;
// Remove requests from the front of the queue that fall outside the window.
// peek() retrieves but does not remove the head of the queue.
// poll() retrieves and removes the head of the queue.
while (!requests.isEmpty() && requests.peek() < windowStart) {
requests.poll();
}
// The current size of the queue represents the number of requests
// within the [t - 3000, t] time frame.
return requests.size();
}
}
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/
Number of Recent Calls in C++
class RecentCounter {
private:
std::queue<int> requests;
public:
RecentCounter() {
// The queue is automatically initialized by its default constructor.
}
int ping(int t) {
// Add the new request timestamp to the queue.
requests.push(t);
// The valid time window starts at t - 3000.
int window_start = t - 3000;
// Remove all requests from the front of the queue that are
// older than the start of the window.
while (!requests.empty() && requests.front() < window_start) {
requests.pop();
}
// The size of the queue is now the number of recent requests.
return requests.size();
}
};
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter* obj = new RecentCounter();
* int param_1 = obj->ping(t);
*/
Number of Recent Calls in TypeScript
class RecentCounter {
private requests: number[];
constructor() {
// Initialize an array to store request timestamps.
// It will be used as a queue.
this.requests = [];
}
ping(t: number): number {
// Add the new request timestamp to the end of the array (enqueue).
this.requests.push(t);
// Calculate the start of the 3000ms time window.
const windowStart = t - 3000;
// Remove timestamps from the front of the array (dequeue) that are
// older than the window's start time.
// The first element is at index 0.
while (this.requests.length > 0 && this.requests[0] < windowStart) {
this.requests.shift(); // This is an O(n) operation for arrays, but acceptable for the problem's constraints.
}
// The length of the array is the number of requests in the window.
return this.requests.length;
}
}
/**
* Your RecentCounter object will be instantiated and called as such:
* var obj = new RecentCounter()
* var param_1 = obj.ping(t)
*/
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.