Algorithmic Complexity & Code-Level Optimization Questions

Reasoning about the time and space complexity of code and applying local optimizations that materially change performance. Covers Big-O and amortized analysis, performance modeling, data-structure selection, hot-loop and allocation reduction, hardware-aware work such as cache locality, vectorization and false sharing, knowing when an algorithmic change beats micro-optimization, and the measurement practice that grounds those calls: flame graphs, hardware counters, and microbenchmarks that survive warmup, noise and statistical scrutiny. Emphasizes performance-aware coding grounded in complexity and measurement rather than premature tuning, scoped to code you can change rather than production incident response or capacity planning.

MediumTechnical
107 practiced

Implement reservoir sampling (Algorithm R) in Python to sample k items uniformly at random from a stream of unknown length. Provide the function signature: def reservoir_sample(stream_iter, k): -> list. Explain time and space complexity and discuss how this algorithm can be used for log sampling in production pipelines.

MediumTechnical
103 practiced

Compare Amdahl's Law and Gustafson's Law for parallel scalability. If 70% of a training job is perfectly parallelizable, compute theoretical speedup on 8 GPUs using both laws. Explain which law is more realistic for strong scaling vs weak scaling in machine learning workloads and why.

MediumTechnical
75 practiced

You must speed up a CPU-bound numeric transformation over 100 million floats. Compare expected speedups, memory usage, and cache behavior for (a) a pure Python loop, (b) vectorized NumPy, and (c) a Numba/Cython implementation. Explain benchmarking approach and criteria to choose which to deploy in production.

EasyTechnical
103 practiced

Explain the computational and memory complexity of standard transformer self-attention for sequence length N and hidden size d. Provide expressions for time and memory per layer and per token, and explain why self-attention becomes a bottleneck for very long sequences.

MediumTechnical
101 practiced

Compare LRU and LFU cache eviction policies for a backend cache storing session objects. Analyze average-case complexity for access/update, explain how workloads like scans or one-off spikes affect hit-rate for each, and propose a hybrid strategy to mitigate weaknesses.

Unlock Full Question Bank

Get access to all Algorithmic Complexity & Code-Level Optimization interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.