Optimal Python Solution for LRU Cache (Interview Script)

2025-09-27

The Logic

  • The bottleneck is supporting both fast lookups and fast eviction ordering.
  • A hash map alone gives O(1) access but cannot track recency.
  • A doubly linked list maintains usage order while the hash map provides direct access.

Implementation / Diagram

Key Invariant

The most recently used item is always at the head of the list, and the least recently used item is always at the tail.

class Node:
    def __init__(self, key, val):
        self.key = key
        self.val = val
        self.prev = None
        self.next = None


class LRUCache:
    def __init__(self, capacity: int):
        self.cap = capacity
        self.cache = {}

        self.left = Node(0, 0)
        self.right = Node(0, 0)
        self.left.next = self.right
        self.right.prev = self.left

    def remove(self, node):
        prev, nxt = node.prev, node.next
        prev.next = nxt
        nxt.prev = prev

    def insert(self, node):
        prev = self.right.prev
        prev.next = node
        node.prev = prev
        node.next = self.right
        self.right.prev = node

    def get(self, key: int) -> int:
        if key in self.cache:
            self.remove(self.cache[key])
            self.insert(self.cache[key])
            return self.cache[key].val
        return -1

    def put(self, key: int, value: int) -> None:
        if key in self.cache:
            self.remove(self.cache[key])

        node = Node(key, value)
        self.cache[key] = node
        self.insert(node)

        if len(self.cache) > self.cap:
            lru = self.left.next
            self.remove(lru)
            del self.cache[lru.key]
The honest play

You've read the playbook. Now make sure you pass the live OA.

Knowing the patterns isn't the same as solving them under a timer with a proctor watching. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem on screen and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Hedge your live OA
Invisible during screen share
Get it