Check Permutation Divisible by Eight
Reported by candidates from HackerRank's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure this one hinges on is a plain digit-count array, and HackerRank candidates reported it in March 2026. You get an array of digit strings and need to say whether any permutation of each one is divisible by 8. Brute-forcing permutations dies fast on long strings, so don't. The trick is a divisibility rule that only cares about the last three digits. If you freeze up on the OA, StealthCoder runs invisibly on your screen as a safety net and hands you the approach in real time.
The problem
You are given an array of strings numbers. Each string consists of decimal digits. For each string, determine whether any permutation of its digits can form a number divisible by 8. Return an integer array where the i-th value is 1 if some permutation of numbers[i] is divisible by 8, and 0 otherwise. A permutation may include leading zeros; the permuted string is interpreted as a decimal number. Function checkPermutationDivisibleByEight(numbers: String[]) → int[] Examples Example 1 numbers = ["61", "75", "123"] return = [1, 0, 1] "61" can be permuted to "16", which is divisible by 8. "75" has no valid permutation. "123" can be permuted to "312", which is divisible by 8. Example 2 numbers = ["8", "10", "1000"] return = [1, 0, 1] "8" is already divisible by 8. Neither "10" nor "01" is divisible by 8. "1000" is divisible by 8. Constraints Each string in numbers contains only decimal digits.
Reported by candidates. Source: FastPrep
Pattern and pitfall
A number is divisible by 8 if its last three digits are. So you never need to permute the whole string. Count the digits of each string into a size-10 frequency array. Then check every multiple of 8 from 0 to 999. For each multiple, pad it to three digits, count its digits, and see if your frequency array can cover them. For strings shorter than three digits, pad only to the string's length, since a permutation can't invent extra digits. Leading zeros are allowed, so "10" fails but "016" style arrangements count. The common pitfall is padding short strings with zeros they don't have. Example: "8" works, "10" doesn't. Handle lengths 1 and 2 separately, or compare against the actual length. If the live OA blanks your memory on that edge case, StealthCoder is the hedge that catches it.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Check Permutation Divisible by Eight cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass HackerRank's OA.
HackerRank reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Check Permutation Divisible by Eight FAQ
What's the trick for Check Permutation Divisible by Eight?+
Only the last three digits decide divisibility by 8. Count the digits in each string, then test whether those counts can build any three-digit multiple of 8. You never generate real permutations, which keeps it fast even for very long strings.
How hard is this problem really?+
Easy to medium. The code is short once you know the rule. The difficulty is seeing that permutations are a distraction. Edge cases with strings of length one or two are where most people lose points.
How do I handle strings shorter than three digits?+
Don't pad with zeros you don't own. For length 1, check multiples of 8 below 10 using that single digit. For length 2, check two-digit forms of multiples of 8 with leading zero allowed, matching your digit counts exactly.
What's the time complexity?+
Per string, counting digits is O(n). Then you check at most 125 multiples of 8 in the range 0 to 999, each with a constant-size comparison. Total is O(n) per string, so linear in total input size.
How do I prepare in 48 hours?+
Practice digit-frequency counting and divisibility rules for 8, 3, 9 and 11. Write this solution once from scratch using a count array of size 10. Test strings like "8", "10", "1000" and "123" by hand before the assessment.