PPractick

DSA interview question

Count Digits That Evenly Divide N

Easy · Algorithms · Topics: basic maths, number theory

The problem

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.

Function signature

int evenlyDivides(int n);  // count of digits d of n with d != 0 and n % d == 0

Test cases

Input nOutputExplanation
12212 % 1 == 0 and 12 % 2 == 0
10123Digits 1, 1, 2 divide; the 0 is skipped
23023 % 2 == 1 and 23 % 3 == 2
51A single-digit number always divides itself
1012Both 1s count; 0 is skipped
1113Repeated digits each count
1000001Only the leading 1 is a usable digit

(Confirm: 1 ≤ n ≤ 10^9; every occurrence of a digit counts separately; zero digits are ignored.)

How interviewers phrase it

Clarifying questions to ask

  1. How should zero digits be treated? (They can't be divisors.)
  2. Do repeated digits count once or once per occurrence?
  3. Can N be negative or zero?
  4. What's the range of N?

Follow-ups to expect

  1. "How do you get the last digit and remove it?"
  2. "Why work on a copy of N?"
  3. "What's the time complexity?"
  4. "How would you just count the digits?"
  5. "What about negative N?"

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