FastPrepBalanced Permutation Subarrays
Problems/Balanced Permutation Subarrays

Balanced Permutation Subarrays

Uber logoUberFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given a permutation p of size n. For each value k from 1 to n, determine whether there exists a contiguous subarray whose elements form a permutation of the integers from 1 to k.

Return a binary string of length n. The k-th character is '1' if k is balanced, and '0' otherwise.

Function

countBalancedNumbers(p: int[]) → String

Examples

Example 1

p = [1, 3, 2, 4]return = "1011"

For k=1, the subarray [1] works. For k=2, no contiguous subarray contains exactly {1,2}. For k=3, [1,3,2] works, and for k=4, the full array works.

Example 2

p = [3, 1, 2, 4]return = "1111"

Each set {1..k} appears as a contiguous subarray for every k from 1 to 4.

Example 3

p = [4, 1, 3, 2]return = "1011"

For k=1, the subarray [1] works. For k=2, no contiguous subarray forms exactly {1,2}. For k=3, the subarray [1,3,2] works, and for k=4, the entire array works.

Constraints

  • 1 <= p.length <= 2 * 10^5
  • p is a permutation of integers from 1 to p.length.

More Uber Online Assessment questions

  • Count Palindromic Ancestor PathsOA · Seen Aug 2026
  • Jump Game with Prime-3 StepsOA · Seen Aug 2026
  • Last Truck to Leave the LaneOA · Seen Jul 2026
  • Chain of CommandOA · Seen Jul 2026
  • Total Palindrome Substring CostOA · Seen Jun 2026
  • Jump Game with Prime-Step RuleOA · Seen May 2026
  • Pipeline Throughput Maximization Under BudgetOA · Seen May 2026
  • Balanced Prefix Sets in a PermutationOA · Seen Apr 2026
  • Minimum Edge Reversals to Root a TreeOA · Seen Apr 2026
  • Maximum Riders in CarOA · Seen Apr 2026
All Uber interview questionsSee Uber hiring insights
Saved
class Solution {
    public String countBalancedNumbers(int[] p) {
      // write your code here
    }
}
Checking account…