Skip to content

Instantly share code, notes, and snippets.

@monotykamary
Last active April 24, 2026 06:04
Show Gist options
  • Select an option

  • Save monotykamary/8a375ba594abdb2ca52d55090aeed135 to your computer and use it in GitHub Desktop.

Select an option

Save monotykamary/8a375ba594abdb2ca52d55090aeed135 to your computer and use it in GitHub Desktop.
LRU

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

  • LRUCache(capacity int) Initialize the cache with positive size capacity.
  • int get(int key) Return the value of the key if it exists, otherwise return -1.
  • void put(int key, value int) Update the value of the key if it exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity, evict the least recently used key.
  • Both get and put must run in O(1) average time complexity.
type LRUCache struct {
    // TODO: design your structs
}

func Constructor(capacity int) LRUCache {
    // TODO
}

func (this *LRUCache) Get(key int) int {
    // TODO: O(1)
    return -1
}

func (this *LRUCache) Put(key int, value int) {
    // TODO: O(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment