Problem Statement:
In Amazon's web infrastructure, the system logs n API requests per second, recording each request's response time in the array responseTimes, where each element denotes the time taken to respond to a single request.
To prevent system overload, a throttling mechanism is applied with the following behaviour:
In each step, select the request with the minimum response time. This request is prioritised for further analysis.
Remove the selected request and its immediate neighbours in the array (based on original indices) from the logs permanently.
This process continues until all requests are removed.
The task is to calculate the total cumulative response time of all selected minimum requests.
Problem Statement:
One of the competitions launched on Amazon gaming platform has come up with a unique ranking system for the participants. It is based on their relative expertise levels and achievement points.
There are p participants where the ith participant has an expertise level of expertise[i] and achievement points of points[i]. The ranking points of the ith participant is defined as the maximum possible sum of points of at most count participants with an expertise level strictly less than expertise[i].
Example:
If expertise = [1, 2, 3, 4], points = [40, 30, 20, 10], and count = 2:
The maximum possible sums of points of any two participants with expertise levels less than the participant in consideration are [0, 40, 70, 70].
Given expertise, points, and count, find each participant's ranking points.
Problem1 - API Request Throttling Solution
Topics -:
Greedy Algorithms
Array Sorting
Overview
We need to repeatedly find the smallest available response time, add it to our total, and remove it along with its original adjacent neighbors. Instead of modifying the array directly, which is slow, we can sort the elements by their values while keeping track of their original indices. By iterating through this sorted list and using a boolean array to skip already removed elements, we efficiently simulate the entire process.
Approach
1. Sorting with Indices - To always process the minimum available response time efficiently, we pair each response time with its original array index. We then sort this list of pairs in ascending order based on the response time. This allows us to process the requests greedily without repeatedly scanning the array.
2. Simulating Removals- We create a boolean array, removed, initially set to false for all indices. We iterate through our sorted list of requests. If the current request's original index is already marked as removed, we simply skip it. If it is not removed, we add its response time to our cumulative total and mark its index, as well as index - 1 and index + 1, as true in our removed array.
Code implementation
Time Complexity
Problem2 -
Gaming Platform Ranking System Solution
Topics Involved / Prerequisites
Overview - : We must process participants in ascending order of their expertise so that we only pull scores from people with strictly lower expertise. By maintaining a min-heap of the highest points seen so far, we can instantly access the top count scores to sum up for the current participant. To handle participants with the exact same expertise, we evaluate all ties together before adding their points to the heap for future players.
Approach
1. Sorting by Expertise We bundle each participant's expertise, points, and original index together, and then sort the list in ascending order based on expertise. This guarantees that as we iterate forward, we only ever look at players with lower expertise levels.
2. Maintaining the Top Points (The Min-Heap) We use a Min-Heap to track the highest count points we've seen so far. If our heap exceeds count, the smallest point value will be at the top, making it O(\log C) to remove and keep our running sum accurate. Crucially, players with the exact same expertise level do not count toward each other. We use a nested loop to assign answers to all tied players before we push any of their points into the heap.
Time Complexity