← Bank of America Interview Insights
My first instinct was just int(s) % 8 == 0 and call it a day, but the constraint kills that immediately.
Explain that divisibility by 8 depends only on the last three digits of the number, so you can extract the last three characters of the string and check if that three-digit number is divisible by 8. This avoids converting the entire string to an integer, which is crucial for very large inputs.
Pro tip: Mention that this approach is O(1) time and space with respect to the number's length, and that it can be extended to other powers of 2 (e.g., divisibility by 16 uses the last four digits). This shows you understand the underlying number theory and can generalize.
Recall that a number is divisible by 8 if and only if its last three digits form a number divisible by 8. This is because 1000 is divisible by 8.
From the decimal string, take the substring of the last three characters. If the string length is less than 3, pad with leading zeros or treat the whole string as the number.
Convert the three-character substring to an integer (which is safe since it's at most 3 digits) and check if it is divisible by 8 (i.e., number % 8 == 0).
Consider cases where the string has leading zeros, is empty, or represents negative numbers (though the problem likely assumes non-negative). Also, if the string length is less than 3, just check the whole number.
Highlight that this method is O(1) in time and space relative to the number's length, and contrast it with the naive approach of converting the entire string to an integer, which would be infeasible for millions of digits.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.