PracHub
QuestionsLearningGuidesInterview Prep
|Home/Software Engineering Fundamentals/Anthropic

Debug Python LRU Cache-Key Construction

Last updated: Jul 14, 2026

Quick Overview

Debug a Python LRU memoization decorator whose cache keys depend incorrectly on argument spelling and keyword order. Build canonical signature-bound keys, freeze only supported nested values with type distinctions, preserve exception behavior, and explain safe thread synchronization.

  • hard
  • Anthropic
  • Software Engineering Fundamentals
  • Software Engineer

Debug Python LRU Cache-Key Construction

Company: Anthropic

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Onsite

# Debug Python LRU Cache-Key Construction The source reports a Python LRU-cache bug in key construction from `args` and `**kwargs` but does not provide the exact implementation. The decorator below is a practice reconstruction of that reported failure mode. ```python from collections import OrderedDict from functools import wraps def memoize_lru(maxsize=128): def decorate(function): cache = OrderedDict() @wraps(function) def wrapped(*args, **kwargs): key = args + tuple(kwargs.items()) if key in cache: cache.move_to_end(key) return cache[key] value = function(*args, **kwargs) cache[key] = value if len(cache) > maxsize: cache.popitem(last=False) return value return wrapped return decorate ``` Find the cache-key bugs and repair the decorator for ordinary Python signatures under this practice contract: - Calls that bind the same values to the same parameters, including defaults, share an entry regardless of positional versus keyword spelling or keyword insertion order. - The supported value domain is `None`, booleans, integers, floats, strings, bytes, and recursively nested tuples or frozensets of supported values. Other argument values raise `TypeError` before the wrapped function runs. - Type is part of the frozen representation, so `True` and `1` do not collide. - `*args` and `**kwargs` are supported. Keyword names are strings and are canonicalized in name order. - Exceptions from the wrapped function are not cached. `maxsize` is a positive integer. ### Clarifying Questions to Ask - Should positional and keyword spellings of the same bound call share an entry? - Which mutable or custom values, if any, are safe to cache? - Should argument types be distinguished when values compare equal? - Are exceptions cached, and is thread safety required? ### What a Strong Answer Covers - Minimal failures for keyword order, signature binding, defaults, and unhashable values. - Canonical binding with `inspect.signature`, including variadic parameters. - A collision-resistant frozen representation and explicit unsupported-value policy. - Correct LRU recency updates, eviction, exception behavior, and complexity. - The difference between map/recency locking and per-key duplicate-work suppression. ### Follow-up Questions 1. How would you add thread safety without holding a global lock during user code? 2. When is content-freezing a mutable argument semantically unsafe? 3. How would code or data version changes invalidate old entries?

Quick Answer: Debug a Python LRU memoization decorator whose cache keys depend incorrectly on argument spelling and keyword order. Build canonical signature-bound keys, freeze only supported nested values with type distinctions, preserve exception behavior, and explain safe thread synchronization.

Related Interview Questions

  • Debug Tokenization and Detokenization - Anthropic (hard)
  • Design a Parallel Image Processor - Anthropic (medium)
  • How do you review a design document? - Anthropic (hard)
  • Explain multithreading vs multiprocessing - Anthropic (medium)
|Home/Software Engineering Fundamentals/Anthropic

Debug Python LRU Cache-Key Construction

Anthropic logo
Anthropic
Jul 8, 2026, 12:00 AM
hardSoftware EngineerOnsiteSoftware Engineering Fundamentals
7
0

Debug Python LRU Cache-Key Construction

The source reports a Python LRU-cache bug in key construction from args and **kwargs but does not provide the exact implementation. The decorator below is a practice reconstruction of that reported failure mode.

from collections import OrderedDict
from functools import wraps

def memoize_lru(maxsize=128):
    def decorate(function):
        cache = OrderedDict()

        @wraps(function)
        def wrapped(*args, **kwargs):
            key = args + tuple(kwargs.items())
            if key in cache:
                cache.move_to_end(key)
                return cache[key]
            value = function(*args, **kwargs)
            cache[key] = value
            if len(cache) > maxsize:
                cache.popitem(last=False)
            return value
        return wrapped
    return decorate

Find the cache-key bugs and repair the decorator for ordinary Python signatures under this practice contract:

  • Calls that bind the same values to the same parameters, including defaults, share an entry regardless of positional versus keyword spelling or keyword insertion order.
  • The supported value domain is None , booleans, integers, floats, strings, bytes, and recursively nested tuples or frozensets of supported values. Other argument values raise TypeError before the wrapped function runs.
  • Type is part of the frozen representation, so True and 1 do not collide.
  • *args and **kwargs are supported. Keyword names are strings and are canonicalized in name order.
  • Exceptions from the wrapped function are not cached. maxsize is a positive integer.

Clarifying Questions to Ask Guidance

  • Should positional and keyword spellings of the same bound call share an entry?
  • Which mutable or custom values, if any, are safe to cache?
  • Should argument types be distinguished when values compare equal?
  • Are exceptions cached, and is thread safety required?

What a Strong Answer Covers Guidance

  • Minimal failures for keyword order, signature binding, defaults, and unhashable values.
  • Canonical binding with inspect.signature , including variadic parameters.
  • A collision-resistant frozen representation and explicit unsupported-value policy.
  • Correct LRU recency updates, eviction, exception behavior, and complexity.
  • The difference between map/recency locking and per-key duplicate-work suppression.

Follow-up Questions Guidance

  1. How would you add thread safety without holding a global lock during user code?
  2. When is content-freezing a mutable argument semantically unsafe?
  3. How would code or data version changes invalidate old entries?
Loading comments...

Browse More Questions

More Software Engineering Fundamentals•More Anthropic•More Software Engineer•Anthropic Software Engineer•Anthropic Software Engineering Fundamentals•Software Engineer Software Engineering Fundamentals

Write your answer

Your first approved answer each day earns 20 XP.

Sign in to write your answer.
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.