Print a Centered Pascal Triangle
FaireSoftware Engineer · Technical ScreenLast 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_valuebe the largest number that appears in the triangle. - Let
wbe the number of decimal digits inmax_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".
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.