Get Balanced String
Updated
Problem statement
You are given an array arr of length n. It is a permutation of 1, 2, ..., n, so every number from 1 to n appears exactly once.
A number k is called balanced if some block of numbers that sit next to each other, arr[i], arr[i+1], ..., arr[j], is a permutation of 1, 2, ..., k. In other words, the numbers 1 to k fill exactly k cells in a row.
For example, in [4, 1, 3, 2], k = 3 is balanced because [1, 3, 2] is a permutation of 1, 2, 3. But k = 2 is not balanced, because 1 and 2 are not next to each other.
Return a binary string of length n. The character at 0-indexed position k - 1 is '1' if k is balanced, and '0' otherwise.
Function
getBalancedString(arr: int[]) → StringExamples
Example 1
arr = [4, 1, 3, 2]return = "1011"k=1: subarray [1] is a permutation of {1} → balanced. k=2: no contiguous subarray equals {1, 2} → not balanced. k=3: subarray [1, 3, 2] is a permutation of {1, 2, 3} → balanced. k=4: full array [4, 1, 3, 2] is a permutation of {1, 2, 3, 4} → balanced.
Example 2
arr = [1, 2, 3]return = "111"For every k from 1 to 3, the prefix arr[0..k-1] is already a permutation of {1, ..., k}.
Example 3
arr = [2, 3, 1]return = "101"k=1: subarray [1] exists → balanced. k=2: no contiguous subarray is exactly {1, 2} — elements 1 and 2 are separated by 3 → not balanced. k=3: full array [2, 3, 1] is a permutation of {1, 2, 3} → balanced.
Example 4
arr = [1]return = "1"There is one number, 1. The block [1] is a permutation of {1}, so k = 1 is balanced and the answer is "1".
Constraints
1 ≤ n ≤ 105, wherenisarr.lengtharris a permutation of1, 2, ..., n
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
Similar questions at other companies
- API Rate Limit Violation DetectionTekion · OATekion interview questions
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
- Count Subarrays with at Least K Equal-Fruit PairsTikTok · OATikTok interview questions
- Consistent LogsRippling · OARippling interview questions
- Minimum Length Good SubarrayMicrosoft · OAMicrosoft interview questions
- Auction Sniping Window MinimumCisco · OACisco interview questions
- Shortest Subarray Covering an Integer RangeMarshall Wace · OAMarshall Wace interview questions
- Count Subarrays with at Least K Distinct ValuesVisa · OAVisa interview questions
- Longest Frequency-Bounded SubarrayZipRecruiter · OAZipRecruiter interview questions