Ctrl K

Which Prefixes Divide Evenly

Medium
You are given a string digits made of decimal digits and a positive integer divisor. For every position i, read the prefix digits[0..i] as a decimal number (leading zeros are fine) and check whether it is divisible by divisor. Return a list as long as digits: position i holds 1 if the prefix of length i + 1 is divisible by divisor, and 0 otherwise.
Example 1
digits="1236", divisor=4 → [0, 1, 0, 1]

The prefixes are 1, 12, 123 and 1236. Of these, 12 and 1236 are divisible by 4.

Example 2
digits="0705", divisor=7 → [1, 1, 1, 0]

The prefixes "0", "07" and "070" are the numbers 0, 7 and 70, all divisible by 7. But 705 = 7 · 100 + 5 is not.

Constraints
  • 1 ≤ len(digits) ≤ 10⁵
  • digits consists of the digits 0–9
  • 1 ≤ divisor ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsClosest Clock Readings MediumRound Prices to a Total MediumAlarm Beams Between Rows MediumNumbers From Working Keys HardLetter Sum Cipher Hard
loading Python…
Input
digits="1236", divisor=4
Expected
[0, 1, 0, 1]

Run checks the first 3 tests in your browser. Submit checks all 8 tests on the server, hidden ones included.