Simulate a Set-Associative LRU Cache Over a Memory Trace
Last updated: November 29, 2026
Quick Overview
Given cache size, line size and associativity, simulate a write-back, write-allocate cache with LRU replacement over a trace of read and write addresses, and report hits, misses and dirty writebacks.
Intel
November 29, 20260
0
0 solved
Given cache size, line size and associativity, simulate a write-back, write-allocate cache with LRU replacement over a trace of read and write addresses, and report hits, misses and dirty writebacks.
Cache hierarchy behavior is central to Intel CPU architecture, performance modeling and tools like VTune. The interviewer checks that you can decompose an address into offset, index and tag, implement LRU per set efficiently, and reason about write policy.
What the Interviewer Expects
- Derives the number of sets from size / (line size * ways)
- Splits the address into offset, set index and tag correctly
- Implements O(1) LRU per set
- Tracks dirty bits and counts writebacks only on dirty evictions
- Explains conflict misses with an example trace
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 add a second-level inclusive cache and back-invalidate on L2 eviction?
- How would you approximate LRU in hardware with tree pseudo-LRU, and how would that change the results?
- How would you classify each miss as compulsory, capacity or conflict?
- What changes for a write-through, no-write-allocate policy?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Inputs: size_bytes, line_bytes, ways (powers of two) and a trace of (op, addr) with op R or W. Policy: LRU within a set, write-allocate on write miss, write-back so a dirty line costs one ...
High-Level Design
block = addr >> log2(line_bytes), set = block % num_sets, tag = block // num_sets. Each set is an OrderedDict from tag to dirty flag ordered LRU to MRU: a hit moves the tag to the end, a miss ...