Pascal's Triangle

EasyDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer num_rows, return the first num_rows rows of Pascal's triangle as a list of lists. Row 0 is [1]. Each following row starts and ends with 1, and every interior entry is the sum of the two entries directly above it in the previous row. Row i therefore has i + 1 entries.

Examples

Input: num_rows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Each interior value is the sum of its two upper neighbours, e.g. 6 = 3 + 3.
Input: num_rows = 1
Output: [[1]]
Only the top row is requested.

Constraints

  • 1 <= num_rows <= 30

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems