Accenture · Question 13 · Arrays
Pascal's Triangle
Problem
Generate the first numRows of Pascal’s triangle.
Approach
Each number is the sum of the two numbers directly above it.
Time O(numRows²)Space O(numRows²)
How it works
- 1Each row starts and ends with 1.
- 2Inner values sum the two above.
Sample input / output
Input: numRows = 5 Output: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
Constraints
1 <= numRows <= 30.
Solution — read it, then walk it
Time O(numRows²)Space O(numRows²)Loops 2Branches 2Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/17
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits