DeepInterview

Weighted Cache: Implementation of Get and Put Methods

Coding2025/12

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) and put(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 get and put methods have a time complexity of O(log N).
  • get: If the key exists, return the corresponding value; otherwise return None.
  • put: Accepts a key, value, and weight, and stores them in the cache.
  • Use a TreeMap or 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.