Data Structures and Algorithms Questions

7 / 13

Number of Recent Calls

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.
  • ping(t: int) Adds a new request at a 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:

Input: [1, 100, 3001, 3002]
Output: [1, 2, 3, 3]

Explanation:

recentCounter = RecentCounter();

recentCounter.ping(1);     // requests = [1], range is [-2999,1], return 1

recentCounter.ping(100);   // requests = [1, 100], range is [-2900,100], return 2

recentCounter.ping(3001);  // requests = [1, 100, 3001], range is [1,3001], return 3

recentCounter.ping(3002);  // requests = [1, 100, 3001, 3002], range is [2,3002], return 3

Constraints:

  • 1 <= t <= 10^9(10 to the power 9)
  • Each test case will call ping with strictly increasing values of t.
  • At most 10^4 calls will be made to ping.
INSTRUCTIONS

  1. Write your code inside a class named RecentCounter
  2. There are no partial marks for the question.
  3. Your function must return the output, it should not print the output.
  4. To execute a block on the right-side coding panel, please press 'shift'+ 'enter' inside the block.
  5. Your code should work for all permitted possible values(check Constraints) of t

Copy the below code and paste it in the right side coding panel, and complete the ping method in RecentCounter class

class RecentCounter:

   def __init__(self):
       pass

   def ping(self, t: int) -> int:
       # your code



def recent_counter_main(calls: int) -> list:
    output_list = []
    rc = RecentCounter()
    for call in calls:
        output_list.append(rc.ping(call))
    return output_list
See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...