CS Performance Optimization 2 — Questions and Answers
Question 1: A program repeatedly accesses a 2D array column-by-column instead of row-by-row in a row-major language like C. What is the primary performance cost?
- Stack overflow from deep recursion
- Increased instruction count from extra loops
- Higher register pressure in the CPU
- Poor cache locality causing frequent cache misses (Correct answer)
Correct answer: Poor cache locality causing frequent cache misses
Column-wise traversal of a row-major array jumps across memory, defeating spatial locality and causing cache misses.
Question 2: Which technique reduces function call overhead by replacing a call with the function's body at compile time?
- Tail call optimization
- Loop unrolling
- Inlining (Correct answer)
- Memoization
Correct answer: Inlining
Inlining substitutes the function body at the call site, eliminating call and return overhead.
Question 3: A profiler shows a program spends 80% of its time in one function. According to Amdahl's Law, what is the maximum overall speedup if that function is made infinitely fast?
- 80x
- 1.25x
- 20x
- 5x (Correct answer)
Correct answer: 5x
With 20% of the runtime unaffected, the speedup limit is 1/0.2 = 5x.
Question 4: Which data structure change most directly improves lookup performance from O(n) to average O(1)?
- Replacing an array with a binary search tree
- Replacing a hash table with a sorted array
- Replacing a linked list with a hash table (Correct answer)
- Replacing a queue with a stack
Correct answer: Replacing a linked list with a hash table
Hash tables provide average constant-time lookups versus linear scans through a linked list.
Question 5: What is the main risk of premature optimization in software development?
- The compiler will refuse to optimize the code further
- The program will always run slower
- Memory usage automatically increases
- Wasted effort and complex code in areas that are not bottlenecks (Correct answer)
Correct answer: Wasted effort and complex code in areas that are not bottlenecks
Optimizing before profiling often complicates code without improving the parts that actually dominate runtime.
Question 6: Which caching strategy stores results of expensive function calls so repeated calls with the same arguments return instantly?
- Write-back caching
- Cache invalidation
- Prefetching
- Memoization (Correct answer)
Correct answer: Memoization
Memoization caches a function's return values keyed by its arguments.
Question 7: A web application makes 50 separate small database queries to render one page. What is the most effective optimization?
- Switch the page to HTTPS
- Add more RAM to the web server
- Batch the queries into fewer round trips (Correct answer)
- Minify the JavaScript files
Correct answer: Batch the queries into fewer round trips
Combining many small queries reduces per-request network and query overhead, the dominant cost here.
A program repeatedly accesses a 2D array column-by-column instead of row-by-row in a row-major language like C.
What is the primary performance cost?