DSA interview question
A primary-school teacher wants a small helper for revision worksheets. Given a number n, it should produce the multiplication table of n from 1 to 10. Given an upper limit, it should produce the sum of all even numbers from 1 to that limit. Both tasks repeat an action a known number of times, which is exactly what a for loop is for. They also expose the three classic loop mistakes: starting at the wrong value, stopping one step early or late, and stepping by the wrong amount.
Example: multiplicationTable(3) → 3 6 9 12 15 18 21 24 27 30; sumOfEvens(10) → 2 + 4 + 6 + 8 + 10 = 30.
vector<long long> multiplicationTable(int n); // n x 1 ... n x 10
long long sumOfEvens(int limit); // 2 + 4 + ... up to limit
| Input | Output | Explanation |
|---|---|---|
multiplicationTable(3) | 3 6 9 12 15 18 21 24 27 30 | Ten entries, 1 through 10 inclusive |
multiplicationTable(0) | 0 0 0 0 0 0 0 0 0 0 | Zero times anything is zero |
sumOfEvens(10) | 30 | 2 + 4 + 6 + 8 + 10, where 10 itself is included |
sumOfEvens(7) | 12 | 2 + 4 + 6; 7 is odd so it's not added |
sumOfEvens(1) | 0 | No even number between 1 and 1 |
sumOfEvens(100000) | 2500050000 | Larger than INT_MAX, so the sum needs long long |
(Confirm: the limit is inclusive and at least 1; the table always has exactly 10 entries.)
sumOfEvens(10) include 10?int.)i <= 10 and not i < 10?"break and continue?"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