Print a Centered Pascal Triangle
Company: Faire
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: 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.
Constraints
- 1 <= height <= 30
- Each interior value in Pascal's triangle is the sum of the two values directly above it
Examples
Input: (1,)
Expected Output: ['1']
Explanation: The triangle has only one row, so the result is just ['1'].
Input: (2,)
Expected Output: [' 1', '1 1']
Explanation: With height 2, the last row is '1 1' and the first row is centered above it with one leading space.
Input: (5,)
Expected Output: [' 1', ' 1 1', ' 1 2 1', ' 1 3 3 1', '1 4 6 4 1']
Explanation: The largest value is 6, so each number uses width 1. Rows are centered relative to the last row '1 4 6 4 1'.
Input: (6,)
Expected Output: [' 1', ' 1 1', ' 1 2 1', ' 1 3 3 1', ' 1 4 6 4 1', ' 1 5 10 10 5 1']
Explanation: The largest value is 10, so each number uses width 2. This adds internal padding to every row, including the last one.
Hints
- Build Pascal's triangle row by row: every row starts and ends with 1, and each middle value comes from two adjacent values in the previous row.
- Because the spacing depends on the largest value and the width of the last row, generate all rows first and format them in a second pass.