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`

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.

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.

Numeric String Divisible by 8

easyGeneralData Scientist

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

View full question (all parts)
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

Examples

Example 1
Input
('16',)
Output
True
Notes
Divisible.
Example 2
Input
('123',)
Output
False
Notes
Not divisible.

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 solution(s) -> bool:
Sample call
solution('16')
Language
Case 1
Input
('16',)
Expected
True
Case 2
Input
('123',)
Expected
False

Browse More Questions