Implement an ASCII Art Printer
Company: Asana
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Implement an ASCII art printer.
A font defines each supported character as a glyph with the same height. A glyph is a list of equal-length text rows. Given an input string and the font, return the rendered ASCII-art output.
Requirements:
- Render characters by concatenating corresponding glyph rows horizontally.
- Insert one blank column between adjacent characters on the same input line.
- Preserve newline characters in the input by starting a new block of glyph rows.
- Render a space character as a blank glyph of the font height.
- For an unsupported character, either return an error or render a configured placeholder; document your choice.
- Do not print directly to stdout; return the final string or list of lines.
Example font of height 3:
| Character | Row 1 | Row 2 | Row 3 |
|---|---|---|---|
| `A` | ` # ` | `# #` | `###` |
| `B` | `## ` | `# #` | `## ` |
Input `AB` with one-column spacing renders:
` # ## `
`# # # #`
`### ## `
Constraints: let `S` be the total number of characters in the rendered output. The implementation should run in O(S) time.
Quick Answer: This question evaluates the ability to implement text rendering and string/array manipulation concepts, including glyph concatenation with inter-character spacing, newline-preserved blocks, blank glyph rendering for spaces, and handling of unsupported characters.
Implement an ASCII art printer. You are given a text string and a font, where the font maps each supported character to a glyph. A glyph is a list of text rows. In this problem, all glyphs in the font have the same height H and the same width W. To render a line of text, concatenate the corresponding glyph rows horizontally and insert exactly one blank column between adjacent characters. Preserve newline characters in the input by starting a new block of H output rows for each input line. A space character must be rendered as a blank glyph of size H x W. For unsupported characters, this problem uses a configurable placeholder: if placeholder is not None, render the placeholder glyph; otherwise raise a ValueError. Return the final rendered string and do not print to stdout.
Constraints
- font contains at least one glyph
- 1 <= H, W <= 10
- Every glyph in font has exactly H rows, and every row has length W
- 0 <= len(text) <= 10^4
- Your algorithm should run in O(S) time, where S is the number of characters in the returned output
Examples
Input: ('AB', {'A': [' # ', '# #', '###'], 'B': ['## ', '# #', '## ']}, None)
Expected Output: ' # ## \n# # # #\n### ## '
Explanation: Rows 1, 2, and 3 of A and B are concatenated with one extra blank column between the two glyphs.
Input: ('A\nB', {'A': [' # ', '# #', '###'], 'B': ['## ', '# #', '## ']}, None)
Expected Output: ' # \n# #\n###\n## \n# #\n## '
Explanation: The newline in the input starts a new block of 3 glyph rows, so A is rendered first and B is rendered below it.
Hints
- Render one input line into H separate row buffers, one buffer for each glyph row.
- Avoid repeated string concatenation inside loops; collect fragments in lists and join them at the end.