Reverse Words While Preserving Spaces
Company: Bytedance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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.
Reverse Words with Normalized Spaces
Constraints
- Inputs are provided as Python literals matching the function signature.
- Return a deterministic exact-match result.
Examples
Input: (' hello world again ',)
Expected Output: 'again world hello'
Explanation: Prompt example part 1.
Input: ('single',)
Expected Output: 'single'
Explanation: One word.
Input: (' ',)
Expected Output: ''
Explanation: Only spaces.
Hints
- Choose a representation that makes the core operation simple.
- Handle empty and boundary inputs before the main algorithm.
Reverse Words Preserving Space Blocks
Constraints
- Inputs are provided as Python literals matching the function signature.
- Return a deterministic exact-match result.
Examples
Input: (' hello world again ',)
Expected Output: ' again world hello '
Explanation: Prompt example part 2.
Input: ('a b',)
Expected Output: 'b a'
Explanation: Simple swap.
Input: (' ',)
Expected Output: ' '
Explanation: Only spaces.
Hints
- Choose a representation that makes the core operation simple.
- Handle empty and boundary inputs before the main algorithm.