Simulate a Set-Associative Cache With LRU Replacement
Last updated: November 29, 2026
Quick Overview
Given a trace of byte addresses and a cache configuration (total size, line size, associativity), simulate a set-associative cache with LRU replacement and return the number of hits, misses and evictions.
Arm
November 29, 20260
0
0 solved
Given a trace of byte addresses and a cache configuration (total size, line size, associativity), simulate a set-associative cache with LRU replacement and return the number of hits, misses and evictions.
Performance modelling and CPU teams at Arm build cycle-approximate models of Cortex and Neoverse cores, and a cache model is a common building block. The interviewer checks that you derive tag, index and offset correctly from the geometry and pick a data structure that makes LRU O(1).
What the Interviewer Expects
- Derive number of sets, offset bits and index bits from the configuration
- Split each address into tag, set index and offset
- Implement LRU per set with O(1) updates
- Count evictions separately from misses
- Validate that sizes are powers of two
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 model a pseudo-LRU tree policy like real hardware uses, and why does hardware avoid true LRU?
- How would you add write-back versus write-through and count dirty evictions?
- How would you classify misses into compulsory, capacity and conflict?
- How would you extend this to a two-level inclusive hierarchy?
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: a list of addresses, cache_size, line_size, ways, all powers of two. Each access is a read. On a miss, allocate the line; if the set is full, evict the least recently used line. Return `...
High-Level Design
Sets = cache_size / line_size / ways. Offset bits = log2(line_size), index bits = log2(sets). For an address, block = addr >> offset_bits, index = block & (sets - 1), tag = block >> index_bits...