Reported September 2026
Together AIstring

Balanced File Partitions Without Splitting Lines

Reported by candidates from Together AI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Together AI OA. Under 2s to a working solution.
Founder's read

Together AI reported this one in September 2026, and it looks friendly until the edge cases hit. You get string chunks from an iterator, cut at random points, and you have to rebuild the logical file, then split it into exactly partCount balanced parts without breaking a line. Nothing fancy in the algorithm. The trap is in the details: lines crossing chunk boundaries, trailing newlines, and empty parts. If you're taking this OA in the next day or two, the pattern is plain string parsing plus arithmetic on counts. StealthCoder is the safety net if you blank mid-assessment, but you can walk in knowing the script.

The problem

A text file arrives through an iterator as the ordered string chunks chunks. Chunk boundaries are arbitrary and may occur in the middle of a line. Split the logical file into exactly partCount consecutive parts.
A newline character ends a line and is not included in that line's text.
A final nonempty suffix without a newline is also a line.
A trailing newline does not create an extra empty line.
No line may be split between parts.
The numbers of lines in any two parts must differ by at most one. Give the earlier parts the extra lines.
Represent each part by joining its lines with \n. Empty parts are empty strings.

Function
splitFileIntoBalancedParts(chunks: String[], partCount: int) → String[]

Examples
Example 1
chunks = ["alpha\nbe","ta\ngamma\n"]
partCount = 2
return = ["alpha\nbeta","gamma"]
The middle line crosses an iterator boundary. Three complete lines split as two then one.
Example 2
chunks = ["a\nb"]
partCount = 4
return = ["a","b","",""]
Parts are retained even when there are fewer lines than parts.

Constraints
1 <= chunks.length <= 100000.
The total input length is at most 1000000.
1 <= partCount <= 100000.
Input contains no carriage-return characters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to stop thinking about chunks. Concatenate everything (total length is capped at 1000000, so that's fine), or stream with a carry buffer, then split on newline into lines. Now handle the edge case that breaks naive solutions: a trailing newline must not produce an extra empty line, but a final nonempty suffix without a newline is still a line. Splitting on \n in most languages leaves a trailing empty string, so drop it only when the file ends with a newline. Then compute base = n / partCount and extra = n % partCount. The first extra parts get base+1 lines, the rest get base. Join each slice with \n. Pitfalls: partCount larger than the line count must still return partCount strings, with empty ones as "". Also an entirely empty file gives zero lines, so every part is "". Don't build strings by repeated concatenation in a loop over chunks. If you freeze live, StealthCoder can supply this structure as a hedge.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Balanced File Partitions Without Splitting Lines cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Together AI's OA.

Together AI reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Balanced File Partitions Without Splitting Lines FAQ

What's the trick in the Together AI balanced file partitions problem?+

Ignore chunk boundaries and rebuild the lines first. Then use base = n / partCount and extra = n % partCount, giving the first extra parts one more line. The rest is slicing and joining with newline. The real difficulty is trailing newline handling, not the algorithm.

How do I handle a trailing newline correctly?+

A trailing newline ends the last line, it doesn't start a new empty one. If you split on newline and the file ends with one, the last element is an empty string, so drop it. If the file ends without a newline, keep the final suffix as a line.

What should I return when partCount exceeds the number of lines?+

Still return exactly partCount strings. Example 2 shows ["a","b","",""] for two lines and four parts. With base = 0 and extra = n, the first n parts get one line each and the remainder get empty strings.

Is it safe to concatenate all chunks into one string?+

Yes. Total input length is at most 1000000, so one join is cheap. Use an array join or string builder, not repeated concatenation in a loop. If you'd rather stream, keep a carry buffer for the partial line at the end of each chunk.

How do I prepare for this in 48 hours?+

Write it once from scratch and test four cases: line crossing a chunk boundary, trailing newline, no trailing newline, and an empty file. Also test partCount greater than the line count. Those cases cover nearly every way a naive solution fails.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Together AI.

OA at Together AI?
Invisible during screen share
Get it