Query Result Cache With LRU Eviction and Table Invalidation
Last updated: November 29, 2026
Quick Overview
Design a cache that stores query results keyed by query text, where each result records the set of tables it read. Support get, put with a capacity limit and LRU eviction, and invalidate(table) that removes every cached result depending on that table, all in time proportional to the work done.
Snowflake
November 29, 20260
0
0 solved
Design a cache that stores query results keyed by query text, where each result records the set of tables it read. Support get, put with a capacity limit and LRU eviction, and invalidate(table) that removes every cached result depending on that table, all in time proportional to the work done.
Snowflake's result cache returns prior query results only while the underlying tables are unchanged, so a data structure question combining LRU with dependency invalidation fits its onsite loop. The interviewer checks that the reverse index stays consistent through eviction, overwrite and invalidation.
What the Interviewer Expects
- Uses an ordered map (or hash map plus doubly linked list) for O(1) LRU operations
- Maintains a reverse index from table to cached keys
- Keeps both indexes consistent on eviction, overwrite and invalidation
- Cleans up empty table entries to avoid leaks
- Analyzes invalidate as O(number of affected entries)
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you make this thread safe without one global lock?
- How would you replace explicit invalidation with table version numbers checked on read?
- How would you account for result size instead of entry count in the capacity?
- How would this work across many compute nodes sharing one cache?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
get(key) returns the result or None and marks it recently used. put(key, result, tables) inserts or replaces, then evicts least recently used entries over capacity. invalidate(table) removes all...
High-Level Design
Two structures: an OrderedDict from key to (result, tables) in recency order, and by_table mapping table to the set of keys that depend on it. Every removal path (eviction, overwrite, invalidati...