Implement an LRU Cache using AI tools in an AI-assisted IDE; later add a validation method for keys.
Asked in the AI Coding stage. The interviewer kept asking for more features. Reported follow-up: add a validation method for keys.
Implement lru_cache_operations(capacity, operations). Each operation is ['put', key, value], ['get', key], or ['validate', key]. Return outputs for get and validate operations in order. A valid key is a non-empty string. Invalid keys must be rejected: put has no output, get returns -1, and validate returns false. A successful get marks the key as most recently used. When capacity is exceeded, evict the least recently used entry.
def lru_cache_operations(capacity, operations):