Back to Interview Prep

Warm Up · Star Pattern

Right-Angled Triangle of Alternating 1s and 0s

Problem Statement

Write a program that prints a triangle of alternating 1s and 0s, starting with 1 on each row.

Output

1
10
101
1010

Approach

  • Outer loop (Rows): Loop i from 0 to n - 1.
  • Initialize toggle = 1: Start each row with toggle = 1.
  • Inner loop (Columns): For each row, loop j from 0 to i. On each iteration, append toggle to the row string, then flip toggle between 1 and 0.
  • Print Row: After the inner loop, print the row string.

Time & Space Complexity

Time Complexity: O(n^2)

Space Complexity: O(n) per 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