Two-Person Chat Layout
Updated
Problem statement
A chat window has room for exactly width characters per line and shows a two-person conversation. messages lists the conversation in order: messages[i][0] is the writer, either "1" or "2"; messages[i][1] is the text.
Turn the conversation into screen lines with these rules:
- Messages are laid out one at a time. Each one begins on a fresh line, even when the previous message is from the same person and would have room left.
- A word with more than
widthcharacters is chopped, from its left end, into chunks ofwidthcharacters. The final chunk may be shorter. - Line breaks are chosen greedily over the words and chunks, in order: a line ends just before the first word or chunk that would push it past
widthcharacters, counting one space between neighbors. - At the end, every line is widened to exactly
widthcharacters with spaces. For person"1"the spaces are appended; for person"2"they are prepended.
Return the screen lines in display order, first line first.
Function
renderChat(messages: String[][], width: int) → String[]Examples
Example 1
messages = [["2","on it"],["2","done"]]width = 12return = [" on it"," done"]- Message 1 from person 2:
on it(5 characters) gets 7 spaces in front. - Message 2 from the same person starts a fresh line, even though
on it donewould fit in 12:donegets 8 spaces in front.
Example 2
messages = [["1","dear accountholder ok"]]width = 6return = ["dear ","accoun","tholde","r ok "]- Chop:
accountholder(13 characters) becomesaccoun,tholde,r. - Fill width 6:
dear|accoun|tholde|r ok(the leftoverrhas room forok). - Pad on the right for person 1.
Example 3
messages = [["1","lunch moved to noon"],["2","fine by me"]]width = 10return = ["lunch ","moved to ","noon ","fine by me"]- Person 1:
lunchandmovedhold 10 letters together, but the space between them makes 11, somovedopens line 2. tojoins it (5 + 1 + 2 = 8);noonwould make 13, so it opens line 3. Each of the three lines gets trailing spaces up to 10.- Person 2:
fine by meis exactly 10 characters with its two spaces, so it stays on one line with no padding.
Constraints
1 <= messages.length <= 50messages[i].length == 2messages[i][0]is"1"or"2".1 <= messages[i][1].length <= 100messages[i][1]consists of one or more words joined by single spaces; it neither begins nor ends with a space. Each word is one or more printable ASCII characters other than a space.1 <= width <= 100
More Capital One Online Assessment questions
- Branch with the Most FilesOA · Seen Nov 2026
- Earliest Meeting Time for All EmployeesOA · Seen Nov 2026
- Earn the Most 🐝OA · Seen Nov 2026
- Rank Football TeamsOA · Seen Oct 2026
- Count Subarrays Matching a Comparison PatternOA · Seen Oct 2026
- Arithmetic Triplets After QueriesOA · Seen Oct 2026
- Count One-Swap Number PairsOA · Seen Oct 2026
- Distinct Values on Maximum-Sum FramesOA · Seen Oct 2026
- Sort Adjacent PairsOA · Seen Oct 2026
- Bouncing Diagonal WeightsOA · Seen Oct 2026
Similar questions at other companies
- Reliable LLM Call with Independent Retry BudgetsStripe · OAStripe interview questions
- Tennis Game ScoreAbridge · OAAbridge interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Robot Assembly Line ConflictsGoogle · OAGoogle interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
- Repair One Movement InstructionOpenAI · OAOpenAI interview questions
- Count Straight-Line Word Occurrences in a GridOptiver · OAOptiver interview questions
- Format TextTikTok · OATikTok interview questions
- Binary String Swap TimeMicrosoft · OAMicrosoft interview questions
- Communications HandlerAkuna Capital · OAAkuna Capital interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences