Back to Interview Prep

Warm Up · Star Pattern

Right-Angled Star Triangle

Problem Statement

Print a right-angled triangle of stars with n rows.

Output

*
**
***
****

Approach

  • Outer Loop (Rows): Run a loop from i = 0 to i = n - 1. Each iteration represents one row.
  • Inner Loop (Stars per Row): For each row i, run another loop from j = 0 to j = i and append a * character to a string.
  • Print Row: Print the string after the inner loop completes for each row.

Time & Space Complexity

Time Complexity: O(n^2) because the total number of stars printed is 1 + 2 + 3 + ... + n = n(n+1)/2.

Space Complexity: O(n) for the temporary string variable storing each row.

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