Katkat

Print a Centered Pascal Triangle

FaireSoftware Engineer · Technical Screen

Last updated

This question evaluates the ability to generate combinatorial structures (Pascal's triangle) and apply precise numeric formatting, alignment, and string-layout skills, reflecting algorithmic thinking and attention to presentation details.

Given a positive integer height, generate the first height rows of Pascal's triangle and print them as a centered text triangle.

Formatting requirements:

  • Let max_value be the largest number that appears in the triangle.
  • Let w be the number of decimal digits in max_value.
  • Print each number right-aligned in a field of width w.
  • Use consistent spacing between values so the output looks visually symmetric.
  • Add leading spaces so each row is centered relative to the last row.

Example: if the largest value is 121, then w = 3, so the value 1 should be printed as " 1".