DSA interview question
Now turn the pyramid upside down: the first row is the widest (2n − 1 stars) and each row below loses a star on each side while gaining a leading space, until the last row has a single centred star. It tests whether you can derive formulas for a decreasing symmetric row, i − 1 spaces and 2(n − i) + 1 stars, instead of relying on the pyramid's formulas by memory.
Example for n = 3:
*****
***
*
vector<string> invertedPyramid(int n); // row i: i - 1 spaces, then 2(n - i) + 1 stars
Input n | Output rows | Explanation |
|---|---|---|
| 1 | "*" | One star |
| 2 | "***", " *" | Widest row first |
| 3 | "*****", " ***", " *" | Spaces grow by one, stars shrink by two |
| 4 | "*******", " *****", " ***", " *" |
(Confirm: no trailing spaces; exactly n rows.)
2(n − i) + 1?"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