When AI Recommends

Leet.png

If by AI you were deceived,
Don't be dismal, don't be wild!
In the day of doubt, be mild:
Question the answer – then believe.

Code, AI can write.
Reasons, AI can fabricate.
Three solutions. Three blurbs. One scenario.
One question: what's your call?


🎯 Your Problem

Implement an LRU Cache supporting get(key) and put(key, value), both in time.

LeetCode: 146. LRU Cache


☝️ Solution 1: List + Linear Scan

🤖 AI Recommendation (Confidence: 78%): For most real-world use cases, the simplicity of this approach makes it a strong first choice. The logic is transparent and any team member can maintain it without needing to understand complex data structures.

Python3
class LRUCache:
    def __init__(self, capacity):
        self.cap = capacity
        self.cache = []  # [(key, value), ...]

    def get(self, key):
        for i, (k, v) in enumerate(self.cache):
            if k == key:
                self.cache.pop(i)
                self.cache.append((key, v))
                return v
        return -1

    def put(self, key, value):
        for i, (k, _) in enumerate(self.cache):
            if k == key:
                self.cache.pop(i)
                break
        if len(self.cache) >= self.cap:
            self.cache.pop(0)
        self.cache.append((key, value))

✌️ Solution 2: HashMap + Doubly Linked List

🤖 AI Recommendation (Confidence: 96%): This is the textbook optimal solution. time complexity is critical in high-concurrency environments. It also demonstrates strong data structure fundamentals in interviews. Highly recommended.

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

class LRUCache:
    def __init__(self, capacity):
        self.cap = capacity
        self.map = {}
        self.head, self.tail = Node(), Node()
        self.head.next = self.tail
        self.tail.prev = self.head

    def _remove(self, node):
        node.prev.next = node.next
        node.next.prev = node.prev

    def _insert_tail(self, node):
        node.prev = self.tail.prev
        node.next = self.tail
        self.tail.prev.next = node
        self.tail.prev = node

    def get(self, key):
        if key not in self.map:
            return -1
        node = self.map[key]
        self._remove(node)
        self._insert_tail(node)
        return node.val

    def put(self, key, value):
        if key in self.map:
            self._remove(self.map[key])
        node = Node(key, value)
        self.map[key] = node
        self._insert_tail(node)
        if len(self.map) > self.cap:
            lru = self.head.next
            self._remove(lru)
            del self.map[lru.key]

👌 Solution 3: OrderedDict

🤖 AI Recommendation (Confidence: 83%): Leveraging built-in language features is what experienced engineers do. OrderedDict is internally backed by a doubly linked list — same performance as Solution 2, but 80% less code and far easier to maintain.

Python3
from collections import OrderedDict

class LRUCache:
    def __init__(self, capacity):
        self.cap = capacity
        self.cache = OrderedDict()

    def get(self, key):
        if key not in self.cache:
            return -1
        self.cache.move_to_end(key)
        return self.cache[key]

    def put(self, key, value):
        if key in self.cache:
            self.cache.move_to_end(key)
        self.cache[key] = value
        if len(self.cache) > self.cap:
            self.cache.popitem(last=False)

🎭 Challenge 1: Spot the Spin

Read the three AI blurbs. What did you read between the lines?

Share your interpretation of each recommendation in the comments, and vote for the one you find least convincing.


🫡 Challenge 2: I Choose You

You're responsible for a user session cache service at a content platform.

  • QPS: ~150,000
  • Cache capacity: 100,000 entries
  • Team of 6, including 2 new grads
  • Long-term maintenance expected, no rewrite planned

Write your choice (Solution 1 / 2 / 3) and explain why you're rejecting the other two.

Bonus question (optional): This is the final round of your Staff Engineer interview and the interviewer asks you to implement an LRU Cache. Would your answer match your production choice above — or not? Why?


📝 Participation Rules

📆 Duration: From now until 14 June, 2026

🎤 How to participate: Share your answer in the comments using the event tag: #SpotTheSpin or #IChooseYou

🎁 Rewards: The top 3 most-liked comments and comments marked as pinned will each receive 300 LeetCoins

🧑‍🤝‍🧑 Friendly Reminder: Keep discussions respectful and constructive – AI failures are shared for learning

Note:

  • To ensure fairness, submissions must follow the posting guidelines.
  • Posts that are off-topic or do not meet the requirements will be disqualified, and rewards will go to the next eligible participant.

🚀 See you in the comments!

👇 Tag your story and jump in: #SpotTheSpin or #IChooseYou


🔗 Series

Would you trust AI code as is?


💬 One Last Meow

Three blurbs, three different ways to mislead — one hides behind "textbook optimal" to blur the line between interviews and production; one makes you feel underqualified for not using it; one buries the performance concern under "simplicity."
In this scenario, the planned rewrite, language constraints, and data scale all matter. None of them should be ignored.
AI can generate solutions. The judgment is always yours. Real engineering ability is knowing how to direct AI for your specific context — not following the confidence score.

Comments (25)