COA Notes
Important cache memory questions for exams and interviews with solutions.
Introduction
Cache memory questions are the bread and butter of computer architecture exams. They appear in GATE every single year, in university end-sems, and in hardware interviews. The good news is that once you master the systematic approach — breaking addresses into tag/index/offset, tracing accesses through the cache, and applying the AMAT formula — these problems become mechanical. This page drills you through every important cache question pattern with detailed solutions.
Question Type 1: Address Bit Breakdown
Q1: Direct-Mapped Cache Address Decomposition
Problem: A computer has 24-bit addresses, a direct-mapped cache of 16 KB with 64-byte blocks. Find: (a) number of cache lines, (b) number of tag, index, and offset bits, (c) total cache memory including tags and valid bits.
Solution:
Q2: Set-Associative Cache
Problem: A 4-way set-associative cache has total size 32 KB, block size 32 bytes, and the processor uses 32-bit addresses. Find the number of sets, tag bits, and total comparators needed.
Solution:
Question Type 2: Hit/Miss Trace
Q3: Trace Direct-Mapped Cache Accesses
Problem: A direct-mapped cache has 4 lines, block size 1 word. Trace accesses to block addresses: 0, 8, 0, 6, 8, 2, 0, 14, 6, 8. Determine hits and misses.
Solution:
Cache line = Block_address mod Number_of_lines = Block_address mod 4
| Access | Block Addr | Line (addr mod 4) | Hit/Miss | Cache State [0,1,2,3] |
|---|---|---|---|---|
| 1 | 0 | 0 | Miss | [0, -, -, -] |
| 2 | 8 | 0 | Miss | [8, -, -, -] (evicts 0) |
| 3 | 0 | 0 | Miss | [0, -, -, -] (evicts 8) |
| 4 | 6 | 2 | Miss | [0, -, 6, -] |
| 5 | 8 | 0 | Miss | [8, -, 6, -] (evicts 0) |
| 6 | 2 | 2 | Miss | [8, -, 2, -] (evicts 6) |
| 7 | 0 | 0 | Miss | [0, -, 2, -] (evicts 8) |
| 8 | 14 | 2 | Miss | [0, -, 14, -] (evicts 2) |
| 9 | 6 | 2 | Miss | [0, -, 6, -] (evicts 14) |
| 10 | 8 | 0 | Miss | [8, -, 6, -] (evicts 0) |
Hit rate = 0/10 = 0%! This is a pathological case — blocks 0 and 8 both map to line 0, causing thrashing. A 2-way set-associative cache would fix this.
Q4: Same trace with 2-way set-associative
Problem: Same access sequence with 2-way set-associative cache (2 sets, 2 lines each). LRU replacement.
Solution:
Set = Block_address mod 2
| Access | Block | Set | Hit/Miss | Set 0 [LRU order] | Set 1 [LRU order] |
|---|---|---|---|---|---|
| 1 | 0 | 0 | Miss | {0, -} | {-, -} |
| 2 | 8 | 0 | Miss | {0, 8} | {-, -} |
| 3 | 0 | 0 | Hit | {8, 0} | {-, -} |
| 4 | 6 | 0 | Miss | {0, 6} evicts 8 | {-, -} |
| 5 | 8 | 0 | Miss | {6, 8} evicts 0 | {-, -} |
| 6 | 2 | 0 | Miss | {8, 2} evicts 6 | {-, -} |
| 7 | 0 | 0 | Miss | {2, 0} evicts 8 | {-, -} |
| 8 | 14 | 0 | Miss | {0, 14} evicts 2 | {-, -} |
| 9 | 6 | 0 | Miss | {14, 6} evicts 0 | {-, -} |
| 10 | 8 | 0 | Miss | {6, 8} evicts 14 | {-, -} |
Hit rate = 1/10 = 10%. Still poor because working set (5 blocks mapping to set 0) > set capacity (2).
Question Type 3: AMAT Calculations
Q5: Multi-level cache AMAT
Problem: A processor has:
- L1: 2 ns access, 8% miss rate
- L2: 15 ns access, 25% miss rate (of L1 misses)
- L3: 40 ns access, 10% miss rate (of L2 misses)
- Main memory: 200 ns
Calculate AMAT and effective CPI (assuming 1 GHz clock and base CPI of 1 with 1.5 memory accesses per instruction).
Solution:
AMAT = L1_time + L1_MR × (L2_time + L2_MR × (L3_time + L3_MR × Mem_time))
= 2 + 0.08 × (15 + 0.25 × (40 + 0.10 × 200))
= 2 + 0.08 × (15 + 0.25 × (40 + 20))
= 2 + 0.08 × (15 + 0.25 × 60)
= 2 + 0.08 × (15 + 15)
= 2 + 0.08 × 30
= 2 + 2.4 = 4.4 ns
Memory stall cycles = AMAT / clock_cycle - L1_cycles
= (4.4 - 2) / 1 = 2.4 cycles per memory access
Effective CPI = Base_CPI + Memory_stalls × Mem_accesses_per_instruction
= 1 + 2.4 × 1.5 = 1 + 3.6...
Wait, let's recalculate properly:
Memory stall per access = L1_MR × (L2_time + L2_MR × (L3_time + L3_MR × Mem_time)) / clock
= 0.08 × (15 + 0.25 × 60) / 1 = 0.08 × 30 = 2.4 cycles stall per memory access
CPI = 1 + 1.5 × 2.4 = 4.6
Without cache: CPI = 1 + 1.5 × 200 = 301 (!)
Speedup from cache hierarchy: 301/4.6 = 65×Q6: Cache design trade-off
Problem: You can choose between: (A) 32 KB direct-mapped with 5% miss rate, 1-cycle access, or (B) 32 KB 4-way set-associative with 3% miss rate, 2-cycle access. Miss penalty is 100 cycles. Which is better?
Solution:
The 2% miss rate reduction saves more than the 1-cycle extra hit time costs. This is why real L1 caches are set-associative despite the latency penalty.
Question Type 4: Write Policy Questions
Q7: Write-back vs Write-through traffic
Problem: A program performs 1000 memory accesses: 700 reads, 300 writes. Cache has 80% write hit rate. Block size is 64 bytes. Compare memory bus traffic for write-through vs write-back.
Solution:
Write-Through
- Every write goes to memory: 300 writes × 4 bytes = 1200 bytes (word writes)
- Read misses: (1000 × overall_miss_rate × 64 bytes for block fetch)
- Total write traffic: 300 × 4 = 1200 bytes (assuming word writes to memory)
Write-Back
- Write hits: 300 × 0.80 = 240 (no memory traffic!)
- Write misses: 300 × 0.20 = 60 (need to fetch block + writeback dirty evict)
- Only dirty blocks written back on eviction
- Typical write traffic: much less (only when dirty blocks are evicted)
Write-back reduces memory bus traffic significantly, which is why modern caches use it despite the complexity of tracking dirty bits.
Question Type 5: Spatial and Temporal Locality
Q8: Block size impact
Problem: A cache with 1024 bytes total capacity. Compare miss rates for block sizes of 16, 32, 64, and 128 bytes for sequential array access of 4096 bytes.
Solution:
| Block size 16B | 4096/16 = 256 block fetches needed |
| Cache holds | 1024/16 = 64 blocks |
| Compulsory misses | 256 (one per new block) |
| Miss rate | 256/4096 = 6.25% (per byte) |
| Or | 1 miss per 16 accesses = 6.25% |
| Block size 32B | 4096/32 = 128 fetches |
| Miss rate | 1/32 = 3.125% |
| Block size 64B | 4096/64 = 64 fetches |
| Miss rate | 1/64 = 1.56% |
| Block size 128B | 4096/128 = 32 fetches |
| BUT | cache only holds 1024/128 = 8 blocks! |
Larger blocks exploit spatial locality better (fewer compulsory misses) but reduce the number of cache lines (more capacity misses). The optimal block size balances these factors — typically 32-64 bytes for L1 caches.
Question Type 6: Cache Coherence
Q9: Multi-core cache coherence
Problem: Two cores share memory location X. Core 1 reads X, Core 2 reads X, Core 1 writes X. Explain what happens under MESI protocol.
Solution:
| Initial | X in memory, not in any cache |
| Step 1 | Core 1 reads X |
| - Core 1 cache miss | fetch from memory |
| - Core 1's line state | Exclusive (E) — only copy, clean |
| Step 2 | Core 2 reads X |
| - Core 2 cache miss | snoops bus |
| Step 3 | Core 1 writes X |
| - Core 2 invalidates its copy ( | Invalid) |
| - Future reads by Core 2 will miss | get updated value from Core 1 |
Practice Problems
- A 64 KB, 4-way cache with 128-byte blocks uses 48-bit addresses. How many bits for tag, index, offset?
- Calculate AMAT for: L1 hit=1ns (miss rate 4%), L2 hit=8ns (miss rate 15%), Memory=80ns
- Trace block addresses 1, 4, 1, 8, 4, 1, 8 through 2-line fully associative cache with LRU
- If doubling cache size reduces miss rate from 5% to 3.5%, and miss penalty is 50 cycles, is it worth the extra 1-cycle hit time?
Key Takeaways
- Always decompose addresses into Tag | Index | Offset — this is the foundation of all cache problems
- Direct-mapped conflict misses are eliminated by increasing associativity (at cost of hit time)
- AMAT formula nests recursively for multi-level caches — each level's miss penalty includes the next level's AMAT
- Larger blocks reduce compulsory misses (spatial locality) but may increase capacity misses (fewer lines)
- Write-back reduces traffic but adds complexity; write-through is simpler but generates more bus traffic
- Working set size relative to cache size determines whether capacity misses dominate performance
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Cache Memory Questions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Computer Organization & Architecture topic.
Search Terms
computer-organization, computer organization & architecture, computer, organization, interview, preparation, cache, memory
Related Computer Organization & Architecture Topics