Buffers & Streams Interview Questions (Node.js)
10 Buffers & Streams questions from the Node.js track. Read each short answer here, then open a question for the full explanation, examples, common mistakes, and sources.
- Junior
What are the four types of streams in Node.js and what is the function of each?
The types are: readable (for reading), writable (for writing), duplex (for both independently), and transform (for modifying data as it passes through).
Read the full explanation - Senior
What is the backpressure phenomenon in Node.js streams and how is it handled in engineering terms?
It occurs when the readable produces data at a rate faster than the writable can consume it, and is handled by pausing reading temporarily when the buffer is full and resuming it on the drain event.
Read the full explanation - Mid-level
Why is it always recommended to use stream.pipeline instead of traditional chaining through .pipe()?
Because pipeline manages error handling and automatically closes and destroys all connected streams if any error occurs, while .pipe() leaves the streams hanging.
Read the full explanation - Mid-level
What is the Buffer object in Node.js and how does Buffer.alloc allocation differ from Buffer.allocUnsafe?
Buffer is a raw binary memory region; Buffer.alloc zero-fills the memory to guarantee safety, while allocUnsafe reserves non-zeroed space faster but with the risk of exposing old data.
Read the full explanation - Senior
What is the role of the highWaterMark option in streams and how does it affect memory efficiency and performance?
It specifies the maximum size of data stored in the internal buffer before backpressure is triggered and reading is paused.
Read the full explanation - Senior
How do you design large file uploads as streams in an HTTP server without storing the file in memory?
Pipe the HTTP request stream (req) directly through a pipeline to a disk file or cloud storage (S3) as a multipart stream as its chunks arrive.
Read the full explanation - Mid-level
How do you create and customize a Transform Stream to process data as it flows through Node.js?
Extend the Transform class and implement _transform(chunk, encoding, callback) to modify the data and pass it to the next stage through this.push(data).
Read the full explanation - Junior
When should you use fs.readFile, and when should you prefer fs.createReadStream for working with files?
Use fs.readFile for small, fixed-size files such as configuration files, and fs.createReadStream for large files or continuous streams to conserve memory.
Read the full explanation - Senior
What is Object Mode in Node.js Streams, and when should it be enabled?
It allows a Stream to pass and receive JavaScript objects and values instead of only bytes and Buffers.
Read the full explanation - Mid-level
How do you close Streams manually with .destroy(), and why is it important to handle error events?
Calling .destroy([error]) closes handles and releases resources immediately to prevent network sockets or open files from leaking.
Read the full explanation