Reverse Words in a String (Preserve Spaces, In-Place)

Asked at ByteDance · Coding Questions · Difficulty: Medium · Last asked Mar 10, 2026

Reverse the word order in a string. A word is a maximal substring of non-space characters. The canonical formulation trims leading/trailing whitespace and collapses internal runs of spaces to a single space.

Loading the problem…