PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Bytedance

Reverse Words While Preserving Spaces

Last updated: May 21, 2026

Quick Overview

This question evaluates proficiency in string manipulation, tokenization, whitespace handling, and careful edge-case reasoning when reversing word order while either normalizing or preserving space patterns.

  • medium
  • Bytedance
  • Coding & Algorithms
  • Software Engineer

Reverse Words While Preserving Spaces

Company: Bytedance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a C++ function that reverses the order of words in a string. Assume a word is any maximal sequence of non-space characters, and the input contains only the space character `' '` as whitespace. **Part 1:** Return the words in reverse order, with leading and trailing spaces removed and exactly one space between adjacent words in the result. **Part 2 (follow-up):** Modify the solution so that the original pattern of space blocks is preserved exactly. That means the lengths of the leading spaces, the spaces between word slots, and the trailing spaces must stay the same; only the word tokens should be reversed. Example: - Input: `" hello world again "` - Part 1 output: `"again world hello"` - Part 2 output: `" again world hello "` Also be prepared to: - write several edge-case test cases, - analyze the time and space complexity of your approach, - and discuss how to optimize the implementation.

Quick Answer: This question evaluates proficiency in string manipulation, tokenization, whitespace handling, and careful edge-case reasoning when reversing word order while either normalizing or preserving space patterns.

Related Interview Questions

  • Reverse Nodes in K-Sized Groups - Bytedance
  • Solve Bracket Matching and Tree Width - Bytedance (hard)
  • Reverse Linked List Groups - Bytedance (medium)
  • Solve Stack and String Shift Problems - Bytedance (medium)
  • Find LCA With Parent Pointers - Bytedance (medium)
Bytedance logo
Bytedance
Mar 29, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0

Implement a C++ function that reverses the order of words in a string.

Assume a word is any maximal sequence of non-space characters, and the input contains only the space character ' ' as whitespace.

Part 1: Return the words in reverse order, with leading and trailing spaces removed and exactly one space between adjacent words in the result.

Part 2 (follow-up): Modify the solution so that the original pattern of space blocks is preserved exactly. That means the lengths of the leading spaces, the spaces between word slots, and the trailing spaces must stay the same; only the word tokens should be reversed.

Example:

  • Input: " hello world again "
  • Part 1 output: "again world hello"
  • Part 2 output: " again world hello "

Also be prepared to:

  • write several edge-case test cases,
  • analyze the time and space complexity of your approach,
  • and discuss how to optimize the implementation.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Bytedance•More Software Engineer•Bytedance Software Engineer•Bytedance Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,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.