FastPrepTwo-Person Chat Layout
Problems/Two-Person Chat Layout

Two-Person Chat Layout

Capital One logoCapital One● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Wrapping text inside a fixed-width window for a two-person conversation, with each speaker kept to one side, was one of four problems in a 2024 Capital One software engineering intern online assessment.

The report gives the messages as an array and says long text wraps at the width. It does not say how the sender of a message is marked, whether a word may be split, how lines are padded, or what to return. Here each message is a [sender, text] pair and begins on its own line. Lines break between words, and a word longer than the width is cut into width-sized pieces. Every line is padded with spaces to the full width, and the lines are returned in order. The limits are our choices.

Our version is about 75% the same as the reported question.

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 width characters is chopped, from its left end, into chunks of width characters. 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 width characters, counting one space between neighbors.
  • At the end, every line is widened to exactly width characters 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 done would fit in 12: done gets 8 spaces in front.

Example 2

messages = [["1","dear accountholder ok"]]width = 6return = ["dear  ","accoun","tholde","r ok  "]
  • Chop: accountholder (13 characters) becomes accoun, tholde, r.
  • Fill width 6: dear | accoun | tholde | r ok (the leftover r has room for ok).
  • 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: lunch and moved hold 10 letters together, but the space between them makes 11, so moved opens line 2.
  • to joins it (5 + 1 + 2 = 8); noon would make 13, so it opens line 3. Each of the three lines gets trailing spaces up to 10.
  • Person 2: fine by me is exactly 10 characters with its two spaces, so it stays on one line with no padding.

Constraints

  • 1 <= messages.length <= 50
  • messages[i].length == 2
  • messages[i][0] is "1" or "2".
  • 1 <= messages[i][1].length <= 100
  • messages[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
All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public String[] renderChat(String[][] messages, int width) {
        // Write your code here.
    }
}
Checking account…