Quick Overview

Decide whether a binary string of up to a billion bits represents a number divisible by 3, without converting it to an integer. It tests modular arithmetic, keeping a running value over a stream of bits, and scanning in constant space.

Check whether a huge binary string is divisible by 3

Company: Adobe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a binary string `s`, decide whether the number it represents is divisible by 3. Return `True` if it is and `False` otherwise. The string can be very long (up to ${10^9}$ characters), so converting it to an integer first may not be feasible. ### Function Signature ```python def is_divisible_by_3(s: str) -> bool: ``` ### Rules - `s` is an unsigned binary number written with the most significant bit first. - `"0"` represents zero, which is divisible by 3. ### Constraints - `1 <= len(s) <= 10^9` - `s` consists only of `'0'` and `'1'`. - `s` has no leading zeros, except when `s` is `"0"` itself. ### Examples ```text Input: s = "110" Output: True ``` `"110"` is 6 in decimal. 6 divided by 3 is 2 with no remainder. ```text Input: s = "101" Output: False ``` `"101"` is 5 in decimal. 5 divided by 3 is 1 with remainder 2. ```text Input: s = "1001" Output: True ``` `"1001"` is 8 + 1 = 9, which is 3 times 3.

Overview: Decide whether a binary string of up to a billion bits represents a number divisible by 3, without converting it to an integer. It tests modular arithmetic, keeping a running value over a stream of bits, and scanning in constant space.

Given a string `s` that holds an unsigned binary number, decide whether the number it represents is divisible by 3. Return `True` if it is and `False` otherwise (`true` or `false` in JavaScript, Java and C++). `s` is written with the most significant bit first. The string `"0"` represents zero, which is divisible by 3. The string can be very long (up to 10^9 characters), so converting it to an integer first may not be feasible. The number it represents can be far larger than 2^31 - 1, and far larger than any 64-bit integer. Only the string and a boolean cross the function boundary, and the length of `s` fits in a 32-bit signed integer, so the signatures need no Java `long` or C++ `long long`. ### Example 1 ```text Input: s = "110" Output: True ``` `"110"` is 6 in decimal. 6 divided by 3 is 2 with no remainder. ### Example 2 ```text Input: s = "101" Output: False ``` `"101"` is 5 in decimal. 5 divided by 3 is 1 with remainder 2. ### Constraints - `1 <= len(s) <= 10^9` - `s` consists only of `'0'` and `'1'`. - `s` has no leading zeros, except when `s` is `"0"` itself.

Constraints

  • 1 <= len(s) <= 10^9
  • s consists only of '0' and '1'.
  • s has no leading zeros, except when s is "0" itself.

Examples

Input: ('0',)

Expected Output: True

Explanation: Zero is divisible by 3; this is the smallest valid input.

Input: ('1',)

Expected Output: False

Explanation: The value 1 leaves remainder 1; a solution that skips the first bit would wrongly return True.

Hints

  1. The number can have up to 10^9 bits, far beyond any built-in integer type, so look for a small amount of information about each prefix of s that is enough to answer the question.
  2. Work through the examples by hand, reading "110" and "1001" one bit at a time from the left, and watch how the value of the prefix changes with each new bit.
  3. Remember that "0" is a valid input and represents zero, which is divisible by 3.

Divisibility by 3 of a Very Long Binary String

easyAdobeSoftware Engineer

Given a string s that holds an unsigned binary number, decide whether the number it represents is divisible by 3. Return True if it is and False otherwise (true or false in JavaScript, Java and C++).

s is written with the most significant bit first. The string "0" represents zero, which is divisible by 3.

The string can be very long (up to 10^9 characters), so converting it to an integer first may not be feasible. The number it represents can be far larger than 2^31 - 1, and far larger than any 64-bit integer. Only the string and a boolean cross the function boundary, and the length of s fits in a 32-bit signed integer, so the signatures need no Java long or C++ long long.

Example 1

Input: s = "110"
Output: True

"110" is 6 in decimal. 6 divided by 3 is 2 with no remainder.

Example 2

Input: s = "101"
Output: False

"101" is 5 in decimal. 5 divided by 3 is 1 with remainder 2.

Constraints

  • 1 <= len(s) <= 10^9
  • s consists only of '0' and '1'.
  • s has no leading zeros, except when s is "0" itself.

Examples

Example 1
Input
('0',)
Output
True
Notes
Zero is divisible by 3; this is the smallest valid input.
Example 2
Input
('1',)
Output
False
Notes
The value 1 leaves remainder 1; a solution that skips the first bit would wrongly return True.

Constraints

  • •1 <= len(s) <= 10^9
  • •s consists only of '0' and '1'.
  • •s has no leading zeros, except when s is "0" itself.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def is_divisible_by_3(s) -> bool:
Sample call
is_divisible_by_3('0')
Language
Case 1
Input
('0',)
Expected
True
Case 2
Input
('1',)
Expected
False

Browse More Questions

Show the approach

Approach

Read the bits from left to right and keep only the remainder, modulo 3, of the prefix read so far. If a prefix has value v and remainder r, appending bit b gives the value 2v + b, whose remainder is (2r + b) mod 3. The invariant is that after reading i characters the stored remainder equals the value of the first i bits modulo 3. It holds before the loop, since the empty prefix has value 0, and each step keeps it by the identity above. After the last character the stored remainder is therefore the value of s modulo 3, and s is divisible by 3 exactly when that remainder is 0. The remainder always stays in {0, 1, 2}, so no intermediate number grows with the input, which is why strings far longer than 64 bits are handled. An equivalent view: 2 is congruent to -1 modulo 3, so bit k counted from the least significant end adds 1 when k is even and subtracts 1 when k is odd, and the number is divisible by 3 exactly when the difference between those two counts of set bits is a multiple of 3. Counting set bits alone gives wrong answers: "111" has three set bits and equals 7. For the edge cases, "0" ends with remainder 0 and returns True, "1" ends with remainder 1 and returns False, and a power of two (a 1 followed by zeros) always leaves remainder 1 or 2, so it is never divisible.

Time complexity:
O(n)
Space complexity:
O(1)