Beautiful Arrangement
Reported by candidates from MathWorks's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
n tops out at 15, and that single number is the whole story of the Beautiful Arrangement question MathWorks reportedly used in July 2026. Brute force over all 15! permutations is over a trillion, so it dies. But the divisibility rule prunes almost everything early, which is the point. This is backtracking, with a bitmask DP as the upgrade. If you've got an OA coming, know the pruning idea cold. StealthCoder sits invisibly on your screen as a safety net in case you blank mid-assessment.
The problem
Given an integer n, consider permutations of the integers from 1 through n. A permutation perm is a beautiful arrangement when, for every 1-indexed position i, at least one of these conditions holds: perm[i] is divisible by i. i is divisible by perm[i]. Return the number of beautiful arrangements. Function countArrangement(n: int) → int Examples Example 1 n = 2 return = 2 The valid arrangements are [1,2] and [2,1]. Each value is compatible with its 1-indexed position. Example 2 n = 1 return = 1 The only arrangement is [1], and 1 is compatible with position 1. Constraints 1 <= n <= 15
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: don't generate permutations and then check them. Build the arrangement position by position, and at each position i only try unused numbers where num % i == 0 or i % num == 0. Dead branches get cut the moment a placement fails. Track used numbers with a boolean array or a bitmask. The common pitfall is filling values 1..n into positions instead of checking the condition per position, or forgetting the arrangement is 1-indexed, so you test i against the wrong index. A good speedup is filling positions from n down to 1, since high positions have fewer valid candidates and prune sooner. For a cleaner approach, use a bitmask DP where the mask's popcount gives the current position, and memoize on mask. That's O(2^n * n). If the live OA freezes you, StealthCoder can hand you the working solution so you aren't stuck.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Beautiful Arrangement 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as beautiful arrangement. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass MathWorks's OA.
MathWorks reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Beautiful Arrangement FAQ
What's the trick to Beautiful Arrangement?+
Backtrack position by position and only place a number if it's divisible by the position or the position is divisible by it. Invalid branches die immediately, so you never enumerate full permutations. Mark used numbers with a visited array or bitmask and count each complete arrangement.
Why does n <= 15 matter so much?+
It signals exponential is fine, but factorial isn't. 15! is far too large, while pruned backtracking or a 2^15 bitmask DP runs comfortably. The small bound is a hint that you should think backtracking or bitmask, not a polynomial formula.
Should I use backtracking or bitmask DP?+
Plain backtracking with pruning is enough for n up to 15 and is quicker to write under pressure. Bitmask DP is cleaner on complexity, with 2^n states and n transitions each. Pick backtracking first, and mention DP if the interviewer asks for optimization.
What's the most common bug on this problem?+
Off-by-one on indexing. The problem is 1-indexed, so position i runs from 1 to n, and you test the value against i, not i-1. Another bug is forgetting to unmark a number when backtracking, which silently undercounts results.
How do I prepare for this in 48 hours?+
Write the backtracking solution from scratch twice, then convert it to a bitmask memo version once. Test n=1, n=2, and n=15 for speed. Related permutation-with-constraints problems use the same skeleton, so you're really learning one reusable pattern.