DSA interview question
Pattern questions ("print this triangle of stars or numbers") look like puzzles, but they're really a test of whether you can translate a picture into loop bounds. Instead of memorising 22 separate patterns, you want one method that solves all of them: (1) the outer loop counts rows, (2) for each row, work out how many of each character appear as a formula in the row number, (3) print them with inner loops, (4) look for symmetry. Apply the method to a medium pattern, a centred pyramid where each row counts up to the row number and back down again.
Example for n = 4:
1
121
12321
1234321
vector<string> numberPyramid(int n); // one string per row, leading spaces included, no trailing spaces
Input n | Output rows | Explanation |
|---|---|---|
| 1 | 1 | Single row, no leading spaces |
| 2 | " 1", "121" | Row 1 has one leading space |
| 3 | " 1", " 121", "12321" | Leading spaces shrink by one per row |
| 4 | " 1", " 121", " 12321", "1234321" | Row i has n − i spaces, then 1..i, then i − 1..1 |
(Confirm: 1 ≤ n ≤ 9 so every number is a single digit and the pyramid stays aligned; rows have 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