DSA interview question
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
vector<string> binaryTriangle(int n); // row i starts with 1 if i is odd, 0 if even; then alternates
Input n | Output rows | Explanation |
|---|---|---|
| 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" | |
| 5 | as in the example | Every row ends with 1 |
(Confirm: space-separated with no trailing space; some judges omit the spaces.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in