0
0
Node.jsframework~20 mins

Transform streams for processing in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Transform 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 of this Node.js transform stream?
Consider this transform stream that converts input text to uppercase. What will be printed to the console when piping 'hello world' through it?
Node.js
import { Transform } from 'stream';

const upperCaseTransform = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    callback();
  }
});

process.stdin.pipe(upperCaseTransform).pipe(process.stdout);

// Input: 'hello world' (typed by user)
Ahello world
BHELLO WORLD
Cerror: chunk.toString is not a function
Dundefined
Attempts:
2 left
💡 Hint
Think about what the transform function does to each chunk of data.
📝 Syntax
intermediate
2:00remaining
Which option correctly implements a transform stream that reverses input strings?
You want to create a transform stream that reverses each chunk of text. Which code snippet is syntactically correct and works as intended?
Anew Transform({ transform(chunk, encoding, callback) { this.push(chunk.toString().split('').reverse().join('')); callback(); } })
Bnew Transform({ transform(chunk, encoding) { this.push(chunk.toString().reverse()); } })
Cnew Transform({ transform(chunk, encoding, callback) { callback(null, chunk.toString().split('').reverse().join('')); } })
Dnew Transform({ transform(chunk, encoding, callback) { this.push(chunk.reverse().toString()); callback(); } })
Attempts:
2 left
💡 Hint
Remember the transform method signature and how to push transformed data.
🔧 Debug
advanced
2:00remaining
Why does this transform stream cause a runtime error?
Examine this transform stream code. Why does it throw an error when processing input?
Node.js
import { Transform } from 'stream';

const faultyTransform = new Transform({
  transform(chunk, encoding, callback) {
    const data = chunk.toString().toUpperCase();
    this.push(data);
    callback();
  }
});
Achunk is a Buffer, so chunk.toUpperCase() is not a function, causing a TypeError
BTransform constructor requires an options object with readable and writable set explicitly
Cthis.push must be called after callback, so order is wrong
DMissing call to callback with error argument causes runtime error
Attempts:
2 left
💡 Hint
Check the type of chunk and what methods it supports.
state_output
advanced
2:00remaining
What is the final output of this transform stream pipeline?
This pipeline reads input, doubles numbers in text, and outputs the result. What will be printed if input is '1 2 3'?
Node.js
import { Transform } from 'stream';
import { Readable } from 'stream';

const doubleNumbers = new Transform({
  transform(chunk, encoding, callback) {
    const input = chunk.toString();
    const doubled = input.split(' ').map(n => Number(n) * 2).join(' ');
    this.push(doubled);
    callback();
  }
});

const inputStream = Readable.from(['1 2 3']);

inputStream.pipe(doubleNumbers).pipe(process.stdout);
ANaN NaN NaN
B1 2 3
Cerror: Number is not a function
D2 4 6
Attempts:
2 left
💡 Hint
Look at how numbers are processed and doubled.
🧠 Conceptual
expert
2:00remaining
Which statement about Node.js transform streams is TRUE?
Select the correct statement about how transform streams work in Node.js.
ATransform streams cannot change the size of data chunks passed through them
BTransform streams automatically buffer all data and do not require calling callback()
CTransform streams can modify data chunks and must always call callback() after pushing data
DTransform streams are only readable and cannot be piped to writable streams
Attempts:
2 left
💡 Hint
Think about the transform method contract and stream behavior.