DSA interview question
The butterfly is the mirror image of the symmetric void (Pattern 19): instead of a hole that opens and closes, two "wings" of stars grow toward the middle and shrink away again, while the gap between them does the opposite. Each row still has three segments (stars, gap, stars), and every row is 2n characters wide. The widest row, where the wings touch, appears once, so there are 2n − 1 rows.
Example for n = 5:
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
vector<string> butterfly(int n); // 2n - 1 rows, each 2n characters wide
Input n | Output rows | Explanation |
|---|---|---|
| 1 | "**" | One row, the wings already touch |
| 2 | "* *", "****", "* *" | Gap 2, then 0, then 2 |
| 3 | "* *", "** **", "******", "** **", "* *" | Stars grow 1, 2, 3 then shrink |
| 5 | as in the example | 9 rows of 10 characters |
(Confirm: the widest row appears once, giving 2n − 1 rows; rows end with a star, so there are no trailing 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