Challenge - 5 Problems
Stream Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why use streams for large files in Node.js?
You want to read a very large file in Node.js. Why is using streams better than reading the whole file at once?
Attempts:
2 left
💡 Hint
Think about memory use when handling big files.
✗ Incorrect
Streams allow reading or writing data in small chunks. This means you don't need to load the entire file into memory, which saves resources and improves performance.
❓ component_behavior
intermediate2:00remaining
What happens when you pipe a readable stream to a writable stream?
In Node.js, you connect a readable stream to a writable stream using pipe(). What is the main effect of this connection?
Node.js
const fs = require('fs'); const readable = fs.createReadStream('input.txt'); const writable = fs.createWriteStream('output.txt'); readable.pipe(writable);
Attempts:
2 left
💡 Hint
Consider how data moves between streams.
✗ Incorrect
Using pipe() connects streams so data flows in chunks from the source (readable) to the destination (writable) automatically.
❓ state_output
advanced2:00remaining
What is the output when reading a file with streams?
Consider this Node.js code reading a file with a stream. What will be printed to the console?
Node.js
const fs = require('fs'); const readable = fs.createReadStream('file.txt', { encoding: 'utf8' }); readable.on('data', chunk => { console.log(chunk.length); });
Attempts:
2 left
💡 Hint
Streams emit 'data' events multiple times for chunks.
✗ Incorrect
The 'data' event fires for each chunk read. Each chunk has a length, so multiple numbers print showing chunk sizes.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this stream code
Which option contains a syntax error when creating a readable stream in Node.js?
Node.js
const fs = require('fs'); const stream = fs.createReadStream('data.txt', { encoding: 'utf8' });
Attempts:
2 left
💡 Hint
Check the commas and braces in the options.
✗ Incorrect
Option A is missing a comma between arguments, causing a syntax error.
🔧 Debug
expert3:00remaining
Why does this stream code cause a memory leak?
This Node.js code reads a large file but causes increasing memory use until crash. What is the likely cause?
Node.js
const fs = require('fs'); const readable = fs.createReadStream('largefile.txt'); readable.on('data', chunk => { // process chunk but do not consume it fully });
Attempts:
2 left
💡 Hint
Think about how streams manage flow control.
✗ Incorrect
If the 'data' event handler does not consume or pause the stream, data buffers grow, causing memory leaks.