Quick Overview

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.

Check if a numeric string is divisible by 8

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

##### Question 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`

Quick Answer: 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.

Return whether a very large non-negative decimal string is divisible by 8 without converting the whole string.

Examples

Input: ('16',)

Expected Output: True

Explanation: Divisible.

Input: ('123',)

Expected Output: False

Explanation: Not divisible.

Hints

  1. A decimal integer is divisible by 8 iff its last three digits are divisible by 8.

Loading coding console...