Bird
Raised Fist0
Node.jsframework~20 mins

Writing data with Writable streams 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
🎖️
Writable Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when writing data to a Writable stream?
Consider the following Node.js code using a Writable stream. What will be printed to the console?
Node.js
import { Writable } from 'stream';

const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
});

writable.write('Hello');
writable.write('World');
writable.end();
A
Hello
World
BHelloWorld
Cundefinedundefined
DError: write method missing callback
Attempts:
2 left
💡 Hint
Remember that each write call triggers the write method separately.
📝 Syntax
intermediate
2:00remaining
Which option correctly implements a Writable stream that converts input to uppercase?
Select the option that correctly creates a Writable stream which converts all written data to uppercase before logging it.
A
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString().toUpperCase());
    callback();
  }
});
B
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk, encoding) {
    console.log(chunk.toString().toUpperCase());
  }
});
C
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk) {
    console.log(chunk.toUpperCase());
    callback();
  }
});
D
import { Writable } from 'stream';
const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toUpperCase());
  }
});
Attempts:
2 left
💡 Hint
The write method must accept three parameters and call callback when done.
🔧 Debug
advanced
2:00remaining
Why does this Writable stream code cause a runtime error?
Examine the code below. Why does it throw an error when writing data?
Node.js
import { Writable } from 'stream';

const writable = new Writable({
  write(chunk, encoding, callback) {
    const data = chunk.toUpperCase();
    console.log(data);
    callback();
  }
});

writable.write('test');
AThe write method must be async to handle string data properly.
Bchunk is a Buffer, so chunk.toUpperCase() is not a function, causing a TypeError.
CMissing callback call causes the stream to hang and throw an error.
DThe Writable constructor requires a 'read' method, which is missing.
Attempts:
2 left
💡 Hint
Check the type of chunk and what methods it supports.
state_output
advanced
2:00remaining
What is the final value of 'data' after writing to this Writable stream?
Given the following code, what will be the value of the variable 'data' after all writes complete?
Node.js
import { Writable } from 'stream';

let data = '';
const writable = new Writable({
  write(chunk, encoding, callback) {
    data += chunk.toString();
    callback();
  }
});

writable.write('Node');
writable.write('JS');
writable.end();
A"undefined"
B"Node JS"
C"Node\nJS"
D"NodeJS"
Attempts:
2 left
💡 Hint
Consider how the chunks are concatenated without extra characters.
🧠 Conceptual
expert
3:00remaining
Which option best explains backpressure handling in Writable streams?
In Node.js Writable streams, what does the return value of the write() method indicate and how should it be handled?
Awrite() returns true when the internal buffer is full; the producer should immediately write more data to avoid blocking.
Bwrite() returns undefined; backpressure is managed by the operating system, not the stream.
Cwrite() returns false when the internal buffer is full; the producer should wait for the 'drain' event before writing more data.
Dwrite() always returns true; backpressure is handled automatically without developer intervention.
Attempts:
2 left
💡 Hint
Think about how streams signal when they can accept more data.

Practice

(1/5)
1. What is the main purpose of a Writable stream in Node.js?
easy
A. To send data piece by piece to a destination
B. To read data from a file
C. To create a server
D. To handle HTTP requests

Solution

  1. Step 1: Understand Writable stream role

    Writable streams are designed to send data to a destination in chunks.
  2. Step 2: Compare with other options

    Reading data is done by Readable streams, not Writable. Creating servers and handling HTTP requests are unrelated to Writable streams.
  3. Final Answer:

    To send data piece by piece to a destination -> Option A
  4. Quick Check:

    Writable stream = send data [OK]
Hint: Writable streams send data out chunk by chunk [OK]
Common Mistakes:
  • Confusing Writable with Readable streams
  • Thinking Writable streams read data
  • Mixing streams with server creation
2. Which of the following is the correct way to implement the _write method in a custom Writable stream?
easy
A. _write(chunk, encoding, callback) { callback(); }
B. _write(chunk, encoding) { return chunk; }
C. _write(chunk) { console.log(chunk); }
D. _write() { return true; }

Solution

  1. Step 1: Recall _write method signature

    The _write method must accept three parameters: chunk, encoding, and callback.
  2. Step 2: Check callback usage

    Calling callback() signals that the chunk was processed. Omitting it causes the stream to hang.
  3. Final Answer:

    _write(chunk, encoding, callback) { callback(); } -> Option A
  4. Quick Check:

    _write needs callback() [OK]
Hint: Always include callback in _write and call it [OK]
Common Mistakes:
  • Forgetting the callback parameter
  • Not calling callback inside _write
  • Wrong number of parameters in _write
3. Consider this code snippet:
const { Writable } = require('stream');
class MyStream extends Writable {
  _write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
}
const stream = new MyStream();
stream.write('Hello');
stream.end('World');

What will be printed to the console?
medium
A. HelloWorld
B. Hello\nWorld
C. Hello\nWorld\n
D. Hello\nWorld printed separately

Solution

  1. Step 1: Understand write and end calls

    stream.write('Hello') sends 'Hello' chunk, then stream.end('World') sends 'World' chunk and ends.
  2. Step 2: Check _write behavior

    Each chunk is logged separately with console.log, so 'Hello' and 'World' print on separate lines.
  3. Final Answer:

    Hello World printed separately -> Option D
  4. Quick Check:

    Each chunk logs separately [OK]
Hint: Each write chunk logs on its own line [OK]
Common Mistakes:
  • Assuming chunks concatenate automatically
  • Expecting no newline between chunks
  • Confusing write and end data handling
4. What is wrong with this Writable stream implementation?
const { Writable } = require('stream');
class BrokenStream extends Writable {
  _write(chunk, encoding) {
    console.log(chunk.toString());
  }
}
const stream = new BrokenStream();
stream.write('Test');
medium
A. Using console.log inside _write is not allowed
B. Missing callback parameter and not calling callback()
C. Not calling stream.end() causes error
D. _write method should be named write

Solution

  1. Step 1: Check _write method signature

    _write must have three parameters: chunk, encoding, callback.
  2. Step 2: Check callback usage

    Callback must be called to signal completion; missing callback causes stream to hang.
  3. Final Answer:

    Missing callback parameter and not calling callback() -> Option B
  4. Quick Check:

    _write needs callback param and call [OK]
Hint: Always include and call callback in _write [OK]
Common Mistakes:
  • Omitting callback parameter
  • Not calling callback inside _write
  • Confusing _write with write method
5. You want to create a Writable stream that collects all written chunks into a single string and logs it only when the stream ends. Which approach is correct?
hard
A. Log each chunk inside _write and ignore 'finish' event
B. Call callback only after all chunks are written, ignoring _write
C. Store chunks in a variable inside _write, call callback, then log in 'finish' event
D. Use readable stream instead of writable for collecting data

Solution

  1. Step 1: Collect chunks inside _write

    Inside _write, append each chunk to a variable and call callback to continue.
  2. Step 2: Log combined data on 'finish' event

    Listen to the 'finish' event to know when writing ends, then log the full collected string.
  3. Final Answer:

    Store chunks in a variable inside _write, call callback, then log in 'finish' event -> Option C
  4. Quick Check:

    Collect chunks + log on finish = Store chunks in a variable inside _write, call callback, then log in 'finish' event [OK]
Hint: Collect chunks in _write, log on 'finish' event [OK]
Common Mistakes:
  • Logging inside _write causing multiple logs
  • Not calling callback causing stream to hang
  • Using readable stream instead of writable