Balanced Prefix Permutation
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this Uber question, reported in February 2026, is that p is a full permutation of 1..n, and you output a binary string saying whether each prefix of length k is exactly {1..k}. Sounds like set checking. It isn't. The pattern is a running maximum, and n goes up to 2 * 10^5, so rebuilding sets per prefix will time out. If you've got an Uber OA invite and 48 hours, this is a ten-minute problem once you see it. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.
The problem
You are given a permutation p of size n. For each prefix p[0..k-1], determine whether it forms a valid permutation of the integers from 1 to k. A prefix is valid if it contains every integer from 1 to k exactly once. Return a binary string of length n, where the k-th character is '1' if the prefix of length k is valid, and '0' otherwise. Function countBalancedPrefixes(p: int[]) → String Examples Example 1 p = [1, 4, 2, 3] return = "1001" The prefix [1] is a permutation of [1], and the full prefix [1,4,2,3] is a permutation of [1,2,3,4]. The middle prefixes are not valid. Example 2 p = [2, 1, 3] return = "011" The first prefix is not [1]. Prefixes of lengths 2 and 3 contain exactly 1..2 and 1..3. Constraints 1 <= p.length <= 2 * 10^5 p is a permutation of integers from 1 to p.length.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: because p is a permutation, all values are distinct. A prefix of length k contains exactly 1..k if and only if its maximum equals k. Distinct values with max k and k elements means they must be 1..k. So walk the array once, track the running max, and at index i (0-based) append '1' if max == i+1, else '0'. That's O(n) time and O(1) extra space beyond the output. The common pitfall is sorting or building a set per prefix, which is O(n^2) or O(n^2 log n) and dies at 2 * 10^5. Another slip is off-by-one on the index. Check Example 2: [2,1,3] gives max 2 at k=1 (0), max 2 at k=2 (1), max 3 at k=3 (1). That's 011. If you freeze during the live OA, StealthCoder is the hedge that hands you this loop.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Balanced Prefix Permutation 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 find the prefix common array of two arrays. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Uber's OA.
Uber 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.
Balanced Prefix Permutation FAQ
What's the trick to Balanced Prefix Permutation?+
Track the running maximum. Since p is a permutation, values are distinct, so a prefix of length k is exactly {1..k} when its max equals k. One pass, compare max to the 1-based index, append '1' or '0'. No sets, no sorting.
How hard is this Uber OA question really?+
Easy once you spot the max observation, easy-medium if you don't. The code is about five lines. The difficulty is realizing you don't need to verify contents, only the maximum, because distinctness is guaranteed by the permutation constraint.
What time complexity do I need for n up to 2 * 10^5?+
Linear, O(n). Anything that rebuilds or sorts each prefix is quadratic or worse and will time out. The running max approach uses a single loop and constant extra space aside from the output string, so it fits comfortably.
What edge cases should I test?+
Test n = 1 with [1], which returns '1'. Test an already sorted array like [1,2,3], which returns all ones. Test a reversed array like [3,2,1], which returns '001'. Also recheck the off-by-one by comparing max to i+1 for 0-based indexing.
How do I prepare for this in 48 hours?+
Practice problems where a permutation or distinctness guarantee lets you replace a set check with a max, sum, or count comparison. Write this solution from memory twice, then trace both examples by hand. Spend the rest of your time on general array and prefix problems.