← Questions
Render, compress and invert a bitmap font
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Stripe's HackerRank assessment, last asked Feb 2026. It tests grids, strings and bit manipulation.
A bitmap font stores each character as a small grid of bits, one string of 0s and 1s per row, all characters the same size. Build three utilities on it, reusing your own code between them: render text, compress a bitmap losslessly, and invert one.
Interface
Unless you agree on a different interface with the interviewer, assume font: dict[str, list[str]], every glyph h rows of w bits, and:
-
render(font, text: str) -> list[str]— the glyphs side by side,#for 1 and.for 0, with one column of.between characters. -
compress(bitmap: list[str]) -> tuple[int, list[tuple[int, int]]]— the width plus the bits read row by row as runs of(bit, length). -
decompress(width, runs) -> list[str]— the exact inverse ofcompress. -
invert(bitmap) -> list[str]— every bit flipped.
Example
font = {"L": ["100", "100", "100", "100", "111"],
"I": ["111", "010", "010", "010", "111"]}
render(font, "LI") ->
#...###
#....#.
#....#.
#....#.
###.###
compress(font["L"]) -> (3, [(1,1), (0,2), (1,1), (0,2), (1,1), (0,2), (1,1), (0,2), (1,3)])
decompress(*compress(font["L"])) == font["L"]
invert(font["L"]) -> ["011", "011", "011", "011", "000"]
Constraints
- Glyphs are small (up to 64 x 64); text can be long.
-
decompress(compress(b)) == bfor every bitmap.
Clarifying questions
- What should
renderdo with a character missing from the font? - Is a particular compressed format required, or is any lossless one fine?
- Should runs cross row boundaries, or restart on each row?
- Should
invertwork on the compressed form too?
What a strong answer covers
- Rendering row by row, building each output line once instead of concatenating in a loop.
- Run-length encoding and its weakness: a checkerboard compresses worse than the raw bits.
- A round-trip test for compress and decompress, including all-zero and all-one glyphs.
- Inverting the compressed form directly by flipping each run's bit, without decompressing.
- Clear helper functions that the three parts share.
Follow-ups
- Choose a compressed format that beats run-length encoding on typical glyphs, and measure it.
- Render with a scale factor, so each bit becomes a
k x kblock. - Store a variable-width font and kern pairs of characters.
Where it's asked
- Stripe · HackerRank assessment · Last asked Feb 2026Leveled HackerRank task; test cases map to the parts in order
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
