Token Bucket Rate Limiter
Function Name: rateLimiter
Description
Write a function 'rateLimiter' that simulates a token bucket rate limiter. The token bucket algorithm is used to control the amount of data that is transferred, which can help prevent denial of service attacks or network congestion. Your rate limiter must be stateful and should handle incrementing tokens in the bucket at a steady rate as well as decrementing them when a request is made.
Requirements
- The rate limiter must be initialized with two parameters: capacity and refillRate.
- Capacity represents the maximum number of tokens the bucket can hold.
- refillRate is the number of tokens added to the bucket every second.
- Implement a method 'attemptRequest' that takes the number of tokens requested as a parameter and returns a boolean - true if the request can be made, and false if there aren't enough tokens.
- Requests that cannot be fulfilled should not drain tokens.
- The rate limiter should handle requests occurring at different times and conform to the refill rate regardless of the request pattern.
Examples
let limiter = rateLimiter(10, 1); // 10 tokens capacity, 1 token refilled per secondlimiter.attemptRequest(3); // returns true if the bucket had 3 or more tokens, false otherwiselimiter.attemptRequest(8); // returns false if called immediately after since there aren't enough tokens left
Links
Merge Intervals
Thu Dec 26 2024
Write a function named 'mergeIntervals' that takes a list of intervals and merges all overlapping intervals.Each interval is represented as a pair of integers, where the first integer is the start time and the second is the end time.The function should return a list of merged intervals sorted by the start times.An interval [a,b] is considered to overlap with [c,d] if b >= c.
Flatten Nested Dictionary
Sat Dec 28 2024
In this challenge, you are to write a function named 'flatten_dictionary' that takes a dictionary with nested dictionaries as values and returns a new dictionary with flattened keys.The keys in the output dictionary should be a combination of the nested keys, separated by a period ('.').If an inner dictionary has an empty key, it should just be appended without the preceding dot.Values from nested dictionaries are to be included in the result as is, without further nesting.The input dictionary will only contain other dictionaries or scalar values (e.g., strings, integers).