Bird
Raised Fist0
Node.jsframework~20 mins

Buffer concatenation 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
🎖️
Buffer Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output of this Buffer concatenation code?
Consider the following Node.js code that concatenates two buffers. What will be the output when logged as a string?
Node.js
const buf1 = Buffer.from('Hello, ');
const buf2 = Buffer.from('World!');
const result = Buffer.concat([buf1, buf2]);
console.log(result.toString());
A"Hello, World!"
B"Hello World!"
C"Hello,World!"
D"HelloWorld!"
Attempts:
2 left
💡 Hint
Remember that Buffer.concat joins buffers exactly as they are without adding extra characters.
Predict Output
intermediate
1:00remaining
What is the length of the concatenated Buffer?
Given these buffers, what is the length of the resulting buffer after concatenation?
Node.js
const buf1 = Buffer.from('abc');
const buf2 = Buffer.from('defg');
const result = Buffer.concat([buf1, buf2]);
console.log(result.length);
A4
B6
C8
D7
Attempts:
2 left
💡 Hint
Length counts all bytes in both buffers combined.
🔧 Debug
advanced
1:30remaining
Why does this Buffer.concat call throw an error?
Examine the code below. Why does it throw a TypeError?
Node.js
const buf1 = Buffer.from('test');
const buf2 = 'not a buffer';
const result = Buffer.concat([buf1, buf2]);
ABecause buf2 is a string, not a Buffer instance
BBecause buf1 is not initialized correctly
CBecause Buffer.concat requires at least three buffers
DBecause Buffer.concat only accepts a single buffer argument
Attempts:
2 left
💡 Hint
Buffer.concat expects an array of Buffer objects only.
component_behavior
advanced
1:30remaining
What happens if you specify a total length smaller than the sum of buffers in Buffer.concat?
Consider this code snippet. What will be the output of the concatenated buffer as a string?
Node.js
const buf1 = Buffer.from('12345');
const buf2 = Buffer.from('67890');
const result = Buffer.concat([buf1, buf2], 8);
console.log(result.toString());
A"1234567"
B"12345678"
C"1234567890"
D"123456789"
Attempts:
2 left
💡 Hint
The total length limits the size of the resulting buffer.
📝 Syntax
expert
2:00remaining
Which option correctly concatenates buffers and converts to uppercase string?
Choose the code snippet that concatenates two buffers and outputs the uppercase string of the result.
A
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');
const result = Buffer.concat([buf1, buf2]).toUpperCase();
console.log(result.toString());
B
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');
const result = Buffer.concat(buf1, buf2);
console.log(result.toString().toUpperCase());
C
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');
const result = Buffer.concat([buf1, buf2]);
console.log(result.toString().toUpperCase());
D
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');
const result = Buffer.concat([buf1, buf2]);
console.log(result.toUpperCase());
Attempts:
2 left
💡 Hint
Remember Buffer.concat takes an array of buffers and toUpperCase() is a string method.

Practice

(1/5)
1. What does Buffer.concat do in Node.js?
easy
A. Creates a new empty Buffer
B. Splits a Buffer into smaller chunks
C. Joins multiple Buffer objects into one larger Buffer
D. Converts a Buffer to a string

Solution

  1. Step 1: Understand Buffer.concat purpose

    Buffer.concat is designed to combine multiple Buffer objects into a single Buffer.
  2. Step 2: Compare options with Buffer.concat behavior

    Only Joins multiple Buffer objects into one larger Buffer describes joining buffers, which matches Buffer.concat functionality.
  3. Final Answer:

    Joins multiple Buffer objects into one larger Buffer -> Option C
  4. Quick Check:

    Buffer.concat joins buffers = D [OK]
Hint: Remember concat means join, not split or convert [OK]
Common Mistakes:
  • Confusing concat with split or slice
  • Thinking it converts buffers to strings
  • Assuming it creates empty buffers
