Problem
Balanced Prefix Permutation
Learn this problemProblem statement
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[]) → StringExamples
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^5pis a permutation of integers from1top.length.
More Uber problems
- Minimum Refueling StopsONSITE INTERVIEW · Seen Jul 2026
- Last Truck to Leave the LaneOA · Seen Jul 2026
- Chain of CommandOA · Seen Jul 2026
- Jump Game with Prime-3 StepsOA · Seen Jun 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · Seen May 2026
- Tournament Rounds by RankPHONE SCREEN · Seen May 2026
- Farthest Seat AssignmentONSITE INTERVIEW · Seen May 2026