PPractick

DSA interview question

Pattern Building Framework: Number Pyramid

Easy · Algorithms · Topics: pattern building

The problem

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

Function signature

vector<string> numberPyramid(int n);  // one string per row, leading spaces included, no trailing spaces

Test cases

Input nOutput rowsExplanation
11Single 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.)

How interviewers phrase it

Clarifying questions to ask

  1. What's the range of N? (Two-digit numbers break the alignment.)
  2. Should rows have trailing spaces, or should each row end right after the last character?
  3. Should I print the pattern directly or return the rows?
  4. Is the pattern 1-indexed as drawn (the first row has one number)?

Follow-ups to expect

  1. "How do you approach any pattern problem?"
  2. "How would you print it upside down?"
  3. "What changes for a diamond?"
  4. "What if N can be 12?"
  5. "What's the 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