mirror of
https://github.com/harivansh-afk/veet-code.git
synced 2026-04-17 13:05:10 +00:00
problems
This commit is contained in:
parent
76062021e9
commit
c548626d7f
13 changed files with 570 additions and 1 deletions
47
problems/hard/lru-cache/solution.py
Normal file
47
problems/hard/lru-cache/solution.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
"""
|
||||
LRU Cache
|
||||
|
||||
You're building a caching layer for a database-heavy web application.
|
||||
To reduce load, implement a Least Recently Used (LRU) cache that
|
||||
automatically evicts the oldest unused entries when capacity is reached.
|
||||
|
||||
Example 1:
|
||||
cache = LRUCache(capacity=2)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
cache.get("a") # Returns 1 (a is now most recently used)
|
||||
cache.put("c", 3) # Evicts "b" (least recently used)
|
||||
cache.get("b") # Returns -1 (not found)
|
||||
cache.get("c") # Returns 3
|
||||
|
||||
Example 2:
|
||||
cache = LRUCache(capacity=1)
|
||||
cache.put("x", 10)
|
||||
cache.put("y", 20) # Evicts "x"
|
||||
cache.get("x") # Returns -1
|
||||
cache.get("y") # Returns 20
|
||||
|
||||
Constraints:
|
||||
- capacity >= 1
|
||||
- Keys are strings, values are integers
|
||||
- get() returns -1 if key not found
|
||||
- put() updates value if key exists (and marks as recently used)
|
||||
- Both get() and put() should be O(1) average time
|
||||
"""
|
||||
|
||||
|
||||
class LRUCache:
|
||||
"""Least Recently Used cache with O(1) operations."""
|
||||
|
||||
def __init__(self, capacity: int):
|
||||
"""Initialize cache with given capacity."""
|
||||
pass # Your implementation here
|
||||
|
||||
def get(self, key: str) -> int:
|
||||
"""Return value for key, or -1 if not found."""
|
||||
pass # Your implementation here
|
||||
|
||||
def put(self, key: str, value: int) -> None:
|
||||
"""Insert or update key-value pair, evicting LRU if needed."""
|
||||
pass # Your implementation here
|
||||
|
||||
86
problems/hard/lru-cache/tests.py
Normal file
86
problems/hard/lru-cache/tests.py
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
"""Tests for lru-cache."""
|
||||
import pytest
|
||||
from solution import LRUCache
|
||||
|
||||
|
||||
class TestBasicCases:
|
||||
"""Test basic functionality with typical inputs."""
|
||||
|
||||
def test_basic_put_get(self):
|
||||
"""Test basic put and get operations."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
assert cache.get("a") == 1
|
||||
assert cache.get("b") == 2
|
||||
|
||||
def test_eviction_lru(self):
|
||||
"""Test that least recently used is evicted."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
cache.get("a") # a is now most recent
|
||||
cache.put("c", 3) # b should be evicted
|
||||
assert cache.get("b") == -1
|
||||
assert cache.get("a") == 1
|
||||
assert cache.get("c") == 3
|
||||
|
||||
def test_update_existing_key(self):
|
||||
"""Test updating an existing key."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("a", 10)
|
||||
assert cache.get("a") == 10
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
"""Test edge cases and boundary conditions."""
|
||||
|
||||
def test_capacity_one(self):
|
||||
"""Test with capacity of 1."""
|
||||
cache = LRUCache(1)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
assert cache.get("a") == -1
|
||||
assert cache.get("b") == 2
|
||||
|
||||
def test_get_nonexistent(self):
|
||||
"""Test getting a key that doesn't exist."""
|
||||
cache = LRUCache(2)
|
||||
assert cache.get("missing") == -1
|
||||
|
||||
def test_put_updates_recency(self):
|
||||
"""Test that put on existing key updates recency."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
cache.put("a", 10) # a is now most recent
|
||||
cache.put("c", 3) # b should be evicted
|
||||
assert cache.get("b") == -1
|
||||
assert cache.get("a") == 10
|
||||
|
||||
def test_many_operations(self):
|
||||
"""Test a sequence of many operations."""
|
||||
cache = LRUCache(3)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
cache.put("c", 3)
|
||||
cache.get("a")
|
||||
cache.put("d", 4) # evicts b
|
||||
cache.put("e", 5) # evicts c
|
||||
assert cache.get("a") == 1
|
||||
assert cache.get("b") == -1
|
||||
assert cache.get("c") == -1
|
||||
assert cache.get("d") == 4
|
||||
assert cache.get("e") == 5
|
||||
|
||||
def test_get_updates_recency(self):
|
||||
"""Test that get updates the access order."""
|
||||
cache = LRUCache(2)
|
||||
cache.put("a", 1)
|
||||
cache.put("b", 2)
|
||||
cache.get("a") # touch a
|
||||
cache.put("c", 3) # evicts b, not a
|
||||
assert cache.get("a") == 1
|
||||
assert cache.get("b") == -1
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue