Description
Company: Netflix
Task Description
Implement a weighted cache system with the following features:
- Supports key-value pairs, with an initial total weight limit.
- Provide
get(key)andput(key, value, weight)methods. - When adding a new key-value pair causes the total weight to exceed the limit, remove the key-value pair with the largest weight.
Requirements
- Ensure the
getandputmethods have a time complexity of O(log N). get: If the key exists, return the corresponding value; otherwise returnNone.put: Accepts a key, value, and weight, and stores them in the cache.- Use a
TreeMapor similar data structure for implementation. - Provide a complete code solution and 5 test cases.
Example
Input put a 1 2 put b 2 3 get a
Discussion (0)
All comments are anonymous. Your identity is not shared.
Loading comments...
Loading editor…
OUTPUTLast run results appear here.
No output yet. Click "Run Code" to see results.