PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates string manipulation, precise output formatting, input validation, and attention to edge cases for fixed-width table rendering. It is commonly asked in Coding & Algorithms interviews because it verifies adherence to exact specifications and robust handling of corner cases; the domain is coding/algorithms with a focus on practical application rather than abstract theory.

  • medium
  • Airbnb
  • Coding & Algorithms
  • Software Engineer

Print Sentences as Table

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a function that formats a list of sentences as a bordered table. Given: - `sentences`: a list of strings - `width`: the fixed content width of each table row, such as `55` Return the table as a list of strings, or print the lines in order. Formatting rules: 1. Each sentence must appear on exactly one line; do not wrap text. 2. The horizontal border is `+` followed by `width` hyphens, followed by `+`. 3. Each content row is `|` followed by the sentence left-aligned and padded with spaces to exactly `width` characters, followed by `|`. 4. Print a horizontal border before the first row, between every pair of rows, and after the last row. Example with a smaller width for readability: Input: ```text sentences = ["Hello world", "How are you", "Bye"] width = 15 ``` Output: ```text +---------------+ |Hello world | +---------------+ |How are you | +---------------+ |Bye | +---------------+ ``` Edge cases to handle: - If a sentence is longer than `width`, report invalid input, for example by throwing an exception. - An empty sentence should produce a blank padded row. - An empty list should produce no output.

Quick Answer: This question evaluates string manipulation, precise output formatting, input validation, and attention to edge cases for fixed-width table rendering. It is commonly asked in Coding & Algorithms interviews because it verifies adherence to exact specifications and robust handling of corner cases; the domain is coding/algorithms with a focus on practical application rather than abstract theory.

Implement a function that formats a list of sentences as a bordered table and returns the table as a list of strings. Given a list of strings `sentences` and an integer `width`, build the table using these rules: 1. Each sentence must appear on exactly one row. Do not wrap text. 2. The horizontal border is `+` followed by `width` hyphens, followed by `+`. 3. Each content row is `|` followed by the sentence left-aligned and padded with spaces to exactly `width` characters, followed by `|`. 4. Place a horizontal border before the first row, between every pair of rows, and after the last row. 5. If any sentence is longer than `width`, raise a `ValueError`. 6. An empty sentence should produce a blank padded row. 7. If `sentences` is empty, return an empty list. Example: If `sentences = ["Hello world", "How are you", "Bye"]` and `width = 15`, the returned list should represent: +---------------+ |Hello world | +---------------+ |How are you | +---------------+ |Bye | +---------------+

Constraints

  • 0 <= len(sentences) <= 10^4
  • 0 <= width <= 10^4
  • Each element of `sentences` is a string
  • If any sentence has length greater than `width`, the function must raise `ValueError`

Examples

Input: (['Hello world', 'How are you', 'Bye'], 15)

Expected Output: ['+---------------+', '|Hello world |', '+---------------+', '|How are you |', '+---------------+', '|Bye |', '+---------------+']

Explanation: Each sentence is placed on its own padded row, with a border before, between, and after rows.

Input: (['', 'A'], 3)

Expected Output: ['+---+', '| |', '+---+', '|A |', '+---+']

Explanation: An empty sentence becomes a fully blank padded row, and 'A' is padded to width 3.

Input: ([], 5)

Expected Output: []

Explanation: An empty input list should produce no output lines.

Input: (['abc', 'x y'], 3)

Expected Output: ['+---+', '|abc|', '+---+', '|x y|', '+---+']

Explanation: Both sentences fit exactly into the given width, so no extra padding is needed.

Input: ([''], 0)

Expected Output: ['++', '||', '++']

Explanation: With zero content width, the border has no hyphens and the empty sentence fits exactly.

Hints

  1. Build the border string once as `'+' + '-' * width + '+'` and reuse it.
  2. For each sentence, left-align it to `width` characters before surrounding it with `|` characters.
Last updated: May 15, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Determine Exact Layover Booking - Airbnb (medium)
  • Solve Linked-List and Iterator Problems - Airbnb
  • Implement Text Layout and Query Parsing - Airbnb (easy)
  • Parse Query Parameters Into a Map - Airbnb (medium)
  • Compute board-game score from regions - Airbnb (medium)