DSA interview question
A fitness app awards points on a rising scale: 1 point on day 1, 2 on day 2, and so on, and it needs the total after N days, which is 1 + 2 + ... + N. There's a famous formula for it, but the exercise is to compute it recursively in two styles: parameterised recursion, where a running sum is carried along as an argument, and functional recursion, where each call returns its answer built from the smaller call's answer. The difference between "carry the result down" and "build the result on the way back up" is the core idea behind every recursive DP later on.
Example: N = 5 → 1 + 2 + 3 + 4 + 5 = 15.
long long sumParameterised(int n, long long acc = 0); // carries the running sum
long long sumFunctional(int n); // returns n + sum(n - 1)
Input n | Output | Explanation |
|---|---|---|
| 5 | 15 | 1 + 2 + 3 + 4 + 5 |
| 1 | 1 | |
| 0 | 0 | Empty sum |
| 100 | 5050 | Matches n(n + 1) / 2 |
| 10000 | 50005000 | 10,000 levels of recursion; fine for the default stack |
(Confirm: 0 ≤ n ≤ 10^4 so the recursion depth is safe; the result can also be checked against n(n + 1) / 2.)
long long?"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