PPractick

DSA interview question

For Loops: Multiplication Table and Sum of Even Numbers

Easy · Algorithms · Topics: language basics

The problem

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.

Function signature

vector<long long> multiplicationTable(int n);  // n x 1 ... n x 10
long long sumOfEvens(int limit);               // 2 + 4 + ... up to limit

Test cases

InputOutputExplanation
multiplicationTable(3)3 6 9 12 15 18 21 24 27 30Ten entries, 1 through 10 inclusive
multiplicationTable(0)0 0 0 0 0 0 0 0 0 0Zero times anything is zero
sumOfEvens(10)302 + 4 + 6 + 8 + 10, where 10 itself is included
sumOfEvens(7)122 + 4 + 6; 7 is odd so it's not added
sumOfEvens(1)0No even number between 1 and 1
sumOfEvens(100000)2500050000Larger 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.)

How interviewers phrase it

Clarifying questions to ask

  1. Is the upper limit inclusive? Should sumOfEvens(10) include 10?
  2. Can the limit be zero or negative?
  3. How large can the limit be? (Decides whether the sum overflows an int.)
  4. Should the function return the values or print them?

Follow-ups to expect

  1. "Why i <= 10 and not i < 10?"
  2. "Can you avoid checking every number for evenness?"
  3. "Can you do it in O(1)?"
  4. "What's the complexity of two nested loops?"
  5. "What's the difference between break and continue?"
  6. "What's a range-based for loop?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in