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?
- A. 1
- B. 0
- C. 2
- 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
- Problem: How many non-empty subsets of {1, 2, 3, 4, 5, 6, 7, 8} have a sum that is a multiple of 4? Run the code. python from itertools…
- Problem: What is the minimum number of operations to make the array [5, 1, 4] strictly increasing where one operation increments any…
- Problem: What is the number of integers in [1, 999] whose digit product is 0? Run the code. python print(sum(1 for x in range(1, 1000) if…
- Problem: How many distinct pairs (i < j) of [6, 10, 15, 21] have gcd(ai, aj) = 1? Run the code. python from math import gcd a = [6, 10…
- Problem: What is the smallest positive integer that is divisible by every integer from 1 to 12? Run the code. python from math import lcm…
- Problem: Which technique gives the number of distinct subarrays of an array of length n in O(n log n) time using a suffix structure?