Get Minimum Time for DNS Resolution
Updated
Problem statement
A browser keeps a small DNS cache. Reading a web address from the cache is faster than asking a DNS server. You will simulate the cache and find how long each request takes.
The cache holds at most cache_size addresses. It starts empty. You get the requests in order. For each request:
- If the address is in the cache, the request takes
cache_time. The address then counts as the most recently used one. - If it is not in the cache, the request takes
server_time. The address is added to the cache as the most recently used one. If the cache is now over its size, the address that was used longest ago is removed.
Return the time each request takes, in the same order as urls.
Complete the function getMinTime in the editor.
getMinTime has the following parameters:
int cache_size: the most addresses the cache can holdint cache_time: the time to read an address from the cacheint server_time: the time to get an address from the DNS serverString[] urls: the addresses requested, in order
The function returns an int[]: the time of each request.
Function
getMinTime(cache_size: int, cache_time: int, server_time: int, urls: String[]) → int[]Examples
Example 1
cache_size = 2cache_time = 3server_time = 5urls = ["www.google.com", "www.yahoo.com", "www.yahoo.com", "www.google.com", "www.yahoo.com", "www.yahoo.com", "www.coursera.com", "www.coursera.com"]return = [5, 5, 3, 3, 3, 3, 5, 3]The cache holds 2 addresses. Hits cost 3 and misses cost 5.
www.google.com: not in the cache, so 5. The cache is [google].www.yahoo.com: not in the cache, so 5. The cache is [google, yahoo].www.yahoo.com: in the cache, so 3. The cache is [google, yahoo].www.google.com: in the cache, so 3. Google is now the most recent: [yahoo, google].www.yahoo.com: in the cache, so 3. The cache is [google, yahoo].www.yahoo.com: in the cache, so 3.www.coursera.com: not in the cache, so 5. The cache is full, so google, used longest ago, is removed: [yahoo, coursera].www.coursera.com: in the cache, so 3.
The answer is [5, 5, 3, 3, 3, 3, 5, 3].
Constraints
1 ≤ n ≤ 105, wherenis the length ofurls1 ≤ cache_size ≤ 1001 ≤ cache_time, server_time ≤ 1091 ≤ urls[i].length ≤ 20
More Walleye Capital interview questions
Similar questions at other companies
- Per-User Sliding-Window Rate LimiterPayPal · OAPayPal interview questions
- Get Minimum Time for DNS Resolution (for Data Science Developer)IBM · OAIBM interview questions
- Incident MonitorStripe · OAStripe interview questions
- Named Async Task SchedulerAirwallex · OAAirwallex interview questions
- Per-Client Sliding-Window Rate LimiterGeneral Motors · OA
- Key-Value Locks with Waiting QueuesKlaviyo · OAKlaviyo interview questions
- Truck Position SubscriberOptiver · OAOptiver interview questions
- Capital Gains Tax from TradesArcesium · OAArcesium interview questions
- Durable Work Queue OperationsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Dynamic Least-Loaded Job DispatcherCresta · PHONE SCREENCresta interview questions