05 / 06

What are the commonly used encoding types when working with buffers in Node.js?

  1. 1

    utf-8 for text data

  2. 2

    ASCII for ASCII characters

  3. 3

    base64: for encoding binary data as text

  4. 4

    hex: for hexadecimal encoding.

Difficulty: 3/10
Topics: Buffer encoding, String conversion, Binary data handling

Scenario Questions

0-2 years experience
  1. 1

    You need to read a small text file and send its contents over a WebSocket as a string. Which Buffer encoding would you choose and why?

  2. 2

    If you call Buffer.from('deadbeef', 'hex') and then .toString('utf8'), what will the output look like and why?

2-5 years experience
  1. 1

    A service you’re integrating returns binary data that appears garbled when you log it as a string. Walk me through how you would debug the encoding issue using Buffer methods.

  2. 2

    When streaming large CSV files, you sometimes see partial multibyte characters at chunk boundaries. How would you handle encoding to avoid data corruption?

5-8 years experience
  1. 1

    Design a microservice that stores user‑uploaded images in memory before persisting them. Which Buffer encodings would you use for in‑memory representation versus network transmission, and what trade‑offs are involved?

  2. 2

    Your API must support both JSON payloads (UTF‑8) and binary payloads (Base64). How would you structure the Buffer handling layer to keep the codebase maintainable and performant?

8+ years experience
  1. 1

    Our legacy system writes logs using the 'binary' encoding, but the new platform expects UTF‑8. Outline a migration plan that minimizes downtime and ensures backward compatibility across teams.

  2. 2

    At scale, you notice increased GC pressure due to large Buffer allocations with 'utf8' strings. What architectural changes could you propose to reduce memory churn while preserving correct encoding semantics?

Follow-up Questions

  • What is the default encoding when you call buffer.toString() without arguments?
  • How does using 'base64' differ from 'hex' in terms of output size?
  • Can you read a UTF‑16LE encoded file directly into a Buffer without conversion?