← Accenture bank

Accenture · Question 13 · Arrays

Pascal's Triangle

ArraysC++code⏱ O(numRows²)💾 O(numRows²)
Service-based

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

  1. 1Each row starts and ends with 1.
  2. 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.
↻ repeat↻ repeatSTARTinclude <iostream>include <vector>using namespace stdvector<vector<int>>generate(int numRows)DEFINE main()auto t = generate(5)cout << "["for (size_t i = 0; i <t.size(); ++i)if (i) cout << ", "cout << "["for (size_t j = 0; j <t[i].size(); ++j)if (j) cout << ", "cout << t[i][j]cout << "]"cout << "]" << endlRETURN 0END
  • 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