Add Dramatic Punctuation to Text
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
# Add Dramatic Punctuation to Text
Implement `add_drama(text)`. Preserve all whitespace exactly. For each maximal non-whitespace token, first replace every period `.` in that token with `!`, then append one additional `!` to the token.
Other characters are unchanged. An empty string or a string containing only whitespace is returned unchanged.
## Constraints
- `0 <= len(text) <= 10^6`
- Input may contain spaces, tabs, and newlines.
- The output must be built in linear time.
## Examples
- `"hello. world"` becomes `"hello!! world!"`.
- `"a b.c"` becomes `"a! b!c!"`.
- `" "` remains `" "`.
## Clarifications
A token that already ends in `!` still receives the required extra `!`. Replacing a final period and then appending therefore produces two exclamation marks.
## Hints
Track whether the scan is inside a token so the extra punctuation is emitted exactly at a token boundary.
## Extensions
- Process the text as a character stream.
- Treat Unicode grapheme clusters as characters.
- Make the replacement and suffix characters configurable.
Quick Answer: Transform text by replacing periods inside each non-whitespace token and adding the required dramatic suffix while preserving every whitespace character. Handle empty or whitespace-only input, existing punctuation, repeated spaces, tabs, newlines, million-character strings, and streaming extensions.
Implement `add_drama(text)`. Preserve all whitespace exactly. For each maximal non-whitespace token, first replace every period (`.`) in that token with an exclamation mark (`!`), then append one additional exclamation mark to the token.
Other characters are unchanged. An empty string or a string containing only whitespace is returned unchanged. A token that already ends in `!` still receives the required extra `!`. Replacing a final period and then appending therefore produces two exclamation marks.
Constraints
- 0 <= len(text) <= 10^6
- Input may contain spaces, tabs, and newlines.
- All whitespace must be preserved exactly.
- The output must be built in linear time.
Examples
Input: ('',)
Expected Output: ''
Explanation: The minimum-length input contains no token and must remain empty.
Input: ('hello. world',)
Expected Output: 'hello!! world!'
Explanation: This exact source example replaces the period in the first token, then appends one exclamation mark to each token.
Hints
- Track whether the scan is inside a token so the extra punctuation is emitted exactly at a token boundary.