Challenge - 5 Problems
Buffer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Buffer write and read code?
Consider the following Node.js code that writes a string to a buffer and then reads it back. What will be logged to the console?
Node.js
const buf = Buffer.alloc(10); buf.write('hello'); console.log(buf.toString('utf8', 0, 5));
Attempts:
2 left
💡 Hint
Remember that Buffer.alloc creates a buffer filled with zeros and write returns the number of bytes written.
✗ Incorrect
The code writes 'hello' into the buffer starting at index 0. Then it reads the first 5 bytes as a UTF-8 string, which matches 'hello'. The rest of the buffer remains zeros but is not included in the output.
❓ component_behavior
intermediate2:00remaining
What happens when you slice a Buffer and modify the slice?
Given this code, what will be the output after modifying the slice?
Node.js
const buf = Buffer.from('abcdef'); const slice = buf.slice(1, 4); slice[0] = 120; console.log(buf.toString());
Attempts:
2 left
💡 Hint
Buffer slices share the same memory as the original buffer.
✗ Incorrect
The slice covers indices 1 to 3 (characters 'b', 'c', 'd'). Changing slice[0] changes buf[1] to 120 (which is 'x'). So the original buffer becomes 'a', 'x', 'c', 'd', 'e', 'f'.
📝 Syntax
advanced2:00remaining
Which option correctly creates a Buffer from a hex string?
You want to create a Buffer from the hex string '48656c6c6f'. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
The encoding parameter tells Buffer how to interpret the string.
✗ Incorrect
Buffer.from with 'hex' encoding interprets the string as hexadecimal bytes. Using 'utf8' treats it as normal text, which is incorrect here. Buffer.alloc and allocUnsafe do not accept string data as input.
🔧 Debug
advanced2:00remaining
Why does this Buffer write return 0 bytes written?
Look at this code snippet. Why does buf.write return 0?
Node.js
const buf = Buffer.alloc(5); const bytesWritten = buf.write('hello world', 0, 5, 'utf8'); console.log(bytesWritten);
Attempts:
2 left
💡 Hint
Check the meaning of the length parameter in buf.write.
✗ Incorrect
The length parameter limits how many bytes to write. The string 'hello world' is longer than 5 bytes, so only the first 5 bytes are written. The function returns the number of bytes written, which is 5, not 0. So the premise is wrong; the code returns 5, not 0.
❓ state_output
expert3:00remaining
What is the final content of the buffer after these operations?
Analyze the code and determine the final string content of the buffer.
Node.js
const buf = Buffer.alloc(6); buf.write('abc'); buf.write('def', 3); buf.fill('x', 1, 4); console.log(buf.toString());
Attempts:
2 left
💡 Hint
Remember that fill replaces bytes in the specified range, and write overwrites bytes starting at the given offset.
✗ Incorrect
First, 'abc' is written at index 0: [a,b,c,?, ?, ?]. Then 'def' is written at index 3: [a,b,c,d,e,f]. Then fill replaces bytes from index 1 to 4 (excluding 4) with 'x': indices 1,2,3 become 'x'. Final buffer: [a,x,x,x,e,f].