|
| 1 | +{ |
| 2 | + "problem_name": "lru_cache", |
| 3 | + "class_name": "LRUCache", |
| 4 | + "method_name": "lru_cache", |
| 5 | + "problem_number": "146", |
| 6 | + "problem_title": "LRU Cache", |
| 7 | + "difficulty": "Medium", |
| 8 | + "topics": "Hash Table, Linked List, Design, Doubly-Linked List", |
| 9 | + "tags": ["grind-75", "top-interview"], |
| 10 | + "problem_description": "Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.\n\nImplement the LRUCache class:\n\n- LRUCache(int capacity) Initialize the LRU cache with positive size capacity.\n- int get(int key) Return the value of the key if the key exists, otherwise return -1.\n- void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.\n\nThe functions get and put must each run in O(1) average time complexity.", |
| 11 | + "examples": [ |
| 12 | + { |
| 13 | + "input": "[\"LRUCache\", \"put\", \"put\", \"get\", \"put\", \"get\", \"put\", \"get\", \"get\", \"get\"]\n[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]", |
| 14 | + "output": "[null, null, null, 1, null, -1, null, -1, 3, 4]" |
| 15 | + } |
| 16 | + ], |
| 17 | + "constraints": "- 1 <= capacity <= 3000\n- 0 <= key <= 10^4\n- 0 <= value <= 10^5\n- At most 2 * 10^5 calls will be made to get and put.", |
| 18 | + "parameters": "capacity: int", |
| 19 | + "return_type": "None", |
| 20 | + "dummy_return": "None", |
| 21 | + "imports": "", |
| 22 | + "test_cases": [ |
| 23 | + { |
| 24 | + "args": [ |
| 25 | + ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"], |
| 26 | + [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] |
| 27 | + ], |
| 28 | + "expected": [null, null, null, 1, null, -1, null, -1, 3, 4] |
| 29 | + } |
| 30 | + ], |
| 31 | + "param_names": "operations, inputs, expected", |
| 32 | + "param_names_with_types": "operations: list[str], inputs: list[list[int]], expected: list[int | None]", |
| 33 | + "input_description": "operations={operations}, inputs={inputs}", |
| 34 | + "input_params": "operations, inputs", |
| 35 | + "expected_param": "expected", |
| 36 | + "method_args": "operations, inputs", |
| 37 | + "test_setup": "cache = None\nresult = []\nfor i, op in enumerate(operations):\n if op == \"LRUCache\":\n cache = LRUCache(inputs[i][0])\n result.append(None)\n elif op == \"get\":\n result.append(cache.get(inputs[i][0]))\n elif op == \"put\":\n cache.put(inputs[i][0], inputs[i][1])\n result.append(None)", |
| 38 | + "test_logging": "logger.info(f\"Testing LRU Cache with operations: {operations}\")\nlogger.info(f\"Inputs: {inputs}\")\nlogger.info(f\"Expected: {expected}\")\nlogger.info(f\"Result: {result}\")", |
| 39 | + "assertion_code": "assert result == expected", |
| 40 | + "test_input_setup": "operations = [\"LRUCache\", \"put\", \"put\", \"get\", \"put\", \"get\", \"put\", \"get\", \"get\", \"get\"]\ninputs = [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]", |
| 41 | + "expected_output_setup": "expected = [None, None, None, 1, None, -1, None, -1, 3, 4]" |
| 42 | +} |
0 commit comments