Thread Safe LRU Cache For Rendered Search Snippets

Last updated: November 29, 2026

Quick Overview

Implement an in-memory LRU cache mapping a (query, document) key to a rendered snippet, safe for concurrent access from many request threads, with O(1) get and put and hit/miss counters.

Yandex
Software Engineering Fundamentals
Software Engineer
Yandex
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


Implement an in-memory LRU cache mapping a (query, document) key to a rendered snippet, safe for concurrent access from many request threads, with O(1) get and put and hit/miss counters.

Snippet generation in Yandex Search is expensive, and front-end serving processes cache recent snippets under heavy concurrency. The interviewer probes data structure choice, locking, and whether you can reason about contention and correctness rather than just LRU mechanics.

What the Interviewer Expects
  • Combine a hash map with a doubly linked list (or OrderedDict) for O(1) recency updates
  • Guard compound operations with a lock, including get, since it mutates order
  • Evict exactly one least recently used entry when over capacity
  • Discuss lock contention and sharding the cache
  • Mention TTL or invalidation when the index updates
Key Topics to Cover
LRU
hash map plus linked list
mutex
concurrency
cache sharding
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • The single lock becomes a bottleneck at 32 threads; how do you shard it and what do you lose?
  • How would you prevent a thundering herd when a hot key expires?
  • How would this look in C++ with std::list and std::unordered_map, and what about iterator invalidation?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements

get(key) returns the value or None and marks the key most recently used. put(key, value) inserts or updates and evicts the LRU entry when size exceeds capacity. All operations are O(1) and lineari...

High-Level Design

An OrderedDict is a hash map whose entries are also in a doubly linked list, so move_to_end and popitem(last=False) are O(1). Because a get changes recency, it is a write; every public method ...


Submit Your Answer
Markdown supported

Related Questions