Find Minimum Time Required
Updated
Problem statement
Some developers at Amazon are building a prototype for a simple rate-limiting algorithm. There are n requests to be processed by the server represented by a string requests where the ith character represents the region of the ith client. Each request takes 1 unit of time to process. There must be a minimum time gap of minGap units between any two requests from the same region: at least minGap time units must pass between them. For example, if one request is processed in time unit 1 and minGap = 2, the next request from the same region can be processed in time unit 4 at the earliest.
The requests can be sent in any order and there can be gaps in transmission for testing purposes. Find the minimum amount of time required to process all the requests such that no request is denied.
Function
findMinimumTimeRequired(requests: String, minGap: int) → int Complete the function findMinimumTimeRequired in the editor.
findMinimumTimeRequired has the following parameters:
String requests: a string where each character represents the region of a clientint minGap: the minimum time gap required between two requests from the same region
Returns int: the minimum number of time units needed to process all requests
Examples
Example 1
requests = "abacadaeafag"minGap = 2return = 16Region a has 6 requests, so there are 5 gaps of at least 2 units between them: at least 6 + 5 * 2 = 16 units. The schedule "ab_ad_afgae_ac_a" (where _ is an idle unit) reaches 16: the a requests are in units 1, 4, 7, 10, 13 and 16, and the other 6 requests and 4 idle units fill the gaps.
Example 2
requests = "aaabbb"minGap = 0return = 6With minGap = 0 the requests can be processed in any order without any gaps, so the 6 requests take 6 units.
Example 3
requests = "aaabbb"minGap = 2return = 8The requests can be sent in the order "ab_ab_ab", where _ means no request is sent in that unit. Two requests from the same region always have exactly minGap = 2 units between them. The total time is 8 units.
Constraints
1 <= n <= 10^5, wherenis the length ofrequestsrequestsconsists of lowercase English letters only; each letter is one region.0 <= minGap <= 10^4
More Amazon interview questions
- Lexicographically Largest IdentifierOA · Seen Nov 2026
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fill One Exact-Length Binary WindowOA · Seen Nov 2026
Similar questions at other companies
- Maximize Palindromic WordsSquarepoint Capital · OASquarepoint Capital interview questions
- Viral Content BalancerTikTok · OATikTok interview questions
- Merging PalindromesOld Mission · OA
- Largest Palindromic Number from Available DigitsMarshall Wace · OAMarshall Wace interview questions
- Minimum Operations to Reduce a Transaction LedgerVisa · OAVisa interview questions
- Maximum String BeautyBank of America · OABank of America interview questions
- Minimum Substrings Without Repeating CharactersMicrosoft · OAMicrosoft interview questions
- Find Length of Longest Good SubsequenceWalmart · OAWalmart interview questions
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
- Rearrange a String So No Neighbors MatchThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions