DSA interview question
A maths-learning app draws Pascal's triangle, where each row starts and ends with 1 and every inner number is the sum of the two numbers diagonally above it. The app needs three things: the first numRows rows for the drawing, a single entry (row r, column c) for a quiz question, and one full row on demand. All three rest on the fact that the entries are binomial coefficients, C(r − 1, c − 1), and that neighbouring coefficients in a row differ by a simple ratio.
Example: numRows = 5 → [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]].
vector<vector<long long>> generate(int numRows); // the first numRows rows
long long entry(int r, int c); // 1-indexed row r, column c: C(r-1, c-1)
vector<long long> row(int r); // the r-th row (1-indexed) in O(r)
| Input | Output | Explanation |
|---|---|---|
generate(5) | [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] | Each inner entry is the sum of the two above |
generate(1) | [[1]] | |
generate(2) | [[1],[1,1]] | |
entry(5, 3) | 6 | C(4, 2) |
row(6) | [1, 5, 10, 10, 5, 1] | Row 6 is C(5, 0..5) |
entry(31, 16) | 155117520 | C(30, 15) computed without factorials |
(Confirm: rows and columns are 1-indexed; numRows ≥ 1; values fit in a long long for the sizes used.)
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