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
Recall & Review
beginner
What is a Readable stream in Node.js?
A Readable stream is a source of data you can read from, like a file or network connection. It lets you read data piece by piece instead of all at once.
Click to reveal answer
beginner
What does a Writable stream do?
A Writable stream is a destination where you can send data to be saved or processed, like writing to a file or sending data over the network.
Click to reveal answer
intermediate
Explain a Transform stream in Node.js.
A Transform stream is both readable and writable. It takes input data, changes or processes it, then outputs the transformed data.
Click to reveal answer
intermediate
What is a Duplex stream?
A Duplex stream can read and write data independently. Think of it like a two-way street where data flows in both directions at the same time.
Click to reveal answer
beginner
How do Readable and Writable streams differ in usage?
Readable streams provide data to your program, while Writable streams accept data from your program. They serve opposite roles in data flow.
Click to reveal answer
Which stream type allows you to modify data as it passes through?
AReadable
BWritable
CDuplex
DTransform
✗ Incorrect
Transform streams let you read data, change it, and then output the changed data.
What kind of stream can both read and write data independently?
ATransform
BWritable
CDuplex
DReadable
✗ Incorrect
Duplex streams support two-way data flow, reading and writing independently.
If you want to save data to a file, which stream type do you use?
AWritable
BReadable
CTransform
DDuplex
✗ Incorrect
Writable streams are used to send data to destinations like files.
Which stream type is only a source of data?
AReadable
BWritable
CTransform
DDuplex
✗ Incorrect
Readable streams provide data to your program but do not accept data.
Which stream type combines reading and writing but processes data in between?
AReadable
BTransform
CWritable
DDuplex
✗ Incorrect
Transform streams read input, process it, and write the transformed output.
Describe the four main stream types in Node.js and their roles.
Think about data flow direction and processing.
You got /4 concepts.
Explain a real-life example where you might use a Transform stream.
Consider converting file formats or compressing data.
You got /3 concepts.
Practice
(1/5)
1. Which type of Node.js stream is designed to only provide data piece by piece for reading?
easy
A. Transform stream
B. Readable stream
C. Writable stream
D. Duplex stream
Solution
Step 1: Understand stream roles
Readable streams are designed to emit data chunks for consumption.
Step 2: Match stream type to description
Only readable streams provide data piece by piece without accepting input.
Final Answer:
Readable stream -> Option B
Quick Check:
Readable = provides data [OK]
Hint: Readable streams only output data, no input accepted [OK]
Common Mistakes:
Confusing writable streams as data providers
Thinking transform streams only read data
Mixing duplex with readable-only behavior
2. Which of the following is the correct way to create a writable stream in Node.js?
easy
A. const stream = new Readable();
B. const stream = new Duplex();
C. const stream = new Transform();
D. const stream = new Writable({ write(chunk, encoding, callback) { callback(); } });
Solution
Step 1: Identify writable stream creation
Writable streams require a write method to handle incoming data chunks.
Step 2: Check options for correct syntax
const stream = new Writable({ write(chunk, encoding, callback) { callback(); } }); correctly creates a Writable stream with a write method and callback.
Final Answer:
const stream = new Writable({ write(chunk, encoding, callback) { callback(); } }); -> Option D
Quick Check:
Writable needs write() method [OK]
Hint: Writable streams need a write() method in options [OK]
Common Mistakes:
Using Readable constructor for writable stream
Omitting write method in Writable options
Confusing Transform or Duplex constructors
3. What will be the output of the following code snippet?
A. The Duplex constructor is missing the object mode option
B. The write method is missing the callback call
C. The read method should push null to signal end of data
D. The duplex stream cannot both read and write
Solution
Step 1: Check read method behavior
The read method pushes 'data' but never signals end by pushing null.
Step 2: Understand stream end signaling
Streams must push null to indicate no more data; missing here causes hanging.
Final Answer:
The read method should push null to signal end of data -> Option C
Quick Check:
read() must push null to end stream [OK]
Hint: read() must push null to end data stream [OK]
Common Mistakes:
Not pushing null in read method
Forgetting callback in write method
Assuming Duplex can't read and write
5. You want to create a custom Duplex stream that reads numbers from 1 to 3 and writes their squares. Which approach correctly implements this behavior?
hard
A. Push numbers 1 to 3 in read(), write() logs squares, and push null after 3
B. Push squares in read(), write() pushes numbers 1 to 3, no null needed
C. Use Transform stream instead of Duplex for this task
D. Only Writable stream can handle both reading and writing
Solution
Step 1: Understand Duplex stream roles
Duplex streams can read and write independently; read() pushes data, write() processes input.
Step 2: Match behavior to implementation
Push numbers 1 to 3 in read(), write() logs squares, and push null after last number to end stream.
Final Answer:
Push numbers 1 to 3 in read(), write() logs squares, and push null after 3 -> Option A
Quick Check:
Duplex reads numbers, writes squares [OK]
Hint: Duplex streams read and write independently [OK]