PPractick

DSA interview question

Pattern 11: Binary Number Triangle

Easy · Algorithms · Topics: pattern building

The problem

This triangle prints alternating 1s and 0s, and the value each row starts with alternates too: odd-numbered rows start with 1, even-numbered rows start with 0. Every row then flips between 1 and 0. It's a small exercise in deriving a cell's value from its position, or tracking a value you toggle as you go, instead of printing a constant or a counter.

Example for n = 5:

1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

Function signature

vector<string> binaryTriangle(int n);  // row i starts with 1 if i is odd, 0 if even; then alternates

Test cases

Input nOutput rowsExplanation
1"1"Row 1 starts with 1
2"1", "0 1"Row 2 starts with 0
4"1", "0 1", "1 0 1", "0 1 0 1"
5as in the exampleEvery row ends with 1

(Confirm: space-separated with no trailing space; some judges omit the spaces.)

How interviewers phrase it

Clarifying questions to ask

  1. Does row 1 start with 1?
  2. Space-separated or not?
  3. Print or return?

Follow-ups to expect

  1. "Can you compute each value directly instead of toggling?"
  2. "Why does every row end with 1?"
  3. "What's the complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in