Balanced Prefix Sets in a Permutation
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Uber asked this in April 2026: given a permutation of 1 to n, for each k from 1 to n, check if there's a contiguous subarray containing exactly the set {1, 2,..., k}. Output a binary string where the k-th bit is '1' if such a subarray exists. You're not studying. You have 24-72 hours and an OA invite. StealthCoder reads this problem live and feeds you the pattern in real time so you don't blank.
The problem
You are given a permutation p of the integers 1 through n. For each k from 1 to n, determine whether there exists a contiguous subarray whose set of values is exactly {1, 2,..., k}. Build a binary string of length n. The k-th character should be '1' if such a subarray exists for that k, otherwise it should be '0'. Function Description Complete the function balancedPrefixSetsMask in the editor below. balancedPrefixSetsMask has the following parameter: Returns The source thread did not provide explicit numeric bounds.Reported by candidates. Source: FastPrep
Pattern and pitfall
The trap is thinking you need to check all subarrays for each k. You don't. The key insight: a contiguous subarray has exactly {1, 2,..., k} if and only if its length is k and its max element is k. For each position, track the span from the smallest position where 1 appears to the current position. When max(subarray) equals k and length equals k, mark k as valid. Use a sliding window or single-pass approach: iterate through the permutation, maintain the minimum and maximum indices seen, and check if max value equals span length. This is a counting and range-checking pattern. StealthCoder catches the off-by-one errors and the need to track indices separately from values, both common mistakes in the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Balanced Prefix Sets in a 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
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 Sets in a Permutation FAQ
Do I need to check every subarray?+
No. Only subarrays where the max element is k and the length is k can be valid for k. A one-pass scan with index tracking avoids the O(n^3) trap. That's the speed hack.
How do I know if a subarray has exactly {1,2,...,k}?+
If it's contiguous and contains k elements with max value k, it must be exactly {1,...,k}. No gaps possible in a permutation. Check length and max only.
What's the time complexity I should aim for?+
O(n) or O(n log n). Don't iterate all O(n^2) subarrays. Track min/max indices of where each value appears, then check the span in constant time per k.
Are there tricky edge cases with k=1 or k=n?+
k=1 is always valid (single element 1 exists). k=n is valid only if the entire array is a contiguous block, rare. Check both ends; they're fast to validate.
How do I prepare in 48 hours without drilling?+
Understand the insight: valid sets correspond to specific spans. Sketch one small example (n=4) by hand, see the pattern, then code the one-pass check. That's it.