Problem · Array

Hex Dump Formatter

Learn this problem
MediumWaymo logoWaymoFULLTIMEONSITE INTERVIEW

Problem statement

Given an array bytes of byte values and a positive row width bytesPerRow, return a formatted hexadecimal dump as an array of rows.

Each row has this exact structure:

  • an eight-character lowercase hexadecimal offset for the row's first byte;
  • two spaces;
  • bytesPerRow uppercase two-digit hexadecimal slots separated by one space, with missing slots in the final row filled by spaces;
  • two spaces;
  • an ASCII column surrounded by | characters.

In the ASCII column, byte values from 0x20 through 0x7E use their ASCII character. Every other byte uses .. Return an empty array when bytes is empty.

Function

formatHexDump(bytes: int[], bytesPerRow: int) → String[]

Examples

Example 1

bytes = [72,101,108,108,111,0,35,255]bytesPerRow = 4return = ["00000000  48 65 6C 6C  |Hell|","00000004  6F 00 23 FF  |o.#.|"]

The first row contains four printable bytes. In the second row, 0x00 and 0xFF become ., while 0x23 becomes #.

Example 2

bytes = [65,66,67]bytesPerRow = 4return = ["00000000  41 42 43     |ABC|"]

The final hexadecimal slot is blank, but the ASCII column contains only the three present bytes.

Constraints

  • 0 <= bytes.length <= 10^5
  • 0 <= bytes[i] <= 255
  • 1 <= bytesPerRow <= 32

More Waymo problems

drafts saved locally
public String[] formatHexDump(int[] bytes, int bytesPerRow) {
  // write your code here
}
bytes[72,101,108,108,111,0,35,255]
bytesPerRow4
expected["00000000 48 65 6C 6C |Hell|", "00000004 6F 00 23 FF |o.#.|"]
checking account