Render Wrapped Two-Party Chat Lines
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that trips people in this ZipRecruiter OA, reported December 2022, is the line "an empty text produces one empty chunk." Everything else is plain string slicing and padding. It's a simulation problem dressed up as a chat renderer, and the points go to whoever handles the edge cases cleanly. You split each message into chunks of contentWidth, pad to totalWidth, and align by sender. If you blank on the details during the live assessment, StealthCoder runs invisibly as a safety net and gives you a working solution while you keep your cool.
The problem
Each message is [sender, text], where sender is 1 or 2. Split text into consecutive character chunks of at most contentWidth; preserve every character, including spaces. An empty text produces one empty chunk. Pad every chunk with spaces to exactly totalWidth: sender 1 is left-aligned and sender 2 is right-aligned. Return all rendered lines in message order. Function renderChat(messages: String[][], contentWidth: int, totalWidth: int) → String[] Examples Example 1 messages = [["1","hello"],["2","world"]] contentWidth = 5 totalWidth = 8 return = ["hello "," world"] The same-width texts align to opposite sides. Example 2 messages = [["1","abcdef"]] contentWidth = 3 totalWidth = 5 return = ["abc ","def "] Text is divided into consecutive chunks. Constraints 1 <= contentWidth <= totalWidth <= 1000 The total message text length is at most 100000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there isn't one. Loop over messages, slice the text in steps of contentWidth, and pad each chunk. Sender 1 gets the chunk followed by spaces. Sender 2 gets spaces followed by the chunk. Pad length is totalWidth minus chunk length. The classic pitfall is the empty text. A normal loop from 0 to length runs zero times, so you'd emit nothing. Handle it explicitly and output one line of totalWidth spaces. Second pitfall: don't trim or split on whitespace. Spaces inside the text are content, and they must survive, including trailing ones at a chunk boundary. Third, the sender comes in as a string, so compare to "1", not the integer 1. Total text is capped at 100000, so linear work is fine. Build the output with a list, not repeated concatenation. StealthCoder is the hedge if the off-by-one in your slicing loop won't resolve under the clock.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Render Wrapped Two-Party Chat 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Render Wrapped Two-Party Chat Lines FAQ
How hard is the ZipRecruiter chat rendering question really?+
Easy on algorithm, annoying on details. There's no data structure or clever insight. The risk is missing the empty text case or mangling spaces. If you read the statement twice and test both examples by hand, you should finish quickly.
What's the trick to splitting the text into chunks?+
Step an index from 0 by contentWidth and take the substring from i to min(i + contentWidth, length). That preserves every character, spaces included. For empty text, the loop doesn't run, so add a special case that produces a single empty chunk.
How do I pad for sender 2 versus sender 1?+
Compute the pad as totalWidth minus chunk length. For sender 1, append that many spaces after the chunk. For sender 2, prepend them. Every output line must be exactly totalWidth characters, so check that with a quick length assertion while testing.
Will this pattern still be asked?+
String simulation problems like this show up constantly in OAs because they test care more than cleverness. This one was reported in December 2022 at ZipRecruiter. Expect variants with different alignment rules, like center alignment or word wrapping instead of hard character chunks.
How do I prepare in 48 hours?+
Write this one from scratch twice. Then practice slicing, padding, and join operations in your language of choice. Test edge cases: empty text, text exactly contentWidth long, contentWidth equal to totalWidth, and text with leading or trailing spaces.