stringPyramid

Medium · Loops and Iterations · 3 test cases

Create a function that returns a pyramid-like string of a given height. Each level of the pyramid should be centered and made of '#' characters.

Examples

stringPyramid(3) → '  #  \n ### \n#####'
stringPyramid(1) → '#'

Starter code

function stringPyramid(n) {

}

Complexity of the optimal solution

Time O(n²), space O(n²). The function uses nested loops. The outer loop runs n times (for each row), and the inner loop runs roughly 2n times (for each column). This results in a quadratic time complexity. The final string and the intermediate array also have a size proportional to n², so space is O(n²).

Solve stringPyramid in the browser