Minimum Data Chunks with an Indivisible Header
Updated
Problem statement
You are sending a payload of data bytes with a leading header of header bytes. Split the transmission into the fewest possible chunks, each containing at most maxChunkSize bytes, and return their sizes in transmission order.
The payload may be split at any integer byte boundary. The entire header must be in the first chunk, and payload bytes may share that chunk. A header of size 0 represents a transmission without a header. Every returned chunk must have positive size, and the sizes must sum to data + header.
Use this rule to pick the chunk sizes among the solutions with the fewest chunks:
- If the total size is
0, return an empty array. - If one chunk is sufficient, return the total size as its only element.
- Otherwise, let
Kbe the minimum chunk count andT = data + header. Set the first size tomax(header, floor(T / K)). - Divide the remaining bytes as evenly as possible among the remaining
K - 1chunks. Their sizes differ by at most1; place smaller sizes before larger sizes.
Function
splitDataWithHeader(data: int, header: int, maxChunkSize: int) → int[]Examples
Example 1
data = 1800header = 0maxChunkSize = 700return = [600,600,600]Two chunks hold at most 1400 bytes, so three are necessary. Without a header, the evenly divided sizes are [600,600,600].
Example 2
data = 300header = 600maxChunkSize = 700return = [600,300]The total is 900 bytes, requiring two chunks. The first chunk must contain the intact 600-byte header, so the remaining 300 bytes form the second chunk.
Example 3
data = 7header = 4maxChunkSize = 6return = [5,6]The total is 11 bytes, requiring two chunks. The first size is max(4, floor(11 / 2)) = 5, which contains the entire header and one payload byte. The remaining 6 bytes use the second chunk.
Constraints
0 <= data, header <= 10^9.1 <= maxChunkSize <= 10^9.header <= maxChunkSize, so the header fits in one chunk.- All sizes are integer byte counts.
- The minimum required chunk count is at most
10000.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- 100 DoorsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Clock Hand Angle with Subsecond PrecisionScale AI · ONSITE INTERVIEWScale AI interview questions
- Recover the Initial Mango PileLSEG · ONSITE INTERVIEWLSEG interview questions
- Estimate Pi from Sampled PointsJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Add Two Numeric Strings with CarryZipRecruiter · ONSITE INTERVIEWZipRecruiter interview questions
- Three- and Five-liter Jug PlanXilinx · ONSITE INTERVIEWXilinx interview questions
- Add Two Numbers Given as Digit ArraysCapital One · ONSITE INTERVIEWCapital One interview questions
- Babysitter Quote with Hourly RatesUpstart · ONSITE INTERVIEWUpstart interview questions
- Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions