Back to Interview Prep

Warm Up · Star Pattern

Right-Aligned Right-Angled Triangle of Stars

Problem Statement

Write a program that prints a right-aligned triangle of stars increasing row by row, with leading spaces for alignment.

Output

   *
  **
 ***
****

Approach

  • Outer loop (Rows): Loop i from 0 to n - 1. Each iteration is a new row.
  • Inner loop 1 (Spaces): For each row, add n - i - 1 spaces before the stars to right-align the triangle.
  • Inner loop 2 (Stars): Add i + 1 stars after the spaces.
  • Print Row: Combine the spaces and stars, then print the row.

Time & Space Complexity

Time Complexity: O(n^2)

Space Complexity: O(n) for the row string.

Your solution

Solve it in JavaScript and print the answer with console.log(). Run to see your output, then Submit to check it against the test cases.

// Write your solution in JavaScript
// Print the answer with console.log()


Console

No output yet. Use console.log() to print values here.

Write your own solution and press Submit once to unlock the answer.

You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in