← Bytedance Interview Insights

Bytedance·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
May 2026

Summary

Bytedance software engineer interview with a coding problem centered on text formatting logic. The question had enough edge cases to keep things interesting, and the border-wrapping requirement at the end felt like a deliberate gotcha.

Questions Asked (1)

Q1

Given a list of paragraphs each tagged with a left or right alignment flag and a max line width W, implement a greedy word-wrapping algorithm that fits words onto lines without splitting them, pads each line to width W based on alignment direction, and wraps the entire result in a border of asterisks. Return the final formatted page as a string.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The greedy line-filling part was straightforward enough, just accumulate words until the next one doesn't fit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints and edge cases, then outline a greedy line-breaking algorithm that processes each paragraph independently. Explain how to pad lines based on alignment and finally wrap the entire page with a border, emphasizing modularity and testability.

Pro tip: Mention that you would write helper functions for line breaking, padding, and border wrapping to keep the code clean and testable, and discuss potential pitfalls like handling words longer than W.

1. Clarify requirements and edge cases

Ask about constraints: maximum word length, empty paragraphs, multiple spaces, and whether W includes border characters. Confirm that words cannot be split and that padding is with spaces.

2. Design greedy line-breaking

For each paragraph, iterate through words and accumulate them into a line until adding the next word would exceed W. Start a new line when needed, ensuring no word is split.

3. Apply alignment padding

For each line, pad with spaces to reach width W: for left alignment, add spaces to the right; for right alignment, add spaces to the left. Handle the last line of a paragraph similarly.

4. Wrap with border and assemble

Determine the total width of the bordered page (W + 2 for border characters). Create top and bottom border lines of asterisks, and prefix/suffix each content line with '* ' and ' *' (or similar) to form the final string.

5. Test and validate

Walk through examples, including edge cases like a single word longer than W, empty paragraphs, and multiple paragraphs. Verify that the output matches the expected format.

Key Points to Mention

  • Greedy algorithm: always fit as many words as possible on a line without exceeding W.
  • Handling words longer than W: either truncate, split, or throw an error—clarify with interviewer.
  • Alignment padding: left-align adds spaces to the right, right-align adds spaces to the left.
  • Border wrapping: add a top and bottom line of asterisks, and pad each content line with asterisks and spaces.
  • Time and space complexity: O(total characters) time, O(total characters) space for output.
  • Modularity: separate functions for line breaking, padding, and border wrapping for clarity and testing.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.