PPractick

DSA interview question

Pattern 20: Symmetric Butterfly

Medium · Algorithms · Topics: pattern building

The problem

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:

*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

Function signature

vector<string> butterfly(int n);  // 2n - 1 rows, each 2n characters wide

Test cases

Input nOutput rowsExplanation
1"**"One row, the wings already touch
2"* *", "****", "* *"Gap 2, then 0, then 2
3"* *", "** **", "******", "** **", "* *"Stars grow 1, 2, 3 then shrink
5as in the example9 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.)

How interviewers phrase it

Clarifying questions to ask

  1. Should the middle (full) row appear once or twice?
  2. Is N the number of stars on each side of the widest row?
  3. Print or return?

Follow-ups to expect

  1. "What's the star count per side in row r?"
  2. "And the gap?"
  3. "How is it related to Pattern 19?"
  4. "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