Max Chunks to Make Sorted
Updated
Problem statement
You are given an integer array arr. Cut it into pieces, where each piece is a block of neighbouring elements and every element belongs to exactly one piece. Then sort each piece on its own, and put the sorted pieces back in their original order.
A cut is valid when the result is the whole array in non-decreasing order. Return the largest number of pieces you can cut the array into.
Function
maxChunksToSorted(arr: int[]) → intExamples
Example 1
arr = [2,5,1,8,6,7]return = 2Cut into [2, 5, 1] and [8, 6, 7]. Sorted, they are [1, 2, 5] and [6, 7, 8], and together [1, 2, 5, 6, 7, 8], the whole array in order. No cut into 3 or more pieces works: the 1 at index 2 drags the 2 and the 5 into its piece, and the 6 at index 4 drags the 8 into its piece. The answer is 2.
Example 2
arr = [9,7,6,3,5,2,1]return = 1The smallest value 1 is last, so the first piece must reach all the way to the end. The array cannot be cut at all. The answer is 1.
Example 3
arr = [1,1,0,3,3]return = 3Cut into [1, 1, 0], [3] and [3]. Sorted, the pieces give [0, 1, 1], [3], [3], which is the whole array in order. Equal values may sit in different pieces. The answer is 3.
Example 4
arr = [-4,-2,-2,0]return = 4The array is already in order, so every element can be its own piece, including the equal pair -2, -2. The answer is 4.
Constraints
1 <= arr.length <= 2000-1000000000 <= arr[i] <= 1000000000- Values can repeat.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Most Segments That Sort the ArrayRippling · OARippling interview questions
- Build Maximum Integer from Subsequence (Google India)Google · PHONE SCREENGoogle interview questions
- Trapping Rain WaterTeradata · ONSITE INTERVIEW
- Minimum Taps to Water a GardenThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Minimum Candies by RatingGroww · ONSITE INTERVIEW
- Allocate Minimum PagesAmazon · ONSITE INTERVIEW
- Largest Rectangle in HistogramTekion · ONSITE INTERVIEWTekion interview questions
- Trapping Rain WaterKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Confirmed Profiling Events with First-Observed StartsAnthropic · PHONE SCREEN · ONSITE INTERVIEWAnthropic interview questions
Interview reports that mention this question
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)