'Rate-limiting throttling request data structure problem

I was giving an interview and the interviewer asked me about a Data structure problem.
Problem: The server receives the requests and it has some limitations.

 4 requests in 1 second
10 requests in 3 seconds
20 requests in 10 seconds
60 requests in 30 seconds

And after 1 minute the server will get restarted. So we count seconds from zero. Max 4 requests can get accepted per second.

Example:

int[] arr = new int[] 
{
    1,1,1,1,1,1,      // Here first four request will get accepted and last two will not
    2,2,2,2,2,2,2,    // Here first four request will get accepted and last three will not
    3,3,3,3,3,        // Here first two request will get accepted and last three will not
    4,4,              // Here all request will get accepted
    5,5,5,5,5,5,      // Here first four request will get accepted and last two will not
    7,7,7,7,7,7,      // Here first four request will get accepted and last two will not
    10,10,10,10,      // Here all request will get rejected
    13,13,13,13,13,13 // Here first four request will get accepted and last two will not
};

Here number means second when the request hit the server, So in first second we received 6 request (1,1,1,1,1,1) in second second we received 7 request(2,2,2,2,2,2,2,) and continue....

I was asked to count how many requests got rejected on which second.



Solution 1:[1]

static int GetMax(int currentSecond, int acceptedRequest)
{
    int freeSlot = 0;

    if (currentSecond > 0 && currentSecond <= 3)
    {
        if (acceptedRequest < 10)
        {
            freeSlot = 10 - acceptedRequest;
        }
    }
    else if (currentSecond > 3 && currentSecond <= 10)
    {
        if (acceptedRequest < 20)
        {
            freeSlot = 20 - acceptedRequest;
        }
    }
    else if (currentSecond > 10)
    {
        if (acceptedRequest < 60)
        {
            freeSlot = 60 - acceptedRequest;
        }
    }

    return freeSlot;
}

static void Main(string[] args)
{
    int[] arr = new int[] {
        1,1,1,1,1,1,
        2,2,2,2,2,2,2,
        3,3,3,3,3,
        4,4,
        5,5,5,5,5,5,
        7,7,7,7,7,7,
        10,10,10,10,
        13,13,13,13,13,13};

    Dictionary<int, int> dict = new Dictionary<int, int>();

    foreach (var item in arr)
    {
        if (dict.ContainsKey(item))
        {
            dict[item] = dict[item] + 1;
        }
        else
        {
            dict.Add(item, 1);
        }
    }

    int rejectedReq = 0;
    int acceptedRequest = 0;
    foreach (var item in dict)
    {
        int freeSlot = GetMax(item.Key, acceptedRequest);

        if (freeSlot > 4)
        {
            freeSlot = 4;
        }

        if (item.Value > freeSlot)
        {
            acceptedRequest += freeSlot;
        }
        else
        {
            acceptedRequest += item.Value;
        }

        if (freeSlot < item.Value)
        {
            rejectedReq += item.Value - freeSlot;
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1