- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of requests where each list contains elements like [uid, time_sec] (uid is the user id and time_sec is the timestamp). This indicates the user whose id is uid has requested to a website at timestamp time_sec. We also have two values u and g where u denotes maximum number of requests that are allowed in any < 60 second frame for a given uid and g is the maximum number of requests that are allowed in any < 60 second frame globally. Now if we want to process each request one by one and rate limit them. And if there are requests at the same time by multiple users, the requests with lower uid will be processed first, otherwise that request will be dropped. We have to find the total number of requests that will be processed successfully.

So, if the input is like requests = [[0, 1],[1, 2],[1,3]] u = 1 g = 5, then the output will be 2, as user 0 and 1 can send at time 1 and 2, but the second request from user 1 will not be processed as one user can send at most 1 request in 60 secs frame.

To solve this, we will follow these steps −

- last := an empty map
- total := empty double ended queue
- windowtime := 60
- sort requests based on time, if they are same then sort based on uid
- amount := 0
- for each r in requests, do
- [uid, time] := r
- while size of total > 0 and total[0] + windowtime <= time, do
- delete left item of total

- while size of last[uid] > 0 and last[uid, 0] + windowtime <= time, do
- delete left item from last[uid]

- if size of total < g and size of last[uid] < u, then
- insert time at the end of last[uid]
- insert time at the end of total
- amount := amount + 1

- return amount

Let us see the following implementation to get better understanding −

from collections import defaultdict, deque class Solution: def solve(self, requests, u, g): last = defaultdict(deque) total = deque() windowtime = 60 requests.sort(key=lambda x: [x[1], x[0]]) amount = 0 for r in requests: uid, time = r while len(total) > 0 and total[0] + windowtime <= time: total.popleft() while len(last[uid]) > 0 and last[uid][0] + windowtime <= time: last[uid].popleft() if len(total) < g and len(last[uid]) < u: last[uid].append(time) total.append(time) amount += 1 return amount ob = Solution() requests = [[0, 1],[1, 2],[1,3]] u = 1 g = 5 print(ob.solve(requests, u, g))

[[0, 1],[1, 2],[1,3]], 1, 5

2

- Related Questions & Answers
- Program to find number of tasks can be finished with given conditions in Python
- Program to count number of switches that will be on after flipping by n persons in python
- Check if the given array can be reduced to zeros with the given operation performed given number of times in Python
- Python program to check if the given number is Happy Number
- Program to count number of unique paths that includes given edges in Python
- Python program to check if the given number is a Disarium Number
- Program to check whether given number is Narcissistic number or not in Python
- Program to find shortest sublist so after sorting that entire list will be sorted in Python
- Program to find length of longest word that can be formed from given letters in python
- Python program to check if a given string is number Palindrome
- Program to find number of subsequences that satisfy the given sum condition using Python
- How to check if a given number is a Fibonacci number in Python Program ?
- Find maximum number that can be formed using digits of a given number in C++
- Find the largest number that can be formed with the given digits in C++
- Program to find the length of the longest, that can be made using given letters in Python

Advertisements