Check if a numeric string is divisible by 8
OtherData Scientist · Technical ScreenLast updated
Determine whether a very large non-negative integer given as a decimal string is divisible by 8 without converting the whole string to an integer. The key insight is that only the last three digits matter (since 1000 is a multiple of 8), giving an O(1) check; a streaming modular-remainder approach is the O(n) alternative.
You are given a very large non-negative integer represented as a decimal string s (it may be too large to fit in standard 64-bit integer types).
Write a function that returns True if the number represented by s is divisible by 8, otherwise return False.
Constraints / Requirements
- Do not convert the entire string to an integer type (e.g., no
int(s)). 1 <= len(s) <= 10^6.sconsists only of digits'0'–'9'.- Leading zeros may exist (e.g.,
"00016").
Examples
s = "16"→Trues = "123"→Falses = "1000"→True
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.