Balanced Permutation Subarrays
Updated
Problem statement
You are given a permutation permutation of length n. For every integer k from 1 through n, check whether some subarray holds exactly the values 1, 2, ..., k, in any order. A subarray is a block of elements that sit next to each other in the array.
Return a binary string of length n. The character at index k - 1 (counting from zero) is '1' if such a subarray exists for k, and '0' if it does not.
A permutation of length n contains every integer from 1 through n exactly once.
Function
balancedPermutationSubarrays(permutation: int[]) → StringExamples
Example 1
permutation = [4,1,3,2]return = "1011"- For
k = 1, the one-element subarray[1]works. - For
k = 2, the values1and2are separated by3, so no length-2subarray contains exactly those values. - For
k = 3, the subarray[1, 3, 2]works. - For
k = 4, the whole permutation works.
Putting the four results together gives "1011".
Example 2
permutation = [2,1,4,3,5]return = "11011"The values 1 and 2 sit next to each other at the start, so the first two checks succeed. Adding 3 fails, because a block that reaches it from the first two elements must also take in the value 4. Adding 4 fixes that: the first four elements are exactly 1 through 4. The whole permutation works for k = 5. So the third character is the only zero in the answer.
Constraints
1 <= permutation.length <= 200000permutationis a permutation of the integers from1throughpermutation.length.
More Microsoft Online Assessment questions
- Maximum Score with Prime JumpsOA · Seen Dec 2026
- Minimum Non-Adjacent Selection CapabilityOA · Seen Dec 2026
- Authentication SystemOA · Seen Nov 2026
- Maximum String OperationsOA · Seen Nov 2026
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Maximum Escape Game ScoreOA · Seen Nov 2026
- Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
- Maximum Interactive Team SizeOA · Seen Nov 2026
- Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
Similar questions at other companies
- API Rate Limit Violation DetectionTekion · OATekion interview questions
- Count Subarrays With Product Equal to KSalesforce · OASalesforce interview questions
- Garden Sprinkler PlacementAirbnb · OAAirbnb interview questions
- Sliding-Window Means with IEEE Special ValuesHeadlands Technologies · OAHeadlands Technologies interview questions
- Maximum Beauty by DeletionsMeesho · OA
- Card Game: Best First-Player ScoreShopify · OAShopify interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Count Pairs of Overlapping SquaresCapital One · OACapital One interview questions
- Payment Partner RoutingStripe · OAStripe interview questions
- Last Object to Fall Off the TableConfluent · OAConfluent interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)