Katkat

Check if a numeric string is divisible by 8

OtherData Scientist · Technical Screen

Last 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.
  • s consists only of digits '0'–'9'.
  • Leading zeros may exist (e.g., "00016").

Examples

  • s = "16" → True
  • s = "123" → False
  • s = "1000" → True