FastPrepBitFont - Render a Kerned Pair

BitFont - Render a Kerned Pair

Stripe logoStripe● EasyINTERNONSITE INTERVIEW
Learn

Problem statement

Two selected bitmap glyphs are supplied as leftGlyph and rightGlyph. Each glyph is a rectangular list of binary row strings. A 1 is ink and a 0 is background. Both glyphs have the same height.

Placement rules

Let L and R be the glyph widths. The left glyph starts at column 0. A kerning value k, with 0 <= k <= L, places the right glyph at column L - k. Thus k = 0 puts the glyphs side by side, and a larger value moves the right glyph closer.

A placement is valid when no output cell receives ink from both glyphs. Background columns and even the glyph bounding boxes may overlap. Evaluate each final placement independently: an invalid smaller value does not rule out a larger valid value. The right glyph may start at column 0, but never at a negative column.

The supplied k is guaranteed to be the largest valid value under these placement rules. Render that placement without recomputing or validating k.

Return the rows using . for background and * for ink. The output height is the common glyph height and its width is max(L, L - k + R). Preserve all leading and trailing background columns; do not crop the canvas.

Function

renderKernedPair(leftGlyph: String[], rightGlyph: String[], k: int) → String[]

Examples

Example 1

leftGlyph = ["101"]rightGlyph = ["010"]k = 3return = ["***"]

The largest valid final-placement shift is 3. The right origin is 0. The canvas preserves both full glyph extents, and every ink pixel is printed as an asterisk.

Example 2

leftGlyph = ["11","11"]rightGlyph = ["1","1"]k = 0return = ["***","***"]

The largest valid final-placement shift is 0. The right origin is 2. The canvas preserves both full glyph extents, and every ink pixel is printed as an asterisk.

Example 3

leftGlyph = ["0010","0100"]rightGlyph = ["100","001"]k = 4return = ["*.*.",".**."]

The largest valid final-placement shift is 4. The right origin is 0. The canvas preserves both full glyph extents, and every ink pixel is printed as an asterisk.

Constraints

  • 1 <= leftGlyph.length == rightGlyph.length <= 100.
  • Every row in leftGlyph has the same width L; every row in rightGlyph has the same width R.
  • 1 <= L, R <= 100.
  • Rows contain only ASCII 0 and 1. Entirely background rows or glyphs are allowed.
  • 0 <= k <= L, and k is the largest non-colliding final placement.
  • The returned canvas contains at most 20,000 pixels.

More Stripe problems

See Stripe hiring insights
public String[] renderKernedPair(String[] leftGlyph, String[] rightGlyph, int k) {
    // Write your code here.
}
leftGlyph["101"]
rightGlyph["010"]
k3
expected["***"]
Checking account…