How to approach scenario questions
Interviewers want to hear how you investigate constraints, make trade-offs, and verify a solution. Use each problem to explain a practical decision process, not just name a language feature.
A clear answer structure
- Clarify requirements and failure conditions.
- Explain the approach and why it fits.
- Cover trade-offs, safety, and edge cases.
- Describe how you would test and measure it.
Real-world problem 1
A value was moved and the compiler rejects a later use. How do you decide what to change?
The scenario
A function takes ownership of a large value, but the caller still needs to read it afterward.
A strong answer
Check whether the function needs ownership or only temporary access. Accept a reference when it only reads or mutates borrowed data, return ownership when transfer is intended, and clone only when a separate owned copy is required and its cost is acceptable.
Real-world problem 2
Several threads need access to shared state. Which Rust types would you consider?
The scenario
A shared cache must be read and updated by worker threads.
A strong answer
Use Arc to share ownership across threads and protect mutation with Mutex or RwLock, selecting based on measured read/write behavior. Keep lock scopes short and avoid holding a lock across slow I/O or an await point.
Real-world problem 3
A low-level function can fail in several ways. How would you expose those failures?
The scenario
Callers currently receive a magic integer or a panic when parsing or reading data fails.
A strong answer
Return Result with an error type that preserves useful context, and use the question-mark operator to propagate failures where recovery is not local. Convert errors at application boundaries into appropriate user-facing or protocol responses; reserve panic for violated invariants.
Real-world problem 4
Malformed external input causes a panic in production. How would you harden the boundary?
The scenario
A parser uses unwrap on data received from a file or network request.
A strong answer
Treat external data as untrusted. Validate lengths and ranges, use fallible parsing and checked conversions, and return a descriptive error for invalid values. Keep unwrap for conditions proven by local invariants, not assumptions about user or network input.
Real-world problem 5
An async task has high contention and stalls while waiting for a lock. What would you inspect?
The scenario
A Tokio service protects shared state with a mutex and holds the guard while awaiting a network call.
A strong answer
Avoid holding a synchronous lock guard across await. Copy or clone the required state while locked, release the guard, then perform asynchronous work. Use an async mutex only when a lock must span an await, and measure contention before changing the design.
Real-world problem 6
A performance rewrite adds unsafe code but there is no benchmark. What would you recommend?
The scenario
The team wants to replace safe collections with manual memory operations to improve throughput.
A strong answer
Establish a representative benchmark and profile first. Optimize the measured bottleneck using safe APIs where possible, then compare correctness and performance. If unsafe code is still necessary, isolate it behind a small documented abstraction and test its invariants.
Real-world problem 7
A Rust service must process a very large file with limited memory. What approach would you use?
The scenario
The current implementation reads the whole file into a String before parsing.
A strong answer
Use buffered streaming with BufRead and process records incrementally, keeping only a bounded working set. Handle partial or malformed records explicitly and commit progress in batches if the job must resume. Measure memory and throughput on representative files.
Real-world problem 8
How would you make an unsafe FFI boundary safer?
The scenario
Rust calls a C library that receives raw pointers and lengths from a network-derived buffer.
A strong answer
Keep unsafe operations inside a narrow wrapper that validates pointer, length, alignment, lifetime, and ownership requirements before constructing safe Rust values. Document each safety invariant, reject invalid input, and add tests and tools such as Miri where applicable.