Questions
18 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
18 / 24

How can you handle memory usage when dealing with large files in Node.js streams?

Difficulty: 5/10
stream backpressure, highWaterMark, memory optimization

To handle memory usage efficiently with large files, you can set a reasonable highWaterMark value to control the buffer size, use streams for both reading and writing, and process data in smaller chunks to avoid loading the entire file into memory.

Scenario Questions

0-2 years experience

  1. 1You need to copy a 2 GB log file to another location using Node.js streams. How would you set up the pipeline to ensure you don't run out of memory?
  2. 2If you notice the process memory spikes while reading a large CSV with a readable stream, what immediate change could you make to the stream configuration?
  3. 3What does the highWaterMark option control, and how would you adjust it when processing a large file?

2-5 years experience

  1. 1Our service streams user‑uploaded video files through a transcoding library and has started leaking memory under load. Walk me through how you'd debug this and what stream‑related changes you'd consider.
  2. 2A nightly batch job pipes a huge JSON file through a transform that aggregates data, but it sometimes crashes with ‘JavaScript heap out of memory’. What trade‑offs would you evaluate between adjusting backpressure, chunk size, or switching APIs?
  3. 3Explain why calling .resume() on a paused stream could cause memory pressure in a long‑running ETL pipeline.

5-8 years experience

  1. 1Design a file ingestion component that can handle tens of gigabytes per minute across multiple worker nodes while keeping each Node.js process under a fixed memory budget. What architectural patterns and stream settings would you employ?
  2. 2We need to process gzip‑compressed log archives on the fly. How would you integrate compression streams without blowing up memory, and what monitoring would you put in place?
  3. 3If a downstream service occasionally slows down, causing backpressure, how would you ensure the upstream file‑read stream doesn't buffer unbounded data, and what fallback mechanisms could you implement?

8+ years experience

  1. 1Our platform is moving from a monolithic Node.js service that reads large data dumps to a microservices architecture. How would you redesign the ingestion pipeline to minimize memory usage across services, and what cross‑team standards would you establish for stream handling?
  2. 2Legacy code uses fs.readFileSync for large files, causing OOM in production. Propose a migration strategy to streams, including testing, rollout, and handling edge cases like partial failures.
  3. 3At scale we need to enforce consistent backpressure handling across dozens of services. What governance, tooling, or library abstractions would you introduce to ensure memory safety company‑wide?

Follow-up Questions

  • How would you test that your stream handling doesn't leak memory?
  • What runtime metrics would you watch to catch memory pressure early?
  • Can you compare using streams versus manually reading the file in chunks with fs.read?
Share

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