DSA interview question
A number-puzzle app awards a badge for "self-divisible" numbers, and its scoring rule is: for each digit of N, check whether that digit divides N with no remainder, and count how many do. A zero digit can't divide anything, so it's skipped, and repeated digits count once for each occurrence. The heart of the task is the most important basic-maths technique in DSA: pulling digits out of a number one at a time with % 10 and / 10, while keeping the original number intact so you can still test divisibility against it.
Example: N = 1012 has digits 1, 0, 1, 2. Zero is skipped; 1, 1 and 2 all divide 1012, so the answer is 3.
int evenlyDivides(int n); // count of digits d of n with d != 0 and n % d == 0
Input n | Output | Explanation |
|---|---|---|
| 12 | 2 | 12 % 1 == 0 and 12 % 2 == 0 |
| 1012 | 3 | Digits 1, 1, 2 divide; the 0 is skipped |
| 23 | 0 | 23 % 2 == 1 and 23 % 3 == 2 |
| 5 | 1 | A single-digit number always divides itself |
| 101 | 2 | Both 1s count; 0 is skipped |
| 111 | 3 | Repeated digits each count |
| 100000 | 1 | Only the leading 1 is a usable digit |
(Confirm: 1 ≤ n ≤ 10^9; every occurrence of a digit counts separately; zero digits are ignored.)
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