Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many ways are there to split the string '1234' into two non-empty parts whose integer values are both divisible by 3? Run the code.

Problem: How many ways are there to split the string '1234' into two non-empty parts whose integer values are both divisible by 3? Run the code.
s = '1234'
print(sum(1 for k in range(1, len(s)) if int(s[:k]) % 3 == 0 and int(s[k:]) % 3 == 0))
What is printed?
  1. A. 1
  2. B. 0
  3. C. 2
  4. D. 3

Correct answer: B. 0

Splits give 1|234, 12|34 and 123|4, and none has both parts divisible by 3 (123 is divisible by 3 but 4 is not), so the count is 0.

More Coding Interview Problems questions

Exams that ask this topic