Number of Ways to Reorder Array to Get Same BST
A hard-tier problem at 54% community acceptance, tagged with Array, Math, Divide and Conquer. Reported in interviews at DE Shaw and 0 others.
You're facing a problem that looks like a tree question but is actually a combinatorics trap. Given an array, you need to count how many ways you can reorder it such that building a BST from the reordered array produces the identical tree as the original. DE Shaw asks this. It sits at hard difficulty with a 54% acceptance rate, which means the naive approach fails fast and the real solution lives in recognizing a hidden mathematical pattern underneath the tree structure.
Companies that ask "Number of Ways to Reorder Array to Get Same BST"
Number of Ways to Reorder Array to Get Same BST is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.
Get StealthCoderThe trick: you're not actually building trees. You're counting permutations. Given a BST built from the original array, elements in the left subtree must appear before the root in any valid reordering, and right subtree elements must also respect their internal ordering. The insight is that you can independently choose how to interleave left and right subtree orderings, then multiply by the counts for each subtree. This is pure combinatorics (Catalan numbers and binomial coefficients), wrapped in a tree traversal. Most candidates start by trying to simulate tree construction or use excessive recursion without memoization. The real solution combines Divide and Conquer on the tree structure with Dynamic Programming to cache subtree results. If you hit this during an OA and the recursive formula isn't clicking, StealthCoder surfaces the exact recurrence relation and modular arithmetic needed to avoid overflow.
Pattern tags
You know the problem.
Make sure you actually pass it.
Number of Ways to Reorder Array to Get Same BST recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Number of Ways to Reorder Array to Get Same BST interview FAQ
Is this really a tree problem or a math problem?+
Both. You traverse the BST to partition elements into left and right subtrees, then apply combinatorics. The tree structure is scaffolding. The core is counting interleavings of two sequences using binomial coefficients. If you skip the math insight and only code tree traversal, you'll time out.
Do I need to actually construct BSTs to solve this?+
No. You infer the partition from the original array and root value. The insight that left subtree values always come before right subtree values in any valid permutation collapses the search space. This is why most brute force attempts fail.
What's the memoization key?+
Typically the indices of the subarray you're processing. You compute the number of valid reorderings for each subtree once, then reuse it. Without memoization, you'll recalculate the same subtree counts multiple times and blow the time limit.
How does Union Find fit into this?+
It doesn't, for most solutions. Some candidates mistakenly chase Union Find because the problem list includes it. Focus on Divide and Conquer with memoization. Union Find is a red herring in the topic list.
Why is the acceptance rate only 54% if it's a single company ask?+
Hard difficulty plus the conceptual leap from 'count reorderings' to 'combinatorics on tree partitions' makes it genuinely tricky. Most people either implement a brute force permutation checker or get stuck on the interleaving formula. The solution is elegant once you see it, brutal if you don't.
Want the actual problem statement? View "Number of Ways to Reorder Array to Get Same BST" on LeetCode →