Bird
Raised Fist0
Node.jsframework~20 mins

Buffer and streams relationship in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Buffer and Streams Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the role of a Buffer in Node.js streams?
In Node.js, when working with streams, what is the main purpose of a Buffer?
AIt temporarily holds chunks of data to manage flow between fast and slow streams.
BIt permanently stores all data from a stream for later use.
CIt converts streams into JSON objects automatically.
DIt encrypts data passing through the stream for security.
Attempts:
2 left
💡 Hint
Think about how data flows smoothly between parts that work at different speeds.
component_behavior
intermediate
2:00remaining
What happens when a readable stream's internal buffer is full?
Consider a readable stream in Node.js. What occurs when its internal buffer reaches its highWaterMark limit?
AThe stream discards new incoming data until space is available.
BThe stream throws an error and closes immediately.
CThe stream pauses reading from the source until the buffer is drained.
DThe stream automatically increases the buffer size without pausing.
Attempts:
2 left
💡 Hint
Think about how streams prevent memory overload by controlling reading speed.
📝 Syntax
advanced
2:00remaining
Identify the correct way to create a Buffer from a stream chunk
Given a chunk of data from a readable stream, which code correctly creates a Buffer from it in Node.js?
Node.js
stream.on('data', (chunk) => {
  // create buffer here
});
Aconst buf = new Buffer(chunk);
Bconst buf = Buffer.from(chunk);
Cconst buf = Buffer.alloc(chunk);
Dconst buf = Buffer.create(chunk);
Attempts:
2 left
💡 Hint
Use the modern, safe method to create buffers from data.
state_output
advanced
2:00remaining
What is the output when piping a readable stream to a writable stream with backpressure?
Consider this Node.js code snippet: const { Readable, Writable } = require('stream'); const readable = Readable.from(['a', 'b', 'c']); const writable = new Writable({ write(chunk, encoding, callback) { setTimeout(() => { console.log(chunk.toString()); callback(); }, 100); } }); readable.pipe(writable); What will be the order and timing of the console output?
AThe letters 'a', 'b', 'c' print in order with about 100ms delay between each.
BAll letters print immediately at once without delay.
COnly 'a' prints, then the program stops due to backpressure.
DThe letters print in reverse order: 'c', 'b', 'a'.
Attempts:
2 left
💡 Hint
Think about how backpressure slows down writing but keeps order.
🔧 Debug
expert
3:00remaining
Why does this stream pipeline cause a memory leak?
Examine this Node.js code: const fs = require('fs'); const { Transform } = require('stream'); const readStream = fs.createReadStream('largefile.txt'); const transformStream = new Transform({ transform(chunk, encoding, callback) { // process chunk callback(null, chunk); } }); readStream.pipe(transformStream); // No pipe to writable stream or data event listener Why might this code cause a memory leak?
ABecause the pipe method requires a third argument to work properly.
BBecause the transform stream is missing the flush method.
CBecause the file 'largefile.txt' does not exist, causing an error.
DBecause the readable stream's data is not consumed, its internal buffer fills up indefinitely.
Attempts:
2 left
💡 Hint
Think about what happens if data is produced but never read or consumed.

Practice

(1/5)
1. What is the main role of a Buffer in Node.js streams?
easy
A. Convert data to strings automatically
B. Send data directly to the network
C. Temporarily store raw data chunks in memory
D. Manage file system permissions

Solution

  1. Step 1: Understand Buffer purpose

    A Buffer holds raw binary data temporarily in memory before processing or sending.
  2. Step 2: Compare Buffer with other options

    Buffers do not send data or manage permissions; they just hold data chunks.
  3. Final Answer:

    Temporarily store raw data chunks in memory -> Option C
  4. Quick Check:

    Buffer = temporary data holder [OK]
Hint: Buffers hold data chunks temporarily in memory [OK]
Common Mistakes:
  • Thinking Buffer sends data directly
  • Confusing Buffer with string conversion
  • Assuming Buffer manages permissions
2. Which of the following is the correct way to create a Buffer from a string in Node.js?
easy
A. const buf = Buffer.from('hello');
B. const buf = new Buffer('hello');
C. const buf = Buffer.create('hello');
D. const buf = Buffer.string('hello');

Solution

  1. Step 1: Recall Buffer creation syntax

    Since Node.js v6+, Buffer.from() is the recommended way to create buffers from strings.
  2. Step 2: Identify deprecated or invalid methods

    new Buffer() is deprecated; Buffer.create() and Buffer.string() do not exist.
  3. Final Answer:

    const buf = Buffer.from('hello'); -> Option A
  4. Quick Check:

    Use Buffer.from() to create buffers [OK]
Hint: Use Buffer.from() to create buffers from strings [OK]
Common Mistakes:
  • Using deprecated new Buffer() constructor
  • Trying non-existent Buffer methods
  • Confusing Buffer creation with other APIs
3. Consider this code snippet using a readable stream and Buffer:
const { Readable } = require('stream');
const readable = Readable.from(['Hello', ' ', 'World']);
readable.on('data', (chunk) => {
  console.log(Buffer.isBuffer(chunk));
});
What will be the output?
medium
A. false false false
B. true true true
C. true false true
D. false true false

Solution

  1. Step 1: Understand Readable.from behavior

    Readable.from emits chunks as strings by default when given strings.
  2. Step 2: Check Buffer.isBuffer for each chunk

    Each chunk ('Hello', ' ', 'World') is a string, so Buffer.isBuffer(chunk) returns false each time.
  3. Final Answer:

    false false false -> Option A
  4. Quick Check:

    Readable.from strings emit strings, not Buffers [OK]
Hint: Readable.from strings emit strings by default [OK]
Common Mistakes:
  • Assuming chunks are Buffers, not strings
  • Expecting mixed true/false outputs
  • Not knowing Buffer.isBuffer usage
4. Identify the error in this code that reads a file stream and logs data chunks:
const fs = require('fs');
const stream = fs.createReadStream('file.txt');
stream.on('data', (chunk) => {
  console.log(chunk.toString('utf8'));
});
stream.on('end', () => {
  console.log('Done');
});
medium
A. The 'end' event should be 'close'
B. Missing error event handler for the stream
C. createReadStream requires a callback function
D. Using toString() on chunk causes a crash

Solution

  1. Step 1: Review stream event handlers

    The code handles 'data' and 'end' events correctly but lacks an 'error' event handler.
  2. Step 2: Understand importance of error handling

    Without an 'error' handler, stream errors (like file not found) will crash the program.
  3. Final Answer:

    Missing error event handler for the stream -> Option B
  4. Quick Check:

    Always add 'error' handler on streams [OK]
Hint: Always add 'error' event handler to streams [OK]
Common Mistakes:
  • Ignoring error events on streams
  • Confusing 'end' and 'close' events
  • Thinking toString() causes errors
5. You want to process a large file efficiently by reading it in chunks and converting each chunk to uppercase before writing to another file. Which approach best uses Buffers and streams together?
hard
A. Read the entire file into a Buffer, convert to uppercase, then write all at once
B. Convert the file to string first, then create a Buffer for writing
C. Use synchronous file read and write with Buffer conversions
D. Use a readable stream to read chunks as Buffers, transform each chunk to uppercase string, then write using a writable stream

Solution

  1. Step 1: Understand efficient large file processing

    Reading in chunks with streams avoids loading the whole file into memory.
  2. Step 2: Use Buffers with streams for chunk processing

    Readable streams provide Buffers; convert each chunk to uppercase string, then write with writable stream.
  3. Step 3: Compare other options

    Reading entire file at once or synchronous methods are inefficient for large files.
  4. Final Answer:

    Use a readable stream to read chunks as Buffers, transform each chunk to uppercase string, then write using a writable stream -> Option D
  5. Quick Check:

    Streams + Buffers + transform chunks = efficient processing [OK]
Hint: Process large files chunk-by-chunk with streams and Buffers [OK]
Common Mistakes:
  • Loading entire file into memory
  • Using synchronous file operations
  • Ignoring chunk-by-chunk processing benefits