Questions
12 of 24
1What are streams?
2List elementary stream types.
3What are advantages of streams.
4Create a readable stream
5How can you create a custom Transform stream in Node.js?
6What is the `end` event in a readable stream, and how is it different from the `finish` event in a writable stream?
7How can you handle errors in Node.js streams?
8Explain the concept of object streams and provide an example use case.
9Explain the difference between `ObjectMode` and `Buffer` mode in a readable stream.
10Explain the difference between a flowing and a non-flowing readable stream.
11What is the difference between the `data` event and the `readable` event in a readable stream?
12How can you handle cleanup operations when working with streams?
13What is the purpose of the `highWaterMark` option when creating a readable stream?
14What is backpressure in Node.js streams, and how can you handle it?
15What is the pause() method used for in a readable stream, and how can you resume data flow?
16What is the purpose of the `stream.finished()` utility in Node.js?
17How do you handle stream errors when working with async/await in Node.js?
18How can you handle memory usage when dealing with large files in Node.js streams?
19Explain the concept of piping in Node.js streams.
20What is the purpose of pipeThrough function
21What is the purpose of the `unpipe()` method in Node.js streams, and how does it work?
22Explain the difference between `pipe()` and `on('data')` when working with streams.
23What is the purpose of the `stream.pipeline()` function in Node.js?
24write code of a stream to read a local file in node js
12 / 24

How can you handle cleanup operations when working with streams?

Difficulty: 6/10
stream lifecycle, error handling, resource cleanup

You can listen for the end event on a readable stream or the finish event on a writable stream to perform cleanup operations, such as closing files or network connections when the stream has finished processing.

Scenario Questions

0-2 years experience

  1. 1You need to read a large file line by line using a readable stream and write each line to another file. How would you ensure the file descriptors are closed properly even if an error occurs while processing?
  2. 2When you pipe a readable stream into a writable stream, what steps do you take to clean up the streams if the client disconnects before the transfer finishes?

2-5 years experience

  1. 1We have a microservice that processes incoming HTTP request bodies as streams and forwards them to a downstream service. Yesterday the service started leaking file descriptors. Walk me through how you would investigate and fix the cleanup logic.
  2. 2Explain why calling stream.destroy() in an error handler might be necessary, and what additional cleanup you might need to perform for a Transform stream that holds a timer.

5-8 years experience

  1. 1Our real-time analytics pipeline uses a chain of Transform streams that run in a cluster of Node processes. At high load we see occasional memory bloat due to streams not being reclaimed. How would you redesign the cleanup strategy to be robust at scale?
  2. 2When integrating with a third‑party library that returns a custom stream, you need to guarantee that all underlying resources (e.g., sockets, file handles) are released when the stream ends or errors. What patterns would you use to enforce this across many such integrations?

8+ years experience

  1. 1We are migrating a legacy monolith that uses many ad‑hoc file streams to a new event‑driven architecture built on Node streams and a message broker. What architectural guidelines would you set to ensure consistent cleanup and avoid resource leaks across teams?
  2. 2Consider a multi‑tenant SaaS platform where each tenant's data is streamed from S3 through a Node service into a processing pipeline. How would you design a global cleanup policy that handles tenant‑specific failures, back‑pressure, and ensures no dangling resources remain after a tenant is de‑provisioned?

Follow-up Questions

  • What would happen if you omitted the error listener on a stream?
  • Can you compare using pipeline versus manual pipe/unpipe for cleanup?
  • How does back‑pressure affect when you should close a stream?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.