2. Which of the following is the correct syntax to concatenate two buffers buf1 and buf2?
easy
A. Buffer.concat([buf1, buf2])
B. Buffer.concat(buf1, buf2)
C. Buffer.concat(buf1 + buf2)
D. Buffer.concat({buf1, buf2})

Solution

  1. Step 1: Check Buffer.concat parameter type

    Buffer.concat expects an array of Buffer objects as its first argument.
  2. Step 2: Match options with correct syntax

    Only Buffer.concat([buf1, buf2]) passes an array [buf1, buf2], which is correct syntax.
  3. Final Answer:

    Buffer.concat([buf1, buf2]) -> Option A
  4. Quick Check:

    Buffer.concat takes array of buffers = A [OK]
Hint: Always pass buffers inside an array to Buffer.concat [OK]
Common Mistakes:
  • Passing buffers as separate arguments
  • Using plus operator to add buffers
  • Passing an object instead of array
3. What will be the output length of the following code?
const buf1 = Buffer.from('Hi');
const buf2 = Buffer.from('!');
const result = Buffer.concat([buf1, buf2]);
console.log(result.length);
medium
A. 4
B. 2
C. 1
D. 3

Solution

  1. Step 1: Calculate length of each buffer

    buf1 contains 'Hi' which is 2 bytes, buf2 contains '!' which is 1 byte.
  2. Step 2: Sum lengths after concatenation

    Total length = 2 + 1 = 3 bytes.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Length of 'Hi' + '!' = 3 [OK]
Hint: Add lengths of all buffers to get result length [OK]
Common Mistakes:
  • Counting characters instead of bytes
  • Forgetting to add all buffer lengths
  • Assuming length stays same as first buffer
4. Identify the error in this code snippet:
const buf1 = Buffer.from('A');
const buf2 = Buffer.from('B');
const combined = Buffer.concat(buf1, buf2);
console.log(combined.toString());
medium
A. Buffer.concat expects an array of buffers, not separate arguments
B. Buffer.from cannot create buffers from strings
C. toString() is not a method on Buffer objects
D. Buffers cannot be concatenated

Solution

  1. Step 1: Check Buffer.concat argument type

    The code passes two buffers as separate arguments, but Buffer.concat requires a single array argument.
  2. Step 2: Verify other parts of code

    Buffer.from correctly creates buffers from strings, and toString() is valid on buffers.
  3. Final Answer:

    Buffer.concat expects an array of buffers, not separate arguments -> Option A
  4. Quick Check:

    Buffer.concat needs array argument = C [OK]
Hint: Pass buffers inside an array to Buffer.concat [OK]
Common Mistakes:
  • Passing buffers as separate arguments
  • Misunderstanding Buffer.from usage
  • Thinking toString() is invalid on buffers
5. You have three buffers: buf1 with length 5, buf2 with length 3, and buf3 with length 7. You want to concatenate them efficiently. Which is the best way to use Buffer.concat for performance?
hard
A. Buffer.concat([buf1, buf2, buf3])
B. Buffer.concat([buf1, buf2, buf3], 15)
C. Buffer.concat([buf1, buf2, buf3], 10)
D. Buffer.concat([buf1, buf2, buf3], 20)

Solution

  1. Step 1: Calculate total length of buffers

    Total length = 5 + 3 + 7 = 15 bytes.
  2. Step 2: Use Buffer.concat with total length for efficiency

    Passing the exact total length as the second argument improves performance by preallocating the buffer.
  3. Step 3: Compare options

    Buffer.concat([buf1, buf2, buf3], 15) correctly passes the array and the exact total length 15.
  4. Final Answer:

    Buffer.concat([buf1, buf2, buf3], 15) -> Option B
  5. Quick Check:

    Pass total length for better performance = B [OK]
Hint: Provide exact total length as second argument for speed [OK]
Common Mistakes:
  • Not passing total length at all
  • Passing incorrect total length
  • Passing length smaller or larger than sum